Skip to main content

12.11 Quantum-computing code sketches

Source: Structural ideas from Computational Physics (Landau, Páez, Bordeianu), §12.11 listings. Original sketches only — do not paste Entangle.py, QFT4.py, QFTn.py, OracleSim.py, OracleIBM.py, or Shor.py. Continues 12.10b. Labs: 12.11b.

Graded locks live in 12.1b–12.10b. This page maps how local notebook pieces fit. Qiskit / Cirq / IBM tokens stay on your machine.

Dipole Hamiltonian (NumPy, local)​

Two-spin dipole piece ∝XX+YY+ZZ−3ZZ\propto XX+YY+ZZ-3ZZ on C4\mathbb C^4. Build the 4×44\times 4 matrix, eig, then optionally rotate HH into the eigenbasis (should look diagonal up to numerics).

# dipole_h_sketch.py — original structure (local NumPy)
# XX = [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]]
# YY, ZZ similarly; H = XX + YY + ZZ - 3*ZZ
# es, ev = numpy.linalg.eig(H)
# Check Bell-like columns for degenerate subspace (see §12.3)

Pure-Python entry locks: 12.3b.

QFT₄ circuit (Qiskit, local)​

Two qubits: H on qubit 1, controlled-phase ±π/2\pm\pi/2, H on qubit 0, SWAP. Export the unitary with Operator and compare to the analytic QFT4\mathrm{QFT}_4 matrix (12.8).

# qft4_sketch.py — structure
# qc = QuantumCircuit(2)
# qc.h(1); qc.cp(±pi/2, 0, 1); qc.h(0); qc.swap(0, 1)
# print(Operator(qc).data) # match §12.8 matrix to ~1e-3

nn-qubit QFT​

Loop: for i=n−1,…,0i = n-1,\ldots,0: Hadamard on ii, then controlled phases onto earlier qubits; optional bit-reversal SWAPs. Diff against the library QFT(n) gate — max abs entry error should be ∼0\sim 0 for small nn.

# qft_n_sketch.py — structure
# for i in reversed(range(n)):
# qc.h(i)
# for j in range(i):
# qc.cp(angle / 2**(i-j-1), j, i)
# swaps for bit reverse unless skip_swap

Grover: oracle + diffuser + driver​

Oracle for marked ω∈{0,…,15}\omega\in\{0,\ldots,15\}: XX on bits that should be 00, multi-controlled XX with HH sandwich on the target (phase kick), undo XXs.

Diffuser: H⊗nH^{\otimes n}, X⊗nX^{\otimes n}, multi-controlled ZZ (via HH–MCX–HH), undo XX and HH.

Driver: H⊗4H^{\otimes 4}, then R≈πN/4R\approx\pi\sqrt{N}/4 rounds of oracle(ω\omega) + diffuser (use N=16N=16, not n=4n=4, in the square root), measure_all, simulator histogram. IBM path: transpile to a live backend — expect a noisier peak (12.9).

# grover_sketch.py — structure
# qc.h(range(4))
# R = ceil(pi * sqrt(16) / 4) # ← N=16 items
# for _ in range(R):
# qc.append(oracle(9), range(4))
# qc.append(diffuser(4), range(4))
# qc.measure_all()
# # simulator vs IBM: same circuit, different backend

Lab: wire sketch pipeline​

Shor: amod15 + QPE (local)​

Modular-multiply gadgets for a∈{2,4,7,8,11,13,14}a\in\{2,4,7,8,11,13,14\} via SWAPs / XXs; phase-estimation register with controlled-U2jU^{2^j} and inverse QFT; classical Fraction.limit_denominator → period rr; gcd(a^{r/2}\pm1,\,15).

# shor15_sketch.py — structure
# while not done:
# a = random in 2..14; k = gcd(a, 15)
# if k != 1: factor k
# else: phase = qpe([amod15(a, 2**j) for j in range(t)])
# r = continued_fraction_denominator(phase)
# if r even: gcd(a**(r//2)±1, 15)

Never hard-code IBM API tokens in the repo.

Visualization note​

Book listings mix Aer, plot_histogram, and provider package names that change. Keep the circuit math; update imports from current Qiskit / IBM docs. This course grades stdout locks, not PNG histograms.

Takeaway​

SketchSection
Dipole HH + eig12.3
QFT₄ / QFTₙ12.8
Oracle + diffuser + IBM12.9
amod15 + QPE12.10
Quiz

QC sketches

1 / 3

Grover’s π√·/4 should use…

Pick an answer to see if you are right.

Next: 12.11b QC sketch labs.