14 min read
The Deutsch-Jozsa Algorithm
One oracle query to tell constant from balanced, the first algorithm with a proven quantum speedup.
Suggested before this lesson
You now have every ingredient: uniform superposition and exact interference cancellation (from Superposition and Interference at Scale), phase kickback (from Phase Kickback), and the oracle convention (from Oracles and Black-Box Functions, sharpened last lesson into the formal constant/balanced distinction). Deutsch-Jozsa combines all four into the first algorithm in this course with a proven quantum speedup over any classical approach.
You're handed an oracle for with a guarantee: is either constant (same output for every input) or balanced (0 for exactly half the inputs, 1 for the other half). Nothing in between. Which one is it?
Classically, in the worst case you have to check more than half of all inputs before you can be sure: if the first all come back 0, you still can't rule out "balanced, and I just haven't seen a 1 yet" until one more check. Deutsch-Jozsa answers with certainty using a single query to the oracle.
Exactly the black box from the oracles lesson: the algorithm never gets to look inside this, only query it once.
Black Box
If every input qubit reads : is constant. If any input qubit reads : is balanced. That's the whole algorithm.
An oracle for the always-0 function does nothing at all (no gates). So step 3 is empty, and steps 2 and 4 become two s in a row on each input qubit with nothing in between: exactly the -then- interference from Superposition and Interference at Scale. Every input qubit returns to with certainty.
Circuit
x
h
Step through the circuit: after gate 6 of 6
7 / 7
Measurement Probabilities
50.0%
0.0%
0.0%
0.0%
50.0%
0.0%
0.0%
0.0%
Exact State
| Basis state | Re | Im | Probability |
|---|---|---|---|
| 0.707 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| -0.707 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 |
A balanced function that depends only on : the oracle is a single from qubit 0 into the ancilla (the oracle lesson's linear-oracle recipe, using just one of the two inputs).
Circuit
x
h
cx
Step through the circuit: after gate 7 of 7
8 / 8
Measurement Probabilities
0.0%
50.0%
0.0%
0.0%
0.0%
50.0%
0.0%
0.0%
Exact State
| Basis state | Re | Im | Probability |
|---|---|---|---|
| 0 | 0 | ||
| 0.707 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| -0.707 | 0 | ||
| 0 | 0 | ||
| 0 | 0 |
Every nonzero outcome now has input bits , never : one query, and you know for certain this is balanced.
Live preview of the circuit detected in your code
Python
Python
Python