Deutsch–Jozsa: constant or balanced in one query
A small instance of the first algorithm with a proven quantum advantage in query count.
Intermediate · 3 qubits · 7 gates · 1 two-qubit · depth 4 · reviewed November 2026
Open in the Circuit BuilderWhat is happening
The algorithm decides whether a hidden function is constant (same output for every input) or balanced (half 0, half 1). A classical computer may need many queries. The quantum version needs one.
Here q0 and q1 are the input register and q2 is the helper. The CNOT from q0 to q2 is the oracle for the balanced function f(x) = x0. If the final measurement of the inputs is all zeros the function is constant, otherwise it is balanced. Our result is 10, so the function is balanced.
Step by step
1. Prepare
X then H on the helper puts it in |−⟩. H on the inputs makes an equal superposition of all inputs.
2. Oracle
CNOT from q0 to the helper writes f(x) into the phase of each input through phase kickback.
3. Interfere
H on the inputs turns the phase pattern into a single measurable outcome.
Expected result
Measuring q0 and q1 gives 10 with 100% probability, which signals a balanced function. Delete the CNOT to model a constant function and the outcome becomes 00.
Qubit 0 is the leftmost bit. In Qiskit, bitstrings are printed in the reverse order.
Qiskit code
from qiskit import QuantumCircuit from math import pi qc = QuantumCircuit(3, 2) qc.x(2) qc.barrier() qc.h(0) qc.h(1) qc.h(2) qc.barrier() qc.cx(0, 2) qc.barrier() qc.h(0) qc.h(1) qc.barrier() qc.measure(0, 0) qc.measure(1, 1) # Qiskit prints bit strings with qubit 0 on the RIGHT; this site writes qubit 0 on the LEFT.
Try this
- Delete the oracle CNOT and rerun.
- Make a different balanced function with a CNOT from q1.
- Use both CNOTs and work out which function you made.
Gates used
Related reading
Questions
Is Deutsch–Jozsa useful in practice?
No, it is a teaching example. The gap exists only against deterministic classical algorithms in a query model, but it was the first clear demonstration of quantum advantage in principle.