Final Project: Build and Verify an Entangled Circuit
Every piece from this course now exists on its own: building circuits, superposition, gates, entanglement, shot-based measurement, and a quantum random number generator. This final project assembles several of them into one small, verifiable quantum program.
Requirements
Your finished quantum_project.py should satisfy every one of these:
- Build a 2-qubit Bell state circuit (
hthencx). - Inspect it with
Statevectorand print its exact probabilities, confirming|01>/|10>are (numerically) zero. - Run the same circuit on
AerSimulatorwith at least 1000 shots, and print the resultingcounts. - Write a Python function
verify_entanglement(counts)that checks every observed bitstring incountsis either'00'or'11'(allow a small tolerance if you experiment with real hardware in the stretch goals), and prints whether entanglement was verified. - Reuse (or rewrite) the
quantum_random_bits(n_bits)function from the RNG lesson, and use it to generate and print 5 random numbers between 0 and 255.
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit_aer import AerSimulator
def build_bell_circuit():
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
return qc
def verify_entanglement(counts):
valid = all(bitstring in ('00', '11') for bitstring in counts)
print('Entanglement verified!' if valid else 'Unexpected outcome, entanglement broken or noisy hardware.')
return valid
# 1-2: build and inspect
qc = build_bell_circuit()
state = Statevector(qc)
print('Probabilities:', state.probabilities())
# 3-4: run and verify
qc.measure([0, 1], [0, 1])
simulator = AerSimulator()
counts = simulator.run(qc, shots=1000).result().get_counts()
print('Counts:', counts)
verify_entanglement(counts)
Stretch goals
- Run your circuit on real IBM Quantum hardware (free tier) and compare the counts to the ideal simulator, is entanglement still verified within a reasonable tolerance?
- Build a 3-qubit GHZ state (
h(0), thencx(0, 1), thencx(1, 2)) and extendverify_entanglementto check that only'000'/'111'appear. - Add a simple quantum coin flip game: use
quantum_random_bits(1)to decide a winner, and explain in a comment why this is fundamentally fairer thanrandom.randint(0, 1). - Plot the
countshistogram withmatplotlibinstead of just printing the dictionary.
Submit a link to your finished project (a repo or gist) below, an instructor will review it before you can mark this lesson complete.