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 (
HthenCNOT). - Inspect it with
simulator.simulate(...)and print its exact probabilities, confirming the01/10outcomes are (numerically) zero. - Run the same circuit (with measurement added) on
cirq.Simulator()with at least 1000 repetitions, and print the resulting histogram. - Write a Python function
verify_entanglement(counts)that checks every observed outcome in the histogram is either0or3(allow a small tolerance if you experiment with noisy simulation 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.
import cirq
def build_bell_circuit():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit([cirq.H(q0), cirq.CNOT(q0, q1)])
return circuit, q0, q1
def verify_entanglement(counts):
valid = all(outcome in (0, 3) for outcome in counts)
print('Entanglement verified!' if valid else 'Unexpected outcome, entanglement broken or noisy simulation.')
return valid
# 1-2: build and inspect
circuit, q0, q1 = build_bell_circuit()
simulator = cirq.Simulator()
state_result = simulator.simulate(circuit)
print('Probabilities:', abs(state_result.final_state_vector) ** 2)
# 3-4: run and verify
circuit.append(cirq.measure(q0, q1, key='result'))
run_result = simulator.run(circuit, repetitions=1000)
counts = run_result.histogram(key='result')
print('Counts:', counts)
verify_entanglement(counts)
Stretch goals
- Add
circuit.with_noise(cirq.depolarize(p=0.02))before running, and check howverify_entanglementhandles the occasional1/2outcomes noise introduces, does your tolerance need adjusting? - Build a 3-qubit GHZ state (
Hon q0,CNOT(q0, q1),CNOT(q1, q2)) and extendverify_entanglementto check that only0(000) or7(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). - If you have access to Google Quantum AI hardware, run your circuit there and compare to both the ideal and noisy-simulated results.
Submit a link to your finished project (a repo or gist) below, an instructor will review it before you can mark this lesson complete.