#!/usr/bin/env python3 """Judge a route's classical estimate of the operator Loschmidt echo. Usage: estimate.py The route folder may hold estimate.json, the report of one estimate: {"instance": "49x1296", # tracker id, with or without the operator_loschmidt_echo_ prefix "value": 0.66, # the reported estimate of f_delta(O) "lower": 0.655, "upper": 0.665, # the defensible error bar; the metric is upper - lower "method": "BP-TN, bond dimension 512", "convergence": [{"bond": 128, "estimate": 0.6599}, ...], # >= 2 entries, numeric "estimate" "reproduce": "python3 bp_tn.py --bond 512 > estimate.json", "runtime_s": 7260, "hardware": "...", "notes": "..." } # optional The check is exact about the contract and agnostic about the physics: it cannot recompute f_delta(O) for 49-70 qubit circuits in a 10-minute, 4-CPU, no-network job, so it enforces what makes an interval comparable within an instance and reports the width as the score. O is a product of Pauli Z operators, O^2 = I, so f_delta(O) lies in [-1, 1]. Exits 0 when the report holds (or when there is no report to judge), 1 when it does not, 2 when the folder cannot be read. metrics.json {"score": width} is written into the route folder when a score is judged; everything the check prints is also on stdout. """ import json import math import os import re import sys INSTANCE_RE = re.compile(r"^(operator_loschmidt_echo_)?([1-9][0-9]{0,2})x([1-9][0-9]{0,4})$") KNOWN = {"49x648", "49x1296", "70x1872", "56x1488"} # baseline + active candidates as of 2026-10-20 CODE_SUFFIXES = (".py", ".ipynb", ".sh", ".jl", ".c", ".cc", ".cpp", ".rs", ".nb") def fail(reason): print(f"estimate: reject: {reason}") sys.exit(1) def number(report, key): value = report.get(key) if isinstance(value, bool) or not isinstance(value, (int, float)): fail(f"{key} must be a number") value = float(value) if not math.isfinite(value): fail(f"{key} must be finite (got {value})") return value def judge(route_dir): path = os.path.join(route_dir, "estimate.json") if not os.path.isdir(route_dir): print(f"estimate: cannot judge: no folder {route_dir}") sys.exit(2) if not os.path.isfile(path): print(f"Nothing to judge yet: {route_dir} holds no estimate.json") sys.exit(0) try: with open(path, encoding="utf-8") as handle: report = json.load(handle) except (OSError, UnicodeDecodeError) as err: fail(f"estimate.json cannot be read: {err}") except json.JSONDecodeError as err: fail(f"estimate.json is not valid JSON: line {err.lineno}: {err.msg}") if not isinstance(report, dict): fail("estimate.json must hold a JSON object") instance = report.get("instance") if not isinstance(instance, str) or not INSTANCE_RE.match(instance): fail("instance must be a tracker id like 49x1296 or operator_loschmidt_echo_56x1488") short = INSTANCE_RE.match(instance).group(2) + "x" + INSTANCE_RE.match(instance).group(3) qubits = int(INSTANCE_RE.match(instance).group(2)) if qubits > 156: fail(f"instance names {qubits} qubits; the circuit files' register has 156") if short not in KNOWN: print(f"estimate: note: instance {short} is not one of the known ids " f"{sorted(KNOWN)}; judged on form only") lower = number(report, "lower") upper = number(report, "upper") value = number(report, "value") if not -1.0 <= lower: fail(f"lower {lower} is below -1: O is a Z string with O^2 = I, so f_delta(O) in [-1, 1]") if not upper <= 1.0: fail(f"upper {upper} is above 1: O is a Z string with O^2 = I, so f_delta(O) in [-1, 1]") if not lower <= value <= upper: fail(f"value {value} is not inside [lower, upper] = [{lower}, {upper}]") width = upper - lower if width <= 0.0: fail("the interval must have positive width: width 0 claims exactness") method = report.get("method") if not isinstance(method, str) or not method.strip(): fail("method must be a non-empty string (what computed the estimate)") convergence = report.get("convergence") if not isinstance(convergence, list) or len(convergence) < 2: fail("convergence must be a list of at least 2 entries: the README asks for evidence of convergence") for i, entry in enumerate(convergence): if not isinstance(entry, dict) or isinstance(entry.get("estimate"), bool) \ or not isinstance(entry.get("estimate"), (int, float)) \ or not math.isfinite(float(entry["estimate"])): fail(f"convergence entry {i} must be an object with a finite numeric \"estimate\"") estimate = float(entry["estimate"]) if not -1.0 <= estimate <= 1.0: fail(f"convergence entry {i} estimate {estimate} is outside [-1, 1]") reproduce = report.get("reproduce") if not isinstance(reproduce, str) or not reproduce.strip(): fail("reproduce must be a non-empty command: anyone should be able to rerun the code on the QASM file") code = [name for name in os.listdir(route_dir) if name.endswith(CODE_SUFFIXES) and os.path.isfile(os.path.join(route_dir, name))] if not code: fail(f"no code in the route folder ({', '.join(CODE_SUFFIXES)}): push what produced the estimate") runtime = report.get("runtime_s") if runtime is not None and (isinstance(runtime, bool) or not isinstance(runtime, (int, float)) or not math.isfinite(float(runtime)) or runtime < 0): fail("runtime_s, when given, must be a finite number >= 0") hardware = report.get("hardware") if hardware is not None and not isinstance(hardware, str): fail("hardware, when given, must be a string") score = round(width, 12) or width # trim float dust, never round a positive width to 0 metrics = {"score": score} metrics_path = os.path.join(route_dir, "metrics.json") try: with open(metrics_path, "w", encoding="utf-8") as handle: json.dump(metrics, handle) handle.write("\n") print(f"estimate: wrote {metrics_path}: {json.dumps(metrics)}") except OSError as err: print(f"estimate: could not write {metrics_path}: {err}; score on stdout only: " f"{json.dumps(metrics)}") print(f"instance {short}: value {value} in [{lower}, {upper}], width {width} " f"({len(convergence)} convergence entries, code: {', '.join(sorted(code))})") sys.exit(0) if __name__ == "__main__": if len(sys.argv) != 2: print(__doc__) sys.exit(2) judge(sys.argv[1])