Cirq Fundamentals

Lesson 8 of 8

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:

  1. Build a 2-qubit Bell state circuit (H then CNOT).
  2. Inspect it with simulator.simulate(...) and print its exact probabilities, confirming the 01/10 outcomes are (numerically) zero.
  3. Run the same circuit (with measurement added) on cirq.Simulator() with at least 1000 repetitions, and print the resulting histogram.
  4. Write a Python function verify_entanglement(counts) that checks every observed outcome in the histogram is either 0 or 3 (allow a small tolerance if you experiment with noisy simulation in the stretch goals), and prints whether entanglement was verified.
  5. 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 how verify_entanglement handles the occasional 1/2 outcomes noise introduces, does your tolerance need adjusting?
  • Build a 3-qubit GHZ state (H on q0, CNOT(q0, q1), CNOT(q1, q2)) and extend verify_entanglement to check that only 0 (000) or 7 (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 than random.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.

This lesson ends in a project. Build it on your own machine, there's nowhere to submit it here, but the brief above is everything you need.