File size: 9,171 Bytes
9755170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""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<v<end-1e-12:points.append(v)
        I+=quad(lambda z:resource_defect(z,float(a),R,eta,xi),theta,end,points=sorted(points),epsabs=1e-10,epsrel=1e-10,limit=200)[0]
        theta=end
    diffs.append(abs(I-(c.log_gain-L)));positive.append(I)
assert max(diffs)<2e-7
report['resource_defect']={'cases':len(diffs),'max_absolute_error':max(diffs),'min_nonnegative_defect':min(positive)}

# Fixed-mean square-root sharpness, exact symbolic Taylor coefficients.
a=sp.symbols('a',real=True)
E=lambda w,h:sp.Matrix([[sp.cos(w*h),sp.sin(w*h)/w],[-w*sp.sin(w*h),sp.cos(w*h)]])
P=E(1,sp.pi/2-a)*E(2,a)*E(1,a)*E(2,sp.pi/4-a)
D=sp.trigsimp(sp.trace(P)/2)
assert sp.simplify(D.subs(a,0)+sp.Rational(5,4))==0
assert sp.simplify(sp.diff(D,a,2).subs(a,0)/2-sp.Rational(9,4))==0
sharp=[]
for z in [1e-2,3e-3,1e-3,3e-4]:
    Pn=monodromy([4,1,4,1],[math.pi/4-z,z,z,math.pi/2-z])
    loss=math.log(2)-log_spectral_radius(Pn)
    sharp.append({'a':z,'loss_over_a_squared':loss/(z*z),'exact_min_mean_L1':6*z/(3*math.pi/4)})
report['resource_sharpness']={'D_a2':'9/4','gain_loss_a2':'3','samples':sharp}

# General rectangle theorem, including fixed-period equality and pointwise selector.
boxes=[Rectangle(1,2,1,3),Rectangle(.7,2.5,.4,4),Rectangle(1,1,1,4),Rectangle(1,3,2,2)]
rcount=0;rw=-math.inf;re=0.;point_violation=0.;rdata=[]
for box in boxes:
    for eta in [-10.,-2.,-.1,0.,.1,2.,10.]:
        cell=rectangle_cell(box,eta);P=cell_monodromy(cell)
        re=max(re,abs(log_spectral_radius(P)-cell.log_gain))
        for x,y,aa,bb,h in cell.arcs:
            theta=(x+y)/2;c,s=math.cos(theta),math.sin(theta)
            H=lambda a,b:((b-a)*s*c-eta)/(b*c*c+a*s*s)
            best=H(aa,bb)
            for av in [box.amin,box.amax]:
                for bv in [box.bmin,box.bmax]:point_violation=max(point_violation,H(av,bv)-best)
    for S in np.linspace(.3,7,14):
        F,cs=rectangle_fixed(box,float(S))
        for n,c in cs:
            re=max(re,abs(log_spectral_radius(np.linalg.matrix_power(cell_monodromy(c),n))-n*c.log_gain))
        for j in range(25):
            N=int(rng.integers(2,15));ts=rng.dirichlet(np.ones(N))*S;P=np.eye(2)
            for h in ts:
                aa=rng.uniform(box.amin,box.amax);bb=rng.uniform(box.bmin,box.bmax)
                P=generator_transfer(aa,bb,float(h))@P
            L=log_spectral_radius(P);rw=max(rw,L-F);rcount+=1
            assert L<=F+2e-8
    fr=rectangle_free(box)
    rdata.append({'bounds':[box.amin,box.amax,box.bmin,box.bmax],'free_eta':fr.eta,'period':fr.period,'log_gain':fr.log_gain})
assert re<1e-8 and point_violation<1e-10
report['rectangle']={'random_profiles':rcount,'max_bound_excess':rw,'max_equality_error':re,'max_selector_excess':point_violation,'free_examples':rdata}

# Quantum exact powers and Euclidean singular squeezing, all three regimes.
qc=0;qe=0.;qnorm=0.;parerr=0.
for j in range(300):
    Nlayers=int(rng.integers(1,10));u=rng.uniform(1,4,Nlayers);t=rng.dirichlet(np.ones(Nlayers))*rng.uniform(.1,4)
    P=monodromy(u,t);omega=float(rng.uniform(.6,2.))
    al,be=bogoliubov(P,omega)
    assert abs(abs(al)**2-abs(be)**2-1)<1e-10
    for N in [1,2,3,5,8]:
        PN=np.linalg.matrix_power(P,N);bn=bogoliubov(PN,omega)[1]
        expected=photon_number(P,N,omega);direct=abs(bn)**2
        qe=max(qe,abs(expected-direct)/(1+direct))
        W=np.diag([math.sqrt(omega),1/math.sqrt(omega)])
        normlog=math.log(np.linalg.norm(W@PN@np.linalg.inv(W),2))
        qnorm=max(qnorm,abs(normlog-squeezing(P,N,omega)));qc+=1
assert qe<1e-8 and qnorm<1e-8
for sign in [-1,1]:
    P=sign*np.array([[1.,.7],[0.,1.]])
    for N in [1,3,10,40]:
        parerr=max(parerr,abs(photon_number(P,N)-N*N*photon_number(P,1)))
M0=sp.diag(-2,-sp.Rational(1,2));X=E(2,sp.pi/8);Mt=sp.simplify(X*M0*X.inv())
beta2=lambda M:sp.simplify(((M[0,0]-M[1,1])**2+(M[1,0]+M[0,1])**2)/4)
assert beta2(M0)==sp.Rational(9,16) and beta2(Mt)==sp.Rational(225,256)
report['quantum']={'power_checks':qc,'max_relative_photon_error':qe,'max_squeezing_norm_error':qnorm,'max_parabolic_error':parerr,'translation_counterexample':['9/16','225/256']}

# Reproducible public design point.
R=2.;S=3*math.pi/4;m=2.
F,cs=resource_optimum(R,S,m);c=cs[0];dual=dual_parameters(R,c);fr=resource_free(R,m)
report['design_example']={'R':R,'S':S,'mean':m,'fixed_log_gain':F,'high':c.high_time,'low':c.low_time,'dual_eta':dual[0],'dual_xi':dual[1],'free_period':fr.high_time+fr.low_time,'free_rate':fr.log_gain/(fr.high_time+fr.low_time),'gap_interval':resource_gap_interval(R,m)}
report['elapsed_seconds']=time.time()-start
report['all_assertions_passed']=True
(ROOT/'results').mkdir(exist_ok=True)
(ROOT/'results'/'verification_v2.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps(report,indent=2))