Free Qiskit tutorial · Beginner
The Bernstein-Vazirani algorithm in Qiskit: find a secret in one query
A function hides a secret string of bits. Classically you need one query per bit. Bernstein-Vazirani reads the whole string in one.
- To work through
- 15 min
- Level
- Beginner
- Tested on
- Qiskit 2.6
- Runs on your laptop
- Free
The idea
Bernstein-Vazirani, in plain words
The oracle computes f(x) = s · x mod 2: it ANDs your input with a secret string s, bit by bit, and returns whether the number of 1s is odd. To learn s classically you ask n questions, one per bit: 1000, 0100, 0010 and so on.
The quantum circuit is the same shape as Deutsch-Jozsa's: inputs in superposition, the output qubit in |−⟩, one call to the oracle, Hadamards, measure. This time the phases spell out s itself, so the measurement reads the secret directly.
The code
The whole script, ready to run
Install Qiskit and its simulator once, save the script as bernstein_vazirani.py and run it with python bernstein_vazirani.py. It runs on Qiskit's free simulator, so you don't need an account or a quantum computer.
Install onceTerminal
pip install qiskit qiskit-aerbernstein_vazirani.pyPython · Qiskit
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
secret = "1011"
n = len(secret)
qc = QuantumCircuit(n + 1, n)
qc.x(n)
qc.h(range(n + 1))
for qubit, bit in enumerate(reversed(secret)): # Qiskit counts qubits from the right
if bit == "1":
qc.cx(qubit, n) # the oracle: f(x) = secret . x mod 2
qc.h(range(n))
qc.measure(range(n), range(n))
sim = AerSimulator(seed_simulator=7)
counts = sim.run(transpile(qc, sim), shots=1000).result().get_counts()
print(counts)
print("found it" if counts == {secret: 1000} else "something is off")What it printedSeed 7 · 1,000 shots
{'1011': 1000}
found itWhat happened
What the output shows
All 1,000 shots read 1011, the secret, from a single call to the oracle. A classical program needs four calls for four bits, and n calls for n bits.
Note the reversed(secret). Qiskit numbers qubits from the right when it prints results, so qubit 0 is the last character of the string. Leave the reversal out and you'll read the secret backwards: it's the most common Qiskit mix-up.
Every 1 in the secret becomes a CNOT onto the output qubit. With the output in |−⟩, each CNOT kicks a minus sign back to its input qubit, and the final Hadamard turns that sign into a 1.
Try next
Three things to try next
- Try a longer secret, like 110010101, and check it still takes one run.
- Take out reversed() and watch the answer come back backwards.
- Add a little noise with a real backend or Aer's noise models, and see how many shots it takes to be sure of the secret.
Take it further · Project 05
Build Grover's Search for real
Grover's search builds bigger oracles than this one and repeats them to find what they mark. The project codes it from scratch, then uses it to crack a tiny password space.
Keep going
More free Qiskit tutorials
Questions
Bernstein-Vazirani in Qiskit
What does the Bernstein-Vazirani algorithm do?
It finds a hidden bit string s from an oracle that returns s · x mod 2, using one query. A classical algorithm needs n queries for an n-bit string.
Why does Qiskit show my bit string backwards?
Qiskit is little-endian: qubit 0 is the rightmost bit of a result like '1011'. Reverse the string when you map characters to qubits, as the code here does.
How is it different from Deutsch-Jozsa?
The circuit is the same shape. Deutsch-Jozsa tells two kinds of function apart; Bernstein-Vazirani reads out the hidden string that defines the function.
Your first circuit is 10 minutes away
Stop watching quantum. Start shipping it.
$0 today. 7 days free, then $7.99 a week or $190 a year. Cancel any time.