∑ | Quantum Computing
Chapter 10 / 20

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

If s=1011, use Z on qubits 0,1,3 under the convention that the least-significant bit is qubit 0; final measurements reveal the corresponding string.

4. Reasoning flow

  1. Define the initial register and its basis states.
  2. Apply the transformations in their intended order.
  3. Track amplitude magnitudes and relative phases.
  4. Measure in the basis that reveals the desired property.
  5. 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 Python

5. 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.

◈ QuantumVerse Hub