interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / research / atemporal / at0 / evaluator / src / at0e_shadow.c

178 lines · 8956 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Shadow oracle: an independent derivation of every AT-0 quantity from the case
18 * alone (AT0_CASE_V1 section 3), used to falsify engine and reference values.
19 *
20 * Exact part (int128 rationals): eigenvalues e_s = h0 +/- |h|, eigenvectors with
21 * complex-rational components, kernel pairs, Psi != 0, exact POVM identity test.
22 * Numeric part (long double, 113-bit mantissa on this host): the conditional
23 * state phi_k, clock probabilities, Pauli probabilities, Schrodinger reference,
24 * POVM residual. Angles are reduced modulo one turn exactly in rationals before
25 * any transcendental call. The stated bound is ESTIMATED, not proven.
26 *
27 * Derivation used (hand, from the contract):
28 * |t_k> = N^-1/2 sum_j exp(-2 pi i E_j k tau) |E_j>
29 * Psi = P0 (|t_r> (x) |psi0>) = sum_{(j,s): E_j + e_s = 0} <E_j|t_r> <e_s|psi0> |E_j>|e_s>
30 * phi_k = (<t_k| (x) I) Psi = (1/N) sum_{s matched} exp(-2 pi i e_s (k - r) tau) <e_s|psi0> |e_s>
31 * which equals Schrodinger evolution of psi0 over t_k - t_r restricted to the
32 * matched eigencomponents. The reference uses all eigencomponents.
33 */
34#include "at0e.h"
35#include <float.h>
36#include <math.h>
37#include <string.h>
38
39typedef struct { long double re, im; } cld;
40static cld cmk(long double re, long double im) { cld z = { re, im }; return z; }
41static cld cadd(cld a, cld b) { return cmk(a.re + b.re, a.im + b.im); }
42static cld cmul(cld a, cld b) { return cmk(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re); }
43static cld cconj(cld a) { return cmk(a.re, -a.im); }
44static long double cabs2(cld a) { return a.re * a.re + a.im * a.im; }
45static cld cscale(cld a, long double s) { return cmk(a.re * s, a.im * s); }
46static long double i128_ld(i128 v) { int neg = v < 0; u128 u = neg ? (u128)0 - (u128)v : (u128)v; long double r = (long double)(unsigned long long)(u >> 64) * 18446744073709551616.0L + (long double)(unsigned long long)u; return neg ? -r : r; }
47static long double rat_ld(rat a) { return i128_ld(a.n) / i128_ld(a.d); }
48static cld crat_ld(crat z) { return cmk(rat_ld(z.re), rat_ld(z.im)); }
49/* exp(-2 pi i theta) with theta a rational, reduced mod 1 exactly first */
50static cld expm2pi(rat theta) {
51 rat t = rat_mod1(theta);
52 if (rat_is_zero(t)) return cmk(1.0L, 0.0L);
53 /* exact quarter turns avoid rounding on the common ideal-clock grid */
54 if (t.d == 4) { if (t.n == 1) return cmk(0.0L, -1.0L); if (t.n == 3) return cmk(0.0L, 1.0L); }
55 if (t.d == 2) return cmk(-1.0L, 0.0L);
56 long double a = 2.0L * 3.14159265358979323846264338327950288L * rat_ld(t);
57 return cmk(cosl(a), -sinl(a));
58}
59
60/* eigenvectors (unnormalized complex rational); index 0 = e_+ = h0 + r, 1 = e_- = h0 - r */
61static void eigvecs(const at0_case *c, crat v[2][2], rat e[2]) {
62 rat r = c->radius;
63 e[0] = rat_add(c->h0, r); e[1] = rat_sub(c->h0, r);
64 if (rat_is_zero(r)) {
65 v[0][0].re = rat_make(1, 1); v[0][0].im = rat_make(0, 1); v[0][1].re = rat_make(0, 1); v[0][1].im = rat_make(0, 1);
66 v[1][0].re = rat_make(0, 1); v[1][0].im = rat_make(0, 1); v[1][1].re = rat_make(1, 1); v[1][1].im = rat_make(0, 1);
67 return;
68 }
69 /* v_+ = (hz + r, hx + i hy); if zero, (hx - i hy, r - hz) */
70 rat a = rat_add(c->hz, r);
71 if (rat_is_zero(a) && rat_is_zero(c->hx) && rat_is_zero(c->hy)) {
72 v[0][0].re = c->hx; v[0][0].im = rat_neg(c->hy); v[0][1].re = rat_sub(r, c->hz); v[0][1].im = rat_make(0, 1);
73 } else {
74 v[0][0].re = a; v[0][0].im = rat_make(0, 1); v[0][1].re = c->hx; v[0][1].im = c->hy;
75 }
76 /* v_- = (hz - r, hx + i hy); if zero, (hx - i hy, -r - hz) */
77 rat b = rat_sub(c->hz, r);
78 if (rat_is_zero(b) && rat_is_zero(c->hx) && rat_is_zero(c->hy)) {
79 v[1][0].re = c->hx; v[1][0].im = rat_neg(c->hy); v[1][1].re = rat_sub(rat_neg(r), c->hz); v[1][1].im = rat_make(0, 1);
80 } else {
81 v[1][0].re = b; v[1][0].im = rat_make(0, 1); v[1][1].re = c->hx; v[1][1].im = c->hy;
82 }
83}
84/* <v|psi0> exactly: conj(v0) psi0_0 + conj(v1) psi0_1 */
85static crat inner_exact(const crat v[2], const crat psi[2]) {
86 crat acc = { rat_make(0, 1), rat_make(0, 1) };
87 for (int i = 0; i < 2; i++) {
88 rat re = rat_add(rat_mul(v[i].re, psi[i].re), rat_mul(v[i].im, psi[i].im));
89 rat im = rat_sub(rat_mul(v[i].re, psi[i].im), rat_mul(v[i].im, psi[i].re));
90 acc.re = rat_add(acc.re, re); acc.im = rat_add(acc.im, im);
91 }
92 return acc;
93}
94static void pauli_probs(cld a, cld b, long double out[3][2]) {
95 long double n2 = cabs2(a) + cabs2(b);
96 cld ab = cmul(cconj(a), b);
97 long double x = 2.0L * ab.re, y = 2.0L * ab.im, z = cabs2(a) - cabs2(b);
98 out[0][0] = (n2 + x) / (2.0L * n2); out[0][1] = (n2 - x) / (2.0L * n2);
99 out[1][0] = (n2 + y) / (2.0L * n2); out[1][1] = (n2 - y) / (2.0L * n2);
100 out[2][0] = (n2 + z) / (2.0L * n2); out[2][1] = (n2 - z) / (2.0L * n2);
101}
102
103int shadow_compute(const at0_case *c, shadow *s, const char **why) {
104 memset(s, 0, sizeof *s);
105 *why = NULL;
106 if (LDBL_MANT_DIG < 64) { *why = "long double narrower than 64 bits; shadow refuses"; return 0; }
107 /* stated ESTIMATED bound: 1e-20 needs the 113-bit binary128 long double (aarch64); a 64-bit mantissa (x86-64) gets 1e-14 */
108 s->bound = LDBL_MANT_DIG >= 113 ? 1e-20L : 1e-14L;
109
110 crat v[2][2]; rat e[2];
111 eigvecs(c, v, e);
112 for (int si = 0; si < 2; si++) {
113 rat neg = rat_neg(e[si]);
114 s->matched[si] = 0;
115 for (int j = 0; j < c->N; j++) if (rat_cmp(c->E[j], neg) == 0) { s->matched[si] = 1; break; }
116 }
117 s->kernel_dim = s->matched[0] + s->matched[1];
118 crat cs[2]; int cs_zero[2];
119 for (int si = 0; si < 2; si++) { cs[si] = inner_exact(v[si], c->psi0); cs_zero[si] = crat_is_zero(cs[si]); }
120 s->psi_nonzero = (s->matched[0] && !cs_zero[0]) || (s->matched[1] && !cs_zero[1]);
121
122 /* exact POVM identity test */
123 s->povm_exact_identity = rat_cmp(c->w, rat_make(c->N, c->M)) == 0;
124 for (int j = 0; j < c->N && s->povm_exact_identity; j++)
125 for (int jj = j + 1; jj < c->N; jj++) {
126 rat D = rat_sub(c->E[jj], c->E[j]);
127 rat dt = rat_mul(D, c->tau);
128 rat dtm = rat_mul(dt, rat_make(c->M, 1));
129 if (rat_is_int(dt) || !rat_is_int(dtm)) { s->povm_exact_identity = 0; break; }
130 }
131 /* numeric POVM residual: Frobenius norm of w sum_k |t_k><t_k| - I */
132 {
133 long double fro = 0.0L, wn = rat_ld(c->w) / (long double)c->N;
134 for (int j = 0; j < c->N; j++)
135 for (int jj = 0; jj < c->N; jj++) {
136 rat D = rat_sub(c->E[j], c->E[jj]);
137 cld sum = cmk(0.0L, 0.0L);
138 for (int k = 0; k < c->M; k++) sum = cadd(sum, expm2pi(rat_mul(rat_mul(D, rat_make(k, 1)), c->tau)));
139 cld m = cscale(sum, wn);
140 if (j == jj) m.re -= 1.0L;
141 fro += cabs2(m);
142 }
143 s->povm_residual = sqrtl(fro);
144 }
145 /* norms and states */
146 long double vn2[2]; cld vl[2][2], csl[2];
147 for (int si = 0; si < 2; si++) {
148 vl[si][0] = crat_ld(v[si][0]); vl[si][1] = crat_ld(v[si][1]);
149 vn2[si] = cabs2(vl[si][0]) + cabs2(vl[si][1]);
150 csl[si] = crat_ld(cs[si]);
151 }
152 long double psi_norm2 = 0.0L;
153 for (int si = 0; si < 2; si++) if (s->matched[si]) psi_norm2 += cabs2(csl[si]) / vn2[si];
154 psi_norm2 /= (long double)c->N;
155 long double psi0_n2 = cabs2(crat_ld(c->psi0[0])) + cabs2(crat_ld(c->psi0[1]));
156 (void)psi0_n2;
157
158 for (int k = 0; k < c->M; k++) {
159 cld phi[2] = { cmk(0, 0), cmk(0, 0) }, ref[2] = { cmk(0, 0), cmk(0, 0) };
160 rat dk = rat_make(k - c->ref_index, 1);
161 for (int si = 0; si < 2; si++) {
162 cld ph = expm2pi(rat_mul(rat_mul(e[si], dk), c->tau));
163 cld coef = cscale(cmul(ph, csl[si]), 1.0L / vn2[si]);
164 for (int i = 0; i < 2; i++) {
165 cld term = cmul(coef, vl[si][i]);
166 ref[i] = cadd(ref[i], term);
167 if (s->matched[si]) phi[i] = cadd(phi[i], cscale(term, 1.0L / (long double)c->N));
168 }
169 }
170 s->clock_p_exact_zero[k] = !s->psi_nonzero;
171 if (s->psi_nonzero) {
172 s->clock_p[k] = rat_ld(c->w) * (cabs2(phi[0]) + cabs2(phi[1])) / psi_norm2;
173 pauli_probs(phi[0], phi[1], s->pauli[k]);
174 }
175 pauli_probs(ref[0], ref[1], s->reference[k]);
176 }
177 return 1;
178}