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
omega-mv / research / atemporal / at0 / model / at0_constraint.c
| 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 | /* Hamiltonian constraint: exact kernel of H_total, nullspace projection of the |
| 18 | * reference product state, and the residual || H_total Psi_hat ||. */ |
| 19 | #include "at0_model.h" |
| 20 | #include <math.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | at0_status at0_constraint_kernel(const at0_case *c, const at0_system_hamiltonian *hs, at0_kernel *out) |
| 24 | { |
| 25 | if (!c || !hs || !out) return AT0_ERR_ARGUMENT; |
| 26 | memset(out, 0, sizeof *out); |
| 27 | /* E_j + h0 -/+ |h| == 0 <=> (E_j + h0) == +/-|h|. E_j + h0 has a denominator <= 2^40 and |
| 28 | * |h| is representable, so at0_rat_cmp decides exactly; the eigenvalues themselves |
| 29 | * (reduced denominator up to 2^80) are never materialized as rationals. */ |
| 30 | at0_rat neg_norm = c->h_norm; |
| 31 | neg_norm.n = -neg_norm.n; |
| 32 | for (int j = 0; j < c->clock_dim; j++) { |
| 33 | at0_rat t; |
| 34 | at0_status st = at0_rat_add(c->clock_energies[j], c->h0, &t); |
| 35 | if (st != AT0_OK) return st; |
| 36 | for (int s = 0; s < AT0_SYSTEM_DIM; s++) { |
| 37 | if (at0_rat_cmp(t, s == 0 ? c->h_norm : neg_norm) == 0) { |
| 38 | if (out->kernel_dim >= AT0_TOTAL_DIM_MAX) return AT0_ERR_INTERNAL; |
| 39 | out->match_j[out->kernel_dim] = j; |
| 40 | out->match_s[out->kernel_dim] = s; |
| 41 | out->kernel_dim++; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return AT0_OK; |
| 46 | } |
| 47 | |
| 48 | /* exact test: is <e_s|psi_0> zero? uses the unnormalized rational eigenvector |
| 49 | * (-(hx - i hy), |h| + hz) for s = 0 and (|h| + hz, hx + i hy) for s = 1; |
| 50 | * in the degenerate case the eigenvectors are |0>, |1>. */ |
| 51 | static at0_status overlap_is_zero(const at0_case *c, const at0_system_hamiltonian *hs, int s, int *zero) |
| 52 | { |
| 53 | at0_crat p0 = c->psi0[0], p1 = c->psi0[1]; |
| 54 | if (hs->degenerate) { |
| 55 | at0_crat p = s == 0 ? p0 : p1; |
| 56 | *zero = at0_rat_is_zero(p.re) && at0_rat_is_zero(p.im); |
| 57 | return AT0_OK; |
| 58 | } |
| 59 | /* a = |h| + hz = A / D with D = dx dy dz (unreduced, |A| < 2^62, D <= 2^60) */ |
| 60 | at0_i128 A, D; |
| 61 | at0_status st = at0_exact_norm_plus_hz(c->hx, c->hy, c->hz, c->h_norm, &A, &D); |
| 62 | if (st != AT0_OK) return st; |
| 63 | if (A == 0) { |
| 64 | /* h = (0, 0, -|h|): the eigenvectors are |0> (s = 0) and |1> (s = 1), as in at0_hamiltonian_system */ |
| 65 | at0_crat p = s == 0 ? p0 : p1; |
| 66 | *zero = at0_rat_is_zero(p.re) && at0_rat_is_zero(p.im); |
| 67 | return AT0_OK; |
| 68 | } |
| 69 | /* conj(v_s) . psi0 with v_0 = (-(hx - i hy), a), v_1 = (a, hx + i hy): |
| 70 | * s=0: conj(v_0) = (-(hx + i hy), a) -> X p0 + Y p1 with X = -(hx + i hy), Y = a |
| 71 | * s=1: conj(v_1) = (a, hx - i hy) -> X p0 + Y p1 with X = a, Y = hx - i hy |
| 72 | * Every coefficient is written over the common denominator D: hx = nx (dy dz) / D, |
| 73 | * hy = ny (dx dz) / D, a = A / D, so each coefficient numerator is < 2^62. |
| 74 | * real = Xr p0r - Xi p0i + Yr p1r - Yi p1i, imag = Xr p0i + Xi p0r + Yr p1i + Yi p1r. |
| 75 | * Each part is a sum of four terms (coef / D) * (m / d) with m <= 2^20, d <= 2^20, of which |
| 76 | * at most three have a nonzero coefficient (X or Y is real). at0_exact_sum_is_zero drops the |
| 77 | * zero terms and cross-multiplies: |coef * m * d' * d''| < 2^62 * 2^20 * 2^40 = 2^122 per |
| 78 | * term, three terms < 2^124, within 128-bit signed range; the common factor D cancels. */ |
| 79 | at0_i128 hxn = (at0_i128)c->hx.n * ((at0_i128)c->hy.d * c->hz.d); |
| 80 | at0_i128 hyn = (at0_i128)c->hy.n * ((at0_i128)c->hx.d * c->hz.d); |
| 81 | at0_i128 Xr, Xi, Yr, Yi; |
| 82 | if (s == 0) { Xr = -hxn; Xi = -hyn; Yr = A; Yi = 0; } |
| 83 | else { Xr = A; Xi = 0; Yr = hxn; Yi = -hyn; } |
| 84 | at0_i128 num[4], den[4]; |
| 85 | int zr, zi; |
| 86 | /* real part */ |
| 87 | num[0] = Xr * p0.re.n; den[0] = p0.re.d; |
| 88 | num[1] = -Xi * p0.im.n; den[1] = p0.im.d; |
| 89 | num[2] = Yr * p1.re.n; den[2] = p1.re.d; |
| 90 | num[3] = -Yi * p1.im.n; den[3] = p1.im.d; |
| 91 | if ((st = at0_exact_sum_is_zero(4, num, den, &zr)) != AT0_OK) return st; |
| 92 | /* imaginary part */ |
| 93 | num[0] = Xr * p0.im.n; den[0] = p0.im.d; |
| 94 | num[1] = Xi * p0.re.n; den[1] = p0.re.d; |
| 95 | num[2] = Yr * p1.im.n; den[2] = p1.im.d; |
| 96 | num[3] = Yi * p1.re.n; den[3] = p1.re.d; |
| 97 | if ((st = at0_exact_sum_is_zero(4, num, den, &zi)) != AT0_OK) return st; |
| 98 | *zero = zr && zi; |
| 99 | return AT0_OK; |
| 100 | } |
| 101 | |
| 102 | at0_status at0_constraint_physical_state(const at0_case *c, const at0_system_hamiltonian *hs, |
| 103 | const at0_kernel *k, at0_state *psi) |
| 104 | { |
| 105 | if (!c || !hs || !k || !psi) return AT0_ERR_ARGUMENT; |
| 106 | at0_status st = at0_state_init(psi, c->clock_dim); |
| 107 | if (st != AT0_OK) return st; |
| 108 | at0_clock_vec tr; |
| 109 | if ((st = at0_povm_clock_state(c, c->reference_index, &tr)) != AT0_OK) return st; |
| 110 | double complex psi0[AT0_SYSTEM_DIM]; |
| 111 | for (int s = 0; s < AT0_SYSTEM_DIM; s++) |
| 112 | psi0[s] = at0_rat_to_double(c->psi0[s].re) + I * at0_rat_to_double(c->psi0[s].im); |
| 113 | /* exact decision whether the projected state is the zero vector */ |
| 114 | int all_zero = 1; |
| 115 | for (int m = 0; m < k->kernel_dim; m++) { |
| 116 | int z; |
| 117 | if ((st = overlap_is_zero(c, hs, k->match_s[m], &z)) != AT0_OK) return st; |
| 118 | if (!z) { all_zero = 0; break; } |
| 119 | } |
| 120 | if (all_zero) return AT0_OK; /* Psi = 0 exactly: leave the zero state */ |
| 121 | for (int m = 0; m < k->kernel_dim; m++) { |
| 122 | int j = k->match_j[m], s = k->match_s[m]; |
| 123 | if (j < 0 || j >= c->clock_dim || s < 0 || s >= AT0_SYSTEM_DIM) return AT0_ERR_ARGUMENT; |
| 124 | const at0_qubit *e = &hs->vec[s]; |
| 125 | double complex overlap = conj(e->v[0]) * psi0[0] + conj(e->v[1]) * psi0[1]; /* <e_s|psi_0> */ |
| 126 | double complex coef = tr.v[j] * overlap; /* <E_j|t_r><e_s|psi_0> */ |
| 127 | for (int sp = 0; sp < AT0_SYSTEM_DIM; sp++) { |
| 128 | double complex cur; |
| 129 | if ((st = at0_state_get(psi, j, sp, &cur)) != AT0_OK) return st; |
| 130 | if ((st = at0_state_set(psi, j, sp, cur + coef * e->v[sp])) != AT0_OK) return st; |
| 131 | } |
| 132 | } |
| 133 | return at0_state_check_finite(psi); |
| 134 | } |
| 135 | |
| 136 | at0_status at0_constraint_residual(const at0_case *c, const at0_system_hamiltonian *hs, |
| 137 | const at0_state *psi, double *out) |
| 138 | { |
| 139 | if (!c || !hs || !psi || !out) return AT0_ERR_ARGUMENT; |
| 140 | double n2; |
| 141 | at0_status st = at0_state_norm2(psi, &n2); |
| 142 | if (st != AT0_OK) return st; |
| 143 | if (!(n2 > 0.0)) return AT0_ERR_ARGUMENT; /* residual of the zero state is undefined */ |
| 144 | at0_state h; |
| 145 | if ((st = at0_hamiltonian_apply_total(c, hs, psi, &h)) != AT0_OK) return st; |
| 146 | double hn2; |
| 147 | if ((st = at0_state_norm2(&h, &hn2)) != AT0_OK) return st; |
| 148 | double r = sqrt(hn2 / n2); |
| 149 | if (!isfinite(r)) return AT0_ERR_NONFINITE; |
| 150 | *out = r; |
| 151 | return AT0_OK; |
| 152 | } |