The Deutsch-Jozsa Algorithm

Imagine a black box that computes some unknown Boolean function on nn 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 2n2^n 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 nn 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 (n=1n=1), 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 2n2^n 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.

Key takeaways
  • Problem: decide if f:{0,1}n→{0,1}f:\{0,1\}^n\to\{0,1\} 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 2n−1+12^{n-1}+1 queries in the worst case; quantum needs exactly 1 oracle call.
  • The algorithm applies Hadamards to nn 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 ±1\pm 1 if ff is constant and exactly 00 if ff 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.
The core insight

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 n≥1n \ge 1. You are given oracle access to a Boolean function

f:{0,1}n→{0,1},f : \{0,1\}^n \to \{0,1\},

with the promise that ff is either constant (i.e. f(x)f(x) is the same value for every xx) or balanced (i.e. f(x)=0f(x) = 0 for exactly half of the 2n2^n inputs and f(x)=1f(x)=1 for the other half). The task is to determine which case holds. Oracle access means you cannot inspect the internal description of ff; you may only query it, classically by evaluating f(x)f(x) for a chosen xx, or quantum-mechanically by applying a unitary UfU_f that implements ff reversibly. The quantum oracle is the unitary

Uf:∣x⟩∣y⟩  ↦  ∣x⟩∣y⊕f(x)⟩,U_f : |x\rangle|y\rangle \;\mapsto\; |x\rangle|y \oplus f(x)\rangle,

acting on an nn-qubit query register ∣x⟩|x\rangle and a 1-qubit target register ∣y⟩|y\rangle. This is the natural reversible implementation of ff: it is a permutation of basis states, hence unitary, and it reduces to ordinary evaluation of ff when ∣x⟩,∣y⟩|x\rangle,|y\rangle are computational basis states. Crucially, UfU_f can be applied to a superposition of inputs xx, evaluating ff at all 2n2^n 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 ∣−⟩=12(∣0⟩−∣1⟩)|-\rangle = \frac{1}{\sqrt2}(|0\rangle - |1\rangle), then

Uf ∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩.U_f \, |x\rangle|-\rangle = (-1)^{f(x)} |x\rangle |-\rangle .

The target register is left completely unchanged, but the query register ∣x⟩|x\rangle picks up a phase of (−1)f(x)(-1)^{f(x)}. Applying UfU_f to a superposition over all xx therefore imprints the entire truth table of ff into relative phases across the 2n2^n-dimensional query register, using only one call to the oracle. The Deutsch-Jozsa algorithm's remaining job is purely classical linear algebra: use the nn-qubit Hadamard transform to convert this phase pattern into a measurement outcome that reveals whether ff was constant or balanced.

How it works

The circuit uses n+1n+1 qubits: an nn-qubit query register initialized to ∣0⟩⊗n|0\rangle^{\otimes n}, and a single target qubit initialized to ∣1⟩|1\rangle (so that after a Hadamard it becomes ∣−⟩|-\rangle).

Step 1 — Initialize. Prepare the state ∣0⟩⊗n⊗∣1⟩|0\rangle^{\otimes n} \otimes |1\rangle.

Step 2 — Superpose. Apply Hadamard HH to every qubit. The query register becomes a uniform superposition over all 2n2^n strings, and the target becomes ∣−⟩|-\rangle:

∣ψ1⟩=(12n∑x∈{0,1}n∣x⟩)⊗∣−⟩.|\psi_1\rangle = \left(\frac{1}{\sqrt{2^n}} \sum_{x \in \{0,1\}^n} |x\rangle\right) \otimes |-\rangle .

Step 3 — Query the oracle once. Apply UfU_f. By phase kickback, each basis term ∣x⟩∣−⟩|x\rangle|-\rangle picks up phase (−1)f(x)(-1)^{f(x)} and the target is untouched:

∣ψ2⟩=12n∑x(−1)f(x)∣x⟩⊗∣−⟩.|\psi_2\rangle = \frac{1}{\sqrt{2^n}} \sum_{x} (-1)^{f(x)} |x\rangle \otimes |-\rangle .

