12 min read
The Bernstein-Vazirani Algorithm
Same circuit as Deutsch-Jozsa, one query, but this time it reads out an entire hidden bitstring instead of one bit of information.
Suggested before this lesson
Bernstein-Vazirani uses the exact same circuit shape as Deutsch-Jozsa. What changes is the oracle, and what you get out of it.
You're given an oracle that computes (the bitwise dot product, mod 2) for some hidden bitstring you don't know. Classically, recovering all bits of takes separate queries: query and you learn , query and you learn , and so on, one bit per query. Bernstein-Vazirani recovers the entire string in a single query.
This is precisely the linear-oracle recipe from the oracles lesson: from input qubit into the ancilla, for every where . Skip the entirely for any where : that input never gets XORed in, exactly matching what means.
Take the hidden string over 2 input qubits, ancilla on qubit 2: every input bit matters, so both s are present:
Circuit
x
h
cx
Step through the circuit: after gate 8 of 8
9 / 9
Measurement Probabilities
0.0%
0.0%
0.0%
50.0%
0.0%
0.0%
0.0%
50.0%
Exact State
| Basis state | Re | Im | Probability |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0.707 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| -0.707 | 0 |
Unlike Deutsch-Jozsa's "is it all-zero or not," here every input qubit lands on a definite, predictable value: qubit measures , with certainty, for every ; one query and the whole string falls out. (Reading the basis-state label itself: this course writes kets as , rightmost character is qubit 0, the convention from way back in the What is a Qubit? lesson's Bloch-sphere work. It's simplest to just read the per-qubit values straight off the circuit rather than the label string.)
Live preview of the circuit detected in your code
Python
Python