In short

Deutsch's algorithm (David Deutsch, 1985) is the first quantum algorithm ever shown to beat its best classical counterpart. The problem: given oracle access to a function f: \{0,1\} \to \{0,1\}, decide whether f is constant (f(0) = f(1)) or balanced (f(0) \neq f(1)). Classically, you must query f twice — once at 0 and once at 1 — then compare. Quantumly, a 5-gate circuit (two Hadamards on the input, the oracle U_f, a Hadamard on the input, and a final measurement) answers the question with one oracle query. The trick is phase kickback: by preparing the target register in |-\rangle, the oracle action |x\rangle|-\rangle \mapsto (-1)^{f(x)}|x\rangle|-\rangle plants the sign of f(x) as a relative phase on the input register. Hadamard interference then converts the phase pattern into a measurable bit. Outcome |0\rangle means constant; outcome |1\rangle means balanced — with certainty. The speedup is tiny in absolute terms, but it is the proof of concept that opened the field: quantum interference can compute global properties of f without learning individual function values.

In 1985, David Deutsch asked a question that nobody had seriously asked before: can a quantum computer actually do something a classical computer cannot? Not in principle, not as a thought experiment — in actual fact, with a specific problem and a specific number of operations.

At the time, the idea of a quantum computer was barely a decade old. Richard Feynman had argued in 1982 that quantum systems are hard to simulate classically, and maybe a quantum machine could simulate them more efficiently. David Deutsch had formalised the notion of a universal quantum Turing machine in 1985. But nobody had shown, concretely, a task on which a quantum algorithm provably beat every classical one. The field had a promise and no demonstration.

Deutsch's own answer, published in the same 1985 paper that defined the quantum Turing machine, is a toy problem. It is so small that the "speedup" — one query instead of two — feels almost like a joke. But the circuit that achieves it introduces every idea that would later power Shor's factoring and Grover's search: superposition on the input register, oracle access to an unknown function, phase kickback as the mechanism, and Hadamard interference as the readout. Every algorithm in Parts 8 through 10 of this track is a descendant of this one. If you want to understand the ancestral move of quantum computing, you begin here.

The problem — constant or balanced

You are given a Boolean function on one bit: f: \{0,1\} \to \{0,1\}. There are exactly four such functions, and you can list them all:

name f(0) f(1) type
constant-0 0 0 constant
constant-1 1 1 constant
identity 0 1 balanced
negation 1 0 balanced

A function is constant if f(0) = f(1) — it returns the same value for every input. It is balanced if f(0) \neq f(1) — half of the inputs map to 0 and the other half to 1. With only one input bit, the two classes are the two "constant" functions and the two "balanced" functions.

