View on GitHub
Open this notebook in GitHub to run it yourself
The Bernstein-Vazirani (BV) algorithm [1], [2], introduced by Ethan Bernstein and Umesh Vazirani, is a fundamental quantum algorithm that addresses a special case of the hidden-shift problem. It employs the same functional circuit structure as the Deutsch-Jozsa algorithm and achieves a linear speedup over its classical counterpart in the oracle query model. The algorithm treats the following problem:where refers to a bitwise dot operation, and is a binary string of length .
- Input: A Boolean function defined as
which reveals the secret string, one bit at a time. If one allows for randomness, the expected number of queries remains linear in . >Complexity: The quantum approach requires a single query call, therefore, the quantum complexity query is . In contrast, Classically, the minimum inquiries of the function for determining the secret string is : is called with these strings:
- Promise:
- Output: Returns the secret string with minimum inquiries of the function.
Keywords: Hidden string problem, Foundational quantum algorithms, Oracle/Query complexity.

How to Build the Algorithm with Classiq
The BV algorithm contains three function blocks: an oracle for the predicate , “sandwiched” between two Hadamard transforms. The resulting state corresponds to the secret string. The full mathematical derivation is at the end of this notebook.Implementing the BV Predicate
A simple quantum implementation of the binary function applies a series of controlled-X operations: starting with the state , we apply an X gate, controlled on the state, for all such that :
Implementing the BV Quantum Function
The quantum part of the BV algorithm is essentially identical to thedeutsch_jozsa function in the Deutsch-Jozsa notebook. However, in contrast to the latter, the predicate function implementation is fixed, depending solely on the secret string . Hereafter, we refer to the secret string as a secret integer, defined as an integer argument for the bv_function:
An Example on Five Qubits
We construct a model for a specific example, setting the secret integer to and . The algorithm requires only a single application of the quantum circuit, as in an ideal noiseless execution, the resulting quantum state corresponds exactly to the hidden integer (see the last Eq. (1) below). Even in the presence of noise, the idea of using only a single query call of has demonstrated algorithmic speedup in Ref. [3], where the authors considered a modified version of the BV algorithm in which the secret integer changes after every inquiry. In this example, we takenum_shots=1000 to highlight the fact that the resulting state is purely the secret string.
Output:
Output: