Measurement and reset
Part G, lesson G6. You need lessons G1 to G5.
A measurement is not a unitary: it reads a qubit and leaves it in the state it read. A reset is not one either: it throws a qubit's state away and starts it at |0⟩. Neither has a matrix, so lessons G2 to G5 left them out. The semantic check, check_cert, compares them anyway, by turning each into a unitary on one more qubit. This lesson does that exactly, shows why moving a measurement past a gate on its qubit changes the experiment, and finds the one kind of move that no unitary can see.
*Lessons G1 to G4 are restated, unseen, above this line: the numbers, matrices, kron (qubit 0 the more significant bit), H, CX, PX, entry, UpToPhase and lesson G4's not_phase_of_zero.*
1. A measurement is a CNOT onto a fresh qubit
Measure qubit q in the Z basis and keep the outcome for the end. The same experiment: take a fresh qubit a in |0⟩, apply a CNOT from q onto a, and measure a at the end. If q holds α|0⟩ + β|1⟩, the CNOT makes α|00⟩ + β|11⟩: a reads 1 with probability |β|², as q would have, and afterwards q is in the state a read. check_cert says it in one line (_defer): "a Z measurement IS that CNOT, read out at the end".
A two-qubit matrix's first column is what it does to |00⟩: qubit 0 the data, qubit 1 the fresh one. The probability that qubit 1 reads 1 is the sum of |amplitude|² over the basis states 01 and 11, entries 1 and 3.
Exercise 1 ★ measure_probability open
h q[0]; measure q[0];: the Hadamard, then the measurement deferred onto qubit 1. Prove the state is (|00⟩ + |11⟩)/√2 and the fresh qubit reads 1 with probability one half, exactly.
2. A reset is a SWAP with a fresh qubit
A reset "hands the old state to a qubit nobody reads and takes a fresh |0⟩" (_defer again): a SWAP with a fresh qubit. Afterwards the data qubit is |0⟩, whatever it held.
Exercise 2 ★ reset_is_swap open
x q[0]; reset q[0];: prove that from |00⟩ the deferred form ends in |01⟩: the data qubit back at 0, the 1 it held now in the fresh qubit nobody reads.
3. The plan: which readouts get a fresh qubit
readout_plan in check_cert pairs the circuit's measurements and resets with the programme's, qubit by qubit. First the kinds must agree: the k-th readout of a qubit is a measurement on both sides, or a reset on both. Then "the j-th measurement of qubit q is a CNOT onto a fresh qubit of its own, the j-th reset a SWAP with one, on BOTH sides, so the two extended unitaries agree exactly when the readouts happen at equivalent points." One case is left out: "a measurement that is the last thing to happen to its qubit on both sides", since "deferring it is exact on each side alone".
Here is the plan for one qubit. A circuit or programme is a list of events; readoutsOf q lists q's readouts with whether each is the last thing done to q; deferredOf q numbers the ones that get a fresh qubit.
The Bell circuit ends measure q -> c;, one measurement per qubit, and the programme ends with one measure instruction on both ions. The Compilation page's verdict for the Bell pair reports "0 measurement(s)/reset(s) compared in place, final measurements at the end"; the reset example's verdict reports 1.
Exercise 3 ★ bell_nothing_deferred open
Prove the Bell certificate defers nothing, on either qubit.
4. A measurement moved before a gate
Now a wrong programme. The circuit is h q[0]; measure q[0];. The programme measures q0 first and applies the Hadamard after. Each side measures qubit 0 once, so the kinds agree; but on the programme's side something happens to qubit 0 after the measurement, so it is not last on both sides, and the plan gives it a fresh qubit.
Exercise 4 ★ moved_is_deferred open
Prove it.
With the measurement deferred onto qubit 1, the circuit is H then the CNOT, and the programme is the CNOT then H. They are different experiments: measured after the Hadamard, the qubit reads 1 half the time; measured before it, on |0⟩, never.
Exercise 5 ★ moved_changes_probability open
Prove both probabilities, exactly.
Exercise 6 ★★ moved_refused open
Prove the two extended unitaries are not equal up to any phase, which is how check_cert refuses the programme. Use not_phase_of_zero at an entry where the programme's matrix is 0 and the circuit's is not: from |00⟩, the circuit reaches |11⟩ and the programme does not.
5. A move no unitary can see
readout_plan adds: "Two readouts that commute with what lies between them stay equal (a Z measurement before or after a CNOT it controls), as they should." Take cx q[0],q[1]; measure q[0]; and a programme that measures q0 before the CNOT. The plan defers the measurement onto a fresh qubit 2, and the two sides become CX(0→1) then CX(0→2), against CX(0→2) then CX(0→1): two CNOTs with the same control.
Those commute, and the proved checker has the proof. The module QCCDC.Cert.Commute writes cx c t as the permutation it is on basis states, cxAct c t s (s gives each qubit its bit), and proves cxAct_comm_same_control. A permutation matrix is determined by its permutation, so equal permutations are equal unitaries: the semantic check cannot tell these two programmes apart. Lesson F6 used the same theorem on two CNOTs of a circuit; here the second CNOT is a measurement, deferred.
Exercise 7 ★★ deferred_reorder_invisible open
Prove the two orders act the same on every basis state, by applying that theorem. Its three hypotheses are facts about the numbers 0, 1 and 2.
6. The strict order
So the programme with the early measurement passes the semantic check, correctly as far as unitaries go. It is refused anyway, by a different check, check_program_order in check_cert: "STRICT: two ops on one qubit must run in their source order even when they commute. The order of a qubit's gates is the circuit author's to choose". Its docstring records why: "A compiler that batched a check's CNOTs by rotation offset passed every one of them with 726 adjacent pairs out of order on a BB144 round."
The proved checker of Part F has the same rule for gates, RespectsOrder. A measurement needs no witness there (Covered exempts measurements, resets and barriers), so a measurement's place in its qubit's order is held by check_program_order alone, and that is computed, not proved.
The Compilation page's reset example is the extreme case. The circuit is reset q[0];; the programme is one reset instruction; the certificate has the one op and no witness at all. Here it is as the proved checker reads it (reset.qcheck.json). The checker accepts it, and has nothing to say about where, or whether, the reset happened: what holds the programme to it is computed, the reset instruction's place by check_program_order and its kind by the readout plan ("1 measurement(s)/reset(s) compared in place", says its verdict on the Compilation page).
resetCert; it appears when Lean has checked the lesson.Here is the order rule, for programmes where each op is one instruction: on every qubit, the programme performs the ops that touch it in the circuit's order.
Exercise 8 ★ order_refuses open
Prove the strict order refuses the programme that measures first, and accepts the faithful one.
The rule is per qubit. Two ops on different qubits may run in either order, or at once: "pulses on OTHER qubits may overlap, which is parallelism, not reordering." That is a theorem about every programme.
Exercise 9 ★★★ disjoint_swap open
Prove that exchanging two adjacent ops that do not both touch qubit q leaves q's sequence unchanged, wherever they stand in the programme. Induction on the ops before them; at the pair itself, four cases on whether each touches q (by_cases), and simp_all with the definitions.
Exercise 10 ★★ parallel_ok open
Apply it: an op on qubit 0 and an op on qubit 1, in either order, give every qubit the same sequence.
7. What this lesson proved
Deferred, a measurement is a CNOT onto a fresh qubit and a reset a SWAP with one, so readouts take part in the exact comparison like gates; moving a measurement before a gate on its qubit is a different experiment, and refused. Moving it past a CNOT it controls is the same experiment as far as any unitary can tell, and is refused by the strict order alone. Lesson G7 puts G1 to G6 together on the Bell certificate.