Deutsch–Jozsa Algorithm
The Deutsch–Jozsa algorithm is the first quantum algorithm that is unconditionally faster than every classical one — provably, exactly, with no caveats about average cases or bounded error. It answers a single yes/no question about a black-box Boolean function that comes with a promise: is either constant (the same value on all inputs) or balanced (value on exactly half the inputs, on the other half). A deterministic classical machine, in the worst case, must open the box times before it can be certain which case holds; the quantum algorithm opens it once. That gap — one query versus more than half of an exponentially large table — is the whole point. The circuit is almost embarrassingly short. Prepare input qubits in and one ancilla in , apply a Hadamard to every qubit, call the oracle once, apply Hadamards again to the input qubits, and measure them. Everything hangs on two tricks stacked on top of quantum parallelism. The ancilla, put into , converts the oracle's bit-flip into a pure phase on each branch — phase kickback. The final Hadamard layer is the Fourier transform over , and it reads out whether that phase pattern has a nonzero average — precisely the constant-versus-balanced distinction in disguise. Deutsch–Jozsa is where a student first sees that the power of a quantum computer is not that it evaluates on all inputs at once — a classical parallel machine could imagine that too — but that it can make the unwanted answers interfere destructively and cancel. It is the direct ancestor of Bernstein–Vazirani, Simon's algorithm, and, through them, Shor's factoring algorithm and the abelian hidden-subgroup framework.
- The Deutsch–Jozsa algorithm decides whether a promised function is constant or balanced using exactly one query to a quantum oracle.
- Any deterministic classical algorithm needs queries in the worst case, so the separation is exponential in the exact (zero-error) query model.
- The engine has three parts: a Hadamard layer builds the uniform superposition, phase kickback turns into a sign , and a second Hadamard layer interferes the signs.
- Measuring the input register yields the all-zeros string with probability if is constant, and with probability if is balanced — a single-shot, deterministic verdict.
- Its importance is structural, not applied: a classical randomized algorithm solves the promise in queries, so the exponential gap lives only in the exact model.
- It is the template — Hadamard, oracle, Hadamard, measure — reused by Bernstein–Vazirani, Simon, and ultimately Shor.
- Key idea: put the ancilla in so the oracle stamps each branch with ; the final Hadamards send that sign pattern to amplitude on , which is for constant and exactly for balanced .
Put the ancilla in so the oracle stamps each branch with the phase ; the final Hadamard transform sends that sign pattern to amplitude on , which is (probability ) when is constant and when is balanced.
Where this lives today
Deutsch–Jozsa was one of the very first quantum algorithms run on real hardware, and it remains a workhorse benchmark. A two-qubit version was demonstrated on a nuclear-magnetic-resonance processor by Jones and Mosca in 1998 and independently by Chuang and collaborators the same year; ion-trap realizations followed, notably Gulde and colleagues in 2003 on a single trapped calcium ion. Because an instance has a known answer, needs only one oracle call, and can be built with either separable or entangling oracles, small- versions are ideal low-overhead test circuits: they probe gate fidelity, oracle construction, and — by comparing entangling and non-entangling oracles — whether a device's advantage is genuinely coherent. The algorithm is a standard element of superconducting- and photonic-processor demonstrations and of compiler test suites, because a machine that fails it is failing at interference, the most basic quantum resource. Its conceptual influence is larger still: the Hadamard–oracle–Hadamard–measure skeleton is the template for Bernstein–Vazirani, Simon's algorithm, and the abelian hidden-subgroup problem, and the on-ramp to the quantum Fourier transform and Shor's algorithm in essentially every course in the field.
The physical picture
Think of the oracle as a rubber stamp that inks a or a onto each computational-basis branch of a superposition according to . A constant function stamps every branch with the same sign, leaving the uniform superposition unchanged up to a global phase; running the Hadamard layer backwards then reassembles the state exactly onto . A balanced function stamps half the branches and half , producing a state that is orthogonal to the uniform superposition, so the amplitude that would have landed on cancels to nothing. The whole algorithm is a machine for asking "are all the signs the same?" in a single query, and the answer is read from one place: the probability of measuring all zeros. The first figure is the circuit itself — five moves, one oracle call — with the state written out at each stage so the phase-kickback step and the Fourier readout are visible in the math, not just asserted.
Why does asking "are the signs all equal?" require only one look? Because the Hadamard layer does not inspect the branches one at a time — it sums them. The amplitude that arrives at is literally the average sign , the mean of the function's encoding over its entire domain. A constant function has average ; a balanced function has average exactly . The quantum computer evaluates this global average of terms with a single oracle call, whereas a classical machine that must be certain the average is nonzero has to inspect a strict majority of the table. The second figure makes the readout concrete for : the full probability distribution over the measurement outcomes, for a constant function beside a specific balanced one.
The core equations
The oracle is the only thing about the algorithm may touch, and it is a reversible bit-flip: it XORs into an output register conditioned on the input . This is the standard way to make any Boolean function unitary, and it costs one query per application.
where: — -qubit input register, ; — single-qubit output (ancilla) register; — addition modulo (XOR); — the unknown Boolean function, promised constant or balanced. Reading it: leaves the input alone and toggles the output iff , so it is its own inverse and unitary. The pivotal trick is to feed the ancilla the state . It is an eigenstate of the bit-flip with eigenvalue , so the oracle's action on it is not to flip a bit but to multiply by a sign that depends on — the information moves out of the output register and into a phase on the input register.
where: — the eigenstate of the bit-flip, prepared by ; — the eigenvalue, which becomes a relative phase on branch . Reading it: the ancilla comes out unchanged and factorizes off, so from here on we may ignore it and track only the phased input register. Hadamards on the input register manufacture quantum parallelism: from a single basis state they build an equal-weight superposition over every one of the inputs at once.
where: — the Hadamard gate, , ; — the size of the input domain. Reading it: one layer of gates produces a superposition with exponentially many terms, the raw material interference will act on. Applied to a general basis state, the same layer is the Walsh–Hadamard transform — the Fourier transform over the group — spreading over all frequency labels with the character sign .
where: — the bitwise inner product mod ; — the output (frequency) label after the transform. Reading it: because is its own inverse, this transform is exactly what will convert the phase pattern back into measurable amplitudes. Composing the three input-register operations — Hadamard, phase kickback, Hadamard — gives the state just before measurement. Its amplitude on outcome is a Fourier coefficient of the sign function .
where: the bracket is the amplitude of outcome , the -th Walsh–Hadamard coefficient of . Reading it: the algorithm has computed the entire Fourier transform of the sign pattern; measurement samples one coefficient's location. Setting kills the second sign (), so the all-zeros amplitude is exactly the average sign — and under the promise that average is or , giving a clean decision rule.
where: — amplitude of ; — probability the -qubit measurement returns all zeros. Reading it: observing certifies constant; observing any nonzero string certifies balanced. Contrast the query counts. The exact quantum cost is one; the worst-case deterministic classical cost is a strict majority of the table plus one — an exponential gap.
where: — exact (zero-error) quantum query complexity; — worst-case deterministic classical query complexity. Reading it: for this is versus ; the ratio grows without bound.
The goal is to carry the joint -qubit register all the way from the standard input through the three unitary layers to the final measurement, and to reduce the whole computation to a single number — the amplitude on — showing that it equals for a constant function and for a balanced one. This is the master calculation of the article; the phase-kickback identity and the Walsh–Hadamard sum are its two load-bearing steps, and every later result is a corollary of the state written down here.
Step 1. Initialize the register as — the input qubits in the all-zeros state and the ancilla in . Why: putting the ancilla in rather than is the deliberate choice that a Hadamard will turn into , arming the phase-kickback trick.
Step 2. Apply Hadamards to all qubits: , using and . Why: one layer builds the uniform superposition over all inputs and simultaneously prepares the ancilla eigenstate — parallelism and the kickback ancilla in a single move.
Step 3. Apply the oracle once. By the eigenvalue relation , it stamps each branch with a sign: . Why: this is the only place enters, and it enters as a phase on the input register — the ancilla is untouched and factorizes off, so it plays no further role.
Step 4. Apply to the input register, using , and collect the coefficient of each : . Why: the second Hadamard layer is the Fourier transform that converts the phase pattern into amplitudes where interference — constructive or destructive — becomes visible to a measurement.
Step 5. Read off the all-zeros amplitude by setting , so for every : . Why: the outcome is exactly the DC (average) Fourier component of the sign function, and the promise controls that average precisely.
Step 6. Evaluate the constant case: if then all terms are equal, , so . Why: identical signs add coherently to full amplitude, so the state reassembles exactly onto and the measurement is deterministic.
Step 7. Evaluate the balanced case: with inputs mapping to each value, , so never occurs and any observed string proves balanced. Why: equal numbers of and signs cancel exactly, the hallmark of destructive interference, and it is total precisely because the promise fixes the counts to be equal. *The normalization follows directly from the unitarity of . For the majority instance: and the four odd-parity strings each carry , summing to .*
Go deeper · Derivation B · 2 minThe classical deterministic lower bound
The goal is to prove that a classical algorithm which must answer with certainty has no shortcut: an adversary can keep both answers alive until the algorithm has inspected a strict majority of the table, forcing queries in the worst case. This is the number the single quantum query beats, and the argument is a clean adversary (or fooling-set) argument that fixes the answers only as it is forced to, never revealing more than the algorithm has earned.
Step 1. Suppose a deterministic algorithm has made distinct queries and received identical answers, say all . Why: if it ever saw two different values it could immediately answer "balanced", so the hard case is a run of equal answers, and we bound how long such a run can be uninformative.
Step 2. A constant completion still exists: extend to the constant- function, which agrees with every answer given so far. Why: nothing seen rules out "constant", so the algorithm cannot yet commit to "balanced".
Step 3. A balanced completion also exists: since at most inputs have been assigned , set the remaining zeros on unqueried inputs and assign to the other , which is a valid balanced function agreeing with all answers — possible exactly when . Why: nothing seen rules out "balanced" either, so with both completions live the algorithm has strictly zero information about which case holds.
Step 4. Therefore after any equal answers the algorithm cannot decide and must query again. Why: a correct deterministic algorithm may not guess; while two consistent completions of opposite type remain, any answer it gives is wrong on one of them.
Step 5. One more query settles it: after queries, if all answers agree then no balanced function can match (a balanced produces each value only times), so is constant; and if two answers ever differ, is balanced. Hence is both necessary and sufficient. Why: crossing the halfway mark by one is exactly the threshold at which a constant and a balanced function can no longer agree, which is what makes the bound tight. *As a sanity check at : queries, meaning a classical algorithm must read both and to be certain — which is correct.*
Go deeper · Derivation C · 1 minBernstein–Vazirani as the linear special case
The goal is to show that the very same circuit, applied to a linear oracle , does more than answer one bit — it reads out all bits of the hidden string in a single query, deterministically. This is the first hint that the Fourier readout carries far more than a constant-versus-balanced verdict, and it turns the "random nonzero string" of a general balanced function into a precisely engineered outcome via the orthogonality of characters.
Step 1. Take for a fixed (this is balanced whenever ), and insert it into the general amplitude, combining the two signs with : . Why: linearity lets the two phase exponents merge into a single character indexed by , which is what makes the sum collapse.
Step 2. Apply character orthogonality on : , so with the amplitude is . Why: a nontrivial character sums to zero over the group and a trivial one sums to , so all amplitude concentrates on the single label .
Step 3. Conclude : one oracle query reveals every bit of at once, versus classical queries — one per basis vector — needed to read the bits one at a time. Why: the outcome is not a random balanced-certifying string but the hidden vector itself, an exact -to- query saving that generalizes to Simon's exponential separation. *This is the Deutsch–Jozsa circuit specialized by the Walsh–Hadamard orthogonality relation. For example, at with : the circuit output is with probability .*
Limiting cases
The algorithm has one moving part — the promise on — and its behaviour at the edges shows exactly where the advantage lives and where it evaporates.
| Case | Result | Meaning |
|---|---|---|
| (Deutsch) | Reduces to Deutsch's algorithm: one query distinguishes (constant) from (balanced). | |
| Constant | All signs equal, average ; the state reassembles onto and the measurement is deterministic. | |
| Balanced | Equal and cancel; never appears, so any observed string certifies balanced. | |
| Linear | Bernstein–Vazirani: the hidden string is read out whole in one query. | |
| Classical randomized | A few random samples decide the promise with tiny error; the exponential gap is exact-model only. | |
| No promise | arbitrary | Without constant-or-balanced, one query reads only the average sign, which is no longer decisive. |
Worked examples
1 · Tracing the full circuit for n = 2 (fully worked)
Run the algorithm by hand on two qubits for a constant function and for the balanced parity function . Track every amplitude.
Step 1. Build the superposition. After the input Hadamards the register is with the ancilla in ; the four branches carry equal amplitude .
Step 2. Kick back the phase, constant case. For every branch gets the sign , giving . This is times the uniform state, so returns it to . Measuring gives with probability 1 — the global sign is unobservable, and certifies constant.
Step 3. Kick back the phase, balanced case. For the signs are on , giving . This factorizes as , and since , the second Hadamard layer sends it to . Measuring gives with probability 1 — a nonzero string, certifying balanced.
Step 4. Read the extra structure. The outcome is not accidental: parity is the linear function with , so by the Bernstein–Vazirani reduction the circuit returns deterministically. Constant ; balanced parity ; both in a single query.
2 · A non-linear balanced function, n = 3 (fully worked)
Take the majority function on three bits, iff at least two of the bits are . It is balanced (four inputs give ). Find the full measurement distribution and show it certifies balanced.
Step 1. Encode the signs. The values over are : the four inputs of weight (namely ) carry , the rest carry .
Step 2. Check the all-zeros amplitude. , so is never observed — the balanced certificate. This is the destructive interference of Step 7 of the master derivation, realized on a concrete non-linear function.
Step 3. Compute the rest of the spectrum. Evaluating for every gives amplitude on , amplitude on , and on . The nonzero outcomes are exactly the odd-parity strings.
Step 4. Read the distribution. The measurement lands on one of , each with probability , summing to . Any of the four proves balanced, but which one is essentially random — unlike the linear case, a general balanced function leaves no further guaranteed structure in the outcome. certifies balanced; the verdict needs one query, where a classical worst case needs .
3 · How large the classical bill gets
Put a number on the separation for and .
Step 1. Quantum cost is fixed at query regardless of .
Step 2. Classical worst-case cost is : for that is queries, and for it is queries — over half a billion evaluations to be certain, versus one. The gap is exactly to , and it is unbounded.
Physics of the portrait
The Deutsch–Jozsa algorithm carries two names, but it grew from one radical idea: that the laws of computation are laws of physics, and a machine built on quantum mechanics can therefore do things no classical machine can. That idea, and the first algorithm to prove it, belong to David Deutsch.
Origins · 1 minDavid Deutsch and how this was discovered
David Deutsch is the physicist who turned quantum computing from a speculation into a theory. In a 1985 paper he wrote down the universal quantum computer — a quantum generalization of the Turing machine — and argued for a physical Church–Turing principle: every finitely realizable physical system can be simulated by a universal computing machine operating by finite means. To show the model was not merely a re-description of the classical one, he exhibited a problem it could solve faster, the single-bit "Deutsch problem", using interference between the two branches of a superposed query. That original 1985 algorithm was probabilistic — it succeeded only half the time — a blemish reflecting how new the ideas were. In 1992, with the mathematician Richard Jozsa, Deutsch generalized it to bits and the constant-versus-balanced promise, giving the first exponential quantum–classical separation in the exact query model. The circuit was sharpened to its modern deterministic single-query form by Cleve, Ekert, Macchiavello and Mosca in 1998, who isolated phase kickback as the mechanism. Deutsch's deeper commitment was always to the physics: a lifelong champion of the many-worlds interpretation, he argued that the speedup of quantum algorithms is best understood as computation distributed across the branches of the wavefunction. The line from his 1985 machine runs through Bernstein–Vazirani, Simon, and Shor to the whole field.
Going further
Texts. Nielsen and Chuang, Quantum Computation and Quantum Information, §1.4.3–1.4.4 and Box 1.2 (Deutsch and Deutsch–Jozsa) · Kaye, Laflamme and Mosca, An Introduction to Quantum Computing, Ch. 6 · Mermin, Quantum Computer Science, Ch. 2 · Preskill, Lecture Notes for Physics 219, Ch. 6 · Kitaev, Shen and Vyalyi, Classical and Quantum Computation · Watrous, quantum-complexity lecture notes (query model).
Primary sources. Deutsch, Quantum theory, the Church–Turing principle and the universal quantum computer, Proc. R. Soc. Lond. A 400, 97 (1985) · Deutsch and Jozsa, Rapid solution of problems by quantum computation, Proc. R. Soc. Lond. A 439, 553 (1992) · Cleve, Ekert, Macchiavello and Mosca, Quantum algorithms revisited, Proc. R. Soc. Lond. A 454, 339 (1998). Key identities to keep in mind: the phase-kickback relation follows by direct expansion of ; the all-zeros amplitude equals for constant and for balanced ; the deterministic classical bound follows from the adversary argument; the Bernstein–Vazirani reduction rests on character orthogonality ; and the majority spectrum gives with the odd-parity strings at each. Every number appearing here — , , probabilities and , — is an exact combinatorial or algebraic value, not an empirical constant.
Prerequisites. Quantum Parallelism and the Oracle Model · Deutsch's Algorithm. Leads to. Bernstein–Vazirani Algorithm · Simon's Algorithm · Hidden Subgroup Problem · Quantum Fourier Transform.
Ask the tutor: Ask me to trace the circuit on any oracle you choose — constant, balanced, or linear — and watch the phase kickback and the final Fourier interference decide the answer in a single query.
Why is the promise that f is constant or balanced essential?
The single-query algorithm reads exactly one number about : the average sign , delivered as the amplitude on the all-zeros outcome. For an arbitrary function this average can take many intermediate values, and the all-zeros probability then tells you nothing decisive. The promise collapses the possibilities to two extremes — average exactly (constant) or exactly (balanced) — so that one bit of information, whether or not was observed, distinguishes them. Remove the promise and you are asking one query to resolve a question that contains more than one bit of information, which no measurement of a single amplitude can do.
What does phase kickback actually do, and what breaks if the ancilla is |0⟩ instead of |−⟩?
Phase kickback exploits the fact that is an eigenstate of the bit-flip: applying , which XORs into the ancilla, multiplies the branch by the eigenvalue and leaves the ancilla unchanged. The information about is thereby moved from the ancilla's value into a relative phase on the input register, exactly where Hadamard interference can act on it. Prepare the ancilla in instead and the oracle entangles it with in the computational basis, producing no useful relative phase. The final Hadamards then return an uninformative mixture and the constant-versus-balanced distinction is lost — you would learn at most at one random point, no better than a single classical query.
If a random classical algorithm solves it in a few queries, why do we care about Deutsch–Jozsa at all?
Because the point was never practical speed on this toy problem — it was a proof of principle. Deutsch–Jozsa was the first demonstration that a quantum computer can be provably exponentially faster than any classical one in a clean, fully specified model (exact, deterministic query complexity), and it isolated interference as the mechanism, distinct from mere parallel evaluation. That clarity is what mattered historically: it convinced researchers the model had real power and set the template that Bernstein–Vazirani and Simon refined into a genuine bounded-error separation, which in turn led Shor to factoring. Judged as a practical algorithm it is uninteresting; judged as the hinge between "quantum computers are a curiosity" and "quantum computers can be exponentially better", it is foundational.
Does observing a particular nonzero string z tell me anything beyond "balanced"?
For a general balanced function, no. Any certifies balanced, because the all-zeros amplitude vanishes only then, but the specific you obtain is a random sample from the Fourier support of and carries no further guaranteed structure — the majority example scatters the outcome uniformly over four strings. The exception is the linear case , where character orthogonality concentrates all amplitude on the single string . There the "random balanced string" becomes a deterministic readout of the hidden vector , which is exactly the Bernstein–Vazirani algorithm — the same circuit extracting bits instead of one.
How can one query beat 2ⁿ⁻¹ + 1 — isn't the quantum computer also just evaluating f?
It evaluates once, in superposition, but the speed does not come from that. If you tried to read out for all from the post-oracle state you would fail — measurement gives one random and one value, no better than classical. The advantage comes entirely from the second Hadamard layer, which makes the phased branches interfere: for a constant function they add coherently onto , for a balanced function they cancel there completely. The machine is not extracting answers, it is computing a single global property — the average sign — by arranging for all the individual answers to interfere. That is why "quantum parallelism" alone is a misleading slogan: parallel evaluation is free and useless without the interference that follows.
How does Deutsch–Jozsa relate to Bernstein–Vazirani, Simon, and Shor?
They are one family, built on the identical skeleton: Hadamard, oracle (via phase kickback), Hadamard, measure. Deutsch–Jozsa asks a one-bit question. Bernstein–Vazirani specializes the oracle to a linear function and reads out the whole hidden string in one query. Simon's algorithm hides a period in a two-to-one function and achieves an exponential separation that survives bounded error — the first problem to do so. Simon's structure is what suggested that a Fourier transform over rather than could expose the period of modular exponentiation, which is exactly Shor's factoring algorithm. All four are instances of the abelian hidden-subgroup problem, solved by transforming to the Fourier basis and sampling; Deutsch–Jozsa is the simplest member and the one where the mechanism is most transparent.