39 """The layers of one check round as lists of (check row, data column); checks against h and qubit conflicts."""
40 layers, seen = [], np.zeros_like(h)
41 for layer
in sched[key][
"layers"]:
42 pairs = [(int(i), int(j))
for mv
in layer[
"moves"]
for i, j
in mv[
"cz_gates"]]
43 checks = [i
for i, _
in pairs]
44 datas = [j
for _, j
in pairs]
45 assert len(set(checks)) == len(checks)
and len(set(datas)) == len(datas), f
"qubit used twice in a layer"
47 assert h[i, j] == 1, f
"{key}: pair ({i}, {j}) is not in the check matrix"
48 assert seen[i, j] == 0, f
"{key}: pair ({i}, {j}) listed twice"
51 assert (seen == h).all(), f
"{key}: the layers do not cover the check matrix"
55def build(m, sched, basis, rounds):
56 """The noiseless memory circuit: the first cycle, then the other rounds - 1 cycles in a REPEAT block."""
57 hx, hz = m[
"Hx"], m[
"Hz"]
58 n, mx, mz = hx.shape[1], hx.shape[0], hz.shape[0]
61 xa = [n + i
for i
in range(mx)]
62 za = [n + mx + i
for i
in range(mz)]
71 c.append(
"CX", [q
for i, j
in pairs
for q
in (xa[i], j)])
76 c.append(
"CX", [q
for i, j
in pairs
for q
in (j, za[i])])
79 for typ, num, off
in ((
"X", mx, -mt), (
"Z", mz, -mz)):
80 if first
and typ != basis:
83 targets = [stim.target_rec(off + i)] + ([]
if first
else [stim.target_rec(off + i - mt)])
84 c.append(
"DETECTOR", targets, [i, 0, 0
if typ ==
"X" else 1])
85 c.append(
"SHIFT_COORDS", [], [0, 1])
90 c.append(
"RX" if basis ==
"X" else "R", range(n))
93 c.append(stim.CircuitRepeatBlock(rounds - 1, cycle(
False)))
94 c.append(
"MX" if basis ==
"X" else "M", range(n))
95 h, lo = (hx, m[
"Lx"])
if basis ==
"X" else (hz, m[
"Lz"])
96 last = -n - mt
if basis ==
"X" else -n - mz
97 for i
in range(h.shape[0]):
98 targets = [stim.target_rec(-n + j)
for j
in np.nonzero(h[i])[0]] + [stim.target_rec(last + i)]
99 c.append(
"DETECTOR", targets, [i, 0, 0
if basis ==
"X" else 1])
100 for k
in range(lo.shape[0]):
101 c.append(
"OBSERVABLE_INCLUDE", [stim.target_rec(-n + j)
for j
in np.nonzero(lo[k])[0]], k)
106 ap = argparse.ArgumentParser(description=__doc__.split(
"\n")[0])
107 ap.add_argument(
"code_dir")
108 ap.add_argument(
"out_dir")
109 ap.add_argument(
"--rounds", type=int, default=0)
110 ap.add_argument(
"--p", type=float, default=0.001)
111 ap.add_argument(
"--basis", default=
"XZ")
112 ap.add_argument(
"--check-rounds", type=int, default=2, help=
"rounds of the noisy test circuit for the DEM check")
114 m, sched =
load(a.code_dir)
115 nkd = [int(x)
for x
in re.findall(
r"\d+", os.path.basename(os.path.normpath(a.code_dir)))]
116 fam = os.path.basename(os.path.dirname(os.path.normpath(a.code_dir)))
117 prefix = {
"mitten":
"mitten",
"structured_mitten":
"smitten"}.get(fam, fam)
118 rounds = a.rounds
or nkd[2]
119 os.makedirs(a.out_dir, exist_ok=
True)
120 for basis
in a.basis:
123 dem = test.detector_error_model(decompose_errors=
False)
125 name = f
"yarn_{prefix}_{nkd[0]}_{nkd[1]}_{nkd[2]}_hookfree_r{rounds}_{basis}.stim"
126 header = (f
"# [[{nkd[0]},{nkd[1]},{nkd[2]}]] {fam} code of a7b/yarn (arXiv:2607.28795), group "
127 f
"{sched['code'].get('group')}; {basis}-basis memory, {rounds} rounds, published hook-free SE "
128 f
"schedule (X-check round, then Z-check round, 12 CX layers each); dist_m4ri.add_noise(p={a.p})\n")
129 with open(os.path.join(a.out_dir, name),
"w")
as f:
130 f.write(header + str(c) +
"\n")
131 print(json.dumps({
"file": name,
"qubits": c.num_qubits,
"detectors": c.num_detectors,
132 "observables": c.num_observables, f
"dem_errors_r{a.check_rounds}": dem.num_errors}),