Welcome back. In our previous lesson, we delved into the mathematics of Grover's algorithm, understanding it as a process of rotation and learning how to calculate the optimal number of iterations to maximize our chances of success. We established that the algorithm hinges on two key components: the oracle and the diffusion operator.
Now, it's time to move from theory to practice. This lesson is dedicated to the first and most crucial of these components. We will focus entirely on fulfilling our next learning outcome: to construct the oracle for a 2-qubit Grover's search in Qiskit. You will learn what a "phase oracle" is, how to implement one using standard quantum gates, and how to generalize this construction to mark any of the possible states in a 2-qubit system.
1. The Oracle's Task: Marking the Winner
Before we write any code, let's solidify our understanding of what the oracle must accomplish. In the context of Grover's algorithm, the oracle's job is not to tell us which state is the winner, but simply to "mark" it. It does this by applying a specific transformation: it flips the phase of the target state's amplitude, while leaving all other states unchanged.
Imagine our 2-qubit system is in a perfect superposition, which we can write as:
If our target state, let's call it , is , the oracle must perform the following transformation:
Notice that only the amplitude of the state has been multiplied by -1. This is the "phase flip" that marks our target.
The following video provides a concise explanation of this core function.
Grovers Algorithm — Programming on Quantum Computers — Coding with Qiskit S2E3
This video from the Qiskit channel, "Grovers Algorithm", directly explains how the quantum oracle works by flipping the sign of the winner.
Watch the short segment from the introduction to the quantum oracle. Pay attention to how it encodes the "yes/no" answer of a classical oracle into a phase change on a quantum state.
2. Implementation: The Controlled-Z Gate as an Oracle
So, how do we build a circuit that performs this specific phase flip? For the case where our target is , there is a conveniently perfect gate for the job: the Controlled-Z (CZ) gate.
As its name implies, the CZ gate applies a Z gate to its target qubit if and only if its control qubit is in the state . Let's trace its effect on all four basis states:
- : The control qubit (let's say qubit 0) is 0. Nothing happens. Output: .
- : The control qubit is 0. Nothing happens. Output: .
- : The control qubit is 1, but the target qubit (qubit 1) is 0. The Z gate has no effect on . Output: .
- : The control qubit is 1, and the target qubit is 1. The Z gate is applied to qubit 1, transforming to . Output: .
The CZ gate does exactly what we need for an oracle marking . The following video explains this from a circuit and matrix perspective. Your background in linear algebra will be helpful here to see how the gate operation corresponds to its matrix form.
2-Bit Example of Grover's Quantum Search Algorithm, Geometric Visualization, Quantum Circuit Diagram
This video, "2-Bit Example of Grover's Quantum Search Algorithm", clearly illustrates how a CZ gate serves as the oracle and shows its matrix representation.
First, watch the explanation of how the CZ gate is used as the oracle. Then, watch the segment on the matrix representation. Note how the '-1' in the matrix directly corresponds to the phase flip on the |11\rangle state.
The image below shows a complete Grover's circuit, where you can clearly see the CZ gate acting as the oracle.

3. Building and Verifying the Oracle in Qiskit
Now, let's translate this into code. We will build our oracle as a QuantumCircuit object and then create a separate test circuit to verify its behavior.
The following Qiskit documentation provides the essential code for defining the oracle.
Grover's Algorithm and Amplitude Amplification
This tutorial page shows the simplest way to specify a phase-flip oracle in Qiskit.
Focus on the first code block under the heading "Running Grover’s algorithm". It demonstrates how to create a QuantumCircuit and use the .cz() method to define an oracle that marks the '11' state.
Following that example, the code to create the oracle is straightforward:
from qiskit import QuantumCircuit
# Create a quantum circuit for the oracle with 2 qubits
oracle = QuantumCircuit(2, name='oracle')
# Apply a Controlled-Z gate to mark the |11> state
oracle.cz(0, 1)
# Let's see what it looks like
oracle.draw(output='mpl')
This creates the oracle, but how do we know it works? We need to test it. We'll do this by:
- Creating a 2-qubit test circuit.
- Applying Hadamard gates to both qubits to generate the uniform superposition state .
- Appending our
oraclecircuit. - Using Qiskit's
StatevectorSimulatorto inspect the final quantum state.
from qiskit.quantum_info import Statevector
# Create the test circuit
test_circuit = QuantumCircuit(2)
# 1. Prepare the uniform superposition
test_circuit.h([0, 1])
test_circuit.barrier()
# 2. Apply the oracle
test_circuit.append(oracle, [0, 1])
test_circuit.barrier()
# 3. Get the final statevector
final_state = Statevector(test_circuit)
# Print the statevector, rounded for clarity
for i, amp in enumerate(final_state.data):
print(f"Amplitude for |{i:02b}>: {amp.real:.2f}")
When you run this code, you should see the following output:
Amplitude for |00>: 0.50
Amplitude for |01>: 0.50
Amplitude for |10>: 0.50
Amplitude for |11>: -0.50
This confirms our oracle successfully flipped the phase of the state, just as intended.
4. Generalizing the Oracle: Marking Other States
The CZ gate is a perfect oracle for , but what if we want to search for a different state, like ? We need a general method.
The key is to use Pauli-X gates to transform the desired state into before the CZ gate, and then transform it back after. This is a common technique in circuit design, analogous to using inverters in classical logic to enable a specific gate.
Let's say we want to mark the state :
- The state we want to mark is . The CZ gate marks .
- To make look like , we need to flip the first qubit from 0 to 1. We do this with an X gate on qubit 0. The state becomes .
- Now we apply the CZ gate, which flips the phase: .
- Finally, we apply the X gate on qubit 0 again to undo our initial transformation. The state becomes .
The resulting oracle for marking is an X-CZ-X "sandwich":X(qubit 0) -> CZ(0, 1) -> X(qubit 0)
We can generalize this pattern for all four possible states in a 2-qubit system:
| Target State | Oracle Circuit Construction |
|---|---|
| $ | 00\rangle$ |
| $ | 01\rangle$ |
| $ | 10\rangle$ |
| $ | 11\rangle$ |
This powerful and modular technique allows you to construct an oracle for any basis state you want to find.
Conclusion
In this lesson, we transitioned from the abstract theory of the oracle to its concrete implementation. You've built your first, and most fundamental, piece of a quantum algorithm.
Key Takeaways:
- An oracle for Grover's search is a phase oracle, which marks a target state by flipping its phase (multiplying its amplitude by -1).
- For a 2-qubit search, the Controlled-Z (CZ) gate acts as a natural and efficient oracle for the state .
- We can construct oracles for any other basis state (e.g., ) by "sandwiching" the CZ gate with Pauli-X gates to transform the target state to and back again.
- You can verify an oracle's correctness in Qiskit by preparing a uniform superposition, applying the oracle, and checking the output with the
StatevectorSimulator.
Preview of the Next Lesson:
With the oracle constructed, we are halfway to a full Grover iteration. The next step is to build the second component: the diffusion operator. This operator, also known as the amplitude amplification step, is responsible for taking the phase flip from the oracle and using it to increase the amplitude of the marked state. In the next lesson, we will construct the diffuser circuit and then combine it with our oracle to run a complete 2-qubit Grover's search.