Deutsch-Jozsa: constant or balanced in one query
30 min
The problem
A function f takes n bits and returns one bit. You are promised it is either constant (the same answer for every input) or balanced (0 for exactly half of the inputs). You may only learn about f by calling it, through an oracle. A deterministic classical algorithm can need 2n−1 + 1 calls before it is certain.
The quantum circuit
- Put the n input qubits in an equal superposition of all inputs with Hadamards.
- Prepare an output qubit y in |−⟩ = (|0⟩ − |1⟩)/√2 with
x ythenh y. - Call the oracle once: it maps |x⟩|y⟩ to |x⟩|y ⊕ f(x)⟩. Because y is |−⟩, this multiplies each input by (−1)f(x), a phase kickback.
- Apply Hadamards to the inputs again and measure them.
The probability of reading all zeros is |2−n Σx (−1)f(x)|²: 1 when f is constant, 0 when it is balanced. One call decides.
The lab
The oracle is f(x) = x0 xor x1 xor x2, built from three CNOTs onto y. It is balanced, so the inputs never read 000; for this f they read 111 on every shot, as in our run. Delete the three cx lines and f becomes the constant 0, and the inputs read 000.
In perspective
The separation is against deterministic algorithms. A randomised classical algorithm that tries a few random inputs is right with high probability, so Deutsch-Jozsa is a clean demonstration of interference, not a practical speed-up.
HiQ lab 1
Deutsch-Jozsa with a balanced oracle
Decide whether f(x) = x0 xor x1 xor x2 is constant or balanced with one call to the oracle.
// Deutsch-Jozsa with a balanced oracle f(x) = x0 xor x1 xor x2.// A constant oracle always reads 000; a balanced one never does.// Delete the three cx lines to make the oracle constant and see 000.program deutsch_jozsaqubits x[3], ybits m[3]x yh yrepeat i in 0..3 { h x[i]}// Oracle: y ^= f(x).cx x[0], ycx x[1], ycx x[2], yrepeat i in 0..3 { h x[i] measure x[i] -> m[i]}- What you should see
- The input bits read 111 on every shot: not 000, so the function is balanced. Delete the three cx lines and the oracle becomes constant: 000 on every shot.
- What we measured
- m[0] = 1, m[1] = 1, m[2] = 1 on all 1,000 shots. QCOS checked this program and ran it on a Softquantus Cloud simulator, 1,000 shots, 13 November 2026.
Check it yourself
Save the program as deutsch-jozsa.qcos, set QCOS_API_KEY to a key from your workspace on the Softquantus Research Platform, and ask QCOS to check it. Change a line, check again, and see what the checker says.
pip install softqcossoftqcos lang check deutsch-jozsa.qcos