The question Deutsch poses: given oracle access to one of these four functions (you don't know which), decide whether it is constant or balanced. You don't have to name the specific function — only its type.

Specifically, the access you have is the standard oracle from the previous chapter:

U_f|x\rangle|y\rangle = |x\rangle|y \oplus f(x)\rangle.

Each application of U_f is one query. Your resource is the number of queries you use. Your goal is to answer "constant or balanced?" using as few as possible.

The four Boolean functions on one bitFour boxes side by side, labelled constant-0, constant-1, identity, negation. Each box has two columns showing x mapping to f of x. The first two boxes are labelled constant and tinted in the accent-soft colour. The last two are labelled balanced and tinted in the ink-soft colour.constant-00010constantconstant-10111constantidentity0011balancednegation0110balancedthe two classes of one-bit functions
The four Boolean functions on one bit. Two are constant (the function is the same at both inputs), two are balanced (the function flips between the two inputs). Deutsch's question: which class does an unknown $f$ belong to?

The classical answer — you need 2 queries

Before meeting the quantum algorithm, get the classical bound pinned down. The claim is that no classical algorithm can answer "constant or balanced?" with fewer than 2 queries.

Suppose you only use 1 query. You pick an input — either 0 or 1 — and you learn f at that point. Say you queried f(0) and got the result b \in \{0, 1\}.

That one bit of information cannot distinguish the two cases. Concretely: if your query returned f(0) = 0, the function could be either constant-0 (in which case f(1) = 0 too) or identity (in which case f(1) = 1). Both are consistent with the single observation. You have no way to tell which class f is in.

Symmetric reasoning applies if you queried f(1) first. Any one query leaves exactly two of the four possible functions alive, and those two always lie in opposite classes — one constant, one balanced. Thus one query is provably insufficient, classically.

Two queries are clearly sufficient: query f(0), query f(1), compare them. If they match, f is constant; if they differ, f is balanced. So the classical query complexity is exactly 2. That is the bar Deutsch wants to beat.

The quantum answer — 1 query suffices

The algorithm Deutsch designed uses exactly one application of U_f. Here is the full circuit.

Deutsch's algorithm circuitA two-wire quantum circuit. Top wire labelled ket zero passes through a Hadamard box H, a U f box spanning both wires, another Hadamard box H, and a measurement meter symbol. Bottom wire labelled ket one passes through a Hadamard H and enters the U f box. Labels below each gate column give the state at that stage: psi naught, psi one, psi two, psi three, and measurement outcome.HHU_fH|0⟩|1⟩result|ψ₀⟩|ψ₁⟩|ψ₂⟩|ψ₃⟩
Deutsch's algorithm. Prepare the input qubit in $|0\rangle$ and the target qubit in $|1\rangle$. Hadamard both. Apply $U_f$ once. Hadamard the input. Measure. The outcome tells you whether $f$ is constant or balanced.

In five gates (two Hs, the oracle U_f, one more H, plus a measurement), the circuit answers the question. The heart of the matter is what happens inside the U_f stage — the magic trick called phase kickback, which converts a bit flip on the target register into a sign flip on the input register. Everything else is standard Hadamard interference.

Walking the circuit step by step

Let's compute the state at each stage of the circuit. Name the states |\psi_0\rangle, |\psi_1\rangle, |\psi_2\rangle, |\psi_3\rangle at the four labelled points in the figure.

Stage 0 — initial state.

|\psi_0\rangle = |0\rangle|1\rangle.

Why the target starts in |1\rangle and not |0\rangle: this is the critical setup choice that makes phase kickback work. When the target is first Hadamarded, |1\rangle becomes |-\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle - |1\rangle) — the minus-sign state. Applying the oracle to |x\rangle|-\rangle pulls out a factor of (-1)^{f(x)} in front. If you started the target at |0\rangle, you would get |+\rangle after the Hadamard and the oracle would just flip the target 50% of the time without kicking any phase back. The initial |1\rangle is the non-negotiable ingredient.

Stage 1 — after the two Hadamards.

|\psi_1\rangle = H|0\rangle \otimes H|1\rangle = |+\rangle \otimes |-\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle) \otimes \tfrac{1}{\sqrt{2}}(|0\rangle - |1\rangle).

Why Hadamard both: the input register needs to be in superposition so U_f can query both f(0) and f(1) in one go; the target register needs to be in |-\rangle so the phase kickback trick fires. Two Hadamards, both essential.

Expanding the tensor product gives

|\psi_1\rangle = \tfrac{1}{2}(|0\rangle + |1\rangle)(|0\rangle - |1\rangle) = \tfrac{1}{2}\big(|0\rangle|0\rangle - |0\rangle|1\rangle + |1\rangle|0\rangle - |1\rangle|1\rangle\big).

Stage 2 — after the oracle. Apply U_f|x\rangle|y\rangle = |x\rangle|y \oplus f(x)\rangle to each term. The rule for the |-\rangle target is the clean one:

U_f|x\rangle|-\rangle = |x\rangle \cdot \tfrac{1}{\sqrt{2}}(|0 \oplus f(x)\rangle - |1 \oplus f(x)\rangle).

Let's unpack the target. If f(x) = 0, then |0 \oplus 0\rangle - |1 \oplus 0\rangle = |0\rangle - |1\rangle = \sqrt{2}|-\rangle. If f(x) = 1, then |0 \oplus 1\rangle - |1 \oplus 1\rangle = |1\rangle - |0\rangle = -(|0\rangle - |1\rangle) = -\sqrt{2}|-\rangle. So in both cases

\tfrac{1}{\sqrt{2}}(|0 \oplus f(x)\rangle - |1 \oplus f(x)\rangle) = (-1)^{f(x)}|-\rangle.

