Building the DJ Oracle
You have seen how the Deutsch–Jozsa algorithm uses a single oracle query to decide whether a Boolean function is constant or balanced. The algorithm works regardless of how the oracle is built — but at some point you have to build one. This lesson shows how to translate a concrete balanced function into a circuit that a quantum computer can execute.
What the oracle must do
Recall the standard oracle construction. Given a function , the oracle unitary acts on an -qubit input register and a 1-qubit ancilla register:
where denotes addition modulo 2 (XOR). The input is left untouched; the function value is XOR-ed into the ancilla . Applying twice returns the original state, confirming that the mapping is reversible.
The XOR function on two input bits
For the Deutsch–Jozsa problem with input bits, the oracle acts on 3 qubits altogether: qubits 0 and 1 form the input register, and qubit 2 is the ancilla. A convenient balanced function to use as an example is
You can verify it is balanced by enumeration: the four possible inputs give outputs , , , — exactly two zeros and two ones, confirming the definition of a balanced function.
The oracle for this function must implement
Building the oracle from CNOT gates
The CNOT gate does exactly one thing: it XOR-flips the target qubit conditioned on the control qubit. Formally, .
To build for we need qubit 2 to accumulate both and via XOR. Two CNOTs suffice, applied in sequence:
- : sends qubit 2 from to .
- : sends qubit 2 from to .
After both gates the ancilla holds , which is exactly what the oracle specification demands. The two input qubits are unchanged throughout.
Try it
Implement the oracle for on a 3-qubit circuit. The grader checks the full unitary matrix, so leaving the circuit empty will not pass — you must add the two CNOT gates that write the function value into the ancilla.
Sign in on the full site to ask questions and join the discussion.