Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/research/atemporal/at1/model/at1_numeric.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / research / atemporal / at1 / model / at1_numeric.c` 277 lines ยท 12299 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 /* binary64 evaluation of the AT-1 quantities (AT1_RESULT_V1 section 3) from the exact kernel. 18 * 19 * |t_k> = N^(-1/2) sum_j exp(-2 pi i E_j k tau) |E_j> turn count E_j k tau reduced 20 * exactly modulo 1 (int64) first 21 * Psi = P_0 (|t_r> (x) psi_0) = sum_j y_j y_j = P_0 (|E_j> (x) psi_0) exact 22 * (at1_exact.c), rounded once 23 * constraint_residual = || H_total Psi_hat ||_2 literal D x D product, V included 24 * povm_residual = || sum_k w |t_k> 42 #include 43 #include 44 #include 45 46 #define AT1_EPS 1.`1102230246251565404e`-16 /* 2^-53 */ 47 static const double TWO_PI = 6.`283185307179586476925286766559005768`; 48 49 typedef struct { double re, im; } cplx; 50 static cplx c_mul(cplx a, cplx b) { cplx r = { a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re }; return r; } 51 static cplx c_add(cplx a, cplx b) { cplx r = { a.re + b.re, a.im + b.im }; return r; } 52 static cplx c_conj(cplx a) { cplx r = { a.re, -a.im }; return r; } 53 static double c_abs2(cplx a) { return a.re * a.re + a.im * a.im; } 54 55 void at1_f64_format(double x, char *buf, size_t cap) 56 { 57 if (!isfinite(x)) { snprintf(buf, cap, "nonfinite"); return; } 58 if (x == 0.0) x = 0.0; /* -0.0 is written as +0.0 */ 59 uint64_t bits; memcpy(&bits, &x, sizeof bits); 60 snprintf(buf, cap, "f64:%016llx", (unsigned long long)bits); 61 } 62 63 /* smallest N@k >= x with at most 7 significant digits, verified exactly */ 64 static void bound_scaled(double x, uint64_t *N, unsigned *k) 65 { 66 if (!(x > 0.0)) { *N = 0; *k = 0; return; } 67 int kk = (int)ceil(6.0 - log10(x)); 68 if (kk < 0) kk = 0; 69 if (kk > AT1_SCALED_K_MAX) kk = AT1_SCALED_K_MAX; 70 double y = floor(x * pow(10.0, kk)) - 2.0; /* start below, step up to the minimum */ 71 uint64_t n = y > 0.0 ? (uint64_t)y : 0; 72 bq ex, sc; bn nb; bq_init(&ex); bq_init(&sc); bn_init(&nb); 73 bq_from_double(&ex, x); 74 for (;;) { /* exact check N / 10^k >= x */ 75 bn_set_u64(&nb, n); bq_from_scaled(&sc, &nb, (unsigned)kk); 76 if (bq_cmp(&sc, &ex) >= 0) break; 77 n++; 78 } 79 bq_free(&ex); bq_free(&sc); bn_free(&nb); 80 while (kk > 0 && n % 10 == 0) { n /= 10; kk--; } 81 *N = n; *k = (unsigned)kk; 82 } 83 84 void at1_bound_format(double x, char *buf, size_t cap) 85 { 86 uint64_t n; unsigned k; 87 bound_scaled(x, &n, &k); 88 snprintf(buf, cap, "%llu@%u", (unsigned long long)n, k); 89 } 90 91 at1_label_status at1_status_rule(double p, double bound, const at1_scaled *tol_zero) 92 { 93 uint64_t n; unsigned k; 94 bound_scaled(bound, &n, &k); 95 bq P, B, T, s; bn nb; bq_init(&P); bq_init(&B); bq_init(&T); bq_init(&s); bn_init(&nb); 96 bq_from_double(&P, p); 97 bn_set_u64(&nb, n); bq_from_scaled(&B, &nb, k); 98 bq_from_scaled(&T, &tol_zero->n, tol_zero->k); 99 at1_label_status st; 100 bq_add(&s, &P, &B); 101 if (bq_cmp(&s, &T) <= 0) st = AT1_LABEL_UNDEFINED; 102 else { bq_sub(&s, &P, &B); st = bq_cmp(&s, &T) > 0 ? AT1_LABEL_DEFINED : AT1_LABEL_INDETERMINATE; } 103 bq_free(&P); bq_free(&B); bq_free(&T); bq_free(&s); bn_free(&nb); 104 return st; 105 } 106 107 static int rat_i64(const bq *q, int64_t *n, int64_t *d) 108 { 109 return bn_fits_i64(&q->num, n) && bn_fits_i64(&q->den, d); 110 } 111 112 /* exp(-2 pi i E k tau) with the turn count reduced exactly to [-1/2, 1/2) */ 113 static int clock_phase(const bq *E, int k, const bq *tau, cplx *out) 114 { 115 int64_t en, ed, tn, td; 116 if (!rat_i64(E, &en, &ed) || !rat_i64(tau, &tn, &td)) return 0; 117 /* in-limit tokens: |en|, ed, tn, td <= 2^20 and k <= 255, so |num| < 2^48, den <= 2^40 */ 118 int64_t num = en * (int64_t)k * tn, den = ed * td; 119 int64_t m = num % den; 120 if (m < 0) m += den; 121 if (2 * m >= den) m -= den; 122 double ang = TWO_PI * ((double)m / (double)den); 123 out->re = cos(ang); out->im = -sin(ang); 124 return 1; 125 } 126 127 /* pairwise sum of a[lo..hi) (real), for the Frobenius norm over N^2 entries */ 128 static double pair_sum_r(const double *a, int lo, int hi) 129 { 130 if (hi - lo == 1) return a[lo]; 131 if (hi - lo == 0) return 0.0; 132 int mid = lo + (hi - lo) / 2; 133 return pair_sum_r(a, lo, mid) + pair_sum_r(a, mid, hi); 134 } 135 136 /* pairwise sum of a[lo..hi) (complex), for the POVM sum over labels */ 137 static cplx pair_sum(const cplx *a, int lo, int hi) 138 { 139 if (hi - lo == 1) return a[lo]; 140 if (hi - lo == 0) { cplx z = { 0, 0 }; return z; } 141 int mid = lo + (hi - lo) / 2; 142 return c_add(pair_sum(a, lo, mid), pair_sum(a, mid, hi)); 143 } 144 145 at1_status at1_compute(const at1_case *c, const at1_kernel *K, int reversed, at1_values *out) 146 { 147 memset(out, 0, sizeof *out); 148 const int N = c->clock_dim, D = 2 * N, M = c->label_count, r = c->ref_index; 149 const double w = bq_to_double(&c->weight); 150 const double inv_sqrt_n = 1.0 / sqrt((double)N); 151 out->kernel_dim = K->kernel_dim; 152 out->M = M; 153 out->trivial = K->kernel_dim == 0 || K->psi_zero; 154 155 /* clock states */ 156 cplx *tk = at1_xmalloc((size_t)M * (size_t)N * sizeof(cplx)); 157 for (int k = 0; k < M; k++) for (int j = 0; j < N; j++) { 158 cplx ph; 159 if (!clock_phase(&c->energies[j], k, &c->tau, &ph)) { free(tk); return AT1_ERR_INTERNAL; } 160 ph.re *= inv_sqrt_n; ph.im *= inv_sqrt_n; 161 tk[k * N + j] = ph; 162 } 163 164 /* povm_residual (independent of Psi) */ 165 { 166 cplx *terms = at1_xmalloc((size_t)M * sizeof(cplx)); 167 double *sq = at1_xmalloc((size_t)N * (size_t)N * sizeof(double)); 168 for (int j = 0; j < N; j++) for (int l = 0; l < N; l++) { 169 for (int k = 0; k < M; k++) { 170 cplx t = c_mul(tk[k * N + j], c_conj(tk[k * N + l])); 171 terms[k].re = w * t.re; terms[k].im = w * t.im; 172 } 173 cplx s = pair_sum(terms, 0, M); 174 if (j == l) s.re -= 1.0; 175 sq[j * N + l] = c_abs2(s); 176 } 177 free(terms); 178 out->povm.value = sqrt(pair_sum_r(sq, 0, N * N)); /* pairwise: N^2 terms, log depth */ 179 free(sq); 180 int lg = 0; while ((1 << lg) < M) lg++; 181 int ln = 0; while ((1 << ln) < N) ln++; 182 out->povm.bound = 2.0 * ((lg + 8) * AT1_EPS * (M * w + 1.0) + (2 * ln + 4) * AT1_EPS * out->povm.value); 183 } 184 185 if (out->trivial) { 186 out->constraint.undefined = 1; 187 for (int k = 0; k < M; k++) { 188 out->status[k] = AT1_LABEL_UNDEFINED; 189 out->clock[k].undefined = 1; 190 for (int s = 0; s < 6; s++) out->pauli[k][s].undefined = 1; 191 } 192 free(tk); 193 return AT1_OK; 194 } 195 196 /* Psi = P_0 (|t_r> (x) psi_0) */ 197 cplx *Psi = at1_xmalloc((size_t)D * sizeof(cplx)); 198 /* Psi = P_0 (|t_r> (x) psi_0) = sum_j P_0 (|E_j> (x) psi_0) (linearity): entry i is the 199 * clock amplitude of its level times the exact Y_i rounded once. No sum, so no cancellation: 200 * every entry has relative error at most about 8 eps (Y_i, cos and sin, 1/sqrt(N), one complex 201 * product); delta = 16 eps bounds ||Psi_c - Psi|| / ||Psi|| with a safety factor 2. */ 202 double norm2 = 0.0; 203 for (int i = 0; i < D; i++) { 204 if (K->owner[i] < 0) { Psi[i].re = 0.0; Psi[i].im = 0.0; continue; } 205 cplx yi = { K->y_re[i], K->y_im[i] }; 206 Psi[i] = c_mul(tk[r * N + K->owner[i]], yi); 207 norm2 += c_abs2(Psi[i]); 208 } 209 const double delta = 16.0 * AT1_EPS; 210 211 /* constraint_residual = || H_total Psi_hat || */ 212 { 213 double inv = 1.0 / sqrt(norm2), res2 = 0.0; 214 for (int i = 0; i < D; i++) { 215 cplx acc = { 0, 0 }; 216 for (int l = 0; l < D; l++) { 217 cplx h = { K->h_re[i * D + l], K->h_im[i * D + l] }; 218 if (h.re == 0.0 && h.im == 0.0) continue; 219 cplx ph = { Psi[l].re * inv, Psi[l].im * inv }; 220 acc = c_add(acc, c_mul(h, ph)); 221 } 222 res2 += c_abs2(acc); 223 } 224 out->constraint.value = sqrt(res2); 225 out->constraint.bound = 2.0 * (K->hmax * delta + 8.0 * AT1_EPS * (K->hmax + 1.0)) + 4.0 * AT1_EPS * out->constraint.value; 226 } 227 228 /* per label: phi_k, p(k), status, Pauli outcomes */ 229 static const cplx proj[6][2][2] = { 230 { { { 0.5, 0 }, { 0.5, 0 } }, { { 0.5, 0 }, { 0.5, 0 } } }, /* X+ */ 231 { { { 0.5, 0 }, { -0.5, 0 } }, { { -0.5, 0 }, { 0.5, 0 } } }, /* X- */ 232 { { { 0.5, 0 }, { 0, -0.5 } }, { { 0, 0.5 }, { 0.5, 0 } } }, /* Y+ */ 233 { { { 0.5, 0 }, { 0, 0.5 } }, { { 0, -0.5 }, { 0.5, 0 } } }, /* Y- */ 234 { { { 1, 0 }, { 0, 0 } }, { { 0, 0 }, { 0, 0 } } }, /* Z+ */ 235 { { { 0, 0 }, { 0, 0 } }, { { 0, 0 }, { 1, 0 } } } /* Z- */ 236 }; 237 const double L = K->levels_in_psi > 0 ? K->levels_in_psi : 1; 238 const double cc = L + 12.0; 239 const double eta = sqrt(L / N) * (delta + cc * AT1_EPS); /* |Delta phi_k| / ||Psi|| */ 240 for (int step = 0; step < M; step++) { 241 int k = reversed ? M - 1 - step : step; 242 cplx phi[2]; 243 for (int s = 0; s < 2; s++) { 244 cplx acc = { 0, 0 }; 245 for (int j = 0; j < N; j++) { 246 cplx v = Psi[2 * j + s]; 247 if (v.re == 0.0 && v.im == 0.0) continue; 248 acc = c_add(acc, c_mul(c_conj(tk[k * N + j]), v)); 249 } 250 phi[s] = acc; 251 } 252 double n2 = c_abs2(phi[0]) + c_abs2(phi[1]); 253 double p = w * n2 / norm2; 254 double pp = p > 0.0 ? p : 0.0; 255 out->clock[k].value = p; 256 out->clock[k].bound = 2.0 * (2.0 * sqrt(w * pp) * eta + (2.0 * delta + (2.0 * L + 12.0) * AT1_EPS) * pp) + 4.0 * w * eta * eta; 257 out->status[k] = at1_status_rule(p, out->clock[k].bound, &c->tol_zero); 258 if (out->status[k] != AT1_LABEL_DEFINED) { 259 for (int s = 0; s < 6; s++) out->pauli[k][s].undefined = 1; 260 continue; 261 } 262 cplx rho[2][2]; 263 for (int a = 0; a < 2; a++) for (int b = 0; b < 2; b++) { 264 cplx t = c_mul(phi[a], c_conj(phi[b])); 265 rho[a][b].re = t.re / n2; rho[a][b].im = t.im / n2; 266 } 267 double bP = 2.0 * (2.0 * eta * sqrt(w / pp) + 16.0 * AT1_EPS); 268 for (int s = 0; s < 6; s++) { 269 cplx tr = { 0, 0 }; 270 for (int a = 0; a < 2; a++) for (int b = 0; b < 2; b++) tr = c_add(tr, c_mul(rho[a][b], proj[s][b][a])); 271 out->pauli[k][s].value = tr.re; 272 out->pauli[k][s].bound = bP; 273 } 274 } 275 free(tk); free(Psi); 276 return AT1_OK; 277 }