Step 4 — Un-superpose the query register. Apply H⊗nH^{\otimes n} to the query register only. Using the identity H⊗n∣x⟩=12n∑z(−1)x⋅z∣z⟩H^{\otimes n}|x\rangle = \frac{1}{\sqrt{2^n}}\sum_z (-1)^{x\cdot z}|z\rangle (the Hadamard Transform on nn qubits, where x⋅zx\cdot z is bitwise inner product mod 2), the amplitude of each basis state ∣z⟩|z\rangle becomes

αz=12n∑x(−1)f(x)(−1)x⋅z.\alpha_z = \frac{1}{2^n} \sum_{x} (-1)^{f(x)} (-1)^{x\cdot z}.

Step 5 — Measure the query register. Measure all nn query qubits in the computational basis. If the outcome is z=0nz = 0^n, 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 UfU_f — everything else is single-qubit gates and measurement, both cheap and classical to simulate in isolation.

Figure
Deutsch-Jozsa circuit: n query qubits plus one ancilla, a single oracle call between two Hadamard layers
⋮ |0⟩ |0⟩ |0⟩ |1⟩ query x1..xn H H H H U_f one query H H H z z z (discarded) z = 0ⁿ ⟺ constant; z ≠ 0ⁿ ⟺ balanced

Complexity & correctness

Query complexity. The algorithm makes exactly one call to UfU_f, regardless of nn. Every other step is a layer of single-qubit Hadamard gates (O(n)O(n) of them) and a final measurement. Total gate complexity is O(n)O(n), and query complexity is 11. Contrast this with the classical deterministic query complexity. Deterministically, an adversary can answer f(x1)=f(x2)=⋯=f(x2n−1)=0f(x_1) = f(x_2) = \dots = f(x_{2^{n-1}}) = 0 for any 2n−12^{n-1} queries you choose, consistent with ff being either the all-zero constant function or some balanced function that happens to be 00 on exactly those points. Only the (2n−1+1)(2^{n-1}+1)-th query can force a decision: if it also returns 0, ff must be constant (since a balanced function cannot be 0 on more than half its inputs); if it returns 1, ff is balanced. So 2n−1+12^{n-1}+1 queries are necessary and sufficient classically — deterministic query complexity is Θ(2n)\Theta(2^n). 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 kk times, guess "constant" with error probability at most 2−(k−1)2^{-(k-1)}, so O(1)O(1) 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 "11 exact query vs. O(1)O(1) 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 11; exact (zero-error) classical deterministic query complexity is 2n−1+12^{n-1}+1.

Correctness proof. We show the amplitude α0n\alpha_{0^n} of the all-zeros outcome equals ±1\pm1 when ff is constant, and equals exactly 00 when ff 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 ∣z⟩|z\rangle in the query register after the second Hadamard layer is

αz=12n∑x∈{0,1}n(−1)f(x)(−1)x⋅z.\alpha_z = \frac{1}{2^n}\sum_{x\in\{0,1\}^n} (-1)^{f(x)}(-1)^{x\cdot z}.

Set z=0nz = 0^n. Then x⋅z=0x \cdot z = 0 for every xx, so

α0n=12n∑x(−1)f(x).\alpha_{0^n} = \frac{1}{2^n}\sum_x (-1)^{f(x)}.