Substituting back:

U_f|x\rangle|-\rangle = (-1)^{f(x)}|x\rangle|-\rangle.

Why this is "phase kickback": the factor (-1)^{f(x)} appears in front of the whole state, but notice that the target register |-\rangle is unchanged. The action of U_f — which was supposed to flip the target by f(x) — has instead been "kicked back" to the input register as a phase. The target was just a catalyst; the information about f(x) ends up on the input qubit as a sign.

Applying this to |\psi_1\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle)|-\rangle:

|\psi_2\rangle = \tfrac{1}{\sqrt{2}}\big((-1)^{f(0)}|0\rangle + (-1)^{f(1)}|1\rangle\big)|-\rangle.

So now the input register holds a superposition of |0\rangle and |1\rangle with signs determined by f(0) and f(1). The target qubit is still in |-\rangle, as if nothing happened to it. (Physically it did — the oracle acted on it — but the net effect is a phase on the input.)

Factor out the common sign (-1)^{f(0)} to get a form that depends only on f(1) - f(0) — the "difference" between the two function values:

|\psi_2\rangle = \tfrac{(-1)^{f(0)}}{\sqrt{2}}\big(|0\rangle + (-1)^{f(1) \oplus f(0)}|1\rangle\big)|-\rangle.

(The overall (-1)^{f(0)} out front is a global phase — it has no physical consequence and will disappear in the final probabilities.) The relative sign inside is (-1)^{f(0) \oplus f(1)}, which depends only on whether f(0) = f(1) or not:

In compact notation:

|\psi_2\rangle = \begin{cases} \pm |+\rangle|-\rangle & f \text{ constant} \\ \pm |-\rangle|-\rangle & f \text{ balanced} \end{cases}

where \pm is a global sign we can drop.

Stage 3 — after the final Hadamard on the input. Apply H to the input register.

H|+\rangle = |0\rangle, \qquad H|-\rangle = |1\rangle.

So

|\psi_3\rangle = \begin{cases} |0\rangle|-\rangle & f \text{ constant} \\ |1\rangle|-\rangle & f \text{ balanced} \end{cases}.

Measurement. Measure the input qubit in the computational basis. If f is constant, you get 0 with certainty. If f is balanced, you get 1 with certainty. The outcome is a deterministic answer to the original question:

\text{outcome} = f(0) \oplus f(1).

One query to U_f, one bit of output, exact answer. The classical lower bound was 2 queries. Deutsch's algorithm solves the problem with half as many queries as any classical algorithm can.

Why this works — the conceptual summary

The quantum algorithm does not learn f(0) or f(1) individually. Those values are gone by the time you measure — in fact, the state |\psi_3\rangle above has no memory of either f(0) or f(1) on their own (except as an unobservable global phase). What the algorithm extracts is the XOR f(0) \oplus f(1) — the single bit that says "are f(0) and f(1) equal or not?"

This is the fundamental difference. A classical algorithm must learn f at both points to compute the XOR; there is no classical way to measure a function's "output XOR" without measuring both outputs. A quantum algorithm can query U_f on a superposition and let interference combine the two outputs into their XOR, without ever committing to a specific value of f at a specific point.

Summarised as a slogan: classical queries extract function values; quantum queries can extract relationships between function values. The relationship f(0) \oplus f(1) is a global property of f — it involves both input points — and the oracle model lets quantum algorithms compute global properties with fewer queries than classical algorithms can. Every large-scale quantum speedup later in the curriculum (Deutsch-Jozsa, Bernstein-Vazirani, Simon, Shor) is a generalisation of this same trick.

Query count comparisonA bar chart comparing classical and quantum query counts. A tall accent-coloured bar labelled "classical 2 queries" and a shorter ink-coloured bar labelled "quantum 1 query" sit side by side. Below, a caption reads "The first proven quantum speedup: 2x fewer queries".classical2 queries necessary2quantum1 query suffices1queriesthe first proven quantum speedup (Deutsch 1985)
Classical query complexity is 2; quantum is 1. A 2x speedup on a toy problem — but the first *proven* separation between classical and quantum query complexity, and the template that all later quantum algorithms would generalise.

