Conventions: outcome , ; qubits numbered from 1 in the math and from 0 in Stim.
Problem 1
Problem 6.1 (Two measurements on a Bell pair).
Start from . Measure with outcome , then with outcome .
Classify each measurement and give the stabilizer generators after each step.
What is the joint distribution of ? What is the final state?
Contrast with measuring and then . Why are the outcomes correlated there but not here?
Solution
commutes with and anticommutes with : random. Using (the only anticommuting generator), replace it by :
Next, anticommutes with (one clash) and commutes with : random again. Replace by :
Each outcome is a fair coin, and the second is random no matter what the first was, so is uniform on : the two outcomes are independent. The final state is the product state , where , in the basis, and is the computational basis state.
After , the surviving generator commutes with and makes an element of , so is deterministic and equal to . After , the generator that would have linked to the first outcome, , has been spent on the measurement, and the surviving anticommutes with . Correlation between two measurements exists exactly when the product of the two measured Paulis is (up to sign) in the stabilizer: but .
Problem 2
Problem 6.2 (The GHZ state).
Let .
For each of , , , , , , say whether the outcome is deterministic (and give it) or random.
Measure with outcome . Give the new generators and describe the remaining two-qubit state.
Measure instead, with outcome . Give the new generators and the state.
Solution
Use the symplectic product to test commutation, then Gaussian elimination for the sign.
Pauli
anticommutes with
case
outcome
none
none
none
random
fair coin
,
random
fair coin
(three clashes)
random
fair coin
For : . For : . For , its binary vector would need for the part, but then and clash on three qubits, an odd number, so they anticommute.
anticommutes only with (the generator has nothing on qubit 1). Replace by :
using . Qubit 1 is in , and qubits 2, 3 are in the Bell state with stabilizer . Measuring one GHZ qubit in the basis leaves the other two entangled.
anticommutes only with . Replace it by :
so the state is . Measuring one GHZ qubit in the basis destroys all entanglement, in contrast to part 2.
Problem 3
Problem 6.3 (Fair coins and a valid update).
Let be a stabilizer state with generators , and let anticommute with () and commute with .
Show .
Show that is a set of pairwise commuting, independent Paulis that stabilize the post-measurement state.
A student instead uses . Show by an example that this list need not be a stabilizer group.
Solution
, so it suffices to show . Since and ,
Stabilize. commutes with every Pauli that commutes with . Each () commutes with (two sign flips) and stabilizes , so . The same holds for . And by definition of the projector.
Commute. The products and the are elements of the old abelian group, so they commute with each other; each of them commutes with as just shown.
Independent. In binary, the old rows are independent, and replacing by () is an invertible row operation, so are independent. Now replace by . If were a combination of the other rows, it would commute with (they all do), contradicting . So the new rows are independent.
Take the GHZ state and , which anticommutes with and . Dropping but keeping gives , and anticommutes with . A list with anticommuting elements cannot stabilize any nonzero state. The correct update replaces by the product , which commutes with (no clashes: they overlap only on qubit 3, where both are ).
Problem 4
Problem 6.4 (An Aaronson-Gottesman trace).
After , , on , the Aaronson-Gottesman tableau is
Check two of the defining relations of a destabilizer tableau.
Measure and suppose the random outcome is . Carry out the three steps of the random case and write the new tableau (ignore destabilizer signs).
Now measure . Show that it is deterministic and compute the outcome with the scratch row.
Solution
vs : one clash, anticommute. vs and : only ‘s, commute. vs : one clash on qubit 2, anticommute; vs and : commute. The destabilizers are pairwise commuting since they act on different qubits.
Column 2 ( bits): has , so this is the random case with . Other rows with : only .
Step 1: (up to sign).
Step 2: .
Step 3: .
New tableau:
(Check: commutes with and (two clashes) and anticommutes with (one clash), as required.)
Column 1: the stabilizers , , all have , so is deterministic. The destabilizers with are and , so the scratch row is . The outcome is , i.e. , as it must be: measuring on GHZ leaves . Similarly is deterministic: the destabilizers with are , , , and gives .
Problem 5
Problem 6.5 (Measuring the Steane code).
The Steane code has -checks , , and the same -checks (Module 4). Classify a measurement of each operator on an arbitrary code state: , , , , . For the logical ones, explain how the outcome distribution depends on the encoded state, and what the code becomes afterwards.
Solution
operator
commutes with all checks?
in ?
case
yes (overlaps 4 with each -check)
no
logical
no: anticommutes with
—
random
yes
yes:
deterministic
yes (overlaps 2, 2, 0 with the -checks)
no
logical (weight-3 )
yes (overlaps 4 with every check)
no
logical
For a logical measurement on , the outcomes are with probability and with : they depend on the encoded data, unlike the fair coins of the random case. Afterwards the stabilizer is , a stabilizer state with ( or ). is a representative of , i.e. a logical up to sign; measuring it likewise collapses the logical qubit into a eigenstate. The deterministic case is a syndrome-type measurement and reveals nothing about the logical state.
Problem 6
Problem 6.6 (Coding: an Aaronson-Gottesman simulator checked against Stim).
Extend your tableau class from Homework 5 to the -row Aaronson-Gottesman layout (destabilizers, stabilizers, scratch) and implement measure(a) returning (m, was_random). Test it against stim.TableauSimulator on 200 random 5-qubit circuits of 40 operations (H, S, CNOT, and Z measurements): use peek_z(a) (which returns , , or for random) to check that your simulator agrees on random versus deterministic and on deterministic values, and postselect_z(a, desired_value=...) to force Stim to follow your random outcomes.
Solution
import numpy as npimport stimclass AG: def __init__(self, n, seed=0): self.n, self.rng = n, np.random.default_rng(seed) self.x = np.zeros((2 * n + 1, n), np.uint8) self.z = np.zeros((2 * n + 1, n), np.uint8) self.r = np.zeros(2 * n + 1, np.uint8) for i in range(n): self.x[i, i] = 1 # destabilizer X_i self.z[n + i, i] = 1 # stabilizer Z_i def h(self, a): self.r ^= self.x[:, a] & self.z[:, a] self.x[:, a], self.z[:, a] = self.z[:, a].copy(), self.x[:, a].copy() def s(self, a): self.r ^= self.x[:, a] & self.z[:, a] self.z[:, a] ^= self.x[:, a] def cnot(self, c, t): self.r ^= self.x[:, c] & self.z[:, t] & (self.x[:, t] ^ self.z[:, c] ^ 1) self.x[:, t] ^= self.x[:, c] self.z[:, c] ^= self.z[:, t] @staticmethod def g(x1, z1, x2, z2): x1, z1, x2, z2 = int(x1), int(z1), int(x2), int(z2) if x1 == z1 == 0: return 0 if x1 == z1 == 1: return z2 - x2 if x1 == 1: return z2 * (2 * x2 - 1) return x2 * (1 - 2 * z2) def rowsum(self, h, i): # row h <- row i * row h e = 2 * int(self.r[h]) + 2 * int(self.r[i]) e += sum(self.g(self.x[i, j], self.z[i, j], self.x[h, j], self.z[h, j]) for j in range(self.n)) self.r[h] = 1 if e % 4 == 2 else 0 # destabilizer signs may be junk; unused self.x[h] ^= self.x[i] self.z[h] ^= self.z[i] def measure(self, a): n = self.n p = next((q for q in range(n, 2 * n) if self.x[q, a]), None) if p is not None: # random for i in range(2 * n): if i != p and self.x[i, a]: self.rowsum(i, p) self.x[p - n], self.z[p - n], self.r[p - n] = self.x[p], self.z[p], self.r[p] self.x[p] = 0; self.z[p] = 0; self.z[p, a] = 1 self.r[p] = self.rng.integers(2) return int(self.r[p]), True sc = 2 * n # deterministic self.x[sc] = 0; self.z[sc] = 0; self.r[sc] = 0 for i in range(n): if self.x[i, a]: self.rowsum(sc, i + n) return int(self.r[sc]), Falserng = np.random.default_rng(5)count = 0for trial in range(200): n = 5 sim, ts = AG(n, seed=trial), stim.TableauSimulator() for _ in range(40): k = rng.integers(4) if k == 0: a = int(rng.integers(n)); sim.h(a); ts.h(a) elif k == 1: a = int(rng.integers(n)); sim.s(a); ts.s(a) elif k == 2: a, b = map(int, rng.choice(n, 2, replace=False)); sim.cnot(a, b); ts.cnot(a, b) else: a = int(rng.integers(n)) peek = ts.peek_z(a) m, was_random = sim.measure(a) assert was_random == (peek == 0) if not was_random: assert (peek == -1) == (m == 1) ts.postselect_z(a, desired_value=bool(m)) count += 1print(count, "measurements agree with stim")
Two details matter. First, the self.x[p - n], ... = self.x[p], ... line copies row into row before row is overwritten (numpy assignment of a row from another row copies the data). Second, the destabilizer rows can pick up a factor during rowsum (destabilizer anticommutes with ), so their sign bits are meaningless; Aaronson and Gottesman note this and never read them. With the random seeds above, all measurements across the 200 circuits agree with Stim.
Problem 7
Problem 6.7 (Coding: detectors in a two-round repetition code).
Build a Stim circuit for the 3-qubit bit-flip code with data qubits 0, 1, 2 and ancillas 3, 4 that measure and (Stim numbering). Do two rounds of syndrome extraction (CNOT data-to-ancilla, then MR on the ancillas), then measure all data qubits. Define six detectors: the two round-1 syndromes; the two round-2 syndromes each XORed with round 1; and the two final data parities each XORed with the round-2 syndrome. Mark OBSERVABLE_INCLUDE on the final measurement of qubit 2. Insert X_ERROR(1) on one data qubit just before round 2, and tabulate which detectors fire for errors on qubits 0, 1, 2 and for no error.
Solution
import stimdef rep_circ(err_qubit=None): c = stim.Circuit() c.append("R", range(5)) for rnd in range(2): if rnd == 1 and err_qubit is not None: c.append("X_ERROR", [err_qubit], 1.0) c.append("CNOT", [0, 3, 1, 3, 1, 4, 2, 4]) c.append("MR", [3, 4]) c.append("M", [0, 1, 2]) rec = stim.target_rec c.append("DETECTOR", [rec(-7)]) # round-1 Z0Z1 c.append("DETECTOR", [rec(-6)]) # round-1 Z1Z2 c.append("DETECTOR", [rec(-5), rec(-7)]) # round 2 vs round 1 c.append("DETECTOR", [rec(-4), rec(-6)]) c.append("DETECTOR", [rec(-3), rec(-2), rec(-5)]) # data vs round 2 c.append("DETECTOR", [rec(-2), rec(-1), rec(-4)]) c.append("OBSERVABLE_INCLUDE", [rec(-1)], 0) return cfor e in [None, 0, 1, 2]: d, o = rep_circ(e).compile_detector_sampler().sample(shots=1, separate_observables=True) print(e, d.astype(int)[0], o.astype(int)[0])
The measurement record is (round-1 ancillas, round-2 ancillas, data 0, 1, 2), so rec[-7] is the first round-1 ancilla and rec[-1] is data qubit 2. Output:
error
detectors
observable
none
0 0 0 0 0 0
0
on qubit 0
0 0 1 0 0 0
0
on qubit 1
0 0 1 1 0 0
0
on qubit 2
0 0 0 1 0 0
1
The round-1 detectors never fire because the error happens after round 1. An error between the rounds lights up the change detectors for the checks it anticommutes with: one for an end qubit, both for the middle qubit. The final-data detectors stay quiet because the final data measurement agrees with round 2. The error on qubit 2 flips the observable, which a decoder would have to undo using . This “fire on change” pattern, and the space-time graph it produces, is the starting point of Module 9.