Case 1: ff constant. Every term (−1)f(x)(-1)^{f(x)} equals the same value c∈{+1,−1}c \in \{+1,-1\}, so the sum has 2n2^n identical terms and α0n=12n⋅2n⋅c=c=±1\alpha_{0^n} = \frac{1}{2^n}\cdot 2^n \cdot c = c = \pm 1. Since amplitudes have magnitude at most 1 and total squared magnitude 1, this forces α0n=±1\alpha_{0^n} = \pm 1 and every other amplitude is exactly 0. Measuring the query register yields z=0nz = 0^n with probability ∣α0n∣2=1|\alpha_{0^n}|^2 = 1 — certainty. Case 2: ff balanced. Exactly 2n−12^{n-1} of the terms (−1)f(x)(-1)^{f(x)} equal +1+1 (where f(x)=0f(x)=0) and exactly 2n−12^{n-1} equal −1-1 (where f(x)=1f(x)=1). The sum is 2n−1−2n−1=02^{n-1} - 2^{n-1} = 0, so α0n=0\alpha_{0^n} = 0. Measuring the query register yields z=0nz = 0^n with probability 00 — it is impossible, so any observed outcome is some z≠0nz \ne 0^n, 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 ff at all 2n2^n points at once." That framing is misleading: after the oracle call, the state ∣ψ2⟩|\psi_2\rangle does encode all 2n2^n values of ff in relative phase, but no measurement of ∣ψ2⟩|\psi_2\rangle directly in the computational basis would reveal that global structure — it would just return a uniformly random xx. The real work is done by the second Hadamard layer, which performs a global linear combination (a Walsh–Hadamard transform) of all 2n2^n phases simultaneously, concentrating them constructively at z=0nz=0^n exactly when they are all equal (constant case) and causing perfect destructive cancellation at z=0nz=0^n exactly when half are +1+1 and half are −1-1 (balanced case). This constructive/destructive interference pattern, not raw parallel evaluation, is what a classical computer cannot cheaply replicate: reproducing αz\alpha_z for even one zz classically requires summing 2n2^n terms.

Tradeoffs and caveats, honestly stated.

  • The advantage is only proven relative to the black-box model, where the only permitted access to ff is through queries. If ff is given by an explicit formula or circuit, classical algorithms may exploit that structure and the separation can vanish.
  • The promise (that ff is constant or balanced, no other case) is essential to the algorithm's logic; if ff 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 O(1)O(1) 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 n=3n = 3 and the balanced function defined on x=x2x1x0x = x_2x_1x_0 (3-bit strings) by

f(x)=x0⊕x1f(x) = x_0 \oplus x_1

(ignore x2x_2; this is a valid balanced function since exactly half of the 8 inputs give x0⊕x1=0x_0\oplus x_1 = 0).

xx (binary)x0x_0x1x_1f(x)=x0⊕x1f(x)=x_0\oplus x_1(−1)f(x)(-1)^{f(x)}
000000+1
001101−1
010011−1
011110+1
100000+1
101101−1
110011−1
111110+1

After Step 2 (Hadamards on all 4 qubits), the query register is a uniform superposition over all 8 strings with amplitude 18\frac{1}{\sqrt 8} each, tensored with ∣−⟩|-\rangle on the target.

After Step 3 (one oracle call, phase kickback), the query register state is

∣ψ2⟩query=18(∣000⟩−∣001⟩−∣010⟩+∣011⟩+∣100⟩−∣101⟩−∣110⟩+∣111⟩).|\psi_2\rangle_{\text{query}} = \frac{1}{\sqrt 8}\Big(|000\rangle - |001\rangle - |010\rangle + |011\rangle + |100\rangle - |101\rangle - |110\rangle + |111\rangle\Big).

Notice the sign pattern depends only on x0⊕x1x_0 \oplus x_1, exactly as expected.

