"""Local format check for decoder-board artifacts (SPEC_v7_decoder s4): python3 -I verifiers/decoder_format.py artifact.json python3 -I verifiers/decoder_format.py pack predictions.b8|predictions.01 artifact.json [batch_ns cores hardware] Prints one JSON line. For a ranked instance it reports a bounded format error or {"format_valid": true, "score_available": false}: ranked labels are private to the QArena server, so no local tool can say how many errors a ranked file makes, and this check never reports an accepted verdict. For a development instance (-dev) the labels are public; run run_one.py to get the exact score. `pack` converts predictions into a submission file: raw Stim b8 (exactly N * ceil(K/8) bytes, experiments concatenated in instance order, zero padding bits) or, for a file ending in .01, one line per shot of exactly K characters 0/1 (observable j is character j) ending in LF. It refuses a wrong shot count, wrong line length, stray characters or nonzero padding bits. """ import base64 import json import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from common import Reject, load_artifact # noqa: E402 import decoder # noqa: E402 def pack(inst, src, out, est): I, _ = decoder.load_instance_bytes(inst) N, K, R, B = decoder.geometry(I) raw = open(src, "rb").read() if src.endswith(".01"): lines = raw.split(b"\n") if lines[-1] != b"" or len(lines) != N + 1: raise SystemExit(f"expected {N} lines, each ending in LF") buf = bytearray(N * B) for s, ln in enumerate(lines[:N]): if len(ln) != K or ln.strip(b"01"): raise SystemExit(f"line {s + 1}: expected exactly {K} characters 0/1") for j in range(K): if ln[j] == 49: buf[s * B + j // 8] |= 1 << (j % 8) raw = bytes(buf) if len(raw) != N * B or not decoder.padding_ok(raw, B, K): raise SystemExit(f"expected {N * B} bytes of b8 rows with zero padding bits") art = {"format": decoder.FORMAT, "instance": inst, "predictions": base64.b64encode(raw).decode("ascii")} if est: art["estimate"] = {"batch_ns": int(est[0]), "cores": int(est[1]), "hardware": est[2]} decoder.check_estimate(art["estimate"]) with open(out, "w", encoding="ascii") as f: json.dump(art, f, separators=(",", ":")) print(json.dumps({"written": out, "shots": N, "bytes": os.path.getsize(out)})) def main(): if sys.argv[1:2] == ["pack"]: try: return pack(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5:8] if len(sys.argv) >= 8 else None) except Reject as e: raise SystemExit(str(e)) inst, path = sys.argv[1:3] try: I, _ = decoder.load_instance_bytes(inst) except (OSError, ValueError): print(json.dumps({"format_valid": False, "reason": "unknown instance"})) return try: decoder.check_format(inst, I, load_artifact(path, decoder.MAX_ARTIFACT_BYTES)) except Reject as e: print(json.dumps({"format_valid": False, "reason": str(e)[:2000]})) return except RecursionError: print(json.dumps({"format_valid": False, "reason": "artifact nesting too deep"})) return out = {"format_valid": True, "score_available": I["mode"] == "debug"} if I["mode"] == "debug": out["note"] = f"development instance: python3 -I verifiers/run_one.py decoder {inst} {path} gives the exact score" print(json.dumps(out)) if __name__ == "__main__": main()