Worked examples — trace the circuit for specific functions

Example 1: Trace the algorithm for $f$ = constant-0

Pick the specific function f(0) = 0, f(1) = 0 (the all-zeros constant). Walk through the circuit and confirm the outcome is |0\rangle as predicted.

Step 1. Initial state |\psi_0\rangle = |0\rangle|1\rangle.

Step 2. Apply H \otimes H.

|\psi_1\rangle = |+\rangle|-\rangle = \tfrac{1}{2}(|0\rangle + |1\rangle)(|0\rangle - |1\rangle).

Expanding:

|\psi_1\rangle = \tfrac{1}{2}\big(|00\rangle - |01\rangle + |10\rangle - |11\rangle\big).

Why: Hadamard on the top qubit turns |0\rangle into |+\rangle; Hadamard on the bottom turns |1\rangle into |-\rangle. The joint state is the tensor product of the two.

Step 3. Apply U_f. Since f \equiv 0, the rule |x\rangle|y\rangle \mapsto |x\rangle|y \oplus 0\rangle = |x\rangle|y\rangle means U_f is the identity for a constant-0 function.

|\psi_2\rangle = |\psi_1\rangle = \tfrac{1}{2}(|00\rangle - |01\rangle + |10\rangle - |11\rangle) = |+\rangle|-\rangle.

Why the state doesn't change: f(x) = 0 for every x, so the target is XORed with 0 — i.e. unchanged. The oracle for a constant-0 function is literally the identity on the joint register, and the state at stage 2 is identical to the state at stage 1.

Step 4. Apply H to the input register. H|+\rangle = |0\rangle, so

|\psi_3\rangle = |0\rangle|-\rangle.

Why: the top qubit was in |+\rangle; Hadamard takes |+\rangle back to |0\rangle (H is self-inverse). The bottom qubit is unchanged — Deutsch's algorithm doesn't touch the target register after stage 1.

Step 5. Measure the input qubit. The state is |0\rangle|-\rangle; the input is exactly |0\rangle, so measurement yields 0 with probability 1.

Result. The algorithm outputs 0 ⇒ classifies f as constant. Correct, because f is indeed constant-0.

State trace through Deutsch's algorithm for constant-0A horizontal timeline with four state-boxes labelled psi naught through psi three. The first box shows ket 0 ket 1, the second shows plus minus state, the third shows plus minus state again (oracle was identity), the fourth shows ket 0 ket minus. Arrows connect them and label the gates applied between: H cross H, then U f as identity, then H cross I. A final measurement box shows outcome 0 with probability 1.|ψ₀⟩|0⟩|1⟩H⊗H|ψ₁⟩|+⟩|−⟩U_f = I|ψ₂⟩|+⟩|−⟩H⊗I|ψ₃⟩|0⟩|−⟩measureoutcome = 0, Prob = 1⇒ classify as constant ✓
State trace for $f$ = constant-0. The oracle is the identity (no flips), the input register stays in $|+\rangle$, the final Hadamard sends it to $|0\rangle$, and measurement returns 0 with certainty. Classification: constant. Correct.

Example 2: Trace the algorithm for $f$ = identity

Now pick f(0) = 0, f(1) = 1 — the identity function. This is a balanced function; the algorithm should output 1.

Step 1. Same initial state |\psi_0\rangle = |0\rangle|1\rangle and the same post-Hadamard state |\psi_1\rangle = |+\rangle|-\rangle as Example 1.

Step 2. Apply U_f for the identity function. The rule on |-\rangle targets gives

U_f|x\rangle|-\rangle = (-1)^{f(x)}|x\rangle|-\rangle = (-1)^x|x\rangle|-\rangle.

Apply term by term to |\psi_1\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle)|-\rangle:

|\psi_2\rangle = \tfrac{1}{\sqrt{2}}\big((-1)^0 |0\rangle + (-1)^1 |1\rangle\big)|-\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle - |1\rangle)|-\rangle = |-\rangle|-\rangle.

Why the minus sign landed on |1\rangle: the oracle for f = identity produces (-1)^{f(0)} = +1 in front of |0\rangle and (-1)^{f(1)} = -1 in front of |1\rangle. The relative sign between the two input terms flipped from + (in |+\rangle) to - (in |-\rangle). That is the phase kickback making itself visible.

