Quantum Algorithms Database
Bernstein-Vazirani Algorithm
Finds a hidden binary string encoded in a function using just one query, versus many queries classically — a simple but illustrative example of quantum parallelism.
Last reviewed September 30, 2026. Check the linked sources for anything that may have changed since.
Year
1993 (conference); 1997 (journal)
Inventor(s)
Ethan Bernstein and Umesh Vazirani
Speedup Type
Exponential Speedup
Difficulty
★★☆☆☆
Overview
The Bernstein-Vazirani algorithm finds a hidden n-bit string s using a single quantum query. The hidden string defines a function f of x equal to the dot product of s and x modulo 2, which is the parity of the bits of x selected by s. A classical computer needs n queries to learn s, one for each bit, while a quantum computer finds all n bits at once.
It is a simple algorithm, closely related to Deutsch-Jozsa, and it is one of the best teaching examples of how interference extracts global information. Beyond teaching, its recursive form gave the first evidence of a super-polynomial separation between quantum and classical bounded-error computation.
The problem
Given a 'black box' function that computes the dot product of the input with a hidden secret string, determine the secret string.
History
Ethan Bernstein and Umesh Vazirani introduced the algorithm in their paper Quantum Complexity Theory, presented at the ACM Symposium on Theory of Computing in 1993 and published in the SIAM Journal on Computing in 1997. The paper had two parts: the linear-speedup parity problem used here, and a recursive version, recursive Fourier sampling, which requires super-polynomially many classical queries.
Because the circuit is short and its answer can be checked, Bernstein-Vazirani became a standard test for quantum hardware. It has been run on NMR devices, trapped ions, and superconducting qubits, and researchers have used it to compare noise levels, since success requires every qubit's gates to be accurate.
Intuition
The Hadamard transform converts between two views of a function on bit strings. In the computational view, the function is evaluated at each input. In the Hadamard view, the function is represented by its Fourier coefficients. For a parity function, the Fourier transform is extremely simple: all the weight is concentrated on a single string, the hidden s.
The algorithm creates an equal superposition of all inputs, queries the oracle in a way that writes the function values into the phases, and applies Hadamard gates. The phases form a pattern that is precisely the Fourier transform of the parity function, so the Hadamard layer converts the phase pattern into the basis state s, and a measurement reveals it.
A classical algorithm cannot do this because each query returns a single bit, so it must query n times to pin down n unknown bits. The quantum oracle is queried once on a superposition, and the output encodes all n bits in the interference pattern.
How it works
Prepares all inputs in superposition, queries the function once, and uses interference so that measuring the resulting state directly reveals the hidden string in a single shot.
- Prepare n input qubits in the zero state and one ancilla qubit in the state 1.
- Apply Hadamard gates to every qubit, which creates a superposition of all inputs and puts the ancilla in the minus state.
- Apply the oracle that maps x and y to x and y XOR s dot x, which in the presence of the ancilla puts a phase of minus 1 to the s dot x on each input x.
- Apply Hadamard gates to the n input qubits.
- Measure the n input qubits. The result is the hidden string s with probability 1.
Worked example
Let n = 3 and the hidden string be s = 101, so f of x is x1 XOR x3. Classically, you would query f at 100, 010, and 001 and read off the bits of s from the three answers: 1, 0, and 1. That requires three queries.
On a quantum computer, the circuit prepares the superposition, applies the oracle once, and applies the final Hadamards. The state after the oracle has phase minus 1 on inputs 001, 011, 100, 110, because f is 1 there, and the final Hadamard transform turns this phase pattern into the single state 101. Measuring gives 101 with certainty after a single query.
Complexity and resources
| Quantum queries | 1 |
|---|---|
| Classical queries | n, which is optimal for classical algorithms |
| Qubits | n plus 1 |
| Gates | 2n plus 1 Hadamards and one oracle call |
| Recursive Fourier sampling | n to the power of about log n classical queries versus polynomially many quantum queries |
Hardware needs
A few qubits and a very short circuit, so it runs on any current device for small n.
The oracle is a set of CNOT gates from the input qubits where s has a 1 to the ancilla, so its cost is low.
Real-world impact
Mainly an educational stepping-stone algorithm, often taught right after Deutsch-Jozsa, used to build intuition for how quantum parallelism extracts global information about a function efficiently.
Applications
A first example of a quantum advantage in the query model with a clear, verifiable answer.
A hardware benchmark that checks correct operation of many qubits and gates.
The starting point for recursive Fourier sampling, which separates BQP from classical bounded-error computation relative to an oracle.
Limitations
- The advantage is only linear in the query model, and the problem is artificial.
- The oracle's cost is not counted, and for a hidden string known in advance, the oracle already contains the answer.
- Noise makes large instances fail on current hardware.
Common misconceptions
- It does not give an exponential speedup. The classical algorithm needs only n queries, which is already efficient.
- It does not extract information without measurement. The speedup comes from interference in the query, and the result is read out by measuring.
How it compares
Deutsch-Jozsa uses the same circuit but decides whether a function is constant or balanced.
Simon's algorithm needs repeated runs and classical post-processing, and offers an exponential gap.
Grover's algorithm is probabilistic and iterative, while Bernstein-Vazirani is exact with one query.
Why it matters
The algorithm demonstrates, in the simplest possible setting, that interference lets a quantum computer extract a whole string of information from a single oracle call. It is the cleanest entry point to the Fourier-sampling idea that powers Simon's and Shor's algorithms.
Its recursive version, recursive Fourier sampling, was the first evidence that quantum computers could beat classical probabilistic machines by more than a polynomial factor, which helped establish the case for quantum computing as a new computational model.
The hidden-string problem
In the Bernstein-Vazirani problem a black box computes f(x) = s . x mod 2, the bitwise dot product of the input with a hidden n-bit string s. The goal is to find s. Classically you need n queries: ask for each unit vector (1000..., 0100..., and so on) and read off one bit of s at a time, and no classical method, even randomized, can do fundamentally better in the worst case. The quantum algorithm finds s with one query.
This is a stronger statement than the Deutsch-Jozsa result in one respect, because the classical lower bound of n queries holds against randomized algorithms as well, so the gap between 1 and n is not removed by randomness. The separation is only linear in n, which is why the problem is viewed as a proof of concept and not a practical speedup.
Why one query reveals every bit
Apply Hadamard gates to n qubits to create an equal superposition, query the oracle with the phase-kickback setup, then apply Hadamard gates again. The oracle multiplies each basis state by (-1)^(s . x). A Hadamard transform of a state whose phases are (-1)^(s . x) is exactly the basis state |s>, so measurement returns s directly. All n bits come out of one interference pattern.
This is the Fourier-transform view of the problem: the function is a character of the group of n-bit strings, and the Hadamard transform is the Fourier transform over that group. Kitaev's paper (arXiv quant-ph/9511026) discusses Fourier transforms over finite Abelian groups as a general tool, and the Bernstein-Vazirani circuit is the simplest case of the pattern.
- Quantum queries: 1.
- Classical queries (worst case, including randomized): n.
- Core step: Hadamard transform converts phase pattern (-1)^(s . x) into the basis state |s>.
Place in the chain of early algorithms
The usual teaching order is Deutsch-Jozsa, then Bernstein-Vazirani, then Simon's problem, then Shor's algorithm. Each step shows a bigger gap against classical methods, from a deterministic-only separation to a linear gap to an exponential gap against randomized algorithms. Seeing them side by side makes it clear that the underlying machine is the same: superposition, an oracle that writes information into phases, and a Fourier transform that converts that information into a readable outcome.
Because the circuit is tiny, it is also used as a first test of a new device. As with Deutsch-Jozsa, treat the result as a sanity check on gate quality, not as evidence of advantage.
Frequently asked questions
What does the Bernstein-Vazirani algorithm do?
It finds a hidden bit string s using one quantum query to a function that computes the dot product of s with the input modulo 2.
How many queries does a classical computer need?
n queries for an n-bit string, one for each bit.
How is it different from Deutsch-Jozsa?
It uses the same circuit but answers a different question: it recovers the hidden string rather than deciding whether the function is constant or balanced.
Can I run it on a real quantum computer?
Yes. It is commonly run on small devices, and success rates measure how well all qubits and gates work together.
What is recursive Fourier sampling?
A recursive version of the problem for which quantum algorithms need polynomially many queries and classical algorithms need super-polynomially many.
Related algorithms
References and further reading
Sources opened and checked on December 1, 2026. Resource estimates and claims change, so check the originals.
- Quantum measurements and the Abelian Stabilizer Problem (arXiv (A. Yu. Kitaev)) Opened. Source for Fourier transforms over finite Abelian groups.
- Quantum Algorithms Revisited (arXiv (Cleve, Ekert, Macchiavello, Mosca)) Opened. Source for the interference view of these algorithms.