10 min read
Oracles and Black-Box Functions
What a quantum oracle is, why it has to be reversible, and how to build one from a classical boolean function.
Suggested before this lesson
Deutsch-Jozsa and Bernstein-Vazirani, the two algorithms this track builds toward, are both phrased as "you're given a black-box function : figure out a property of it using as few queries as possible." Before either algorithm makes sense, it's worth being precise about what that black box actually is as a circuit.
A classical function isn't automatically something you can turn into a quantum gate: quantum gates are unitary, which means reversible: you must always be able to undo them. A generic classical function usually isn't reversible (many inputs can map to the same output), so you can't just "compute and overwrite the answer."
The standard fix is to never overwrite anything: XOR the result into a fresh ancilla qubit instead:
(the input register) passes through unchanged. (the ancilla) comes out XORed with . This is always reversible (applying twice XORs back out), no matter what itself does, which is exactly why it works as a gate even when isn't invertible.
This is the shape every oracle in this course has: query register in on the left, unchanged on the right; ancilla in on the left, XORed with on the right.
Black Box
For any that's a linear function of its inputs, (XOR together whichever inputs the function actually depends on), the oracle is just a from each relevant input qubit into the ancilla. Each flips the ancilla exactly when that one input is 1, and XOR is exactly "flip an odd number of times."
Take over 2 input qubits, with qubit 2 as the ancilla:
Circuit
cx
Step through the circuit: after gate 2 of 2
3 / 3
Exact State
| Basis state | Re | Im | Probability |
|---|---|---|---|
| 1.000 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 | ||
| 0 | 0 |
This kind of (depends on some but not all inputs in a perfectly balanced way) is called a balanced function: exactly half of all possible inputs give and half give . The other extreme is a constant function, for every (an oracle that does nothing at all) or for every (an oracle that's just on the ancilla, ignoring the input register completely). Constant vs. balanced is exactly the distinction Deutsch-Jozsa is built to tell apart.
Live preview of the circuit detected in your code
Python
Python