Bernstein–Vazirani Algorithm
The Bernstein–Vazirani algorithm is the cleanest demonstration in all of quantum computing that interference — not brute-force parallelism — is where the power lives. You are handed a black box that computes a hidden linear function for some secret -bit string , and asked to recover . Classically the box is stingy: each query returns a single parity bit, so learning an -bit secret costs queries, one bit at a time. The quantum algorithm reads the entire string with a single query. It does so with a circuit already familiar from Deutsch and Deutsch–Jozsa — a layer of Hadamards, one oracle call implemented through phase kickback, and a second layer of Hadamards — and the answer drops out of the final measurement deterministically, with probability exactly one. The mechanism is worth stating up front because it is so tidy. The first Hadamard layer spreads the register into an equal superposition over all inputs. The oracle stamps each term with the sign , painting a checkerboard of pluses and minuses across the amplitudes — and that sign pattern is precisely the -th Walsh function. The second Hadamard layer is the inverse Walsh–Hadamard transform, which recognizes that pattern and focuses every scrap of amplitude onto the one basis state , cancelling everywhere else. Bernstein and Vazirani published this in 1993 alongside a deeper recursive variant and the very definition of the complexity class BQP; the flat version here has become the canonical first lesson in Fourier sampling and a standard benchmark for real quantum hardware.
- The Bernstein–Vazirani problem: given oracle access to for an unknown -bit string , recover .
- Classically each query yields one bit of , so queries are needed; the quantum algorithm recovers all of in one query.
- The circuit is on with an ancilla in — it maps deterministically to .
- The engine is amplitude encoding of an inner product: the Walsh–Hadamard transform of is the single basis state , by character orthogonality .
- It is the abelian hidden subgroup problem over and the deterministic core of Deutsch–Jozsa, foreshadowing Simon, the QFT, and Shor.
- The -to- query gap is exact and deterministic; a randomized classical algorithm solves the flat problem in , so the true super-polynomial separation is the recursive variant.
- Key idea: apply before and after the phase oracle for ; the first layer builds an equal superposition, the oracle stamps the phase pattern , and the second layer is the inverse Walsh–Hadamard transform that maps that pattern exactly onto , so one measurement returns with certainty.
Apply before and after the phase oracle for : the first Hadamard layer creates an equal superposition, the oracle stamps each term with , and the second Hadamard layer is the inverse Walsh–Hadamard transform, which collapses that parity pattern onto the single basis state — so one measurement yields deterministically.
Where this lives today
Bernstein–Vazirani has outgrown its origins as a proof-of-concept and become a practical instrument. Because its ideal output is a single, known bitstring, it is one of the most popular fixed-output benchmarks for real quantum processors: you program in a known , run the shallow circuit — one layer of Hadamards, a CNOT-based oracle, another layer of Hadamards — and any spread of the measured distribution away from the delta at is a direct, interpretable measure of gate and readout error. IBM, Google, IonQ, Rigetti and university groups have all run BV as a scaling test on dozens of qubits, and its low, constant depth makes it a favourite for probing crosstalk and mid-circuit fidelity. Conceptually it is the doorway to Fourier sampling: the Hadamard layer is the quantum Fourier transform over , and recognizing the oracle's phase as a single group character is exactly the move that, scaled up to , becomes phase estimation and Shor's algorithm. The same "learn a hidden linear function from one query" motif reappears across quantum learning theory — learning parities, juntas, and Fourier-sparse functions — where quantum examples can be exponentially more sample-efficient than classical labeled data. And as the base case of the abelian hidden subgroup problem, with hidden subgroup , it is the first rung on the ladder whose top rungs are discrete logarithm and integer factoring.
The physical picture
Think of the oracle as a rubber stamp for phases. After the first Hadamard layer the register holds an equal sum over every input , all amplitudes identical. The oracle then multiplies each term by — a where the parity is even, a where it is odd. Nothing about the magnitudes changes; only a pattern of signs is laid down. Yet that pattern is the entire fingerprint of the secret , because it is the -th Walsh function. The circuit below is the standard three-act structure: Hadamards to spread, one oracle call to stamp, Hadamards to focus.
The second Hadamard layer is where the magic happens, and it is pure interference. Measuring right after the oracle would be useless — every amplitude still has the same magnitude , so a measurement returns a uniformly random string that says nothing about . The information sits entirely in the phases, which a basis measurement cannot see. The final converts phase into population: it sums the signed amplitudes with its own sign pattern, and the two patterns reinforce perfectly for the outcome and cancel exactly for every . The bar chart below makes this collapse quantitative for the same shown in the circuit.
The core equations
The whole story is set by the promise on : it is not an arbitrary Boolean function but a linear one, a single inner product with the hidden string. Fixing that promise is what lets one query suffice.
where: — the unknown -bit hidden string to recover; — the -bit query input; — bitwise inner product modulo 2, the parity of the overlapping 1-bits; — addition mod 2 (XOR). The reading: answers a single yes/no question about each input, "is the overlap of with odd?", and probing returns the single bit . The oracle is supplied in the reversible XOR form standard for all quantum query algorithms, writing the answer into a separate register so the map stays unitary.
where: — the -qubit query register; — the single-qubit answer register; — XOR on that answer qubit. The reading: leaves the query register alone and toggles the answer qubit exactly when ; it is its own inverse, . Preparing the answer register in turns that bit-flip oracle into a phase oracle — the phase-kickback trick that all three of Deutsch, Deutsch–Jozsa and Bernstein–Vazirani run on.
where: — the eigenstate of the XOR action; — the phase kicked back onto each query term. The reading: the ancilla is an eigenvector, so it factors out unchanged and the oracle's effect is a pure sign on the query register — exactly the sign we can later interfere. The tool that both builds the superposition and reads it out is the -fold Hadamard, whose matrix elements are the characters of the group .
where: — the single-qubit Hadamard, ; — the output label summed over all -bit strings; — the bitwise inner product in the phase. The reading: setting makes every sign and gives the uniform superposition; a general imprints the parity pattern that is the -th Walsh function. Chaining the first Hadamard layer and the oracle produces the signed superposition — the state the second figure charts on its left.
where: — the sum over all inputs; — the parity-phase fingerprint of . The reading: at this stage every amplitude has magnitude , so a measurement here is uniformly random and worthless — the answer is hidden in the signs. The second Hadamard layer inverts the transform and returns the hidden string as a single deterministic basis state.
where: — the computational basis state encoding the hidden string. The reading: because is its own inverse and is literally the pattern produces from , applying it again undoes the spreading and returns exactly. That collapse rests on one identity — character orthogonality on — which is the true engine of the algorithm.
where: — shorthand for ; — 1 if is the all-zero string, else 0. The reading: for exactly half the inputs give and half give , so the sum is zero; only (that is, ) survives, with full weight . The payoff is a query-count separation that is as sharp as separations get in the exact model.
where: — oracle queries used by the algorithm; — deterministic classical queries needed to learn exactly. The reading: one quantum query replaces classical ones — a gap that grows without bound as the strings get longer, though (as the warning notes) it is a statement about the exact deterministic model.
The goal is to prove the single identity that makes the whole algorithm run: that the standard reversible oracle , when its answer qubit is prepared in the state , acts as a pure phase on the query register and leaves the ancilla untouched. This is what lets us treat not as something that scribbles an answer into a scratch register but as something that stamps a computable sign onto each branch of a superposition, and it is the exact point where quantum querying diverges from classical querying.
Step 1. Act on a single query term with the ancilla in and push it through by linearity: . Why: the oracle is linear, so it acts on the and pieces of the ancilla separately, and each simply gets XORed with .
Step 2. Evaluate the two branches using and (the complement): the state becomes . Why: XOR either leaves the bit alone or flips it, so the ancilla's two kets are always and its complement — the same two labels and , possibly swapped.
Step 3. Read off the sign by cases. If the bracket is ; if it is . Both are captured by , so . Why: flipping the two labels of is the same as multiplying it by , which is exactly how a bit-flip on the ancilla becomes a sign on the whole term — the phase "kicks back" to the query register.
Step 4. Note that the ancilla emerges in unchanged, so it factors out of the entire computation and can be ignored from here on; the query register alone carries . Why: because the ancilla is an eigenvector of 's action, it never entangles with the query register, which is precisely what allows the clean single-register analysis of the next derivation.
Go deeper · Derivation B · 2 minOne query yields the hidden string
Go deeper · Derivation C · 2 minWhy classical querying needs n queries
The purpose is to show that no deterministic classical algorithm can identify with fewer than oracle queries, so that the quantum single-query result is a genuine -to- separation and not an artifact of a weak classical strategy. The argument is linear algebra over the two-element field : each classical query returns one linear equation in the unknown , and pinning down an -dimensional vector requires independent equations. This is the exact, worst-case, deterministic lower bound; the warning below explains why relaxing to randomized bounded-error querying changes the picture.
Step 1. Observe that a classical query at a chosen point returns the single bit , which is one linear constraint on the unknown . Why: the promise that is linear is double-edged — it is what the quantum algorithm exploits, but classically it means each query reveals only one scalar equation, never more.
Step 2. After queries the algorithm holds the linear system , where has the queried points as its rows. A unique solution for exists only if has full column rank, . Why: if the rank were below the solution set would contain at least two candidate strings consistent with all answers, and the algorithm could not decide between them.
Step 3. Bound the rank by the number of rows, . Combining with the requirement forces . Why: rank can never exceed the count of equations, so fewer than queries can never supply the independent constraints needed to isolate .
Step 4. Check that queries also suffice: probing the standard basis vectors returns , reading off the bits of one at a time. So is exactly optimal classically. Why: this constructive matching strategy shows the bound is tight — the classical cost is precisely , against the quantum cost of . *The rank argument over gives the exact/deterministic bound, and the basis-probing strategy shows it is tight at . Note that a randomized classical algorithm sampling random and running Gaussian elimination recovers in expected queries — so this bound is specific to the exact deterministic model.*
Limiting cases
Degenerate inputs and small sharpen exactly what the algorithm is doing and how it connects to its neighbours.
| Case | Result | Meaning |
|---|---|---|
| All-zero hidden string | The oracle applies no phase; returns , correctly reporting — the "constant-zero" overlap with Deutsch–Jozsa. | |
| Single-bit string | The algorithm returns ; classically the query gives the same one bit, but the quantum recipe handles all with the same one query. | |
| reduces to Deutsch | Distinguishing (constant) from (identity/balanced) on one qubit is exactly Deutsch's problem; Bernstein–Vazirani is the -qubit lift. | |
| Same circuit as Deutsch–Jozsa | read all bits vs. read one bit | Identical circuit; DJ asks only "is the outcome ?" while BV interprets the full bitstring as . |
| Randomized classical model | Sampling random and solving the resulting linear system recovers in expected queries, so the flat gap is exact/deterministic only; the recursive variant restores a super-polynomial bounded-error gap. |
Worked examples
1 · Reading a = 101 in a single query (fully worked)
Take and hidden string . Run the full circuit on and show the measurement returns deterministically.
Step 1. First Hadamard layer. Starting from , builds the equal superposition over all eight strings , each amplitude . The ancilla, prepared as , is ready for kickback.
Step 2. Oracle query. With the parity is (bits at the positions where is 1). Phase kickback stamps on each term, giving the sign pattern over of . The state is now . All magnitudes are still ; only signs distinguish it from the pre-oracle state.
Step 3. Second Hadamard layer. The amplitude of outcome is . Test two cases. For : , so every term is and the sum is , giving amplitude . For, say, : , so half the terms are and half , the sum is , and the amplitude is .
Step 4. Measure. Every amplitude vanishes by the same cancellation, so the state is exactly . The measurement returns with probability , recovering the full 3-bit secret in one oracle call — where a classical solver would need three (probing ).
2 · Query accounting for a 20-bit secret (classical vs. quantum)
A hidden string has bits, e.g. . Compare the deterministic classical query cost with the quantum cost, and state what a measurement yields in each shot.
Step 1. Classical, worst case. Each query returns one parity bit . To pin down a 20-dimensional vector over you need 20 linearly independent equations; the simplest choice probes the basis vectors , so that query returns directly. Deterministic classical cost: queries, one per bit.
Step 2. Quantum. The circuit prepares the equal superposition over all strings, applies the oracle once, and inverts the transform. The output register is exactly . Quantum cost: query, and the single measurement returns all 20 bits at once, deterministically.
Step 3. The honest caveat. If instead of exact determinism we allow a small error probability, a randomized classical algorithm can pick random inputs , collect the parities, and solve the linear system by Gaussian elimination — recovering with high probability in rounds of parallel sampling. So the clean -to- statement is about the exact deterministic model; the quantum win over even randomized classical querying is the recursive variant, not this flat one.
Physics of the portrait
The algorithm carries two names, but its intellectual center of gravity is the framework it was born inside — the theory of quantum complexity, which asked for the first time not just can a quantum computer do something, but how much faster than a classical one, made rigorous.
Origins · 1 minUmesh Vazirani and how this was discovered
Umesh Vazirani is one of the founders of quantum computational complexity theory and, through decades at UC Berkeley, an advisor to a large fraction of the field. Trained under Manuel Blum, he first made his name in classical theory — randomized algorithms, expander graphs, and the widely used algorithms textbook he wrote with Dasgupta and Papadimitriou. His decisive contribution to quantum computing came in 1993 with his PhD student Ethan Bernstein: the paper "Quantum Complexity Theory," presented at STOC and expanded in SIAM J. Comput. (1997). That paper did three lasting things. It defined the class BQP — the problems a quantum computer solves efficiently with bounded error — and showed it is robust, giving quantum computing a complexity-theoretic home. It constructed the universal quantum Turing machine rigorously. And it introduced the hidden-linear-string problem solved here, together with its recursive Fourier-sampling variant, exhibiting the first super-polynomial oracle separation between quantum and classical computation. The flat algorithm — a Hadamard sandwich around one oracle call — is a corollary, but its clarity made it the standard classroom gateway to quantum speedups. Vazirani's fingerprints are everywhere downstream: Simon's algorithm, which he helped shepherd, directly inspired Shor's factoring algorithm the following year, so the line from this modest one-query trick to the breaking of RSA runs straight through his group at Berkeley.
Going further
Texts. Nielsen & Chuang, Quantum Computation and Quantum Information, §1.4.3–1.4.4 (the hidden-string / Deutsch–Jozsa circuit) · Bernstein & Vazirani, "Quantum Complexity Theory," SIAM J. Comput. 26(5):1411–1473 (1997), orig. STOC 1993 · Preskill, Lecture Notes for Physics 219, Ch. 6 (quantum algorithms) · Watrous, The Theory of Quantum Information and lecture notes on query algorithms · Kitaev, Shen & Vyalyi, Classical and Quantum Computation, §13 (abelian HSP) · Mermin, Quantum Computer Science, Ch. 2. The orthogonality relation is character orthogonality on . Key checkpoints: (i) the phase-kickback identity from equation (2); (ii) the four-line circuit evolution ending in ; (iii) the worked case , where the signed amplitudes with signs Walsh-transform to a unit peak at ; (iv) the classical rank bound and the matching basis-probing strategy. The query-complexity claims — one quantum vs. exact classical, the randomized-classical solvability of the flat problem, and the super-polynomial separation only in the recursive variant — are from Bernstein & Vazirani (1997). No empirical constants are quoted.
Prerequisites. Quantum Parallelism and the Oracle Model · Deutsch's Algorithm · Deutsch–Jozsa Algorithm. Leads to. Simon's Algorithm · Quantum Fourier Transform · Hidden Subgroup Problem · Shor's Factoring Algorithm.
Ask the tutor: ask me to trace the circuit for any hidden string you pick, show the character sum cancelling a wrong outcome to zero, or explain how the same Fourier-sampling idea grows into Simon's and Shor's algorithms.
Is Bernstein–Vazirani really an "exponential" speedup?
It depends on how you count and which classical model you compare against. In the exact, deterministic query model the gap is quantum query versus classical queries — the quantum cost is independent of the secret's length while the classical cost grows with it. That is a dramatic and unbounded query gap, and it is the honest headline. But it is only linear in , not exponential in the usual runtime sense, and crucially a randomized classical algorithm cracks the flat problem in expected queries by sampling and Gaussian elimination. So flat BV does not separate the bounded-error complexity classes BPP and BQP. The genuinely exponential (super-polynomial) separation is the recursive Bernstein–Vazirani problem from the same paper, which resists the randomized shortcut.
Why prepare the ancilla in the minus state rather than zero?
Because the minus state is an eigenvector of the oracle's bit-flip action, and that is what converts the oracle from something that writes an answer into a scratch register into something that stamps a phase on the query register. Concretely, : the ancilla comes out unchanged and the sign appears on the query term. If you instead used , the oracle would entangle the answer qubit with the query register, and the phases the algorithm needs would never appear where the Hadamards can act on them. You get for free by starting the ancilla in and Hadamarding it alongside the query qubits. This "phase kickback" is the shared trick behind Deutsch, Deutsch–Jozsa, and Grover.
Where exactly does the probability-1 certainty come from?
From perfect destructive interference, guaranteed by character orthogonality. After the second Hadamard layer, the amplitude of a candidate outcome is . When the exponent is zero for every , so all terms are and add up to full weight — amplitude exactly . When the string is nonzero, and for a nonzero string precisely half of the inputs give and half give , so the terms cancel in exact pairs to zero. There is no leftover amplitude, no small error term, no need to repeat: the ideal circuit returns on the first and only shot. Any deviation you see on real hardware is noise, not the algorithm.
The circuit is identical to Deutsch–Jozsa — so what is different?
The hardware and the circuit are the same ; what differs is the promise on and what you do with the measured bits. Deutsch–Jozsa promises is either constant or balanced and asks a one-bit question — is the outcome the all-zeros string or not? Bernstein–Vazirani makes the stronger promise that is linear, , and then reads the entire measured bitstring as the hidden . So BV extracts bits of information where DJ extracts one, and it does so precisely because the linear promise makes the post-Hadamard state a single clean basis state rather than merely " or not." BV is, in effect, the same experiment interrogated more finely.
Can the oracle really be built without knowing a?
Yes — that is the whole point of the black-box model, and it is not a cheat. The oracle is a fixed circuit that someone (or some physical process) has wired up to compute for a specific ; concretely, is just a set of CNOT gates, one from each query qubit where into the answer qubit. The person running Bernstein–Vazirani is given this circuit as a sealed box and is not told — their job is to discover it by querying. The algorithm never inspects the oracle's internals; it only feeds in a superposition and reads the output. This is exactly the setting in which "one query" is a meaningful and fair measure of cost, and it is why BV is a statement about query complexity rather than about circuit construction.
How does this little algorithm connect to Shor's factoring algorithm?
Through Fourier sampling and the hidden subgroup problem. The Hadamard layer in Bernstein–Vazirani is the quantum Fourier transform over the group , and the oracle's phase is a single character of that group; the algorithm works by sampling that character and reading off which one it is. Replace with the cyclic group and the Walsh–Hadamard transform with the full QFT, and the same "prepare a periodic/character state, Fourier-transform, measure" recipe becomes phase estimation and period finding — the engine of Shor's algorithm. Bernstein–Vazirani and its sibling Simon's algorithm are the abelian base cases; Shor is the same idea deployed on the group that encodes modular exponentiation. Learning BV first is why the QFT-based algorithms later feel inevitable rather than magical.