|q⟩ Bad Qubits

beginner · Programming · First Quantum Algorithms: Deutsch & Deutsch–Jozsa

Quantum vs Classical Queries

The Deutsch–Jozsa algorithm is striking not just because it works, but because of how much better it works. This lesson puts the quantum and classical query counts side by side, derives the classical lower bound from first principles, and explains exactly what makes the gap exponential.

The question being asked

Recall the problem: given an oracle for an nn-bit function f:{0,1}n{0,1}f:\{0,1\}^n \to \{0,1\} that is promised to be either constant or balanced, determine which it is. Each call to the oracle counts as one query.

Classical worst case

A classical algorithm must call ff on specific input strings and read back 00 or 11 each time. No amount of cleverness lets it probe two inputs simultaneously.

After kk queries, the algorithm has seen kk output bits. Suppose all kk of them equal 00. At this point, two scenarios are still compatible with the oracle's promise:

The second scenario remains possible as long as k2n1k \le 2^{n-1}, because a balanced function could produce kk identical outputs in a row by chance. Only when k=2n1+1k = 2^{n-1}+1 is the situation resolved: if you have seen 2n1+12^{n-1}+1 identical outputs, there are no longer enough inputs mapping to the other value to make the function balanced, so it must be constant.

Therefore, the worst-case classical query complexity is

Qclassical=2n1+1.Q_{\text{classical}} = 2^{n-1} + 1.

This is an exponential number of queries in nn. For n=50n = 50 it exceeds 5×10145 \times 10^{14}, and for n=51n = 51 it surpasses 101510^{15}.

Quantum: always one query

The Deutsch–Jozsa algorithm answers the same question with exactly one oracle call regardless of nn. There is no probabilistic shortcut here — the output is deterministic. If the measurement yields 0n|0\rangle^{\otimes n}, the function is constant; any other outcome certifies it is balanced, and both conclusions hold with certainty.

The separation is therefore as sharp as possible:

| Approach | Queries (worst case) | |----------|---------------------| | Classical | 2n1+12^{n-1}+1 | | Deutsch–Jozsa | 11 |

What makes one query sufficient

Three ingredients combine to achieve the single-query result:

  1. Quantum parallelism. Applying HnH^{\otimes n} to nn qubits initialized to 0n|0\rangle^{\otimes n} creates the uniform superposition 12nxx\tfrac{1}{\sqrt{2^n}}\sum_{x}|x\rangle over all 2n2^n inputs at once. A single oracle call therefore effectively evaluates ff on all inputs simultaneously.

  2. Phase kickback. The ancilla qubit, prepared in |-\rangle, causes the oracle to write ff's value as a (1)f(x)(-1)^{f(x)} phase factor on each input component rather than into a separate register. No measurement is disturbed.

  3. Interference. The second layer of HnH^{\otimes n} recombines the 2n2^n phase-tagged amplitudes. For a constant function, every term in the sum 12nx(1)f(x)\tfrac{1}{2^n}\sum_x (-1)^{f(x)} carries the same sign, giving constructive interference at 0n|0\rangle^{\otimes n} and probability 11 for the all-zeros outcome. For a balanced function, the +1+1 and 1-1 terms cancel exactly, driving that probability to 00.

Neither superposition alone nor phase kickback alone would suffice. It is their coordinated interaction — parallelism to spread, kickback to encode, interference to decode — that compresses exponentially many logical comparisons into a single physical query.

How far this advantage extends

Deutsch–Jozsa is often described as a "toy" problem because the promise (constant or balanced) is artificial. No cryptographic or optimization task naturally comes in this form. Yet the structural lesson is profound: query complexity can be exponentially smaller in the quantum model than in any classical model, even deterministic ones.

This principle reappears throughout quantum computing. Bernstein and Vazirani showed a polynomial separation for a related problem; Simon's algorithm demonstrated an exponential gap for a group structure problem; and Shor's factoring algorithm traces its efficiency, in part, back to the same combination of superposition and interference that powers Deutsch–Jozsa. Understanding the query comparison here is the first step toward appreciating why those deeper algorithms work.

Sign in on the full site to ask questions and join the discussion.