Bernstein–Vazirani algorithm

On this page 6

The Bernstein–Vazirani algorithm is a quantum algorithm that identifies an unknown n-bit secret string s hidden inside an oracle computing f(x) = s·x mod 2, using exactly one oracle query, whereas any classical method needs n queries. Among quantum algorithms in quantum computing, it is the standard illustration of how phase kickback and interference extract global information about a function in a single evaluation.

Ethan Bernstein and Umesh Vazirani introduced the problem in 1993 as part of their work on quantum complexity theory, where a recursive version provided an early superpolynomial oracle separation between quantum and classical probabilistic computation. Today the basic algorithm is used mainly for teaching and as a hardware benchmark.

The problem the algorithm solves

The task is a hidden linear function problem. A black box accepts an n-bit input x and returns a single bit equal to the bitwise inner product of x with s, modulo 2. The secret s is fixed but unknown, and the goal is to determine all n of its bits while making as few calls to the black box as possible.

A classical algorithm can query the inputs 100…0, 010…0 and so on, learning one bit of s per query. Because each answer is a single bit, information theory shows that no deterministic or randomized classical strategy can do better than n queries. The quantum version obtains every bit at once by querying a superposition of all inputs.

The table compares the query cost of the classical and quantum approaches for several string lengths.

Secret length nClassical queriesQuantum queries
4 bits41
16 bits161
64 bits641
n bitsn1
Query count by approach

How the circuit recovers the secret string

The circuit uses n input qubits and one ancilla qubit. Its logic relies on phase kickback: when the ancilla is prepared in the |−⟩ state, an oracle that would flip the ancilla instead multiplies the input state by (−1)^f(x), writing the function value into the phase of each branch of the superposition.

  1. Initialize the input register to |0…0⟩ and the ancilla to |1⟩.
  2. Apply a Hadamard gate to every qubit, producing a uniform superposition of all 2^n inputs and putting the ancilla in |−⟩.
  3. Query the oracle once, which attaches the phase (−1)^(s·x) to each input string x.
  4. Apply Hadamard gates to the input register again, which maps the phase pattern back to the single basis state |s⟩.
  5. Measure the input register, which returns s with certainty in the ideal, noise-free case.

The final step works because the phase pattern (−1)^(s·x) is exactly the signature of the basis vector |s⟩ after the n-qubit Hadamard transform. All other amplitudes cancel through destructive interference, so the measurement is deterministic rather than probabilistic.

Inside the oracle, the linear function is built from one CNOT per 1-bit of s, each controlled by the corresponding input qubit and targeting the ancilla. The table shows the resulting resource counts.

ResourceCountComment
Qubitsn + 1Inputs plus one ancilla
Hadamard gates2n + 1Two layers plus ancilla preparation
CNOT gatesHamming weight of sBetween 0 and n
Oracle calls1Versus n classically
MeasurementsnAncilla is not measured
Circuit resources for an n-bit secret

Relation to Deutsch–Jozsa and Simon’s algorithm

The Bernstein–Vazirani algorithm uses the same circuit skeleton as the Deutsch–Jozsa algorithm; only the promise on the oracle differs. Deutsch–Jozsa decides a yes-or-no property, while Bernstein–Vazirani reads out a full n-bit answer. Simon’s algorithm extends the idea to a hidden XOR period and achieves an exponential separation against randomized classical algorithms.

AlgorithmPromise on fOutputClassical cost
Deutsch–JozsaConstant or balancedOne bit of informationExponential deterministically
Bernstein–VaziraniLinear, f(x) = s·xFull string sn queries
Simon’s algorithmTwo-to-one with period sPeriod sExponential, even randomized
Shor’s algorithmPeriodic modular functionPeriod, then factorsSuperpolynomial in best known methods
Comparison of early oracle algorithms

Formally, these problems are all instances or relatives of the hidden subgroup problem over the group of n-bit strings under XOR. The single-query speedup of Bernstein–Vazirani is only linear, not exponential, which is why its significance lies in illustrating the mechanism rather than in practical advantage.

Benchmarking use and practical limits

Because the ideal output is one known bit string with probability 1, the algorithm makes a convenient benchmark. Any deviation from the expected result directly measures accumulated gate, crosstalk and readout errors. Hardware vendors and researchers run it at increasing widths to show how success probability degrades with qubit count and connectivity.

The benchmark is sensitive to qubit routing. On devices with limited connectivity, the CNOTs that all target one ancilla often require additional SWAP gates, which raise circuit depth and error. The table summarizes typical failure modes.

SymptomLikely causeMitigation
Single wrong bitsReadout errorMeasurement error mitigation
Errors grow with weight of sTwo-qubit gate infidelityBetter calibration, fewer CNOTs
Errors grow with widthRouting overheadChoose a central ancilla qubit
Random stringsDecoherence before measurementShorter circuits, dynamical decoupling
Common sources of incorrect output
  • The speedup assumes oracle access. If the circuit for f is written out explicitly, s can be read from its gate list, so the problem has no practical value outside the query model.
  • A classical simulation is easy. The circuit uses only Clifford gates, so the Gottesman–Knill theorem guarantees efficient classical simulation.
  • The recursive version matters theoretically. Only the recursive Fourier sampling variant gives the superpolynomial separation in the original paper.

Stripped to the essentials

Imagine a locked box that hides a secret code made of zeros and ones. A normal computer can ask the box one question at a time and learns one digit per question, so a 20-digit code takes 20 questions.

The Bernstein–Vazirani algorithm lets a quantum computer ask about every possible input at once and then combine the answers so that the wrong possibilities cancel each other out. After a single question, the full code appears. It does not solve a useful real-world task, but it shows clearly why quantum computers can sometimes need far fewer steps.

Where it is applied

A university quantum computing course uses the algorithm as its first hands-on hardware exercise. Each student group chooses a secret string such as 101101, builds the six-qubit oracle from CNOT gates and runs the circuit on a cloud-accessible superconducting device for 2,000 shots.

In the ideal simulator, every shot returns 101101. On the real device, the correct string appears in about 70 to 85 percent of shots, depending on which physical qubits the compiler selects. Students then repeat the run with a string of higher Hamming weight and observe the success rate fall, which turns an abstract speedup into a concrete lesson about noise, routing and the gap between textbook circuits and current hardware.