After Step 4 (Hadamard transform), compute α000=18∑x(−1)f(x)\alpha_{000} = \frac{1}{8}\sum_x (-1)^{f(x)} using the table: the sum of the last column is 1−1−1+1+1−1−1+1=01-1-1+1+1-1-1+1 = 0. So α000=0\alpha_{000} = 0, confirming that z=000z = 000 is impossible, consistent with ff being balanced. A fuller computation shows the surviving nonzero amplitudes are exactly those zz orthogonal (mod 2, under dot product) to the "direction" of ff's phase pattern; here one finds nonzero amplitude only at z∈{011,100,111,000}z \in \{011, 100, 111, 000\}-style patterns determined by x0⊕x1x_0\oplus x_1's dependency — concretely αz≠0\alpha_z \ne 0 exactly for zz with z0=z1z_0 = z_1 pattern matching the XOR structure (this generalizes the Bernstein–Vazirani phenomenon, foreshadowed below). Measuring might yield, say, z=011z = 011: since z≠000z \ne 000, the algorithm correctly outputs "balanced," using a single call to UfU_f, whereas a deterministic classical strategy could have been fed the answers f(000)=0,f(001)=1f(000){=}0, f(001){=}1 and would need up to 23−1+1=52^{3-1}+1 = 5 queries in the worst case to be certain in general. For contrast, take the constant function g(x)=1g(x) = 1 for all xx. Then every entry of the (−1)g(x)(-1)^{g(x)} column is −1-1, so α000=18(−8)=−1\alpha_{000} = \frac{1}{8}(-8) = -1, forcing all other amplitudes to zero. The measurement returns z=000z=000 with certainty, and the algorithm outputs "constant" — correctly, and again from a single query.

Common pitfalls & traps

PitfallWhy it happensFix
Measuring the target qubit and expecting informationThe target is left in ∣−⟩|-\rangle regardless of ff; it carries no distinguishing informationOnly the query register after the second Hadamard layer matters; discard the target
Forgetting to initialize the target to ∣1⟩\lvert1\rangle before the HadamardWithout the ∣−⟩\lvert-\rangle state, phase kickback does not occur and UfU_f merely computes f(x)f(x) into yy, not a phaseAlways prepare target as ∣1⟩\lvert1\rangle, then HH, to get ∣−⟩\lvert-\rangle
Applying the second Hadamard layer to the target tooHarmless numerically here but conceptually confuses which register carries the answerApply the un-computing Hadamard layer only to the nn query qubits
Assuming the algorithm works without the promiseIf ff is neither constant nor balanced, z=0nz=0^n can still occur with ff unbalanced, giving a meaningless "constant" verdictThe 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 algorithmsRandomized algorithms already solve the bounded-error version in O(1)O(1) queriesState the separation precisely: exact quantum (11 query) vs. exact/deterministic classical (Θ(2n)\Theta(2^n) queries)
Believing UfU_f "computes all values of ff at once" in a usable senseSuperposition alone gives access to only one xx upon measurement; the values are hidden in relative phaseEmphasize that interference (the second Hadamard layer), not parallel evaluation, extracts the global answer

Pioneers & history

Origins · 1 minDavid Deutsch and how this was discovered
DD
David Deutsch
born 1953 · University of Oxford, pioneer of quantum computation theory

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.

