Skip to main content

12.5 An intro to QC programming

Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §12.5–12.5.1. Original sketches — do not paste textbook Hadamard.py / CirqSwap.py dumps. Continues 12.4b. Labs: 12.5b.

Qubits and gates become circuits (programs). Cirq is a Python library for writing, simulating, and optimizing those circuits. Graded work on this site uses pure-Python amplitude / bit locks (no Cirq in the sandbox). Run the sketches below in a local conda/venv when you want the real simulator.

Install (local)​

python -m pip install --upgrade pip
python -m pip install cirq
# or: conda install -c conda-forge cirq

First circuit: Hadamard on ∣0⟩|0\rangle​

# hadamard_sketch.py — structure only (run locally with Cirq)
import cirq

circuit = cirq.Circuit()
q = cirq.GridQubit(0, 0)
circuit.append(cirq.H(q))
sim = cirq.Simulator()
print(circuit)
print(sim.simulate(circuit)) # ≈ 0.707|0⟩ + 0.707|1⟩

Matches

H∣0⟩=12(∣0⟩+∣1⟩).H|0\rangle=\tfrac1{\sqrt2}(|0\rangle+|1\rangle).

Two Hadamards = identity​

H2=IH^2=I. Append a second cirq.H(a) on a NamedQubit('a'); the statevector returns to ∣0⟩|0\rangle.

X then H → ∣−⟩|-\rangle​

X maps ∣0⟩→∣1⟩|0\rangle\to|1\rangle; then H yields 12(∣0⟩−∣1⟩)\tfrac1{\sqrt2}(|0\rangle-|1\rangle), an eigenstate of XX with eigenvalue −1-1.

Measure: X, Z, H + shots​

Measurement is not unitary. Append cirq.measure(a, key='result'), then sim.run(circuit, repetitions=1000) and plot a histogram. Ideal amplitudes after X Z HX\,Z\,H on ∣0⟩|0\rangle are equal magnitude (±1/2\pm 1/\sqrt2), so counts of 00 and 11 each sit near 500500 (noise → slight tilt — Figure 12.3).

Figure 12.3 — measurement histogram sketch.

Two qubits: LineQubit, SWAP, CNOT​

q0, q1 = cirq.LineQubit.range(2)
circuit.append(cirq.X(q0)) # |10⟩ if start |00⟩
circuit.append(cirq.Z(q1)) # phase only on |1⟩ of q1
# expected state |10⟩

X on q0, Z on q1 → |10⟩.

SWAP exchanges the two lines: after XX on q0 you get ∣01⟩|01\rangle.

Then SWAP → |01⟩.

CNOT(control=q0, target=q1): with ∣10⟩|10\rangle input, flips the target → ∣11⟩|11\rangle.

Three qubits: Toffoli​

q0, q1, q2 = cirq.LineQubit.range(3)
circuit.append(cirq.X(q0))
circuit.append(cirq.Toffoli(q0, q1, q2)) # flips q2 only if q0=q1=1

With only q0 flipped, controls are not both 11, so the target stays put (e.g. ∣100⟩|100\rangle or ∣101⟩|101\rangle depending on q2’s prep). Flip both controls to see T∣110⟩=∣111⟩T|110\rangle=|111\rangle.

12.5.1 Half and full adders​

Half-adder (sum + carry)​

Classical: sum =q0⊕q1=q0\oplus q1, carry =q0⋅q1=q0\cdot q1. Quantum recipe on three lines (q2 starts at ∣0⟩|0\rangle):

  1. Toffoli(q0, q1, q2) → write carry into q2
  2. CNOT(q0, q1) → write sum into q1

Half-adder circuit.

Lab: wire the half-adder​

Check truth table: 1+11+1 → sum 00, carry 11; 1+01+0 and 0+10+1 → sum 11, carry 00.

Full adder​

A full adder folds in a carry-in. One Cirq-shaped skeleton (four qubits): cascade Toffoli / CNOT so that after prep of q0,q1,Cin the sum and Cout match classical binary addition. Fill the table in labs for all four input pairs (Cin fixed or varied).

q0q1CinSumCout
∥0⟩\|0\rangle∥0⟩\|0\rangle………
∥0⟩\|0\rangle∥1⟩\|1\rangle………
∥1⟩\|1\rangle∥0⟩\|0\rangle………
∥1⟩\|1\rangle∥1⟩\|1\rangle………

Takeaway​

GoalCirq idea
SuperpositionH on GridQubit / NamedQubit
H2=IH^2=Itwo H on same line
Statisticsmeasure + run(..., repetitions=)
Entangle / flipCNOT, SWAP, Toffoli
AddToffoli carry + CNOT sum
Quiz

Cirq / adders

1 / 3

Two Hadamard gates on |0⟩ yield…

Pick an answer to see if you are right.

Next: 12.5b Cirq labs · 12.6 IBM Quantum.