The Deutsch–Jozsa Algorithm

A classical detective, promised a coin is either double-headed or perfectly fair, might have to flip it thousands of times before ruling out a run of lucky heads. A quantum detective, handed the same promise, glances once and knows for certain. That is the Deutsch–Jozsa algorithm — the Deutsch algorithm grown from one input bit to n — and it was the first problem ever shown to need exponentially fewer questions on a quantum computer than on any deterministic classical one. Same black box, same promise; one quantum query settles what could cost a classical machine over half a million queries.

The whole trick is interference. Instead of reading the function value on one input, the algorithm arranges for every input's value to fold together so that a single global property — is f the same everywhere, or split down the middle? — decides one clean measurement outcome.

The promise

You are given a function f:\{0,1\}^n \to \{0,1\} as a black-box oracle, together with a promise: f is one of exactly two kinds.

Your job is only to decide which — constant or balanced — not to learn the whole function. The promise is what makes the problem crisp: you never have to worry about a function that is "mostly 0 with a couple of 1s," because you are guaranteed it is one of these two extremes.

Why classical determinism is expensive

Suppose you must be certain, using a deterministic classical strategy. Each query reveals one value f(x). Imagine the first 2^{n-1} queries you make all come back the same, say all 0. Are you done? No — the function could be constant, or it could be balanced with every one of its 0s handed to you first and all its 1s still hidden. Both remain possible. You need to look at one more input to break the tie.

So the deterministic worst case is 2^{n-1}+1 queries: half of all inputs, plus one. For n = 20 that is 524{,}289 calls — just to be sure. The quantum algorithm below needs one, for any n, with certainty.

The circuit

Deutsch–Jozsa is four moves on one bundled n-qubit register (plus a single ancilla qubit that supplies the phase). Hadamard everything, query the oracle once, Hadamard the register again, and measure. Step through it:

The ancilla starts in |-\rangle = \tfrac{1}{\sqrt2}(|0\rangle - |1\rangle). When the bit-oracle XORs f(x) into it, |-\rangle is an eigenstate that comes back unchanged except for a global (-1)^{f(x)} — that is phase kickback, and it is what turns the oracle into an effective phase oracle |x\rangle \mapsto (-1)^{f(x)}|x\rangle on the register. From here we can forget the ancilla and track the register alone.

The one amplitude that decides everything

Start the register in |0\dots0\rangle and apply H^{\otimes n} to reach the uniform superposition:

H^{\otimes n}|0\dots0\rangle = \frac{1}{\sqrt{2^n}}\sum_{x\in\{0,1\}^n} |x\rangle .

One oracle query stamps the phase (-1)^{f(x)} on each term, giving \tfrac{1}{\sqrt{2^n}}\sum_x (-1)^{f(x)}|x\rangle. Now apply H^{\otimes n} again. Using H^{\otimes n}|x\rangle = \tfrac{1}{\sqrt{2^n}}\sum_y (-1)^{x\cdot y}|y\rangle, the amplitude that lands on the all-zeros outcome |0\dots0\rangle (where x\cdot 0 = 0, so every sign is +1) is simply the average of all the stamped signs:

\langle 0\dots0 | \,\psi\rangle = \frac{1}{2^n}\sum_{x\in\{0,1\}^n} (-1)^{f(x)} .

This one number tells the whole story:

So the rule is: all-zeros ⟺ constant; any nonzero result ⟺ balanced. Pure interference does the sorting, in a single query.

Worked example 1: n=2, constant vs balanced

Take n=2, so 2^n = 4 inputs 00,01,10,11. After H^{\otimes 2}|00\rangle the register is \tfrac{1}{2}(|00\rangle+|01\rangle+|10\rangle+|11\rangle).

Constant f\equiv 0: every phase is (-1)^0 = +1, so the state is unchanged. Applying H^{\otimes 2} a second time inverts the first one and returns |00\rangle exactly:

\tfrac{1}{2}\big(|00\rangle+|01\rangle+|10\rangle+|11\rangle\big) \;\xrightarrow{\,H^{\otimes 2}\,}\; |00\rangle .

Balanced f(x_1x_2)=x_1 (i.e. f(00)=f(01)=0, f(10)=f(11)=1): the query flips the sign of the two inputs beginning with 1:

\tfrac{1}{2}\big(|00\rangle+|01\rangle-|10\rangle-|11\rangle\big) \;\xrightarrow{\,H^{\otimes 2}\,}\; |10\rangle .

The constant case reads 00; the balanced case reads 10 — nonzero. One query each, and the two are told apart with certainty.

Worked example 2: the all-zeros amplitude collapses to 0

You do not have to run the whole circuit to predict the balanced case — just evaluate the deciding amplitude \tfrac{1}{2^n}\sum_x (-1)^{f(x)}. Reuse the balanced f(x_1x_2)=x_1 from above and list the four signs:

\frac{1}{4}\Big[(-1)^{f(00)} + (-1)^{f(01)} + (-1)^{f(10)} + (-1)^{f(11)}\Big] = \frac{1}{4}\big[\,(+1) + (+1) + (-1) + (-1)\,\big] = 0 .

The two +1s and two -1s annihilate: the all-zeros amplitude is zero, confirming you can never measure 00 for this f. Contrast the constant f\equiv 1, where every sign is -1:

\frac{1}{4}\big[(-1)+(-1)+(-1)+(-1)\big] = -1 ,

an amplitude of magnitude 1 — all-zeros with certainty. Every balanced function, whatever its pattern, hits the same perfect cancellation; every constant function saturates the amplitude to \pm1. That gap between 0 and 1 is the entire algorithm.

The Deutsch algorithm is exactly this circuit at n=1: one input qubit, constant vs balanced meaning f(0)=f(1) vs f(0)\neq f(1), decided in one query instead of two. Deutsch and Jozsa's contribution was to notice that the same Hadamard–oracle–Hadamard sandwich keeps working as you stack more input qubits — and that the classical cost balloons to 2^{n-1}+1 while the quantum cost stays pinned at one. It was the first time anyone exhibited an exponential quantum–classical gap, and it set the template — superpose, query, interfere, measure — that Bernstein–Vazirani and Simon's algorithm then sharpened toward Shor.

The headline "1 query vs 2^{n-1}+1" compares the quantum algorithm to a classical one that must be always right — a deterministic, exact solver. Loosen that to allow a tiny error and the classical side becomes cheap: pick a handful of inputs at random and compare their values. If they are ever different, f is certainly balanced; if a balanced function keeps matching by chance, the probability it fools you halves with each fresh sample, so after, say, 20 random queries the error is under one in a million — independent of n.

So Deutsch–Jozsa is an exact vs bounded-error separation in the query model, not evidence of a practical exponential speedup. The genuine exponential separations that survive randomisation come later — Simon's problem separates quantum from randomised classical, and Shor's factoring is the one with real-world teeth. Deutsch–Jozsa's job is to be the clean, first, provable example of quantum interference beating a classical guarantee — not a faster spreadsheet.