The Bernstein–Vazirani algorithm is one of the earliest and clearest examples of a quantum algorithm that significantly outperforms any classical counterpart for a well-defined problem. It can be viewed as an application of the Deutsch-Jozsa algorithm. The algorithm was first introduced by Ethan Bernstein and Umesh Vazirani in their 1993 paper and later discussed again in their 1997 paper. The algorithm treats the following problem:
Definition (Bernstein–Vazirani problem): Let be a function from n-bit strings to a single bit,
, such that there exists a secret bit string
with the property that, for every input
,
, where the dot product is defined by
, with
and
. The task is to determine the secret string
using as few queries to
as possible.
Exercise. 1. For the 3-bit string , compute
for all
. As an example, take
. Then
The function defined by
is an example of a function satisfying the Bernstein–Vazirani property.
2. Show that the majority function
is a counterexample: it does not satisfy the Bernstein–Vazirani property.
Hint: Any function is linear over
, i.e. it obeys
To disprove that has the Bernstein–Vazirani form, find explicit
for which
For instance, take and
. Then
, while
so , showing the required contradiction. ∎
Classically, we can solve the problem by considering a complete basis of bit strings:
If we query the function on these strings, we obtain
From this, it is clear that the secret string can be determined with
queries. Because the
bits of
are independent, there is no way to do better classically. Remarkably, a quantum computer allows us to solve the same problem with far fewer queries.
From the expression,
we observe that an inner product appears in the phase factor . If we can prepare the corresponding state, applying Hadamard gates directly yields
. This state naturally appears just before the final Hadamard transforms in the Deutsch–Jozsa algorithm(see this post), so the same circuit can be used to solve the Bernstein–Vazirani problem with only a single query.
To implement this, we prepare an -qubit input register in the state
and a single auxiliary qubit in the state
. Applying Hadamard gates to all
qubits yields
We then apply the quantum oracle encoding
:
After acting on , the combined state becomes
This has been carefully discussed in Deutsch–Jozsa algorithm(see this post).
On the input register, this action is equivalently described by the operator :
Recall that , then apply quantum oracle
to get
Then applying Hadamard gates will give
. By measuring the final state, we can read out
.
The quantum circuit of the Bernstein-Vazirani algorithm is completely the same as that for Deutsch-Jozsa. A simpler version is given in Figure 2 based on .


