"""Ideal benchmark predictions, not hardware data. Standard-library Python 3.10+.
Bell reference remains untouched. Fixed output only; no postselection.
Uses the separately published conditional Eq8 audit and an explicitly defined
phase-corrected single-excitation exchange channel. Neither simulates gravity.
"""
import importlib.util
from pathlib import Path
import math
import json
p=Path(__file__).with_name('hawking-phase-and-time-audit.py')
spec=importlib.util.spec_from_file_location('bounded_qmm',p)
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)

def choi_from_branches(branches):
    size=len(branches[0]); env=size//2
    return [[sum(branches[i//2][2*k+i%2]*branches[j//2][2*k+j%2].conjugate() for k in range(env)) for j in range(4)] for i in range(4)]

def metrics(r):
    x=[[0,1],[1,0]];y=[[0,-1j],[1j,0]];z=[[1,0],[0,-1]]
    def corr(a):
        return sum(r[i][j]*a[j//2][i//2]*a[j%2][i%2] for i in range(4) for j in range(4)).real
    xx,yy,zz=[corr(a) for a in (x,y,z)]
    witness=(1+xx-yy+zz)/4
    direct=((r[0][0]+r[0][3]+r[3][0]+r[3][3])/2).real
    assert abs(witness-direct)<1e-12
    assert abs(sum(r[i][i] for i in range(4))-1)<1e-12
    return {'XX':xx,'YY':yy,'ZZ':zz,'bell_overlap':witness}

h=1/math.sqrt(2)
qmm=[]
for theta in (0,math.pi/2,math.pi):
    branches=[m.cycle(m.initial([h,0j]),theta,.6),m.cycle(m.initial([0j,h]),theta,.6)]
    v=metrics(choi_from_branches(branches))
    expected=(1+math.sin(theta/2)**2)/4
    assert abs(v['bell_overlap']-expected)<1e-12
    qmm.append({'theta':theta,'predicted_overlap':expected,**v})
exchange=[]
for gt in (0,math.pi/6,math.pi/2):
    # Excitation exchange gives -i*sin(gt) on the output excitation;
    # the fixed output phase correction removes -i. Environment is the memory.
    a=[complex(h),0j,0j,0j]
    b=[0j,complex(h*math.sin(gt)),complex(h*math.cos(gt)),0j]
    v=metrics(choi_from_branches([a,b]))
    expected=(1+math.sin(gt))**2/4
    assert abs(v['bell_overlap']-expected)<1e-12
    exchange.append({'gamma_t':gt,'predicted_overlap':expected,**v})
swap=metrics(choi_from_branches([m.swap_input_output(m.initial([h,0j])),m.swap_input_output(m.initial([0j,h]))]))
# Global access control: undo the entire Eq8 unitary, then SWAP to output.
theta,phi=.7,.6
columns=[m.cycle([complex(i==j) for i in range(8)],theta,phi) for j in range(8)]
def recover(v):
    w=m.cycle(v,theta,phi)
    return m.swap_input_output([sum(columns[j][k].conjugate()*w[k] for k in range(8)) for j in range(8)])
global_control=metrics(choi_from_branches([recover(m.initial([h,0j])),recover(m.initial([0j,h]))]))
assert abs(global_control['bell_overlap']-1)<1e-12
assert abs(swap['bell_overlap']-1)<1e-12
shots,alpha,bias=4096,.01,.02
# Hoeffding +/-1 observables, union bound over three settings.
epsilon=math.sqrt(2*math.log(6/alpha)/shots)
radius=3*epsilon/4
print(json.dumps({'scope':'Ideal predictions and proposed finite-shot rule; no executed hardware experiment','qmm_output_only':qmm,'phase_corrected_exchange':exchange,'swap_control':swap,'global_access_inverse_then_swap':global_control,'statistics':{'shots_per_setting':shots,'primary_settings':3,'alpha':alpha,'assumed_maximum_witness_bias':bias,'correlator_radius':epsilon,'witness_statistical_radius':radius,'required_observed_overlap_strictly_above':.5+radius+bias,'bias_status':'Acceptance budget only, not an experimentally established calibration bound','assumptions':'Independent bounded shots, fixed candidate and decoder, trustworthy reference and measurement calibration, no postselection or adaptive stopping','total_shots_per_arm':3*shots},'identifiability_limit':'An output-only witness cannot distinguish information destroyed from information retained only in inaccessible registers.'},indent=2))
