"""Exact replay of ordinary branches followed by any local reddening core. This independently tests the proof in local_dt_gluing.tex. It does not call verify_gate, use a weighted Baxter gate, or use the existing verify_tree. """ import argparse import json from pathlib import Path import sys import time ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "weighted")) import verify as v def core_reddening(a, b, source): """Read a +B reddening certificate and turn it into a -B word.""" rec = json.loads(Path(source).read_text()) R = [k - 1 for k in rec["word"]] p = [k - 1 for k in rec["terminal_permutation"]] dim, weight = {0: a, 1: b}, {0: 1, 1: 2} labels = v.strips(dim) + [("edge", 0, 1)] B = v.principal(labels, dim, weight) positive = v.Framed(B) for k in R: positive.step(k) v.require(positive.endpoint() == p, "Supplied local terminal permutation differs") Rminus = [p[k] for k in reversed(R)] negative = v.Framed(v.negate(B)) for k in Rminus: negative.step(k) negative.endpoint() return Rminus def direct_gluing(kind, rank, root, local_word): dim, weight = v.family(kind, rank, root) edges = [(i, i - 1) if i <= root else (i - 1, i) for i in dim if i - 1 in dim] seams = [e for e in edges if weight[e[0]] != weight[e[1]]] v.require(len(seams) == 1, "Expected one multiple bond") seam = seams[0] long, short = sorted(seam, key=weight.get) core_colors = {long, short} labels = v.strips(dim) + [("edge", *e) for e in edges] + [("frame", root)] B = v.principal(labels, dim, weight) seed = v.Framed(v.negate(B)) target = v.identity(seed.n) slots = list(range(seed.n)) for edge in edges: if edge == seam: continue i, j = edge for pivot in v.rectangle(i, j, dim): seed.step(slots[labels.index(pivot)]) lab = ("edge", i, j) target[labels.index(lab)] = [ -int(z == lab or z[0] in ("s", "t") and z[1] in edge) for z in labels] slots = seed.permutation(target) framing = [("frame", root)] for h in range(dim[root] - 1, 0, -1): framing.extend([("t", root, h), ("s", root, h)]) for pivot in framing: seed.step(slots[labels.index(pivot)]) target[labels.index(("frame", root))] = [ -int(z == ("frame", root) or z[0] in ("s", "t") and z[1] == root) for z in labels] slots = seed.permutation(target) for i, d in dim.items(): if i in core_colors: continue for turn in range(d): kind_of_strip = "s" if turn % 2 == 0 else "t" for h in range(1, d): seed.step(slots[labels.index((kind_of_strip, i, h))]) # Locate the unchanged core by its initial positive unit rows, and verify # exactly the hypotheses of the red-complement completion lemma. local_labels = v.strips({long: dim[long], short: dim[short]}) + [("edge", *seam)] core_indices = [labels.index(z) for z in local_labels] rows = {tuple(row[seed.n:]): k for k, row in enumerate(seed.rows)} local_slots = [rows[tuple(v.identity(seed.n)[j])] for j in core_indices] outside_indices = set(range(seed.n)) - set(core_indices) outside_slots = set(range(seed.n)) - set(local_slots) v.require(all(max(seed.rows[k][seed.n:]) <= 0 for k in outside_slots), "An exterior row was not red before local completion") v.require(all(any(seed.rows[k][seed.n + h] < 0 for h in outside_indices) for k in outside_slots), "An exterior row lacks an exterior negative coefficient") # Reflection of particle order identifies the two isolated orientations. reflected = list(range(len(local_labels))) if seam != (long, short): reflected = [local_labels.index((z[0], z[1], dim[z[1]] - z[2])) if z[0] in ("s", "t") else len(local_labels) - 1 for z in local_labels] prefix_length = len(seed.word) for pivot in local_word: seed.step(local_slots[reflected[pivot]]) negative_permutation = seed.endpoint() # Replay on the original +B convention and check its entire terminal seed. word_plus = [negative_permutation[k] for k in reversed(seed.word)] check = v.Framed(B) for pivot in word_plus: check.step(pivot) permutation = check.endpoint() return {"case": f"{kind},{rank},{root}", "rank": check.n, "word": [k + 1 for k in check.word], "terminal_permutation": [k + 1 for k in permutation], "red": check.red, "local_pair": [dim[long], dim[short]], "ordinary_prefix_length": prefix_length, "local_reddening_length": len(local_word), "method": "ordinary branch prefix, then local reddening"} def main(): ap = argparse.ArgumentParser(description=__doc__) ap.add_argument("--case", required=True) ap.add_argument("--source", type=Path, required=True) ap.add_argument("--output", type=Path) args = ap.parse_args() kind, n, r = args.case.upper().split(",") n, r = int(n), int(r) dim, weight = v.family(kind, n, r) edge = next((i - 1, i) for i in dim if i - 1 in dim and weight[i - 1] != weight[i]) long, short = sorted(edge, key=weight.get) started = time.monotonic() local = core_reddening(dim[long], dim[short], args.source) result = direct_gluing(kind, n, r, local) if args.output: args.output.write_text(json.dumps(result, separators=(",", ":")) + "\n") print(json.dumps({k: value for k, value in result.items() if k != "word"}) + f"\nlength={len(result['word'])}, seconds={time.monotonic()-started:.3f}") if __name__ == "__main__": main()