Step 3. Apply H to the input register. H|-\rangle = |1\rangle, so

|\psi_3\rangle = |1\rangle|-\rangle.

Why: The input register was in |-\rangle after stage 2. Hadamarding |-\rangle gives |1\rangle (from the basic Hadamard identities). The target is untouched.

Step 4. Measure the input qubit. The state is |1\rangle|-\rangle; the input is exactly |1\rangle, so measurement yields 1 with probability 1.

Result. The algorithm outputs 1 ⇒ classifies f as balanced. Correct.

State trace through Deutsch's algorithm for identityA horizontal timeline with four state-boxes labelled psi naught through psi three. The first box shows ket 0 ket 1, the second shows plus minus state, the third shows minus minus state (phase kickback put the input register in the minus state), the fourth shows ket 1 ket minus. Final measurement yields 1 with probability 1, classifying f as balanced.|ψ₀⟩|0⟩|1⟩H⊗H|ψ₁⟩|+⟩|−⟩U_f (kickback)|ψ₂⟩|−⟩|−⟩H⊗I|ψ₃⟩|1⟩|−⟩measureoutcome = 1, Prob = 1⇒ classify as balanced ✓
State trace for $f$ = identity. The phase kickback flips the input register from $|+\rangle$ to $|-\rangle$ during the oracle call. The final Hadamard maps $|-\rangle$ to $|1\rangle$, and measurement returns 1 with certainty. Classification: balanced. Correct.

For the two remaining functions (constant-1 and negation), the story is the same up to a global phase that cannot be measured. Constant-1 produces |\psi_2\rangle = -|+\rangle|-\rangle — a global -1 in front of the same state as Example 1 — and still measures 0. Negation produces -|-\rangle|-\rangle — a global -1 in front of the same state as Example 2 — and still measures 1. The algorithm correctly sorts all four functions into their classes.

Common confusions

Going deeper

If you are here to know what Deutsch's algorithm does and why, you have it: one query distinguishes constant from balanced by using phase kickback to plant the XOR of f(0) and f(1) on the input register as a phase pattern, which Hadamard interference reads out. The rest of this section goes further — the information-theoretic framing, the historical context of 1985, the connection to parity-learning problems, and the generalisation path that leads to Deutsch-Jozsa, Bernstein-Vazirani, Simon, and eventually Shor.

Information-theoretically — measuring XOR, not components

Here is the cleanest framing of why 1 quantum query beats 2 classical queries.

The problem "is f constant or balanced?" is equivalent to the problem "compute f(0) \oplus f(1)". If the answer is 0, the function is constant; if 1, balanced. A classical algorithm cannot compute f(0) \oplus f(1) from fewer than 2 queries because a single query gives one bit, and f(0) \oplus f(1) depends on two bits that a single query cannot fix simultaneously. Formally, any classical oracle-access algorithm making k queries produces an output that is a function of k bits (the oracle answers); computing f(0) \oplus f(1) deterministically from k bits requires those k bits to cover both f(0) and f(1), which forces k \geq 2.

A quantum algorithm is not restricted to extracting bit values. By querying in superposition and using phase kickback, it can extract parities (XOR combinations) with one query. Deutsch's algorithm specifically computes the parity f(0) \oplus f(1) — a two-input linear function of the output bits — using one oracle call. This is the first example of a general pattern: quantum queries can compute parities of oracle outputs in one shot, even when the individual outputs are unknown. Bernstein-Vazirani later cashes this pattern in at scale, learning an n-bit hidden string from one query.

Historical context — 1985

A few things about 1985 are worth holding onto.

First, the field of quantum computing did not exist as a recognised discipline. Feynman's 1982 paper had suggested quantum-based simulators for quantum physics; Deutsch's 1985 paper did two things: (a) it defined the quantum Turing machine as a computational model, and (b) it gave the first non-trivial algorithm in that model. The paper — "Quantum theory, the Church-Turing principle and the universal quantum computer" [1] — is readable and landmark.

