Quantum
Module 7 · Algorithms · Lesson 2

Bernstein-Vazirani

Same recipe as Deutsch-Jozsa, slightly different promise. The quantum algorithm reads a hidden bitstring in a single query — classical needs n queries.

8 min read · Lesson 22 of 32

The Bernstein-Vazirani problem (1992) is a cousin of Deutsch-Jozsa. It uses almost the same circuit but solves a different promise problem — and it’s arguably a cleaner illustration of how quantum interference extracts structure from an oracle in one query.

The problem

You’re given a function f:{0,1}n{0,1}f: \{0,1\}^n \to \{0,1\} with the promise that

f(x)=sxmod2f(x) = s \cdot x \mod 2

for some fixed, hidden string s{0,1}ns \in \{0,1\}^n. Here sxs \cdot x is the dot product isixi\sum_i s_i x_i — the bitwise AND followed by a parity check. Your job: find ss.

Classically, you need nn queries: each query f(ei)f(e_i) (where eie_i is the iith basis string) returns sis_i directly. So you read each bit of ss one at a time — a total of nn queries.

Quantum: one query.

The algorithm

The circuit is almost identical to Deutsch-Jozsa:

  1. Prepare nn qubits in 0n|0\rangle^{\otimes n}.
  2. Apply HH to each qubit.
  3. Apply the phase oracle: x(1)f(x)x=(1)sxx|x\rangle \mapsto (-1)^{f(x)}|x\rangle = (-1)^{s \cdot x}|x\rangle.
  4. Apply HH to each qubit.
  5. Measure.

The punchline: after step 5, you measure s|s\rangle with certainty. The hidden string is the measurement outcome. One query, guaranteed.

Why it works

The key identity is about the Hadamard transform on all nn qubits:

Hnx=12ny{0,1}n(1)xyyH^{\otimes n}|x\rangle = \frac{1}{\sqrt{2^n}} \sum_{y \in \{0,1\}^n} (-1)^{x \cdot y} |y\rangle

After step 2, we have a uniform superposition. After step 3 (the oracle), the state is:

12nx(1)sxx=12nx(1)sxx\frac{1}{\sqrt{2^n}} \sum_x (-1)^{s \cdot x} |x\rangle = \frac{1}{\sqrt{2^n}} \sum_x (-1)^{s \cdot x} |x\rangle

Now apply HnH^{\otimes n}:

Hn12nx(1)sxx=12nx(1)sxy(1)xyyH^{\otimes n} \cdot \frac{1}{\sqrt{2^n}} \sum_x (-1)^{s \cdot x} |x\rangle = \frac{1}{2^n} \sum_x (-1)^{s \cdot x} \sum_y (-1)^{x \cdot y} |y\rangle

=12ny[x(1)(s+y)x]y= \frac{1}{2^n} \sum_y \left[\sum_x (-1)^{(s + y) \cdot x}\right] |y\rangle

The inner sum is 2n2^n if s+y=0(mod2)s + y = 0 \pmod 2 (i.e., y=sy = s), and 00 otherwise. So the final state is exactly s|s\rangle. Measure and you read off the hidden string directly.

Why this is beautiful

The same three-step recipe — Hadamard, oracle, Hadamard — reads a hidden string in one query. It feels almost like cheating. Classically, you’d query f(100)f(10\ldots0), f(0100)f(010\ldots0), etc., one bit at a time. Quantumly, the oracle acts on the full superposition at once, the phases it stamps encode the entire bitstring, and the final Hadamard layer transforms those phases into a single definite basis state — the hidden ss.

The core mechanism behind all of the “Hadamard-oracle-Hadamard” algorithms (Deutsch-Jozsa, Bernstein-Vazirani, Simon’s) is the same:

This “phase pattern → position pattern” is a recurring theme. Grover’s search, Shor’s algorithm, and the quantum Fourier transform all use variations of it.

Quick check
What does Bernstein-Vazirani find in a single quantum query?
Quick check
What's the core trick shared by Deutsch-Jozsa, Bernstein-Vazirani, and Simon's algorithms?

What’s next

Simon’s algorithm applies the same recipe to find a hidden period — and crucially, its quantum speedup is exponential even against randomized classical algorithms. That’s a hint that some quantum speedups are genuine, not just artifacts of insisting on zero error.