Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master the crown jewel of unstructured search: the Phase Oracle, Inversion about the Mean (Diffusion Operator), and O(sqrt(N)) speedup.
Interactive 3D state vector exploration, unitary transformation gates, and Born rule collapse for Topic 7.4
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Total search space size | Number of items in the unsorted database () | |
| Optimal iteration count | Number of times to run the Oracle + Diffusion loop | |
| Uniform superposition state | Equal mixture of all states: | |
| Diffusion operator (Inversion about mean) | , reflecting amplitudes across the average | |
| Rotation angle per Grover step | Angular step rotating the state vector toward the target item in 2D Hilbert space |
Searching for 1 marked item among N = 4 items (2 qubits, |s⟩ = [0.5, 0.5, 0.5, 0.5]^T) where target is |11⟩:
Result: In just 1 single iteration, the target item reaches exactly 100% probability (P() = 1.0² = 1.00)!
Initialize register into |s> using Hadamard gates: all items have equal height 1/sqrt(N).
Invert the sign of the target item |omega> -> -|omega>.
Apply Diffusion operator; target amplitude shoots up while non-targets shrink.
Halt after exactly R iterations and measure target with probability > 99%.
Problem: Evaluating symmetric key strength against quantum brute-force search.
Solution: Grover cuts effective key security in half ($2^{256} \rightarrow 2^{128}$), prompting security standards to recommend 256-bit keys.
Interactive activity component
Implement Grover’s quantum search algorithm to find target state |11> using Oracle (CZ) and Diffusion Operator functions composed onto a 2-qubit circuit with Aer simulation and histogram plotting.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install qiskit qiskit-aer matplotlib1 from qiskit import QuantumCircuit 2 from qiskit.visualization import plot_histogram 3 from qiskit_aer import Aer 4 import matplotlib.pyplot as plt
from qiskit import QuantumCircuit → Assemble quantum circuits in Qiskit
from qiskit.visualization import plot_histogram → Visualize measurement distribution
from qiskit_aer import Aer → Import Aer simulator backend
import matplotlib.pyplot as plt → Matplotlib plotting backend
1 # Define the Oracle (marks the |11⟩ state) 2 def grover_oracle(): 3 oracle = QuantumCircuit(2) 4 oracle.cz(0, 1) # Controlled-Z gate to mark |11⟩ 5 return oracle 6 7 # Define the Diffusion Operator (amplifies the correct state) 8 def diffusion_operator(): 9 diffusion = QuantumCircuit(2) 10 diffusion.h([0, 1]) # Apply Hadamard gates 11 diffusion.x([0, 1]) # Apply X gates 12 diffusion.cz(0, 1) # Apply Controlled-Z gate 13 diffusion.x([0, 1]) # Apply X gates again 14 diffusion.h([0, 1]) # Apply Hadamard gates again 15 return diffusion 16 17 # Create a 2-qubit quantum circuit 18 qc = QuantumCircuit(2, 2) 19 20 # Step 1: Initialize with Hadamard gates 21 qc.h([0, 1]) 22 23 # Step 2: Apply Grover's Oracle using .compose() 24 qc = qc.compose(grover_oracle()) 25 26 # Step 3: Apply the Diffusion Operator using .compose() 27 qc = qc.compose(diffusion_operator()) 28 29 # Step 4: Measure the qubits 30 qc.measure([0, 1], [0, 1]) 31 32 # Execute on Aer simulator 33 simulator = Aer.get_backend('aer_simulator') 34 job = simulator.run(qc, shots=1000) 35 result = job.result() 36 counts = result.get_counts(qc) 37 print("Grover Search Measurement Results (Lesson 9):", counts) 38 plot_histogram(counts) 39 plt.show()
# Define the Oracle (marks the |11⟩ state)
Oracle function
def grover_oracle():
Function definition
oracle = QuantumCircuit(2)
2-qubit circuit for oracle
oracle.cz(0, 1) # Controlled-Z gate to mark |11⟩
Controlled-Z adds negative phase to |11>
return oracle
Return oracle circuit
# Define the Diffusion Operator (amplifies the correct state)
Diffusion function
def diffusion_operator():
Function definition
diffusion = QuantumCircuit(2)
2-qubit circuit for diffusion
diffusion.h([0, 1]) # Apply Hadamard gates
H on all wires
diffusion.x([0, 1]) # Apply X gates
X on all wires
diffusion.cz(0, 1) # Apply Controlled-Z gate
Controlled phase inversion
diffusion.x([0, 1]) # Apply X gates again
Uncompute X
diffusion.h([0, 1]) # Apply Hadamard gates again
Uncompute H
return diffusion
Return diffusion circuit
# Create a 2-qubit quantum circuit
Main algorithm circuit
qc = QuantumCircuit(2, 2)
Allocate 2 qubits and 2 classical bits
# Step 1: Initialize with Hadamard gates
Equal superposition
qc.h([0, 1])
Put register into uniform superposition
# Step 2: Apply Grover's Oracle using .compose()
Mark target state
qc = qc.compose(grover_oracle())
Append oracle sub-circuit
# Step 3: Apply the Diffusion Operator using .compose()
Amplify marked amplitude
qc = qc.compose(diffusion_operator())
Append diffusion sub-circuit
# Step 4: Measure the qubits
Readout
qc.measure([0, 1], [0, 1])
Measure qubits into classical bits
# Execute on Aer simulator
Run simulation
simulator = Aer.get_backend('aer_simulator')
Load aer_simulator backend
job = simulator.run(qc, shots=1000)
Execute 1,000 shots
result = job.result()
Get simulation results
counts = result.get_counts(qc)
Retrieve measurement counts
print("Grover Search Measurement Results (Lesson 9):", counts)
Display counts (100% |11>)
plot_histogram(counts)
Render histogram of measurement counts
plt.show()
Display figure
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Lesson 9: Grover Search and Amplitude Amplification 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install qiskit qiskit-aer matplotlib 8 9 # Step 2: Import libraries 10 from qiskit import QuantumCircuit 11 from qiskit.visualization import plot_histogram 12 from qiskit_aer import Aer 13 import matplotlib.pyplot as plt 14 15 # Step 3: Run Demonstration 16 # Define the Oracle (marks the |11⟩ state) 17 def grover_oracle(): 18 oracle = QuantumCircuit(2) 19 oracle.cz(0, 1) # Controlled-Z gate to mark |11⟩ 20 return oracle 21 22 # Define the Diffusion Operator (amplifies the correct state) 23 def diffusion_operator(): 24 diffusion = QuantumCircuit(2) 25 diffusion.h([0, 1]) # Apply Hadamard gates 26 diffusion.x([0, 1]) # Apply X gates 27 diffusion.cz(0, 1) # Apply Controlled-Z gate 28 diffusion.x([0, 1]) # Apply X gates again 29 diffusion.h([0, 1]) # Apply Hadamard gates again 30 return diffusion 31 32 # Create a 2-qubit quantum circuit 33 qc = QuantumCircuit(2, 2) 34 35 # Step 1: Initialize with Hadamard gates 36 qc.h([0, 1]) 37 38 # Step 2: Apply Grover's Oracle using .compose() 39 qc = qc.compose(grover_oracle()) 40 41 # Step 3: Apply the Diffusion Operator using .compose() 42 qc = qc.compose(diffusion_operator()) 43 44 # Step 4: Measure the qubits 45 qc.measure([0, 1], [0, 1]) 46 47 # Execute on Aer simulator 48 simulator = Aer.get_backend('aer_simulator') 49 job = simulator.run(qc, shots=1000) 50 result = job.result() 51 counts = result.get_counts(qc) 52 print("Grover Search Measurement Results (Lesson 9):", counts) 53 plot_histogram(counts) 54 plt.show()
Apply Hadamard gates to initialize both qubits into uniform equal superposition.
The CZ gate adds a -1 phase to target state |11>, marking it without altering classical probabilities.
The H-X-CZ-X-H sequence inverts amplitudes about their mean, amplifying |11> to 100% amplitude.
1,000 shots on AerSimulator reveal 100% probability for the marked target |11> in a single query.
2-qubit circuit initialized to |00>, CZ oracle targeting |11>, and Grover diffusion operator.
Grover Search Measurement Results (Lesson 9): {'11': 1000}Demonstrates quadratic speedup: in an unsorted 4-item search space, Grover algorithm finds target |11> in exactly one quantum iteration with 100% deterministic success.
Oracle, diffusion operator, interference, and quadratic search speedup — Presentation by Dr. Syed Muzamil Basha.
Test your understanding with these questions. Remember, this is for learning, not examination!