Second, the problem itself is contrived. Deutsch is not solving anything you would care about in a practical sense — you would not build a quantum computer just to decide whether a 1-bit function is constant or balanced. The problem is designed to be the simplest possible instance where a quantum advantage can be demonstrated cleanly. The real payoff came in 1992 when Deutsch and Jozsa generalised the argument to n-bit functions, showing an exponential classical-quantum query gap. Bernstein and Vazirani (1993), Simon (1994), and Shor (1994) followed in rapid succession, each generalising the core ideas further.

Third, Deutsch's original 1985 algorithm was slightly different from the modern textbook version. The original used a probabilistic (not deterministic) version that only gave the right answer half the time. The clean deterministic version above is due to Cleve, Ekert, Macchiavello, and Mosca (1998), who cleaned up several of the early algorithms including Deutsch and Grover.

The generalisation to n bits — preview

The obvious question: what if f is a function on n bits instead of one? If f: \{0,1\}^n \to \{0,1\} is promised to be either constant (same value on every input) or balanced (equal number of 0s and 1s in its output), can you still distinguish the two classes with one quantum query?

Yes — and this is the Deutsch-Jozsa algorithm, which comes two chapters later. The circuit is a direct generalisation: Hadamard on every input qubit, Hadamard on the target qubit (initialised to |1\rangle), one oracle query, Hadamard on every input qubit, measure. One query suffices. Classically, in the worst case, you need 2^{n-1} + 1 queries (a little more than half the inputs) to be sure, so the query gap is exponential.

The phase-kickback argument is the same: the oracle acts on |-\rangle in the target to deposit a phase (-1)^{f(x)} on each term of the input superposition, and the final Hadamard layer interferes those phases to produce a deterministic outcome (all |0\rangle^n if constant, otherwise some non-zero string). Every subsequent algorithm you meet — Bernstein-Vazirani, Simon, Grover, Shor — builds on this same skeleton: Hadamard layer → oracle → Hadamard (or more general) unitary → measurement.

Why this chapter is load-bearing

Deutsch's algorithm is the smallest instance of every idea that makes quantum computing work.

Every algorithm later in the track does these four moves at a bigger scale. Grover replaces f(0) \oplus f(1) with "is there an x with f(x) = 1?" — a different global property, but still computed by interference on a phase-marked register. Shor replaces the XOR with the period of a modular exponentiation function — yet another global property, unreachable classically. The machinery is the same.

Indian CS and quantum theory

Deutsch's algorithm is standard material in every Indian theoretical computer science programme that touches quantum. Courses at IIT Madras, IIT Bombay, IISc Bangalore, CMI Chennai, and TIFR (Mumbai) introduce it early as the archetypal quantum algorithm, before moving on to Deutsch-Jozsa and the later algorithms. The algorithm's small size makes it amenable to pen-and-paper analysis, and students are typically expected to work through the constant/balanced case by hand (as you did in the worked examples) before tackling the n-bit generalisation.

Alongside this, India has a strong tradition in mathematical theory of quantum information — Ambainis-style adversary bounds, characters of finite groups for hidden subgroup problems, and the combinatorial identities underlying phase estimation all attract theoretical attention at these institutes. Deutsch's algorithm's role is as the first rung: the smallest example of a quantum speedup, which students of all these sub-fields master on the way to the harder material.

Where this leads next

References

  1. David Deutsch, Quantum theory, the Church-Turing principle and the universal quantum computer (1985), Proceedings of the Royal Society ADOI:10.1098/rspa.1985.0070. The original paper.
  2. Cleve, Ekert, Macchiavello, Mosca, Quantum algorithms revisited (1998) — arXiv:quant-ph/9708016. The modern deterministic reformulation of Deutsch's algorithm.
  3. Nielsen and Chuang, Quantum Computation and Quantum Information (2010), §1.4.3 and §1.4.4 — Cambridge University Press.
  4. John Preskill, Lecture Notes on Quantum Computation, Chapter 6 — theory.caltech.edu/~preskill/ph229.
  5. Wikipedia, Deutsch–Jozsa algorithm — includes the single-bit Deutsch case and the n-bit generalisation.
  6. Qiskit Textbook, Deutsch-Jozsa Algorithm — hands-on implementation in Qiskit, runnable on IBM's quantum hardware.