Quantum
Module 7 · Algorithms · Lesson 1

Deutsch-Jozsa

The first algorithm that showed a quantum computer can beat a classical one. A toy problem — but a meaningful proof of concept.

10 min read · Lesson 21 of 32

The Deutsch-Jozsa algorithm (1992) was the first demonstrated example of a quantum algorithm that is exponentially faster than the best classical algorithm — and it’s simple enough to explain in a single lesson. It’s a toy problem, yes. But it’s the proof that quantum speedup is real.

The problem

You’re given a function f:{0,1}n{0,1}f: \{0, 1\}^n \to \{0, 1\}. You don’t know what ff is, but you’re promised that it’s either:

Your job: decide which. You can only access ff through a “query” — you give it an xx and it tells you f(x)f(x).

The classical lower bound

How many queries do you need, in the worst case, to be certain? If you’ve queried ff on 2n12^{n-1} inputs and they were all 0, you still can’t be sure — maybe it’s constant 0, or maybe it’s balanced and all the 1s are in the unsampled inputs. You need to query one more to be sure.

So the classical worst-case cost is 2n1+12^{n-1} + 1 queries. That’s exponential in nn: for n=30n = 30 bits, you’d need over half a billion queries.

The quantum algorithm

One query. That’s the full quantum cost, regardless of nn.

Here’s the circuit (in the phase-kickback form, which is a little cleaner):

  1. Initialize nn qubits in 0n|0\rangle^{\otimes n}.
  2. Apply HH to each qubit: uniform superposition 12nxx\frac{1}{\sqrt{2^n}}\sum_{x} |x\rangle.
  3. Apply the phase oracle: x(1)f(x)x|x\rangle \mapsto (-1)^{f(x)}|x\rangle. This is “one query,” in the sense that ff is evaluated in superposition.
  4. Apply HH to each qubit again.
  5. Measure all qubits.

Result: If ff is constant, you measure 0n|0\rangle^{\otimes n} with probability 1. If ff is balanced, you measure anything other than 0n|0\rangle^{\otimes n} with probability 1.

So a single run of the circuit, followed by checking whether the measurement was all-zeros, decides the question with certainty.

Try it

Pick each of the five functions in turn. Notice:

A single measurement is enough to distinguish the two cases.

Why it works

The H-gates create a uniform superposition, the oracle stamps each basis state with (1)f(x)(-1)^{f(x)}, and the final layer of H-gates converts this phase pattern into a position pattern via interference. The key identity is:

Hn0n=12nx{0,1}nxH^{\otimes n} |0\rangle^{\otimes n} = \frac{1}{\sqrt{2^n}}\sum_{x \in \{0,1\}^n} |x\rangle

and then the final H layer amounts to a Fourier-like transform: the amplitude at 0n|0\rangle^{\otimes n} after everything is 12nx(1)f(x)\frac{1}{2^n}\sum_x (-1)^{f(x)}. This sum is either ±1\pm 1 (if ff is constant) or 00 (if ff is balanced, because half the terms cancel the other half).

So:

The Hadamard gates set up interference; the oracle imposes a phase pattern; the final Hadamards cash in the interference to read the answer.

What this proves — and what it doesn’t

Deutsch-Jozsa proves a promise problem can be solved exponentially faster on a quantum computer than on a classical one, if you require zero error probability. If you relax the requirement and allow a small probability of error, classical randomized algorithms can solve it in constant time (try a few random inputs; if they differ, it’s balanced; otherwise it’s very probably constant).

So Deutsch-Jozsa is a demonstration, not a breakthrough. The real breakthroughs are Shor’s algorithm (exponential speedup for factoring, with no classical equivalent even with randomness) and Grover’s search (quadratic speedup for unstructured search). We’ll see both this module.

Quick check
In the Deutsch-Jozsa algorithm, how many queries to f does the quantum algorithm require?
Quick check
If f is balanced, what is the probability of measuring |0...0⟩ at the end of the Deutsch-Jozsa circuit?
Quick check
Why is Deutsch-Jozsa considered a 'toy' speedup even though it's exponentially faster than the classical worst case?

What’s next

The next two algorithms in Module 7 — Bernstein-Vazirani and Simon’s — are variants of the same “phase-kickback + Hadamard interference” recipe, applied to slightly different promise problems. Then we get to the real headliners: Grover’s search and Shor’s factoring.