Running Deutsch–Jozsa and Bernstein–Vazirani in code
Short Qiskit sketches of the circuits in this lab, labelled clearly as not run here, and a checklist for verifying them against the lab’s numbers.
7 min read · reviewed December 2026
What the lab guarantees, and what these sketches do not
The simulator in the lab is exact and was tested for every case on this page: constant and balanced functions for n from 2 to 6, every Bernstein–Vazirani secret on a grid of sizes, and every Simon secret for n from 2 to 5. The code below is a sketch written for readers who want to reproduce the circuits in a framework. It was not run when this page was written, so check it against your installed version. Qiskit changes its interfaces often.
Code on this page is labelled not run. The numbers in the tables are computed by the lab’s own simulator.
Bernstein–Vazirani in Qiskit, sketch (not run)
Qiskit orders bits with qubit 0 on the right of a printed string, which is why the secret is reversed. In the lab, bit 1 is on the left. Mismatched ordering is the most common reason a hand-written circuit appears to return the wrong secret.
from qiskit import QuantumCircuit
s = '10110' # the secret, written with qubit 0 on the right
n = len(s)
qc = QuantumCircuit(n + 1, n)
qc.x(n); qc.h(range(n + 1)) # answer qubit in |−>, inputs in uniform superposition
for i, bit in enumerate(reversed(s)):
if bit == '1': qc.cx(i, n) # the oracle: add s·x into the answer qubit
qc.h(range(n))
qc.measure(range(n), range(n))
Deutsch–Jozsa with a balanced oracle, sketch (not run)
For a constant oracle, apply nothing for f = 0 or X on the answer qubit for f = 1, and expect all zeros every time.
qc = QuantumCircuit(n + 1, n)
qc.x(n); qc.h(range(n + 1))
qc.cx(0, n) # balanced oracle: f(x) = first bit of x (qubit 0 here)
qc.h(range(n))
qc.measure(range(n), range(n))
# Expect a reading other than all zeros every time.
A checklist for comparing with the lab
- Bit order: decide which end is qubit 0 before you compare strings.
- Count shots: with a perfect simulator a Bernstein–Vazirani run returns the secret every time. A single wrong reading means a circuit or ordering mistake.
- Deutsch–Jozsa: every constant oracle gives all zeros and every balanced oracle gives a non-zero reading.
- Simon: every reading y must satisfy y·s = 0 modulo 2.
- Noise: a real device will return wrong readings. The Noise Lab shows how errors accumulate.
Where to go next
The Circuit Builder has ready-made examples for both algorithms, and the Circuit Converter can translate a circuit between frameworks. For a longer walk-through in code, see the lesson on Qiskit, Deutsch–Jozsa and Bernstein–Vazirani.
Try it
Questions
Why is my circuit returning the reversed secret?
Almost always bit ordering. Qiskit prints qubit 0 on the right, while the lab prints the first bit on the left.
Can I run these on real hardware?
Small versions can be run, but readings will contain errors. The ideal results in this lab show what a noise-free machine returns.
Read next
Sources and further reading
- D. Deutsch, “Quantum theory, the Church–Turing principle and the universal quantum computer” (1985)
- D. Deutsch and R. Jozsa, “Rapid solution of problems by quantum computation” (1992)
- R. Cleve, A. Ekert, C. Macchiavello and M. Mosca, “Quantum algorithms revisited” (1998)
- E. Bernstein and U. Vazirani, “Quantum complexity theory” (1993, journal version 1997)
- D. Simon, “On the power of quantum computation” (1994)
- M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, section 1.4
Check the original sources for exact citations.