Bernstein–Vazirani algorithm
Recover a hidden bit string from a parity oracle.
1. Core idea
A hidden string s defines a parity function. Querying its phase oracle on a uniform superposition creates a sign pattern. A second Hadamard layer converts this pattern into the basis state |s⟩. The query comparison depends on the specified oracle model.
2. Mathematical foundation
The notation describes a precise relationship among states, transformations or probabilities. Identify each symbol, check the dimensions of the objects, and distinguish amplitudes from the probabilities produced by a measurement.
3. Worked example
4. Reasoning flow
- Define the initial register and its basis states.
- Apply the transformations in their intended order.
- Track amplitude magnitudes and relative phases.
- Measure in the basis that reveals the desired property.
- Interpret the result and check the resource assumptions.
Qiskit implementation
Run this standalone Python script with Qiskit installed. It uses exact statevector simulation rather than noisy hardware shots.
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
secret = '1011'
qc = QuantumCircuit(len(secret))
qc.h(range(len(secret))) # uniform superposition
for q, bit in enumerate(reversed(secret)):
if bit == '1':
qc.z(q) # phase oracle for parity with secret
qc.h(range(len(secret))) # recover secret by interference
print(qc.draw())
print(Statevector.from_instruction(qc).probabilities_dict())
# Expected: {'1011': 1.0}
Download runnable Python5. Check your understanding
Practice question
Write the sign (−1)^{s·x} for every two-bit x when s=10.
6. What to watch out for
Ideal-state calculations assume perfect gates and measurements. When assessing an algorithm, separate its mathematical guarantee from the cost of building oracles, preparing inputs, repeating experiments and correcting physical noise.