"""NIRAEL quantum-language checks and manuscript figures. All data are finite synthetic calculations. The analytic proofs are in the PDF. Requires Python 3.10+, NumPy, Matplotlib. No network access is used. """ from pathlib import Path from itertools import combinations from math import comb import json import numpy as np ROOT = Path(__file__).resolve().parent OUT = ROOT.parent / 'output' / 'pdf' / 'figures' OUT.mkdir(parents=True, exist_ok=True) def zeta_upper(a, n): a = np.array(a, float, copy=True) for i in range(n): bit = 1 << i for mask in range(1 << n): if not (mask & bit): a[mask] += a[mask | bit] return a def walsh(a): v = np.array(a, float, copy=True) h = 1 while h < len(v): for i in range(0, len(v), 2*h): left, right = v[i:i+h].copy(), v[i+h:i+2*h].copy() v[i:i+h], v[i+h:i+2*h] = left + right, left - right h *= 2 return v def periods(f): N = len(f) return [h for h in range(N) if all(f[x] == f[x ^ h] for x in range(N))] def check_periods(): n = 3; N = 1 << n checked = 0 rng = np.random.default_rng(260624) for fm in range(1 << N): f = np.array([(fm >> x) & 1 for x in range(N)]) H = periods(f) annihilator = [a for a in range(N) if all((a & h).bit_count() % 2 == 0 for h in H)] p = np.zeros(N) p[annihilator] = 1/len(annihilator) w = walsh(p) assert max((abs(w[x ^ y]) for x in range(N) for y in range(N) if f[x] != f[y]), default=0) < 1e-12 # A translation-invariant randomized mixture of annihilator cosets. base = rng.random(N); base /= base.sum() q = sum(base[np.arange(N) ^ a] for a in annihilator)/len(annihilator) wq = walsh(q) assert all(abs(wq[h]) < 1e-12 for h in range(N) if h not in H) checked += 1 hamming = [] for n in range(1, 9): for k in range(n): f = np.array([int(x.bit_count() <= k) for x in range(1 << n)]) assert periods(f) == [0] hamming.append((n,k)) return {'boolean_functions_checked': checked, 'hamming_ball_predicates_checked':len(hamming)} def rank_f2(a): a=np.array(a,dtype=np.uint8,copy=True) if not a.size: return 0 r=0 for c in range(a.shape[1]): piv=np.flatnonzero(a[r:,c]) if not len(piv): continue j=r+int(piv[0]); a[[r,j]]=a[[j,r]] for i in range(a.shape[0]): if i!=r and a[i,c]: a[i]^=a[r] r+=1 if r==a.shape[0]: break return r def reduced_betti(faces): # Augmented simplicial complex, over F_2. Empty face included. levels={d:[f for f in faces if f.bit_count()==d+1] for d in range(-1,6)} ranks={} for d in range(0,6): rows=levels[d-1]; cols=levels[d] mat=np.zeros((len(rows),len(cols)),dtype=np.uint8) lookup={r:i for i,r in enumerate(rows)} for j,f in enumerate(cols): for i in range(5): if f & (1<exact_value: exact_value=val; exact=S violations=0; min_slack=1 for _ in range(1500): p=rng.permutation(len(candidates)) A=p[:2]; B=p[:5]; j=p[6] ga=float(prior@(r[j]*np.prod(1-r[A],axis=0))) gb=float(prior@(r[j]*np.prod(1-r[B],axis=0))) min_slack=min(min_slack,ga-gb) violations+=int(ga+1e-12n//2) for x in range(2**n)]) d=1-2*f; local=[]; glob=[] for s in ss: _,R=product_gram([s]*n) H=(R*d[None,:])@R/(2**n) glob.append((1+np.sum(abs(np.linalg.eigvalsh(H))))/2) local.append((1+np.trace(d[:,None]*H))/2) ax.plot(ss,glob,label='Optimal collective readout',lw=2) ax.plot(ss,local,label='Bitwise Helstrom, then f',lw=1.8) ax.fill_between(ss,local,glob,color='#73E2C0',alpha=.25) ax.set(title=f'{kind} on {n} bits',xlabel='Pure-state overlap s',ylabel='Success probability',ylim=(.48,1.01)) axs[0].legend(fontsize=8) fig.savefig(OUT/'local_global_boundary.pdf'); plt.close(fig) fig,axs=plt.subplots(1,2,figsize=(9,3.1),layout='constrained') q=np.linspace(0,1,301) for w in [3,5,8]: vals=sum(comb(w,j)*q**j*(1-q)**(w-j) for j in range(3,w+1)) axs[0].plot(q,vals,label=f'{w} active symbols',lw=1.8) axs[0].set(xlabel='Local conclusive activation q',ylabel='Optimal refusal probability',title='Refuse the at-most-two grammar') axs[0].legend(fontsize=8) axs[1].plot(q,q**2+2*q**3-2*q**4,lw=2,label='Three overlapping forbidden sets') axs[1].plot(q,q**3,lw=1.8,label='One three-symbol certificate') axs[1].set(xlabel='Local conclusive activation q',ylabel='Optimal refusal probability',title='Grammar changes the response law') axs[1].legend(fontsize=8) fig.savefig(OUT/'grammar_reliability.pdf'); plt.close(fig) c=report['compiler']; fig,ax=plt.subplots(figsize=(7,2.7),layout='constrained') ax.plot(range(5),c['greedy_success'],'o-',color='#187F70',lw=2,label='Greedy complete-certificate packets') ax.scatter([3],[c['exact_three_packet_success']],marker='D',color='#AD5269',s=55,label='Exhaustive optimum at three packets',zorder=3) ax.set(xlabel='Independent packet budget',ylabel='Expected sound refusal',xticks=range(5),ylim=(0,.55)) ax.legend(fontsize=8) fig.savefig(OUT/'certificate_compiler.pdf'); plt.close(fig) def main(): report={'status':'finite synthetic verification, not experimental quantum hardware', 'periods':check_periods(),'homology':check_homology(),'compiler':check_compiler()} (ROOT/'nirael_results.json').write_text(json.dumps(report,indent=2)) make_figures(report) print(json.dumps(report,indent=2)) if __name__=='__main__': main()