"""Raw-spin and conditional-likelihood auditor; does not import the constructor.""" import itertools import argparse import json import hashlib import platform import sys import time from fractions import Fraction as F from pathlib import Path HERE=Path(__file__).resolve().parent Q=F(1117,2000); P=1-Q DROP=F(13,200); KEEP=F(59,100) SPINS=(Q**2,Q*P,P*Q,P**2) def ensure(ok,detail): if not ok: raise ValueError(detail) def edge(s,t,forward): lo,hi=(s,t) if forward else (t,s) return bool(lo&hi or lo&2 and hi&1) def raw_record(parent,children,maj,minor,forward): major_bits=[edge(parent,t,forward) for t in children[:maj]] mask=sum(int(v)<>i&1: result*=natural elif maj==2 and mask.bit_count()==1: result*=d if natural else 1 else: result*=not natural if minor: success=F(edge(parent,children[-1],not forward)) if maj==2 and mask.bit_count()==1 and children[-1]==weak: success*=KEEP result*=success if record>>maj else 1-success return result def conditional_laws(maj,minor,forward): size=1<<(maj+minor) rows=[[F(0)]*size for _ in range(4)] identities=0 for parent in (1,2,3): for children in itertools.product(range(4),repeat=maj+minor): w=F(1) for t in children: w*=SPINS[t] trans=raw_record(parent,children,maj,minor,forward) ensure(sum(trans)==1,"randomized rule total mass") for record,prob in enumerate(trans): ensure(prob==factored(parent,children,maj,minor,forward,record),"factorization") rows[parent][record]+=w*prob identities+=1 return rows,identities def minimum(laws,dim,forward,z): raw=(0,Q,Q*z,P) if forward else (0,Q*z,Q,P) diff=[sum(row[m]*w for row,w in zip(laws,raw))/sum(raw)-Q**m.bit_count()*P**(dim-m.bit_count()) for m in range(1<>m&1] if all(up>>(m|1<