def select_pauli(b, sel_bits, x):
"""One content-pruned descent over the 3,221 frame blocks; seven
flag wires XOR-accumulate the 14 interval gates; frames nest in
the fixed application order (F innermost); per-block sign
polynomials ride free channels plus one CCZ per surviving
monomial (shared prefix AND per degree-3 group), the seven
node-hosted monomials emitting once at their walk nodes.
"""
_gauge_c, lin_signs, mono_signs, node_monos = sign_tables()
def apply_row(ctrl, iv):
m = MASKS[iv]
for j in range(X): # Z-part first: CZ sees the INPUT x values
if (m >> (X + j)) & 1:
b.cz(ctrl, x[j])
xor_word(b, ctrl, m & XMASK, x) # then the X-part flips
if SIGN[iv]: # constant sign (-1)^(theta ^ ny2)
b.z(ctrl)
for i in lin_signs.get(iv, ()):
b.cz(ctrl, sel_bits[i])
pref = pref_bit = None
for mn in mono_signs.get(iv, ()):
if len(mn) == 2:
b.ccz(ctrl, sel_bits[mn[0]], sel_bits[mn[1]])
else: # degree >= 3: prefix AND per group
if pref_bit != mn[0]:
if pref is not None:
b.and_release(pref, ctrl, sel_bits[pref_bit])
pref = b.and_compute(ctrl, sel_bits[mn[0]])
pref_bit = mn[0]
if len(mn) == 3:
b.ccz(pref, sel_bits[mn[1]], sel_bits[mn[2]])
else:
t2 = b.and_compute(pref, sel_bits[mn[1]])
b.ccz(t2, sel_bits[mn[2]], sel_bits[mn[3]])
b.and_release(t2, pref, sel_bits[mn[1]])
if pref is not None:
b.and_release(pref, ctrl, sel_bits[pref_bit])
hosted = set()
def node_enter(lo, span, ctrl):
monos = node_monos.get((lo, span))
if not monos:
return
for mn in monos:
if len(mn) == 2:
b.ccz(ctrl, sel_bits[mn[0]], sel_bits[mn[1]])
else:
t2 = b.and_compute(ctrl, sel_bits[mn[0]])
b.ccz(t2, sel_bits[mn[1]], sel_bits[mn[2]])
b.and_release(t2, ctrl, sel_bits[mn[0]])
hosted.add((lo, span))
Frame flags 136 Toffolis · peak 126 qubits
with b.phase("select-blk"):
flags = {}
for g in PRIORITY:
if not GEN_IVS[g]:
continue
fq = b.qubit()
b.claim("flag", [fq])
flags[g] = fq
xor_gate_flags(b, sel_bits, flags)
Frames on 131 Toffolis · peak 112 qubits
with b.phase("select-conj"):
for g in reversed(PRIORITY): # outermost first, F innermost last
if g in flags:
Block select 3,327 Toffolis · peak 124 qubits
with b.phase("select-pauli"):
select_walk(b,
list(
reversed(sel_bits)), node_enter,
lambda lo, ctrl: apply_row(ctrl, lo))
assert hosted == set(node_monos), "node-hosted monomial span missed"
Frames off 131 Toffolis · peak 112 qubits
with b.phase("select-unconj"):
for g in PRIORITY: # innermost first
if g in flags:
conjugate_close(b, flags[g], x, g)
Flags cleared 136 Toffolis · peak 126 qubits
with b.phase("select-unblk"):
xor_gate_flags(b, sel_bits, flags)
for g in reversed(PRIORITY):
if g not in flags:
continue
b.release_recycle("flag", [flags[g]])
⋯ 2 lines
def build():
b = Builder()
regs = declare_registers(b, [("i", A), ("sigma", S), ("x", X)])
idx, sigma, x = regs["i"], regs["sigma"], regs["x"]
for name in ("i", "sigma", "x"):
b.claim(name, regs[name])
# ONE walk carries BOTH payloads: keep bits 0-14 into the 15-wire
# word bank plus one carrier for bit 15, alt into the 16-wire
# selection scratch (alt before the swap, sel during SELECT).
word = b.qubits(B)
b.claim("word", word)
kc = b.qubit()
b.claim("word", [kc])
keep_carriers = list(word) + [kc]
sel = b.qubits(A)
b.claim("sel", sel)
def leaf(lo, ctrl):
xor_word(b, ctrl, KEEPW[lo], keep_carriers)
xor_word(b, ctrl, ALTW[lo], sel)
Alias lookup 10,271 Toffolis · peak 119 qubits
with b.phase("lookup"):
# f = [sigma < keep] on one scratch wire; the keep bank stays live
# through SELECT so the identical comparator pass can erase f after
# the selection is unwound
Compare and select 32 Toffolis · peak 122 qubits
with b.phase("cmp"):
f = b.qubit()
b.claim("carry", [f])
compare_into(b, sigma, keep_carriers, f)
# the selection swap: sel <-> i under f — the index wires carry the
# unselected branch through SELECT and the identical swap restores
# them after it
with b.phase("cmp/select"):
for j in range(A):
b.cswap(f, sel[j], idx[j])
select_pauli(b, sel, x)
Unselect and uncompare 32 Toffolis · peak 122 qubits
with b.phase("cmp/unselect"):
for j in range(A):
b.cswap(f, sel[j], idx[j])
with b.phase("cmp/rev"):
compare_into(b, sigma, keep_carriers, f)
b.release_recycle("carry", [f])
b.release("word", word)
b.release("word", [kc])
Banks measured 0 Toffolis · peak 104 qubits
with b.phase("unlookup/measure-keep"):
m_keep = erase_wires(b, keep_carriers)
for q in keep_carriers:
b.recycle(q)
b.release("sel", sel)
with b.phase("unlookup/measure-alt"):
m_alt = erase_wires(b, sel)
for q in sel:
b.recycle(q)
Phase repair 782 Toffolis · peak 99 qubits
return b, regs
PEAK = 112 + 14 # the SELECT-stage peak
H_LOOK = PEAK - 104 # lookup-walk hold budget: the lookup holds 104 live wires
⋯ 429 lines
def _content_tree(lo, span, limit, uniform, skip, leaf_load):
if lo >= limit:
return None
if span == 1 or (uniform is not None and uniform(lo, span)):
return _WNode("leaf", lo=lo, span=span, load=leaf_load(lo) if leaf_load else 0)
half = span >> 1
if skip is not None and skip(lo, span):
return _WNode("skip", (_content_tree(lo, half, limit, uniform, skip, leaf_load),), lo, span)
left = _content_tree(lo, half, limit, uniform, skip, leaf_load)
right = _content_tree(lo + half, half, limit, uniform, skip, leaf_load)
if right is None:
return _WNode("one", (left,), lo, span)
return _WNode("two", (left, right), lo, span)
⋯ 138 lines
def _lk_uniform(lo, span):
hi = min(lo + span, L)
first = DATA[lo]
return all(v == first for v in DATA[lo + 1 : hi])
def _lk_skip(lo, span):
half = span >> 1
rh = min(lo + span, L) - (lo + half)
return rh > 0 and DATA[lo : lo + rh] == DATA[lo + half : lo + half + rh]
_LK_ROOT = _content_tree(0, 1 << A, L, _lk_uniform, _lk_skip, None)
_LK_ANDS, _LK_POLICY = _walk_policy(_LK_ROOT, H_LOOK)
assert _LK_ANDS < _INF
def lookup_walk(b, idx_msb, leaf_at):
"""One content-pruned descent over [0, 2^A): periodic skip (two
halves with identical in-domain content recurse once, no AND) and
one-sided descent past L are free; everywhere else the load-time
DP policy places the held chain, split, and accumulator wires.
"""
_policy_walk(
b, _LK_ROOT, _LK_POLICY, H_LOOK, lambda n: idx_msb[A - (n.span.bit_length() - 1)], leaf_at
)
def conjugate_f(b, ctrl, x):
for p in range(X // 2):
lo, hi = x[2 * p], x[2 * p + 1]
b.cx(hi, lo)
b.ccx(ctrl, lo, hi)
b.cx(hi, lo)
b.ccz(ctrl, lo, hi)
⋯ 197 lines
_MIN_FORM = {g: min_form_net(PERM_MACH[g][3], PERM_MACH[g][4], prefix_mat(X), X) for g in PERM_MACH}
_C_FORM = min_form_net([(j - 1, j) for j in range(1, 20)], list(range(19)), prefix_mat(20), 20)
⋯ 2 lines
def _run_form(b, ctrl, wires, form):
net, ccz, cz = form
for s, d in net:
b.cx(wires[s], wires[d])
for a, bb in ccz:
b.ccz(ctrl, wires[a], wires[bb])
for c in cz:
b.cz(ctrl, wires[c])
for s, d in reversed(net):
b.cx(wires[s], wires[d])
⋯ 2 lines
def conjugate_c(b, ctrl, x):
"""C^ctrl: per spin sector, e2(y) at its bilinear rank — 10 CCZs
behind a free congruence net (19 in prefix coordinates).
"""
for par in (0, 1):
sec = [x[2 * p + par] for p in range(X // 2)]
_run_form(b, ctrl, sec, _C_FORM)
⋯ 2 lines
def _pf_perm(b, ctrl, x, perm):
done = set()
for k in range(X):
j = perm[k]
if k in done or j == k:
continue
done.add(j)
b.cx(x[j], x[k])
b.ccx(ctrl, x[k], x[j])
b.cx(x[j], x[k])
⋯ 29 lines
def conjugate_open(b, ctrl, x, g):
if g == 0:
conjugate_f(b, ctrl, x)
return
if g == 1:
conjugate_c(b, ctrl, x)
return
perm = PERM_MACH[g][0]
_pf_perm(b, ctrl, x, perm)
_run_form(b, ctrl, x, _MIN_FORM[g])
_gauge_layer(b, ctrl, x, g) # inner gauge layer (free CX/CZ)
SPAN_AT = {}
for _t, _lo, _hi in REGIONS:
_n = 1 << len(_t)
for _base in range(_lo, _hi, _n):
assert _base % _n == 0, (_t, _base)
SPAN_AT[_base] = _n
del _t, _lo, _hi, _n, _base
⋯ 942 lines
def select_walk(b, bits_msb, enter, leaf):
"""Content-pruned descent over the block partition. At a
one-sided node (right half entirely >= L) the parent control
already implies the branch on every graded shot, so the walk
descends with it unchanged — no narrowing AND (the spare copies
land on sel values no shot reaches). ``enter(lo, span, ctrl)``
hooks every controlled node entry — the walk-node monomial channel
emits there; ``leaf(lo, ctrl)`` fires per block.
"""
def rec(bits, lo, span, ctrl):
if lo >= L:
return
if ctrl is not None:
enter(lo, span, ctrl)
if span == 1 or SPAN_AT.get(lo) == span:
leaf(lo, ctrl)
return
hb = bits[0]
half = span >> 1
if ctrl is None:
b.x(hb)
rec(bits[1:], lo, half, hb)
b.x(hb)
rec(bits[1:], lo + half, half, hb)
elif lo + half < L:
anc = b.and_compute(ctrl, hb)
rec(bits[1:], lo + half, half, anc)
b.cx(ctrl, anc)
rec(bits[1:], lo, half, anc)
b.cx(ctrl, anc)
b.and_release(anc, ctrl, hb)
else:
rec(bits[1:], lo, half, ctrl) # one-sided node: no narrowing
rec(bits_msb, 0, 1 << A, None)
def b2_fixup(b, idx, m_keep, m_alt): # noqa: C901 - straight-line circuit emitter
"""Emit the joint deferred unlookup repair at B(2) granularity: a dense
window walk over bits 6-15 (642 windows of 64 rows, DP-capped at
hold budget H_FIX), the 16-slot one-hot routing of bits 2-5,
per-slot outcome-conditioned CZs for the quad-base fix, and
Reed-Muller plane ANDs for the low two bits — one temporary AND
per (window, plane) with free conditioned CZs. Out-of-domain rows
inside the last window inherit their quad base (rows i >= L are
untested).
"""
def row_terms(iv):
t = [m_keep[t2] for t2 in range(16) if (KEEPW[iv] >> t2) & 1]
t += [m_alt[t2] for t2 in range(16) if (ALTW[iv] >> t2) & 1]
return t
mid, hu = idx[2:6], idx[6:]
with b.phase("unlookup/window:prep"):
u = b.qubits(KW)
b.claim("window", u)
b.x(u[0])
for i in reversed(range(LOG_KW)):
for s in range(0, KW, 1 << (i + 1)):
b.cswap(mid[i], u[s], u[s + (1 << i)])
def fixup_leaf(ctrl, wv):
base = wv * FIX_WIN
planes = {}
for Smask in (1, 2, 3):
need = False
for lv in range(KW):
gbase = base + (lv << 2)
if gbase >= L:
continue
acc = []
for rr in range(4):
if rr & ~Smask:
continue
row = gbase + rr
acc = _xor_terms(acc, row_terms(row if row < L else gbase))
if xor_outcomes(acc) is not None:
need = True
break
if need:
planes[Smask] = None
assert frozenset(planes) == _PLANE_SETS[wv]
win_terms = {}
for lv in range(KW):
gbase = base + (lv << 2)
if gbase >= L:
continue
win_terms[lv] = {
rr: row_terms(gbase + rr if gbase + rr < L else gbase) for rr in range(4)
}
gb = xor_outcomes(win_terms[lv][0])
if gb is not None:
b.cz(ctrl, u[lv], cond=gb)
def emit_plane(pw, Smask):
for lv, terms in win_terms.items():
acc = []
for rr in range(4):
if rr & ~Smask:
continue
acc = _xor_terms(acc, terms[rr])
gb = xor_outcomes(acc)
if gb is not None:
b.cz(pw, u[lv], cond=gb)
# the r0r1 plane chains through a HELD single-bit plane: two
# plane wires live only across the pair's emission (the same
# transient the build itself has), so its release recomputes
# nothing, and a needed single rides the held wire for free
done = set()
if 3 in planes:
lo_S = 1 if 1 in planes else 2
lo_bit, hi_bit = (idx[0], idx[1]) if lo_S == 1 else (idx[1], idx[0])
plo = b.and_compute(ctrl, lo_bit)
p3 = b.and_compute(plo, hi_bit)
emit_plane(p3, 3)
b.and_release(p3, plo, hi_bit)
if lo_S in planes:
emit_plane(plo, lo_S)
done.add(lo_S)
b.and_release(plo, ctrl, lo_bit)
done.add(3)
for Sm, ibit in ((1, idx[0]), (2, idx[1])):
if Sm in planes and Sm not in done:
pw = b.and_compute(ctrl, ibit)
emit_plane(pw, Sm)
b.and_release(pw, ctrl, ibit)
with b.phase("unlookup/window:fixup"):
_capped_window_walk(b, hu, fixup_leaf)
with b.phase("unlookup/window:unprep"):
for i in range(LOG_KW):
for s in range(0, KW, 1 << (i + 1)):
v = u[s + (1 << i)]
b.cx(v, u[s])
b.release("window", [v])
b.and_release(v, mid[i], u[s])
b.x(u[0])
b.release_recycle("window", [u[0]])