|q⟩ Bad Qubits

beginner · Programming · First Quantum Algorithms: Deutsch & Deutsch–Jozsa

Scaling Up: Deutsch–Jozsa

Deutsch's original algorithm solved a one-bit problem: is a function f:{0,1}{0,1}f:\{0,1\}\to\{0,1\} constant or balanced? The Deutsch–Jozsa algorithm, developed by David Deutsch and Richard Jozsa in 1992, lifts that result to nn input bits — and the quantum advantage becomes exponential.

The generalized problem

We have a black-box function f:{0,1}n{0,1}f:\{0,1\}^n \to \{0,1\} that is promised to be either:

Classically, in the worst case you must evaluate ff at 2n1+12^{n-1}+1 different inputs before you can be certain of the answer — the first half of the inputs could all return the same value, and only the (2n1+1)(2^{n-1}+1)-th query guarantees a conclusion. The Deutsch–Jozsa algorithm answers the question with a single oracle query, regardless of nn.

The circuit

The algorithm uses nn input qubits (initialized to 0|0\rangle) and one ancilla qubit (initialized to 1|1\rangle). The steps are:

  1. Apply HH to the ancilla to prepare it in =12(01)|-\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle - |1\rangle).
  2. Apply HnH^{\otimes n} to all nn input qubits, creating the uniform superposition 12nx{0,1}nx\tfrac{1}{\sqrt{2^n}}\sum_{x \in \{0,1\}^n}|x\rangle.
  3. Query the oracle UfU_f once.
  4. Apply HnH^{\otimes n} to the input qubits again.
  5. Measure the input qubits.

If the measurement yields 0n|0\rangle^{\otimes n} (all zeros), the function is constant; any other outcome means it is balanced.

Why it works

After the second layer of Hadamards, the amplitude for the all-zeros outcome 0n|0\rangle^{\otimes n} is

12nx{0,1}n(1)f(x).\frac{1}{2^n}\sum_{x \in \{0,1\}^n}(-1)^{f(x)}.

If ff is constant, every term has the same sign, so all 2n2^n terms add constructively and the amplitude equals ±1\pm 1 — measuring all zeros is certain. If ff is balanced, exactly half the terms contribute +1+1 and half contribute 1-1, so the sum is zero and the all-zeros outcome has probability zero.

The first Hadamard layer spreads the input over all 2n2^n basis states at once (quantum parallelism), the oracle imprints the function value as a phase via the |-\rangle ancilla (phase kickback), and the second Hadamard layer converts that global phase pattern back into a measurable amplitude difference. Interference does all the work in a single pass.

Classical vs quantum query complexity

| Strategy | Queries in worst case | |----------------|-----------------------| | Classical | 2n1+12^{n-1}+1 | | Deutsch–Jozsa | 11 |

This 2n12^{n-1} vs 11 separation is exact: there is no classical algorithm that can solve the promised problem with fewer than 2n1+12^{n-1}+1 queries in the worst case, and the quantum algorithm never needs more than one. For n=270n = 270, classical worst-case requires 226910812^{269} \approx 10^{81} queries — more than the estimated 1080\sim 10^{80} atoms in the observable universe; the quantum algorithm still uses exactly one.

Deutsch–Jozsa is often called a "toy" algorithm because the promise (constant or balanced) makes the problem somewhat artificial. Its real value is didactic: it is the cleanest demonstration that quantum parallelism combined with interference can yield an exponential query advantage, and many deeper algorithms — Bernstein–Vazirani, Simon's algorithm, Shor's — inherit exactly this structure.

Sign in on the full site to ask questions and join the discussion.