The Deutsch-Jozsa Algorithm
Imagine a black box that computes some unknown Boolean function on input bits. You are promised the function is either constant, always returning the same value, or balanced. A balanced function returns 0 on exactly half of all inputs, and 1 on the other half. How many times must you query the box to tell which case you're in? Classically, in the worst case, you might need to check more than half of all inputs. You'd need this many checks before you're certain — an exponential amount of work. The Deutsch-Jozsa algorithm resolves this question with a single quantum query. This holds no matter how large is. This problem is artificial — nobody needs to classify functions this way in practice. But its resolution was historic. It was the first demonstration that a quantum computer could achieve an exponential separation from any classical algorithm. This was shown on a well-defined computational task. The separation does live in the somewhat generous "black-box" or "oracle" model, though. It generalizes The Deutsch Algorithm, which handles the single-bit case (), to arbitrarily many input bits. It does so using exactly the same two ingredients. These are quantum superposition via the Hadamard Transform and a clever encoding of function values into quantum phase. The big picture is this: quantum interference lets you compute a global property of a function. This means telling constant versus balanced apart. It does so without ever learning the function's value at any particular input. The algorithm queries the oracle once, in superposition over all inputs simultaneously. It then uses interference to collapse all that information. The result is a single measurable bit of global structure. This is the essential trick behind almost every later quantum query algorithm. First, encode structure into phase. Then use a Hadamard transform to convert phase structure into measurement statistics. This article builds the algorithm from the ground up. It covers the precise problem statement and oracle model, and the phase-kickback mechanism that makes it work. It also includes a full pseudocode listing and circuit diagram. There is a rigorous correctness proof via the Hadamard transform's algebra. A worked trace on a concrete 3-bit function is included too. Finally, it discusses where the "one query" claim is honest, and where it needs footnotes. It closes by pointing toward The Bernstein-Vazirani Algorithm and the broader field of Query Complexity and Quantum Speedups.
- Problem: decide if is constant or balanced, given a black-box oracle and the promise that one of these two cases holds.
- Classical deterministic algorithms need up to queries in the worst case; quantum needs exactly 1 oracle call.
- The algorithm applies Hadamards to query qubits and one ancilla, calls the oracle once via a phase-kickback trick, applies Hadamards again, and measures.
- Correctness follows from an exact algebraic identity: the amplitude of the all-zero output state equals if is constant and exactly if is balanced.
- The speedup is exact (zero error) and relies on the promise; without the promise, or with a general oracle model, the picture is more subtle.
- It is the ancestor of The Bernstein-Vazirani Algorithm and the starting point for the theory of Query Complexity and Quantum Speedups.
A single quantum query, combined with the Hadamard transform's ability to convert phase patterns into interference, can distinguish global properties of a function that require exponentially many classical queries.
The idea
The Deutsch-Jozsa problem is defined as follows. Fix . You are given oracle access to a Boolean function
with the promise that is either constant (i.e. is the same value for every ) or balanced (i.e. for exactly half of the inputs and for the other half). The task is to determine which case holds. Oracle access means you cannot inspect the internal description of ; you may only query it, classically by evaluating for a chosen , or quantum-mechanically by applying a unitary that implements reversibly. The quantum oracle is the unitary
acting on an -qubit query register and a 1-qubit target register . This is the natural reversible implementation of : it is a permutation of basis states, hence unitary, and it reduces to ordinary evaluation of when are computational basis states. Crucially, can be applied to a superposition of inputs , evaluating at all points "simultaneously" in the sense of linear superposition — though, as we will see, this alone does not let you read off all the values; the payoff comes only through interference. The key mechanism, inherited directly from The Deutsch Algorithm, is phase kickback. If the target qubit is prepared in the state , then
The target register is left completely unchanged, but the query register picks up a phase of . Applying to a superposition over all therefore imprints the entire truth table of into relative phases across the -dimensional query register, using only one call to the oracle. The Deutsch-Jozsa algorithm's remaining job is purely classical linear algebra: use the -qubit Hadamard transform to convert this phase pattern into a measurement outcome that reveals whether was constant or balanced.
How it works
The circuit uses qubits: an -qubit query register initialized to , and a single target qubit initialized to (so that after a Hadamard it becomes ).
Step 1 — Initialize. Prepare the state .
Step 2 — Superpose. Apply Hadamard to every qubit. The query register becomes a uniform superposition over all strings, and the target becomes :
Step 3 — Query the oracle once. Apply . By phase kickback, each basis term picks up phase and the target is untouched:
Step 4 — Un-superpose the query register. Apply to the query register only. Using the identity (the Hadamard Transform on qubits, where is bitwise inner product mod 2), the amplitude of each basis state becomes
Step 5 — Measure the query register. Measure all query qubits in the computational basis. If the outcome is , output "constant"; otherwise output "balanced." ``` Algorithm DEUTSCH-JOZSA(oracle U_f, n) // U_f acts as |x>|y> -> |x>|y XOR f(x)>, x in {0,1}^n allocate query register q[1..n], each initialized to |0> allocate target qubit t, initialized to |1> for i = 1 to n: apply H to q[i] apply H to t // t is now |-> = (|0>-|1>)/sqrt(2) apply U_f to (q[1..n], t) // single oracle call; phase kickback // t returns to |-> unchanged, // q picks up phase (-1)^f(x) per branch for i = 1 to n: apply H to q[i] // undo the initial superposition z = MEASURE(q[1..n]) // z in {0,1}^n if z == 0^n: return "f is CONSTANT" else: return "f is BALANCED" ``` The target qubit is never measured; it served only as a scratchpad to route phase into the query register. Note the single call to — everything else is single-qubit gates and measurement, both cheap and classical to simulate in isolation.
Complexity & correctness
Query complexity. The algorithm makes exactly one call to , regardless of . Every other step is a layer of single-qubit Hadamard gates ( of them) and a final measurement. Total gate complexity is , and query complexity is . Contrast this with the classical deterministic query complexity. Deterministically, an adversary can answer for any queries you choose, consistent with being either the all-zero constant function or some balanced function that happens to be on exactly those points. Only the -th query can force a decision: if it also returns 0, must be constant (since a balanced function cannot be 0 on more than half its inputs); if it returns 1, is balanced. So queries are necessary and sufficient classically — deterministic query complexity is . A randomized classical algorithm does much better on average: sample a few random points, and if they ever disagree, declare "balanced" (correct with certainty); if they agree times, guess "constant" with error probability at most , so queries suffice for any fixed bounded error. The genuinely exponential separation is against deterministic, zero-error classical algorithms; against randomized classical algorithms with bounded error, the quantum advantage is " exact query vs. probabilistic queries" — still a real advantage (exactness, and a strictly smaller constant), but not itself exponential. The historically clean statement is: exact quantum query complexity is ; exact (zero-error) classical deterministic query complexity is .
Correctness proof. We show the amplitude of the all-zeros outcome equals when is constant, and equals exactly when is balanced; since amplitudes must have squared magnitudes summing to 1, this fully determines the measurement statistics. From Step 4, the amplitude of basis state in the query register after the second Hadamard layer is
Set . Then for every , so
Case 1: constant. Every term equals the same value , so the sum has identical terms and . Since amplitudes have magnitude at most 1 and total squared magnitude 1, this forces and every other amplitude is exactly 0. Measuring the query register yields with probability — certainty. Case 2: balanced. Exactly of the terms equal (where ) and exactly equal (where ). The sum is , so . Measuring the query register yields with probability — it is impossible, so any observed outcome is some , correctly signaling "balanced." Both cases are exact — no bounded-error rounding, no probability of failure. The algorithm is a zero-error (Las Vegas-with-certainty, in fact Monte-Carlo-with-zero-error) quantum procedure: it always terminates and always answers correctly, given that the promise holds.
Why interference, not parallelism, does the work. It is tempting to say the speedup comes from "evaluating at all points at once." That framing is misleading: after the oracle call, the state does encode all values of in relative phase, but no measurement of directly in the computational basis would reveal that global structure — it would just return a uniformly random . The real work is done by the second Hadamard layer, which performs a global linear combination (a Walsh–Hadamard transform) of all phases simultaneously, concentrating them constructively at exactly when they are all equal (constant case) and causing perfect destructive cancellation at exactly when half are and half are (balanced case). This constructive/destructive interference pattern, not raw parallel evaluation, is what a classical computer cannot cheaply replicate: reproducing for even one classically requires summing terms.
Tradeoffs and caveats, honestly stated.
- The advantage is only proven relative to the black-box model, where the only permitted access to is through queries. If is given by an explicit formula or circuit, classical algorithms may exploit that structure and the separation can vanish.
- The promise (that is constant or balanced, no other case) is essential to the algorithm's logic; if is neither, the "constant vs. balanced" output is meaningless, though the algorithm still runs and still outputs something.
- The exponential separation is against deterministic exact classical algorithms specifically. Bounded-error randomized classical algorithms solve the promise problem with queries, so the practically fair comparison (bounded-error quantum vs. bounded-error classical) shows only a constant-factor advantage, not an exponential one. The Deutsch-Jozsa algorithm's real historical significance is conceptual: it is a clean existence proof that quantum query algorithms can beat classical exact algorithms by an exponential factor, opening the door to later problems (Simon's problem, and ultimately Shor's factoring algorithm) where the bounded-error separation is also exponential.
Worked example
Take and the balanced function defined on (3-bit strings) by
(ignore ; this is a valid balanced function since exactly half of the 8 inputs give ).
| (binary) | ||||
|---|---|---|---|---|
| 000 | 0 | 0 | 0 | +1 |
| 001 | 1 | 0 | 1 | −1 |
| 010 | 0 | 1 | 1 | −1 |
| 011 | 1 | 1 | 0 | +1 |
| 100 | 0 | 0 | 0 | +1 |
| 101 | 1 | 0 | 1 | −1 |
| 110 | 0 | 1 | 1 | −1 |
| 111 | 1 | 1 | 0 | +1 |
After Step 2 (Hadamards on all 4 qubits), the query register is a uniform superposition over all 8 strings with amplitude each, tensored with on the target.
After Step 3 (one oracle call, phase kickback), the query register state is
Notice the sign pattern depends only on , exactly as expected.
After Step 4 (Hadamard transform), compute using the table: the sum of the last column is . So , confirming that is impossible, consistent with being balanced. A fuller computation shows the surviving nonzero amplitudes are exactly those orthogonal (mod 2, under dot product) to the "direction" of 's phase pattern; here one finds nonzero amplitude only at -style patterns determined by 's dependency — concretely exactly for with pattern matching the XOR structure (this generalizes the Bernstein–Vazirani phenomenon, foreshadowed below). Measuring might yield, say, : since , the algorithm correctly outputs "balanced," using a single call to , whereas a deterministic classical strategy could have been fed the answers and would need up to queries in the worst case to be certain in general. For contrast, take the constant function for all . Then every entry of the column is , so , forcing all other amplitudes to zero. The measurement returns with certainty, and the algorithm outputs "constant" — correctly, and again from a single query.
Common pitfalls & traps
| Pitfall | Why it happens | Fix |
|---|---|---|
| Measuring the target qubit and expecting information | The target is left in regardless of ; it carries no distinguishing information | Only the query register after the second Hadamard layer matters; discard the target |
| Forgetting to initialize the target to before the Hadamard | Without the state, phase kickback does not occur and merely computes into , not a phase | Always prepare target as , then , to get |
| Applying the second Hadamard layer to the target too | Harmless numerically here but conceptually confuses which register carries the answer | Apply the un-computing Hadamard layer only to the query qubits |
| Assuming the algorithm works without the promise | If is neither constant nor balanced, can still occur with unbalanced, giving a meaningless "constant" verdict | The promise is a hard precondition; without it, only weaker statements (e.g. approximate counting) are valid |
| Confusing this with an exponential speedup over randomized classical algorithms | Randomized algorithms already solve the bounded-error version in queries | State the separation precisely: exact quantum ( query) vs. exact/deterministic classical ( queries) |
| Believing "computes all values of at once" in a usable sense | Superposition alone gives access to only one upon measurement; the values are hidden in relative phase | Emphasize that interference (the second Hadamard layer), not parallel evaluation, extracts the global answer |
Pioneers & history
Origins · 1 minDavid Deutsch and how this was discovered
David Deutsch, a physicist at Oxford, formulated the earliest version of a quantum algorithm exploiting superposition to outperform classical computation, publishing the single-bit case in 1985 as part of his broader argument that quantum mechanics implies a physical, universal model of computation strictly more powerful than the classical Turing machine. His 1985 paper is widely regarded as the founding document of quantum computational complexity theory, introducing the universal quantum computer as a physical hypothesis rather than a purely mathematical abstraction.
Where this matters
The Deutsch-Jozsa algorithm itself solves no problem of direct industrial interest — no real dataset asks "is this function constant or balanced?" Its importance is pedagogical and structural. It is the first and cleanest illustration of the phase kickback plus Hadamard sandwich pattern that recurs throughout quantum algorithm design: prepare a uniform superposition, query an oracle so that function values become relative phases, then apply an inverse transform to convert phase structure into a measurable pattern. This exact skeleton reappears, with modifications, in The Bernstein-Vazirani Algorithm (recovering a hidden string instead of a binary property), in Simon's algorithm (finding a hidden XOR-mask period using a group-theoretic Hadamard trick), and ultimately in the quantum Fourier transform underlying Shor's factoring algorithm, where the "Hadamard sandwich" is replaced by a more general Fourier sandwich over . In teaching and research, Deutsch-Jozsa is the standard first experiment run on new quantum hardware and new simulators, precisely because its correctness condition ( iff constant) is trivial to check and its circuit is shallow — a small number of Hadamard layers plus one oracle call — making it a natural benchmark for gate fidelity and small-scale coherence. It also anchors the theoretical subfield of Query Complexity and Quantum Speedups, which classifies computational problems by how many oracle queries quantum versus classical algorithms need, a framework that later produced provably exponential bounded-error separations (Simon's problem) and, eventually, practically consequential ones (integer factoring, discrete logarithms) that threaten current public-key cryptography.
Ask the tutor: How would the algorithm's amplitudes change if the promise were dropped and f were an arbitrary Boolean function?
Does Deutsch-Jozsa give a real, practical exponential speedup?
Not in the sense most people assume. The exponential gap is proven only against deterministic, zero-error classical algorithms, which need up to queries. Randomized classical algorithms with a small allowed error probability solve the same promise problem using only queries on average, so the practically relevant comparison shows a constant-factor advantage, not an exponential one. What makes Deutsch-Jozsa historically important is that it was the first rigorous existence proof that quantum query algorithms can outperform classical exact algorithms by an exponential factor at all, establishing the conceptual template later exploited by problems (like Simon's problem and Shor's algorithm) where the bounded-error separation is also exponential and has real cryptographic consequences.
What exactly is phase kickback, and why does it not disturb the target qubit?
Phase kickback occurs when an oracle acts on a control register and a target register prepared in an eigenstate of the XOR operation, namely . Since , when carries the state , XORing into it either leaves unchanged (if ) or flips its overall sign to (if ), because and exactly swaps the two terms of , introducing a global minus sign. The target's state is identical before and after; only a phase factor is factored out and attached to instead.
Why does the algorithm need the promise that f is constant or balanced?
The correctness proof relies on the sum taking one of exactly two possible values: (constant case, giving amplitude ) or (balanced case, giving amplitude ). If is neither constant nor balanced, this sum can take any value strictly between and , and the amplitude becomes some intermediate number whose squared magnitude is a probability strictly between and . The binary "constant vs. balanced" output no longer has a guaranteed correct interpretation; a measurement becomes merely suggestive rather than conclusive. The promise is what converts a statistical estimation problem into an exact decision problem.
How does this relate to the single-qubit Deutsch algorithm?
The single-qubit Deutsch algorithm is the special case : it distinguishes the four possible one-bit functions into "constant" () versus "balanced" () using one query. The Deutsch-Jozsa algorithm generalizes the circuit verbatim — same Hadamard-oracle-Hadamard structure — to query qubits, and the correctness argument scales identically via the -qubit Hadamard transform's algebra rather than a single-qubit calculation. Historically, Deutsch published the case in 1985, and the -bit generalization, with Richard Jozsa, followed in 1992, closing the gap to a fully exponential classical-versus-quantum separation.
Can the algorithm ever fail or give a wrong answer due to noise or measurement error?
In the idealized, noiseless quantum circuit model described here, no — the algorithm is provably exact: the probability of measuring is exactly when is constant and exactly when is balanced, with no intermediate cases under the promise. On real, noisy hardware, imperfect gates and decoherence introduce small probabilities of the "wrong" outcome, so practical implementations report a success rate rather than a guarantee; this is precisely why Deutsch-Jozsa is used as a calibration benchmark, since deviations from the theoretically perfect -or- probability directly quantify hardware error rates.
What is the relationship to the Bernstein-Vazirani algorithm?
Bernstein-Vazirani uses an almost identical circuit — Hadamards, one oracle call via phase kickback, Hadamards, measurement — but solves a different, more information-rich problem: recovering an entire hidden -bit string from an oracle computing , again in a single query. Where Deutsch-Jozsa extracts one bit of global information (constant versus balanced) via a single amplitude, Bernstein-Vazirani extracts all bits of at once, because the post-oracle amplitude pattern concentrates entirely on the single basis state rather than just at . It is the natural next step after mastering Deutsch-Jozsa's amplitude analysis.
Why does the second layer of Hadamard gates work — what is the underlying mathematics?
It is the algebra of the -qubit Hadamard transform, , which is exactly the discrete Walsh–Hadamard transform over . Applying it to the phase-encoded state produces amplitudes , which for reduces to the average of over all inputs. This average is exactly when all terms agree (constant ) and exactly when they split evenly (balanced ), by simple arithmetic — the same interference principle underlying the discrete Fourier transform used later in Shor's algorithm.