Part V · Chapter 27

Bernstein–Vazirani Algorithm

Read ~16 min Prerequisites Ch. 26 Tools hidden-string widget, code cell Live
Basic

Same circuit as Ch. 26, one more time — but instead of classifying f into one of two buckets, this version's oracle hides an entire n-bit secret, and the algorithm reads the whole thing out, exactly, from a single query.

The problem

The oracle computes f(x) = s·x mod 2 — the bitwise dot product of x with some hidden n-bit string s, mod 2 — for an s you don't know and need to find. Classically this takes n separate queries: query x=1000...0 to learn s's first bit, x=0100...0 for the second, and so on, one bit revealed per query. The quantum algorithm needs exactly 1.

Intermediate
Fig. 27.1 — set the hidden string, run the circuitrecovered exactly, every time

Hidden s = (click to change it):

The blue bar always lands exactly on the binary string you set as s — try every corner case: all zeros, all ones, alternating bits. One query, every time, perfect recovery.

Why this works: it's Deutsch–Jozsa's math, reused

Run through the identical H–oracle–H derivation from Ch. 26, but now with f(x)=s·x mod 2 instead of a generic balanced/constant function. The final amplitude on basis state y works out to be exactly 1 when y=s, and exactly 0 for every y≠s. There's no new mathematical machinery here at all — Bernstein–Vazirani is a specific, clever choice of oracle slotted into the exact circuit Ch. 26 already built.

Advanced

What's genuinely new here

Deutsch–Jozsa only ever answers a single yes/no question (constant or balanced); Bernstein–Vazirani extracts n full bits of hidden information in that same one query. This is the first algorithm in the book that demonstrates a quantum computer reading out a complete, exact classical answer — not just a property of one — exponentially faster than any classical strategy. It's also a direct structural preview of Simon's algorithm and the hidden subgroup problem more broadly, the family of problems Shor's algorithm (Ch. 31) ultimately belongs to: "find a hidden algebraic structure using interference from a superposition of queries" is the same recipe, just with progressively richer structure being hidden.

Fig. 27.2 — code, runs in your browser
Every one of the 4 test strings — including the two extreme corner cases — recovers exactly, in a single simulated query.
Check your understanding

What does the Bernstein-Vazirani oracle compute?

f(x) = s·x mod 2 — the bitwise dot product of the input x with a hidden string s, summed mod 2.

Check your understanding

How many queries does the classical and the quantum approach each need to find an n-bit hidden string?

Classically, n queries (one bit revealed per query). Quantum: exactly 1.

Check your understanding

What circuit does Bernstein-Vazirani reuse from Ch. 26, and what's actually new?

The exact same H–oracle–H circuit. What's new is just the specific oracle (f(x)=s·x mod 2) plugged into it, and the fact that the answer it extracts is a full n-bit string rather than a single yes/no classification.

Check your understanding

After running the circuit, which measurement outcome do you expect, and with what probability?

The outcome y=s exactly, with probability 1 — every other outcome has probability exactly 0.

Check your understanding

Why is Bernstein-Vazirani described as a preview of the "hidden subgroup problem" family that Shor's algorithm belongs to?

Both follow the same recipe: use superposition and interference to extract a hidden algebraic structure (a string here, a period in Shor's case) from a black-box function, using far fewer queries than any classical strategy.

Review queue

Scroll back to a chapter's quiz cards to review them once they're due — a full cross-chapter queue view is a planned upgrade.