History
1985
— David Deutsch introduces the universal quantum computer and the single-query algorithm for n=1n=1 (the ancestor of this algorithm, The Deutsch Algorithm).
1992
— Deutsch and Richard Jozsa generalize the result to nn bits, proving the exponential separation against deterministic classical algorithms described here.
1993
— Richard Cleve, and later Michael Nielsen, along with Ethan Bernstein and Umesh Vazirani, refine and simplify the algorithm, connecting it to Fourier/Hadamard-transform reasoning that becomes standard in the field.
1997
— Ethan Bernstein and Umesh Vazirani publish The Bernstein-Vazirani Algorithm, reusing the identical circuit skeleton to recover a hidden bit-string in one query, sharpening the phase-kickback idea into an information-extraction tool.
1990
s–2000s — The Deutsch-Jozsa construction becomes the canonical opening example in the theory of Query Complexity and Quantum Speedups, and its Hadamard-sandwich structure recurs in Simon's algorithm and the quantum Fourier transform machinery behind Shor's algorithm.

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 ZN\mathbb{Z}_N. In teaching and research, Deutsch-Jozsa is the standard first experiment run on new quantum hardware and new simulators, precisely because its correctness condition (z=0nz=0^n 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 2n−1+12^{n-1}+1 queries. Randomized classical algorithms with a small allowed error probability solve the same promise problem using only O(1)O(1) 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 UfU_f acts on a control register ∣x⟩|x\rangle and a target register prepared in an eigenstate of the XOR operation, namely ∣−⟩=12(∣0⟩−∣1⟩)|-\rangle = \frac{1}{\sqrt2}(|0\rangle-|1\rangle). Since Uf∣x⟩∣y⟩=∣x⟩∣y⊕f(x)⟩U_f|x\rangle|y\rangle = |x\rangle|y\oplus f(x)\rangle, when yy carries the state ∣−⟩|-\rangle, XORing f(x)f(x) into it either leaves ∣−⟩|-\rangle unchanged (if f(x)=0f(x)=0) or flips its overall sign to −∣−⟩-|-\rangle (if f(x)=1f(x)=1), because ∣1⟩⊕1→∣0⟩|1\rangle\oplus 1 \to |0\rangle and ∣0⟩⊕1→∣1⟩|0\rangle \oplus 1 \to |1\rangle exactly swaps the two terms of ∣−⟩|-\rangle, introducing a global minus sign. The target's state is identical before and after; only a phase factor (−1)f(x)(-1)^{f(x)} is factored out and attached to ∣x⟩|x\rangle instead.

Why does the algorithm need the promise that f is constant or balanced?

The correctness proof relies on the sum ∑x(−1)f(x)\sum_x (-1)^{f(x)} taking one of exactly two possible values: ±2n\pm 2^n (constant case, giving amplitude ±1\pm1) or 00 (balanced case, giving amplitude 00). If ff is neither constant nor balanced, this sum can take any value strictly between −2n-2^n and 2n2^n, and the amplitude α0n\alpha_{0^n} becomes some intermediate number whose squared magnitude is a probability strictly between 00 and 11. The binary "constant vs. balanced" output no longer has a guaranteed correct interpretation; a z=0nz=0^n 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 n=1n=1: it distinguishes the four possible one-bit functions f:{0,1}→{0,1}f:\{0,1\}\to\{0,1\} into "constant" (f(0)=f(1)f(0)=f(1)) versus "balanced" (f(0)≠f(1)f(0)\ne f(1)) using one query. The Deutsch-Jozsa algorithm generalizes the circuit verbatim — same Hadamard-oracle-Hadamard structure — to nn query qubits, and the correctness argument scales identically via the nn-qubit Hadamard transform's algebra rather than a single-qubit calculation. Historically, Deutsch published the n=1n=1 case in 1985, and the nn-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 z=0nz=0^n is exactly 11 when ff is constant and exactly 00 when ff 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 00-or-11 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 nn-bit string ss from an oracle computing fs(x)=s⋅x  2f_s(x) = s \cdot x \bmod 2, 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 nn bits of ss at once, because the post-oracle amplitude pattern concentrates entirely on the single basis state ∣s⟩|s\rangle rather than just at ∣0n⟩|0^n\rangle. 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 nn-qubit Hadamard transform, H⊗n∣x⟩=2−n/2∑z(−1)x⋅z∣z⟩H^{\otimes n}|x\rangle = 2^{-n/2}\sum_z (-1)^{x\cdot z}|z\rangle, which is exactly the discrete Walsh–Hadamard transform over F2n\mathbb{F}_2^n. Applying it to the phase-encoded state 2−n/2∑x(−1)f(x)∣x⟩2^{-n/2}\sum_x (-1)^{f(x)}|x\rangle produces amplitudes αz=2−n∑x(−1)f(x)(−1)x⋅z\alpha_z = 2^{-n}\sum_x (-1)^{f(x)}(-1)^{x\cdot z}, which for z=0z=0 reduces to the average of (−1)f(x)(-1)^{f(x)} over all inputs. This average is ±1\pm1 exactly when all terms agree (constant ff) and 00 exactly when they split evenly (balanced ff), by simple arithmetic — the same interference principle underlying the discrete Fourier transform used later in Shor's algorithm.

Before this

Where this leads