Bernstein–Vazirani step by step: reading a hidden string in one query
The same circuit as Deutsch–Jozsa, applied to a different promise, returns a whole n-bit secret with certainty. See why the Hadamard layer is a decoder.
9 min read · reviewed December 2026
The problem
A hidden string s of n bits defines f(x) = s·x ⊕ b, where s·x is the parity of the bits of x at the positions where s has a 1, and b is a constant bit. You may query f. Find s. A classical algorithm can read s one bit at a time by querying the n inputs with a single 1. If b is known to be 0, that costs n queries. If b is unknown, one more query, at x = 0, reveals it. The quantum circuit needs one query for any b.
Why the circuit works
The circuit is identical to the Deutsch–Jozsa circuit. After the oracle, the input register holds the signs (−1)^(s·x ⊕ b), which equal (−1)^b times (−1)^(s·x). The factor (−1)^b is one sign for the whole register, so it is a global phase and cannot be detected. What remains is the state in which the amplitude of x has sign (−1)^(s·x).
That state is exactly what a Hadamard layer produces when applied to the basis state |s⟩. Hadamards are their own inverse, so applying them again returns |s⟩. Measuring the input register reads s with 100 percent probability.
H^⊗n |s⟩ = (1/√N) Σ_x (−1)^(s·x) |x⟩
so H^⊗n [ (1/√N) Σ_x (−1)^(s·x) |x⟩ ] = |s⟩
Why the offset b does not matter
Flip the offset button in the lab’s Bernstein–Vazirani tab. The chance of reading s stays at 100 percent. The quantum state changes only by an overall sign, which no measurement can see, so b is invisible to the algorithm. Classically, b matters because it shifts every answer.
| Secret s | Chance of reading s (b = 0) | Chance of reading s (b = 1) |
|---|---|---|
| 0000 | 100.00% | 100.00% |
| 0001 | 100.00% | 100.00% |
| 1010 | 100.00% | 100.00% |
| 1111 | 100.00% | 100.00% |
The classical side, in the lab
The table on the right of the tab shows the classical method. It queries f at 00…0 to learn b, then at each one-hot string. The answer at a one-hot string e_i is s_i ⊕ b, so s_i is that answer XOR the answer at zero. The lab counts n + 1 queries when b is unknown.
What to take from it
- The Hadamard layer acts as a decoder from signs back to a bit string.
- One query returns n bits, but you must know in advance that f has the linear form.
- Bernstein and Vazirani used this problem to show that a quantum machine can differ from a classical one in a precise complexity-theoretic sense, relative to an oracle.
The speed-up is n queries to 1. That is a polynomial gap, not an exponential one, and a randomised classical algorithm cannot close it either, because each query returns one bit while the answer has n.
Try it
Questions
Is Bernstein–Vazirani really faster than classical?
In query count, yes: 1 against n. Each classical query returns a single bit and the secret has n bits, so no classical method, randomised or not, can do it in fewer than about n queries.
What if f is not linear?
Then the promise is broken and the output is not a reliable secret. The lab does not model that case.
Read next
Sources and further reading
- D. Deutsch, “Quantum theory, the Church–Turing principle and the universal quantum computer” (1985)
- D. Deutsch and R. Jozsa, “Rapid solution of problems by quantum computation” (1992)
- R. Cleve, A. Ekert, C. Macchiavello and M. Mosca, “Quantum algorithms revisited” (1998)
- E. Bernstein and U. Vazirani, “Quantum complexity theory” (1993, journal version 1997)
- D. Simon, “On the power of quantum computation” (1994)
- M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, section 1.4
Check the original sources for exact citations.