The Deutsch–Jozsa Algorithm
The Deutsch–Jozsa algorithm, introduced by David Deutsch and Richard Jozsa in 1992, extends Deutsch's one-bit result to input bits. It decides whether an unknown Boolean function is constant or balanced using a single oracle query — while any deterministic classical algorithm needs up to queries in the worst case.
The complete circuit
The algorithm uses input qubits and one ancilla qubit. All qubits start in .
Step 1 — Initialise the ancilla. Apply to the ancilla qubit so it begins in .
Step 2 — Create superpositions. Apply a Hadamard to every qubit. The input qubits enter the uniform superposition ; the ancilla becomes
Step 3 — Query the oracle once. The oracle unitary acts as . With the ancilla in , the phase kickback identity applies:
The function value is now encoded as a relative phase on the input register; the ancilla is untouched and disentangles.
Step 4 — Interfere. Apply to all input qubits. The amplitude for the all-zeros bitstring after this step is
When is constant, every term has the same sign, so all contributions add constructively and the amplitude is . When is balanced, exactly half the terms contribute and half , so the sum is zero and the all-zeros outcome has probability exactly zero.
Step 5 — Measure. Read the input qubits. Outcome means constant; any other outcome means balanced. The answer is deterministic — no probability of error.
The n = 2 case worked out
For the input register holds qubits 0 and 1, and qubit 2 is the ancilla. Using the balanced oracle , encoded by two CNOT gates followed by , the phase kickback gives
= \tfrac{1}{2}\bigl(|00\rangle - |01\rangle - |10\rangle + |11\rangle\bigr).$$ Applying $H \otimes H$ to this state collapses the amplitude entirely onto $|11\rangle$ — both input qubits measure to 1 with certainty, confirming the function is balanced. ## Try it Complete the circuit for $n = 2$ with the balanced oracle $f(x_0, x_1) = x_0 \oplus x_1$. Follow the five steps above: initialise the ancilla, apply Hadamards to all qubits, add the two CNOT oracle gates, apply Hadamards to the input qubits, then measure them. The grader checks the measurement distribution — for a balanced function both input qubits must read 1. <RunnableExample /> After the algorithm completes, the input qubits deterministically show $|11\rangle$: both amplitudes concentrate on that outcome, and the Probabilities panel shows two equal bars for $|110\rangle$ and $|111\rangle$ (the ancilla is in $|-\rangle$ and contributes equal weight to each).Sign in on the full site to ask questions and join the discussion.