"""Reproducible v2 numerical/symbolic adversarial checks; not a proof assistant.""" from __future__ import annotations import json,math,sys,platform,time from pathlib import Path import numpy as np import scipy,sympy as sp from scipy.optimize import brentq from scipy.integrate import quad from floquet import monodromy,log_spectral_radius,transfer from resource_floquet import * from maxwell_rectangle import Rectangle,rectangle_cell,rectangle_fixed,rectangle_free,cell_monodromy,generator_transfer from quantum_floquet import bogoliubov,photon_number,squeezing ROOT=Path(__file__).resolve().parents[1] rng=np.random.default_rng(260925) report={'seed':260925,'versions':{'python':platform.python_version(),'numpy':np.__version__,'scipy':scipy.__version__,'sympy':sp.__version__}} start=time.time() def fixed_mean_profile(B,S,m,j): N=int(rng.integers(2,17));t=rng.dirichlet(np.ones(N))*S raw=rng.uniform(-1,1,N) p=(m-1)/(B-1) shift=brentq(lambda a:float(np.dot(np.clip(raw+a,0,1),t)/S)-p,-3,3,xtol=1e-14) u=1+(B-1)*np.clip(raw+shift,0,1) if j%2==0: # Repeated alternating high/low layers with exactly the same resource. nh=int(rng.integers(1,8)) th=rng.dirichlet(np.ones(nh))*p*S tl=rng.dirichlet(np.ones(nh))*(1-p)*S u=np.array([B,1.]*nh);t=np.column_stack((th,tl)).ravel() return u,t count=0;hyper=0;worst=-math.inf;meanerr=0.;defect_cases=[] for R in [1.2,1.5,2.,3.,5.]: B=R*R for S in np.linspace(.4,9,16): for p in [.1,.3,.5,.7,.9]: m=1+(B-1)*p;F,cands=resource_optimum(R,float(S),m) for j in range(12): u,t=fixed_mean_profile(B,float(S),m,j) M=monodromy(u,t);L=log_spectral_radius(M) err=L-F;worst=max(worst,err);count+=1 meanerr=max(meanerr,abs(np.dot(u,t)/S-m)) assert err<2e-8,(R,S,m,L,F,u,t) if L>1e-6: hyper+=1 if len(defect_cases)<80 and j%3==0:defect_cases.append((R,float(S),m,u,t,M,L,cands)) report['resource_random']={'cases':count,'hyperbolic':hyper,'max_bound_excess':worst,'max_mean_error':meanerr} # Candidate duals, moment matching and exact monodromy equality. ccount=0;cerr=0.;derr=0.;select_error=0.;concavity_violation=0.;nearest_fail=0 for R in [1.2,1.5,2.,3.,5.]: for p in [.1,.2,.35,.5,.7,.9]: m=1+(R*R-1)*p;lo,hi=resource_gap_interval(R,m) free=resource_free(R,m);topt=free.high_time+free.low_time vals=[] for tau in np.linspace(lo+(hi-lo)*.01,hi-(hi-lo)*.01,21): F,cs=resource_optimum(R,float(tau),m) c=[c for c in cs if c.winding==1][0] eta,xi,a,b=dual_parameters(R,c) assert selector((a+b)/2,R,eta,xi)==R*R assert selector((b+a+math.pi)/2,R,eta,xi)==1. P=transfer(1,c.low_time)@transfer(R*R,c.high_time) cerr=max(cerr,abs(log_spectral_radius(P)-c.log_gain)) derr=max(derr,abs(switching(a,eta,xi)),abs(switching(b,eta,xi))) vals.append(c.log_gain);ccount+=1 concavity_violation=max(concavity_violation,float(np.max(np.diff(vals,2)))) for S in np.linspace(.5,20,40): F,cs=resource_optimum(R,float(S),m) if cs: w=max(cs,key=lambda c:c.log_gain).winding allowed={math.floor(S/topt),math.ceil(S/topt)} assert w in allowed,(R,S,m,w,allowed) report['resource_certificates']={'canonical_cells':ccount,'max_gain_error':cerr,'max_switch_root_residual':derr,'max_discrete_concavity_excess':concavity_violation,'winding_neighbor_checks':1200} # Defect integrals, split at the exact switching angles of the certificate. diffs=[];positive=[] for R,S,m,u,t,M,L,cs in defect_cases: vals,vecs=np.linalg.eig(M);x=np.real(vecs[:,int(np.argmax(abs(vals)))]) theta0=math.atan2(-x[1],x[0])%math.pi theta=theta0 for a,h in zip(u,t):theta=advance_phase(theta,float(a),float(h)) n=round((theta-theta0)/math.pi) candidates=[c for c in cs if c.winding==n] assert candidates,('No branch for actual growing eigenline',R,S,m,n) c=candidates[0];eta,xi,z0,z1=dual_parameters(R,c) I=0.;theta=theta0 for a,h in zip(u,t): end=advance_phase(theta,float(a),float(h)) points=[] for z in [z0,z1]: for k in range(-5,n+7): v=z+k*math.pi if theta+1e-12