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, orShor.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 on . Build the matrix, eig, then optionally rotate 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 , H on qubit 0, SWAP. Export the unitary with Operator and compare to the analytic 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
-qubit QFT
Loop: for : Hadamard on , then controlled phases onto earlier qubits; optional bit-reversal SWAPs. Diff against the library QFT(n) gate — max abs entry error should be for small .
# 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 : on bits that should be , multi-controlled with sandwich on the target (phase kick), undo s.
Diffuser: , , multi-controlled (via –MCX–), undo and .
Driver: , then rounds of oracle() + diffuser (use , not , 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 via SWAPs / s; phase-estimation register with controlled- and inverse QFT; classical Fraction.limit_denominator → period ; 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
| Sketch | Section |
|---|---|
Dipole + eig | 12.3 |
| QFT₄ / QFTₙ | 12.8 |
| Oracle + diffuser + IBM | 12.9 |
amod15 + QPE | 12.10 |
QC sketches
1 / 3Grover’s π√·/4 should use…
Pick an answer to see if you are right.
Next: 12.11b QC sketch labs.