def build():
"""Build the circuit and assert its pins. Returns (builder, Toffoli
count); the occupancy/liveness profilers consume its wire_log.
"""
# ---- registers, held scratch, allocators --------------------------------
b = Builder()
Start 0 Toffolis · peak 863 qubits
with b.phase("init"): # profile-stage blocks (metadata; emit nothing)
syn = list(declare_registers(b, [(f"s{r:02d}", B) for r in range(N)]).values())
assert all(list(syn[r]) == list(range(B * r, B * r + B)) for r in range(N)), (
"syn ids not contiguous"
) # qubits Br..Br+B-1
ANC = []
def An(n):
qs = b.qubits(n)
ANC.extend(qs)
return qs
Dw = An(B) # fused-Q working discrepancy register
dW = An(DWB) # two's-complement d counter
OMW = [syn[i] for i in range(W)] # omega lives IN PLACE in syn[0..W-1]
# Ownership wire-sets (claim/release bookkeeping; emits no op). Every
# long-lived wire joins exactly one named set with its full id list, so
# the anonymous pool live - sum(owned) is empty from init to cleanup;
# tools/occupancy_profile.py validates the discipline fail-closed.
b.claim("syn", [w for reg in syn for w in reg])
b.claim("dw", Dw)
b.claim("dcount", dW)
# Transient scratch round-trips through the allocator (A.open /
# A.close). Only state that persists between windows (discrepancies,
# stack, controls, counters, the Em pivots) stays held; everything that
# returns to |0> mid-circuit is freed there, so peak_qubits is the
# measured max co-live width and the liveness profile shows the true
# windows. Journal replays (the BM reverse, the Psi route) run
# closures over hard wire ids, so A logs the window ids a journal
# touches and re-materializes exactly that set before its replay
# (A.replay; reclaim fails closed when any id is not free).
A = Alloc(b)
sA = An(1)[0]
b.claim("pivA", [sA])
sB = An(1)[0]
b.claim("pivB", [sB])
sT = An(1)[0]
b.claim("fanT", [sT])
E = Em(b, sA, sB, sT, A)
bm = BM(E, A, syn, Dw, dW)
SIG = bm.SIG
# Width ledger: N * B syn + (B + DWB + 3) held (Dw, dW, pivots) + the
# lazily-materialized wires ((2W+3) B bank + STK_CAP tagged stack slots)
# + the late BM/reverse rounds' window tiling. The peak assert below
# enforces the total.
# ---- 1. zlin: free Z part sum_i Tr(u_i S_i) -----------------------------
Linear phase 0 Toffolis · peak 863 qubits
with b.phase("zlin"):
for i in range(N):
for bb in bitlist(ZMASKI[i], B):
b.z(syn[i][bb])
# ---- 2. bm: BM forward (q-form conditional-swap, single-chain m) --------
Berlekamp–Massey rounds 141,071 Toffolis · peak 1,813 qubits
# ---- 3. monicize: SIG[i] <- inv(sigma_0)*SIG[i] for i=1..W --------------
Normalize 1,583 Toffolis · peak 1,793 qubits
mon_stage(E, SIG, "monicize", range(1, W + 1), True)
# ---- 4. omega70 = sigma_m*S mod z^N, IN PLACE in syn[0..N-1] ------------
# sigma_m is monic: the triangles' diagonals span sigma_1..sigma_{W-1},
# the off-diagonal tail block's sigma_1..sigma_W (omega_rts).
# Rows W..N-1 (the TAIL) end at 0 on every correct decode and host the
# Bezout pair (stage 5).
MACRT = omega_rts(SIG, syn)
Evaluator 15,246 Toffolis · peak 1,758 qubits
omega_pass(E, MACRT, "omega", range(len(MACRT)))
# ---- 5. euclid: the half-degree Bezout pass (Psi route) -----------------
# sigma_m = E(z)^2 + z O(z)^2 with E_i = sqrt(sigma_2i), O_i =
# sqrt(sigma_2i+1): SIG[1..35] are square-rooted IN PLACE (a free linear
# map) and the Euclid bank EB is E = [ONE, SIG2, .., SIG34], O = [SIG1, ..,
# SIG35] (ONE is a fresh word holding 1 = E_0). m = floor((35 - L)/2) =
# (dW + 2) >> 2 (dW = the BM counter, +2 in place), and the barrel pad
# shifts both halves up by m words so deg E' = 17 on every correct decode.
# JSET journals the setup (ONE, the in-place square roots, dW += 2) so
# stage 7 undoes it with one reverse.
# PSI_IDS logs every scratch id the Psi journals touch; stages R and 7
# hold exactly that set around their replays.
Half-degree Euclid 44,470 Toffolis · peak 1,837 qubits
with b.phase("euclid/setup"):
ONE = A.open("one", B)
EPS = A.open("zprec", 6) # eps + 5-bit idx
PRE = A.open("pre", 1)[0]
PSI_IDS = set()
JSET, JPAD, JPRE, JF, JSEED = (Journal(), Journal(), Journal(), Journal(), Journal())
with E.using(J=JSET), A.logging(PSI_IDS):
E.x(ONE[0])
for i in range(1, W + 1):
JSET.step(
lambda r=list(SIG[i]): linear_in_place(b, r, SQRT_ROWS),
lambda r=list(SIG[i]): linear_in_place(b, r, SQUARE_ROWS),
)
# dW += 2: a constant increment on bits 1..7 (ladder carries, MBU-erased)
with opened(E, "inc", DWB - 3) as tr:
E.inc1(dW[1:DWB], tr)
EW = [ONE] + [list(SIG[2 * i]) for i in range(1, NH)]
OWD = [list(SIG[2 * i + 1]) for i in range(NH)]
MBITS = dW[2:7] # m = (dW + 2) >> 2
with b.phase("euclid/pad"), E.using(J=JPAD):
pad_barrel(E, MBITS, EW, OWD)
with b.phase("euclid/pre"), E.using(J=JPRE), A.logging(PSI_IDS):
psi_pre(E, EW, OWD, PRE)
with b.phase("euclid/F"), E.using(J=JF), A.logging(PSI_IDS):
F_FIN, _G_FIN, RECS = psi_forward(E, EW, OWD, EPS[0], EPS[1:6])
# ---- seed + R: the Bezout pair (a, b) grows in the TAIL + one EXT word --
# a[(m+1) % 18] <- inv(h) (h = the final F pivot), then R = the journal
# reverse of F interleaved with the cofactor recurrence: on exit EB holds
# (E', O') again and (a, b) = (A, B) with A E' + B O' = z^m, deg <= 17.
with b.phase("euclid/seed"):
TAILW = [list(syn[i]) for i in range(W, N)]
AW = TAILW[:NH]
with E.using(J=JSEED), A.logging(PSI_IDS):
psi_seed(E, F_FIN[NH - 1], MBITS, AW)
with b.phase("euclid/R"):
with A.replay(PSI_IDS, "psipool"):
EXT = A.open("ext", B) # opened under the replay hold: never a PSI_IDS id
BWD = TAILW[NH:] + [EXT]
JR, AFIN, BFIN = build_R(E, JF, RECS, AW, BWD, PRE, PSI_WIN, (EW, OWD))
JR.forward()
A.close("zprec", EPS)
# ---- 6. sweep: root flag + rank-12 Psi pairing over the M points --------
Position scan 92,552 Toffolis · peak 1,806 qubits
with b.phase("sweep"):
# ---- 7. euclid_inv: R^-1, unseed, F^-1, un-pre, un-pad, un-setup --------
# The zprec and psipool holds span R..setup, so they open and close by
# hand (A.replay's two halves) in the first and last phase.
PSI_POOL = sorted(PSI_IDS)
Euclid reversed 44,470 Toffolis · peak 1,837 qubits
with b.phase("euclid_inv/R"):
A.reopen("zprec", EPS)
A.reopen("psipool", PSI_POOL)
JR.reverse()
A.close("ext", EXT)
with b.phase("euclid_inv/seed"):
JSEED.reverse()
with b.phase("euclid_inv/F"):
JF.reverse()
with b.phase("euclid_inv/pre"):
JPRE.reverse()
with b.phase("euclid_inv/pad"):
JPAD.reverse()
with b.phase("euclid_inv/setup"):
JSET.reverse()
A.close("psipool", PSI_POOL)
A.close("pre", [PRE])
A.close("zprec", EPS)
A.close("one", ONE)
# ---- 8. omega_restore: exact reversed block list ------------------------
# each block's targets are disjoint from its operands, so re-emitting the
# same term list un-XORs it; the reversed block order restores S exactly.
Evaluator reversed 15,246 Toffolis · peak 1,758 qubits
omega_pass(E, MACRT, "omega_restore", reversed(range(len(MACRT))))
# ---- 9. unmonicize: SIG[i] <- sigma_0*SIG[i] for i=W..1 -----------------
Normalize reversed 1,583 Toffolis · peak 1,793 qubits
mon_stage(E, SIG, "unmonicize", range(W, 0, -1), False)
# ---- 10. reverse: the journaled BM reverse pass -------------------------
Rounds reversed 86,176 Toffolis · peak 1,813 qubits
# ---- 11. cleanup ---------------------------------------------------------
# Release-before-free through the only teardown path: every workspace set
# returns its wires (full id lists, reverse claim order) ahead of the
# frees; the N * B syndrome wires stay owned to the end as the declared
# registers.
Cleanup 0 Toffolis · peak 863 qubits
with b.phase("cleanup"):
b.release("fanT", [sT])
b.release("pivB", [sB])
b.release("pivA", [sA])
b.release("dcount", dW)
b.release("dw", Dw)
b.recycle_all(reversed(ANC))
# =========================================================================
# Metrics gate, pre-emission: the pins under the measured window
# schedule and STK_CAP = 1.
# =========================================================================
TOF = assert_metrics(b, __file__)
return b, TOF
def round_head(self, r, bb_l):
"""Open round r (inside its bm/rNN phase block): journal mark, the
round's cascade stack slots, and the fused-Q bank rotation (a
relabel). Returns the rotated bank.
"""
self.A.round = r
self.A.log = self.pools.setdefault(r, set())
self.JROUND.append(self.J.mark())
# Lazy-materialization invariant: a register touched mid-round may
# only take free-list ids that strictly earlier rounds' windows freed
# (those return before the descending replay reaches them). At the
# bank/ctl touch sites this holds because the round's own freed ids
# are always reconsumed by the windows still open there; the cascade
# runs after every window closed, so its stack depths materialize at
# the round head instead. reclaim fails closed on any breach.
for s in self.STK[: CAPS[r]]:
s.mat()
return [bb_l[W + 1]] + bb_l[: W + 1]
⋯ 1 line
def discrepancy(self, r):
"""Dw <- sum_{i <= IMAXV[r]} sigma_i S_{r-i} (the truncated window)."""
rtD = []
for i in range(IMAXV[r] + 1):
bil_terms(rtD, KT, spec_plain(self.SIG[i]), spec_plain(self.syn[r - i]), self.Dw)
self.E.mac(rtD)
⋯ 2 lines
def quotient(self, ctl, bb_l):
"""Yield the round quotient m without a stored accepted discrepancy.
Invariant (README): sigma_0 * DEL == 1 on every
correct-path round, DEL the accepted discrepancy of the q-form
recursion, so the recursion's m = D'/DEL' is
ctl=0: m = Delta * sigma_0 = p,
ctl=1: m = DEL_old / Delta = 1/(Delta * sigma_0) = inv(p).
Exit erases m (a function of p, inv(p), ctl), the chain and inv(p),
then p. Dw = Delta on every branch there; p = Delta * sigma_0_old,
and sigma_0_old now sits in bb_l[0] (ctl=1) or SIG[0] (ctl=0, where
Delta != 0 forces bb_l[0] == 0); on ctl=1 the update left SIG[0] =
1/Delta, so Delta*(bb_l[0] + SIG[0]) = p + ctl:
p == Dw * (bb_l[0] ^ SIG[0]) ^ ctl*[1] (one KT claim).
"""
E = self.E
with opened(E, "pq", B) as pv:
E.mac(prod_rt(pv, self.Dw, self.SIG[0]))
with opened(E, "dinv", B) as dv, chain_live(E, pv, dv), opened(E, "m", B) as mq:
# m = p ^ ctl*(p ^ inv(p)): B free copies + B CCX
rtM = []
for t_ in range(B):
rtM.append(("L", (pv[t_],), 0, (mq[t_],)))
rtM.append(norm_term((ctl,), 0, tuple(sorted((pv[t_], dv[t_]))), 0, (mq[t_],)))
E.mac(rtM)
yield mq
E.free(rtM)
rtPe = []
bil_terms(
rtPe,
KT,
spec_plain(self.Dw),
[frozenset((bb_l[0][j], self.SIG[0][j])) for j in range(B)],
pv,
)
rtPe.append(("L", (ctl,), 0, (pv[0],)))
E.free(rtPe)
⋯ 1 line
def bank_swaps(self, r, ctl, bb_l):
"""cswap(ctl) of the sigma and B banks over slots 0..KMSV[r]."""
for k2 in range(KMSV[r] + 1):
if r == 1 and k2 == 1:
# provable identity: both branches leave sigma_1 ==
# B_1 entering this swap (2-branch enumeration)
continue
for t_ in range(B):
self.E.cswap(ctl, self.SIG[k2][t_], bb_l[k2][t_])
⋯ 1 line
def update(self, r, k0, mq, bb_l):
"""sigma_k ^= m * B_k over slots k0..KMMV[r] (one KT12 each)."""
for k2 in range(k0, KMMV[r] + 1):
self.E.xor(prod_rt(self.SIG[k2], mq, bb_l[k2]))
⋯ 112 lines
def round(self, r, bb_l):
"""Emit a general round 0 < r < N-1."""
with self.b.phase(f"bm/r{r:02d}"):
bb_l = self.round_head(r, bb_l)
with opened(self.E, "ctl", 1) as (ctl,):
self.discrepancy(r)
# ctl is set iff Dw != 0 and d >= 0
flag_test(self.E, self.Dw, (self.dW[SGN],), ctl)
with self.quotient(ctl, bb_l) as mq:
self.bank_swaps(r, ctl, bb_l)
self.update(r, 0, mq, bb_l)
self.push(r)
self.erase_dw(ctl)
self.close_ctl(r, ctl)
if r < N - 2:
# +1 always -> d = ctl ? -d : d+1 (at r=N-2 the increment is
# dropped: the counter has no later consumer and the complement
# alone arms the erase anchor)
self.inc_d()
return bb_l
def sweep_plan(b, A, omw, ebw, abw):
"""Return the body as [(gate vector over t, emitter(s_))] in emission order,
s_ the body's wire handles; a None vector emits ungated (the tree
window open on allocator A). Mirrors the per-point form: som fan-ins, the root-test
ladder (pivot_pair builds, sorted operands), the rank-12 pairing
(fan_pair), the ladder erase (emit_free, reversed), the som unwind.
"""
m_om = rot_member(list(range(W))) & LIVE # som fan-ins
m_eb = rot_member(LOG_EB) & LIVE # root-test groups
m_v = rot_member(LOG_AB) & LIVE # pairing V groups
prow = np.zeros((B, M), dtype=np.int64) # r_a(lam) masks
for t in range(M):
if LIVE[t]:
prow[:, t] = pair_rows(LAMJ2[(M - t) % M])
single = np.stack([prow == (1 << c) for c in range(B)], axis=1) # [a, c, t]
built = ~single.any(axis=1) & LIVE # [a, t]
m_sa = np.stack([((prow >> c) & 1).astype(bool) & built for c in range(B)], axis=1) # [a, c, t]
# the per-point spelling the body mirrors: every root-test group is
# non-empty (both pivots build), every V group has >= 2 wires (it builds)
assert (m_eb.any(axis=(1, 2)) | ~LIVE).all(), "empty root-test group"
assert (m_v.sum(axis=(1, 2)) >= 2)[:, LIVE].all(), "degenerate V group"
eb_sorted = sorted(
(w, k, j) for k, word in enumerate(ebw) for j, w in enumerate(word)
) # terms sort operands
assert len({w for w, _, _ in eb_sorted}) == len(eb_sorted)
plan = []
def op(vec, fn):
plan.append((vec, fn))
def eb_build(g, s, inverse):
cands = [(w, m_eb[g, k, j]) for w, k, j in eb_sorted]
if inverse:
op(LIVE, lambda s_: b.x(s_[s]))
cands.reverse()
for w, v in cands:
op(v, lambda s_, w=w: b.cx(w, s_[s]))
if not inverse:
op(LIVE, lambda s_: b.x(s_[s]))
def ua(i, s_):
return s_["sA"] if i == 1 else s_["tr"][i - 2]
def rung(i, mid):
"""ladder rung i >= 1: wa = group 0 (i == 1, built in sA) or
tree[i-2] (direct), wb = group i (built in sB).
"""
if i == 1:
eb_build(0, "sA", False)
eb_build(i, "sB", False)
op(LIVE, mid)
eb_build(i, "sB", True)
if i == 1:
eb_build(0, "sA", True)
om = [(bb, k, j) for bb in range(B) for k in range(W) for j in range(B)]
for bb, k, j in om:
op(m_om[bb, k, j], lambda s_, bb=bb, k=k, j=j: b.cx(omw[k][j], s_["som"][bb]))
op(None, lambda s_: s_.__setitem__("tr", A.open("tree", B - 1)))
for i in range(1, B):
rung(i, lambda s_, i=i: b.ccx(ua(i, s_), s_["sB"], s_["tr"][i - 1]))
vw = [(k, j) for k in range(len(abw)) for j in range(B)]
for a_ in range(B):
cases = [(built[a_], lambda s_: s_["sA"])] + [
(single[a_, c] & LIVE, lambda s_, c=c: s_["som"][c]) for c in range(B)
]
for c in range(B):
op(m_sa[a_, c], lambda s_, c=c: b.cx(s_["som"][c], s_["sA"]))
for k, j in vw:
op(m_v[a_, k, j], lambda s_, k=k, j=j: b.cx(abw[k][j], s_["sB"]))
for v, u in cases:
op(v, lambda s_, u=u: b.ccx(u(s_), s_["sB"], s_["sT"]))
op(LIVE, lambda s_: b.cz(s_["tr"][B - 2], s_["sT"]))
op(LIVE, lambda s_: s_.__setitem__("m", b.hmr(s_["sT"])))
for v, u in cases:
op(v, lambda s_, u=u: b.cz(u(s_), s_["sB"], cond=s_["m"]))
for k, j in reversed(vw):
op(m_v[a_, k, j], lambda s_, k=k, j=j: b.cx(abw[k][j], s_["sB"]))
for c in reversed(range(B)):
op(m_sa[a_, c], lambda s_, c=c: b.cx(s_["som"][c], s_["sA"]))
for i in range(B - 1, 0, -1):
op(LIVE, lambda s_, i=i: s_.__setitem__("m", b.hmr(s_["tr"][i - 1])))
rung(i, lambda s_, i=i: b.cz(ua(i, s_), s_["sB"], cond=s_["m"]))
op(LIVE, lambda s_: A.close("tree", s_["tr"]))
for bb, k, j in reversed(om):
op(m_om[bb, k, j], lambda s_, bb=bb, k=k, j=j: b.cx(omw[k][j], s_["som"][bb]))
return plan
⋯ 2 lines
def emit_sweep(b, E, A, omw, ebw, abw):
"""Emit the sweep: one gate-table row of M bits per distinct gate
vector (the forward and unwind passes share rows; row r gates
iteration t on bit r * M + t), then the repeat(M) body over the held
pivots and the som / tree windows of allocator A.
"""
plan = sweep_plan(b, A, omw, ebw, abw)
rows = {}
for vec, _ in plan:
if vec is not None:
rows.setdefault(vec.tobytes(), vec)
rows.setdefault(LIVE.tobytes(), LIVE)
row_of = {key: r for r, key in enumerate(rows)}
table = b.declare_table(np.concatenate(list(rows.values())).astype(np.uint8).tolist())
with b.repeat(M) as it:
def gate(vec):
return b.gated(table, it + row_of[vec.tobytes()] * M)
s_ = {"sA": E.sA, "sB": E.sB, "sT": E.sT, "som": A.open("som", B)}
for vec, fn in plan:
if vec is None:
fn(s_)
else:
with gate(vec):
fn(s_)
with gate(LIVE):
A.close("som", s_["som"])
def push(self, r, ctl=True):
"""Emit the fused-Q push cascade on pqc = dW[SGN] (d<0 forces ctl=0);
ctl=False spells it with plain swaps (the unconditional r=N-1
push, 0 Toffoli, no tag). Extent CAPS[r] = min(DP extent,
STK_CAP): the CLAMPED Fredkin ladder still acts on pairwise-distinct
qubits; on a full stack it rotates STK[STK_CAP-1] into Dw (overflow
drop, the E2 class); on a not-full stack it is the identical
shift as the uncapped ladder. Inter-slot swaps carry all STKW
lanes; the Dw level is value-only.
"""
E, STK = self.E, self.STK
capr = CAPS[r]
if not capr:
return
pqc = self.dW[SGN]
def sw(x, y):
if ctl:
E.cswap(pqc, x, y)
else:
E.swap(x, y)
for i2 in range(capr - 2, -1, -1):
for t_ in range(STKW):
sw(STK[i2][t_], STK[i2 + 1][t_])
for t_ in range(B):
sw(self.Dw[t_], STK[0][t_])
if not ctl:
return
# tag the incoming entry with its round: the vacated slot's tag lanes
# are |0> below overflow, so free CX from the push flag writes r
# exactly (overflow shots are the E2 class)
for bp in bitlist(r, TAGB):
E.cx(pqc, STK[0][B + bp])
⋯ 1 line
def erase_dw(self, ctl):
"""Erase Dw. Post-cascade Dw == ctl * Delta on every branch (d<0:
pushed, Dw = 0; ctl=0, d>=0: Delta = 0), and ctl=1 left sigma_0 =
1/Delta: Dw == ctl * inv(sigma_0), one chain on SIG[0] and a B-term
claim (stack overflow is the E2 class).
"""
E = self.E
with opened(E, "wreg", B) as wr, chain_live(E, self.SIG[0], wr):
E.free([norm_term((ctl,), 0, (wr[t_],), 0, (self.Dw[t_],)) for t_ in range(B)])
⋯ 1 line
def close_ctl(self, r, ctl):
"""d complement if ctl, then erase ctl (the caller closes its
window). Post-complement dW[SGN] = [ctl OR pushed], and a push at
this round iff the top tag reads r,
so ctl == dW[SGN] AND NOT [top tag == r]. Rounds the DP proves
push-free (and r=0) anchor on dW[SGN] alone, for free. On a stack
overflow the rotated-out tag corrupts the compare -- overflow shots
are already the E2 class.
"""
E, dW = self.E, self.dW
for i in range(DWB):
E.cx(ctl, dW[i])
if CAPS_FULL[r] == 0:
E.free([norm_term((dW[SGN],), 0, (), 1, (ctl,))])
else:
tag = self.STK[0][B:] # materialized at round_head
with opened(E, "eqt", TAGB - 1) as eqw:
flips = bitlist(~r & TAGMASK, TAGB)
for bp in flips:
E.x(tag[bp])
eqr = E.ladder([((tag[i],), 0) for i in range(TAGB)], eqw)
E.free([norm_term((dW[SGN],), 0, (eqw[TAGB - 2],), 1, (ctl,))])
E.frees(eqr)
for bp in flips:
E.x(tag[bp])
⋯ 107 lines
def reverse(self):
"""Stage 10, the journaled measurement-based reverse pass. Every BM
step went through J with its conjugate (macs record
release-or-recompute per the emit-time pebbling choice), so
replaying the conjugates in reverse re-materializes each round's
intermediates at the mirrored points and the replayed Fredkin
cascades pop the stack at exactly the right moments. The journal
replays closures over hard wire ids: each round's window pool is
re-materialized with reclaim right before its replay and freed
right after, and every bank slot is freed after the replay of its
first-touch round (its state there equals the forward state before
that round: |0>). The availability lines up by time symmetry -- an
id a bank slot took at forward round f is released before the
replays of rounds < f, exactly when their pools want it back -- and
reclaim fails closed on any gap.
"""
b, A, JR = self.b, self.A, self.JROUND
freed = 0
for k in range(len(JR) - 2, -1, -1):
rr = k - 1 # round -1 = the seed
with b.phase("reverse/seed" if rr < 0 else f"reverse/r{rr:02d}"):
if JR[k + 1] > JR[k]:
with A.replay(self.pools.get(rr, ())):
self.J.reverse(since=JR[k], until=JR[k + 1])
for reg in reversed(A.bank_log.get(rr, [])):
A.close(reg.name, reg.wires)
freed += 1
assert freed == len(self.SIG) + len(self.BB) + len(self.STK), (
"lazy registers leaked past the reverse",
freed,
)