Part V · Chapter 26

Deutsch–Jozsa Algorithm

Read ~18 min Prerequisites Ch. 25 Tools adjustable-n widget, code cell Live
Basic

Take Deutsch's circuit from Ch. 25 — H, oracle, H, measure — and run it on n qubits instead of one. Nothing about the circuit changes in structure. What changes is the classical comparison: a 1-vs-2 query advantage becomes 1-vs-exponentially-many.

The generalized problem

f: {0,1}ⁿ → {0,1}, promised to be either constant (same output for every input) or balanced (exactly half the inputs give 0, half give 1) — never anything in between. Classically, in the worst case, you might need to check 2ⁿ⁻¹+1 inputs before you're sure: get unlucky and see the same output 2ⁿ⁻¹ times in a row, and you still can't rule out "constant" until one more query either confirms it or reveals a different value.

Intermediate
Fig. 26.1 — pick n and a function typesame H–oracle–H circuit as Ch. 25, just wider

Blue bar = the all-zero outcome; amber = everything else. Constant functions put 100% of the probability on the blue bar; every balanced function tested puts exactly 0% there — try n=5 and watch this still hold across 32 possible outcomes.

Why "all-zero or not" is enough

Ch. 22's Hadamard-transform math generalizes exactly: P(measure 0...0) = |(1/N)Σₓ(−1)^f(x)|² . If f is constant, every term in that sum has the same sign, so they add up to ±1 and the probability is exactly 1. If f is balanced, exactly half the terms are +1 and half are −1 — they cancel completely, giving probability exactly 0. There's no overlap between these two cases, ever, which is exactly why a single measurement settles the question with total certainty.

Advanced

The promise is doing a lot of work

It's worth being honest about what makes this algorithm "easy": the promise that f is either perfectly constant or perfectly balanced, with nothing in between, is doing enormous work. Real-world functions are rarely so cooperative. Deutsch–Jozsa is the cleanest possible demonstration that quantum interference can produce an exponential query-complexity separation, but it's also a famous example of a problem engineered specifically to showcase that gap — Grover's algorithm (Ch. 28) and Shor's algorithm (Ch. 31) tackle problems with real-world motivation and correspondingly messier, harder-won speedups.

Fig. 26.2 — code, runs in your browser
At n=5 the classical worst case is 17 queries; the quantum algorithm still needs exactly 1 — the same gap Fig. 26.1 shows you visually.
Check your understanding

What's the promise on f that makes Deutsch–Jozsa solvable in 1 query?

f is guaranteed to be either perfectly constant or perfectly balanced — never anything in between.

Check your understanding

What's the classical worst-case query count for n=4 qubits?

2³+1 = 9 queries.

Check your understanding

Why does measuring anything other than all-zero prove f is balanced, with certainty?

Because P(all-zero) is exactly 1 for constant functions and exactly 0 for balanced ones — there's no overlap, so any nonzero measurement outcome is only possible if f is balanced.

Check your understanding

What structurally changes between Deutsch's algorithm (Ch. 25) and Deutsch–Jozsa?

Nothing structurally — it's the exact same H–oracle–H circuit, just run on n qubits instead of 1. The only thing that changes is the size of the classical comparison, which grows exponentially.

Check your understanding

Why is it important to acknowledge that the constant-vs-balanced promise is unusually generous?

Because it shows the exponential speedup here comes partly from a problem specifically engineered to showcase quantum interference, not from solving a "naturally occurring" hard problem — unlike Grover's or Shor's algorithms, which tackle messier, more realistic problems.

Review queue

Scroll back to a chapter's quiz cards to review them once they're due — a full cross-chapter queue view is a planned upgrade.