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 / at1 / model / at1_exact.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 | /* Exact kernel of the literal constraint matrix. |
| 18 | * |
| 19 | * H_total = H_C (x) I_S + I_C (x) H_S + sum_j |E_j><E_j| (x) (v_j . sigma) (AT1_CASE_V1 s3) |
| 20 | * is built term by term with an explicit Kronecker product, as a 2N x 2N matrix of exact |
| 21 | * Gaussian rationals. Nothing about its block structure or its level eigenvectors is assumed: |
| 22 | * the kernel is found by exact Gauss-Jordan elimination of that matrix (rank, free columns, |
| 23 | * one basis vector per free column), the orthogonal projector is formed exactly as |
| 24 | * P_0 = B (B^dagger B)^(-1) B^dagger, and both H_total B = 0 and the kernel dimension are |
| 25 | * re-checked exactly. The closed forms of AT1_SPEC.md sections 2 to 4 are not used here; they |
| 26 | * are what the tests compare the engine with. |
| 27 | * |
| 28 | * Zero test (AT1_CASE_V1 s3): Psi = P_0 (|t_r> (x) psi_0) = N^(-1/2) sum_j w_j y_j with |
| 29 | * |w_j| = 1 and y_j = P_0 (|E_j> (x) psi_0) exact. When the nonzero y_j have pairwise disjoint |
| 30 | * supports, Psi = 0 exactly iff every y_j = 0. Disjointness is checked, not assumed; it always |
| 31 | * holds for a clock-diagonal V, and its failure is reported as an internal error. */ |
| 32 | #include "at1_model.h" |
| 33 | |
| 34 | #include <math.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | typedef struct { int r, c; gq **e; } mat; /* row major; NULL entry = exact zero */ |
| 39 | |
| 40 | static mat mat_new(int r, int c) |
| 41 | { |
| 42 | mat m = { r, c, at1_xcalloc((size_t)r * (size_t)c, sizeof(gq *)) }; |
| 43 | return m; |
| 44 | } |
| 45 | static void ent_free(gq *x) { if (x) { gq_free(x); free(x); } } |
| 46 | static void mat_free(mat *m) |
| 47 | { |
| 48 | if (!m->e) return; |
| 49 | for (int i = 0; i < m->r * m->c; i++) ent_free(m->e[i]); |
| 50 | free(m->e); m->e = NULL; |
| 51 | } |
| 52 | static gq **at(mat *m, int i, int j) { return &m->e[i * m->c + j]; } |
| 53 | static const gq *get(const mat *m, int i, int j) { return m->e[i * m->c + j]; } |
| 54 | |
| 55 | /* *slot += x (x exact); keeps exact zeros as NULL */ |
| 56 | static void ent_add(gq **slot, const gq *x) |
| 57 | { |
| 58 | if (!x || gq_is_zero(x)) return; |
| 59 | if (!*slot) { *slot = at1_xmalloc(sizeof(gq)); gq_init(*slot); gq_copy(*slot, x); return; } |
| 60 | gq_add(*slot, *slot, x); |
| 61 | if (gq_is_zero(*slot)) { ent_free(*slot); *slot = NULL; } |
| 62 | } |
| 63 | static void ent_set(gq **slot, const gq *x) { ent_free(*slot); *slot = NULL; ent_add(slot, x); } |
| 64 | |
| 65 | static void gq_set_rat(gq *g, const bq *re, const bq *im) |
| 66 | { |
| 67 | if (re) bq_copy(&g->re, re); else bq_set_i64(&g->re, 0, 1); |
| 68 | if (im) bq_copy(&g->im, im); else bq_set_i64(&g->im, 0, 1); |
| 69 | } |
| 70 | static void gq_set_int(gq *g, int64_t re, int64_t im) { bq_set_i64(&g->re, re, 1); bq_set_i64(&g->im, im, 1); } |
| 71 | |
| 72 | /* C = A (x) B */ |
| 73 | static mat kron(const mat *A, const mat *B) |
| 74 | { |
| 75 | mat C = mat_new(A->r * B->r, A->c * B->c); |
| 76 | gq t; gq_init(&t); |
| 77 | for (int i = 0; i < A->r; i++) for (int j = 0; j < A->c; j++) { |
| 78 | const gq *a = get(A, i, j); |
| 79 | if (!a) continue; |
| 80 | for (int k = 0; k < B->r; k++) for (int l = 0; l < B->c; l++) { |
| 81 | const gq *b = get(B, k, l); |
| 82 | if (!b) continue; |
| 83 | gq_mul(&t, a, b); |
| 84 | ent_add(at(&C, i * B->r + k, j * B->c + l), &t); |
| 85 | } |
| 86 | } |
| 87 | gq_free(&t); |
| 88 | return C; |
| 89 | } |
| 90 | /* A += B */ |
| 91 | static void mat_addto(mat *A, const mat *B) |
| 92 | { |
| 93 | for (int i = 0; i < A->r * A->c; i++) if (B->e[i]) ent_add(&A->e[i], B->e[i]); |
| 94 | } |
| 95 | /* A * s (exact scalar) into a new matrix */ |
| 96 | static mat mat_scale(const mat *A, const gq *s) |
| 97 | { |
| 98 | mat C = mat_new(A->r, A->c); |
| 99 | gq t; gq_init(&t); |
| 100 | for (int i = 0; i < A->r * A->c; i++) if (A->e[i]) { gq_mul(&t, A->e[i], s); ent_set(&C.e[i], &t); } |
| 101 | gq_free(&t); |
| 102 | return C; |
| 103 | } |
| 104 | static mat mat_mul(const mat *A, const mat *B) |
| 105 | { |
| 106 | mat C = mat_new(A->r, B->c); |
| 107 | gq t; gq_init(&t); |
| 108 | for (int i = 0; i < A->r; i++) for (int k = 0; k < A->c; k++) { |
| 109 | const gq *a = get(A, i, k); |
| 110 | if (!a) continue; |
| 111 | for (int j = 0; j < B->c; j++) { |
| 112 | const gq *b = get(B, k, j); |
| 113 | if (!b) continue; |
| 114 | gq_mul(&t, a, b); |
| 115 | ent_add(at(&C, i, j), &t); |
| 116 | } |
| 117 | } |
| 118 | gq_free(&t); |
| 119 | return C; |
| 120 | } |
| 121 | static mat mat_adjoint(const mat *A) |
| 122 | { |
| 123 | mat C = mat_new(A->c, A->r); |
| 124 | gq t; gq_init(&t); |
| 125 | for (int i = 0; i < A->r; i++) for (int j = 0; j < A->c; j++) { |
| 126 | const gq *a = get(A, i, j); |
| 127 | if (a) { gq_conj(&t, a); ent_set(at(&C, j, i), &t); } |
| 128 | } |
| 129 | gq_free(&t); |
| 130 | return C; |
| 131 | } |
| 132 | static void mat_set_int(mat *m, int i, int j, int64_t re, int64_t im) |
| 133 | { |
| 134 | gq t; gq_init(&t); gq_set_int(&t, re, im); ent_set(at(m, i, j), &t); gq_free(&t); |
| 135 | } |
| 136 | |
| 137 | /* Gauss-Jordan to reduced row echelon form, in place. pivcol[r] = pivot column of row r. |
| 138 | * Returns the rank. Exact: a pivot is any exactly nonzero entry. */ |
| 139 | static int rref(mat *A, int *pivcol) |
| 140 | { |
| 141 | int rank = 0; |
| 142 | gq inv, t, one; gq_init(&inv); gq_init(&t); gq_init(&one); gq_set_int(&one, 1, 0); |
| 143 | for (int c = 0; c < A->c && rank < A->r; c++) { |
| 144 | int p = -1; |
| 145 | for (int i = rank; i < A->r; i++) if (get(A, i, c)) { p = i; break; } |
| 146 | if (p < 0) continue; |
| 147 | if (p != rank) for (int j = 0; j < A->c; j++) { gq *s = *at(A, p, j); *at(A, p, j) = *at(A, rank, j); *at(A, rank, j) = s; } |
| 148 | gq_div(&inv, &one, get(A, rank, c)); |
| 149 | for (int j = 0; j < A->c; j++) if (get(A, rank, j)) gq_mul(*at(A, rank, j), get(A, rank, j), &inv); |
| 150 | for (int i = 0; i < A->r; i++) { |
| 151 | if (i == rank || !get(A, i, c)) continue; |
| 152 | gq f; gq_init(&f); gq_copy(&f, get(A, i, c)); |
| 153 | for (int j = 0; j < A->c; j++) { |
| 154 | const gq *rj = get(A, rank, j); |
| 155 | if (!rj) continue; |
| 156 | gq_mul(&t, &f, rj); gq_neg(&t, &t); |
| 157 | ent_add(at(A, i, j), &t); |
| 158 | } |
| 159 | gq_free(&f); |
| 160 | } |
| 161 | pivcol[rank++] = c; |
| 162 | } |
| 163 | gq_free(&inv); gq_free(&t); gq_free(&one); |
| 164 | return rank; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void at1_kernel_free(at1_kernel *k) |
| 169 | { |
| 170 | free(k->h_re); free(k->h_im); free(k->y_re); free(k->y_im); free(k->owner); |
| 171 | memset(k, 0, sizeof *k); |
| 172 | } |
| 173 | |
| 174 | at1_status at1_kernel_build(const at1_case *c, at1_kernel *k) |
| 175 | { |
| 176 | memset(k, 0, sizeof *k); |
| 177 | const int N = c->clock_dim, D = 2 * N; |
| 178 | at1_status st = AT1_OK; |
| 179 | gq t; gq_init(&t); |
| 180 | |
| 181 | /* single-factor matrices */ |
| 182 | mat HC = mat_new(N, N), IN = mat_new(N, N), I2 = mat_new(2, 2); |
| 183 | mat X = mat_new(2, 2), Y = mat_new(2, 2), Z = mat_new(2, 2); |
| 184 | for (int j = 0; j < N; j++) { gq_set_rat(&t, &c->energies[j], NULL); ent_set(at(&HC, j, j), &t); mat_set_int(&IN, j, j, 1, 0); } |
| 185 | mat_set_int(&I2, 0, 0, 1, 0); mat_set_int(&I2, 1, 1, 1, 0); |
| 186 | mat_set_int(&X, 0, 1, 1, 0); mat_set_int(&X, 1, 0, 1, 0); |
| 187 | mat_set_int(&Y, 0, 1, 0, -1); mat_set_int(&Y, 1, 0, 0, 1); |
| 188 | mat_set_int(&Z, 0, 0, 1, 0); mat_set_int(&Z, 1, 1, -1, 0); |
| 189 | const mat *pauli[3] = { &X, &Y, &Z }; |
| 190 | |
| 191 | /* H_S = h0 I + hx X + hy Y + hz Z */ |
| 192 | mat HS = mat_new(2, 2); |
| 193 | { gq_set_rat(&t, &c->h0, NULL); mat s = mat_scale(&I2, &t); mat_addto(&HS, &s); mat_free(&s); } |
| 194 | for (int a = 0; a < 3; a++) { gq_set_rat(&t, &c->h[a], NULL); mat s = mat_scale(pauli[a], &t); mat_addto(&HS, &s); mat_free(&s); } |
| 195 | |
| 196 | /* H_total = H_C (x) I + I (x) H_S + sum_j |E_j><E_j| (x) v_j.sigma */ |
| 197 | mat H = kron(&HC, &I2); |
| 198 | { mat s = kron(&IN, &HS); mat_addto(&H, &s); mat_free(&s); } |
| 199 | for (int j = 0; j < N; j++) { |
| 200 | mat Pj = mat_new(N, N), Vj = mat_new(2, 2); |
| 201 | mat_set_int(&Pj, j, j, 1, 0); |
| 202 | for (int a = 0; a < 3; a++) { gq_set_rat(&t, &c->v[j][a], NULL); mat s = mat_scale(pauli[a], &t); mat_addto(&Vj, &s); mat_free(&s); } |
| 203 | mat s = kron(&Pj, &Vj); mat_addto(&H, &s); |
| 204 | mat_free(&s); mat_free(&Pj); mat_free(&Vj); |
| 205 | } |
| 206 | |
| 207 | /* exact nullspace */ |
| 208 | mat R = mat_new(D, D); |
| 209 | for (int i = 0; i < D * D; i++) if (H.e[i]) ent_set(&R.e[i], H.e[i]); |
| 210 | int *pivcol = at1_xmalloc((size_t)D * sizeof(int)); |
| 211 | int rank = rref(&R, pivcol); |
| 212 | int dk = D - rank; |
| 213 | k->dim = D; k->kernel_dim = dk; |
| 214 | int *is_piv = at1_xcalloc((size_t)D, sizeof(int)); |
| 215 | for (int r = 0; r < rank; r++) is_piv[pivcol[r]] = 1; |
| 216 | mat B = mat_new(D, dk > 0 ? dk : 1); |
| 217 | { |
| 218 | int col = 0; |
| 219 | for (int f = 0; f < D; f++) { |
| 220 | if (is_piv[f]) continue; |
| 221 | mat_set_int(&B, f, col, 1, 0); |
| 222 | for (int r = 0; r < rank; r++) { |
| 223 | const gq *x = get(&R, r, f); |
| 224 | if (x) { gq_neg(&t, x); ent_set(at(&B, pivcol[r], col), &t); } |
| 225 | } |
| 226 | col++; |
| 227 | } |
| 228 | } |
| 229 | mat P0 = mat_new(D, D); |
| 230 | if (dk > 0) { |
| 231 | /* re-check H_total B = 0 exactly */ |
| 232 | mat HB = mat_mul(&H, &B); |
| 233 | for (int i = 0; i < HB.r * HB.c; i++) if (HB.e[i]) st = AT1_ERR_INTERNAL; |
| 234 | mat_free(&HB); |
| 235 | /* P_0 = B (B^dagger B)^(-1) B^dagger */ |
| 236 | mat Bd = mat_adjoint(&B); |
| 237 | mat G = mat_mul(&Bd, &B); |
| 238 | mat Aug = mat_new(dk, 2 * dk); |
| 239 | for (int i = 0; i < dk; i++) { |
| 240 | for (int j = 0; j < dk; j++) if (get(&G, i, j)) ent_set(at(&Aug, i, j), get(&G, i, j)); |
| 241 | mat_set_int(&Aug, i, dk + i, 1, 0); |
| 242 | } |
| 243 | int *pc = at1_xmalloc((size_t)dk * sizeof(int)); |
| 244 | int gr = rref(&Aug, pc); |
| 245 | if (gr != dk) st = AT1_ERR_INTERNAL; /* Gram matrix of a basis is invertible */ |
| 246 | for (int r = 0; r < gr && st == AT1_OK; r++) if (pc[r] != r) st = AT1_ERR_INTERNAL; |
| 247 | free(pc); |
| 248 | mat Gi = mat_new(dk, dk); |
| 249 | for (int i = 0; i < dk; i++) for (int j = 0; j < dk; j++) if (get(&Aug, i, dk + j)) ent_set(at(&Gi, i, j), get(&Aug, i, dk + j)); |
| 250 | mat W = mat_mul(&B, &Gi); |
| 251 | mat_free(&P0); |
| 252 | P0 = mat_mul(&W, &Bd); |
| 253 | mat_free(&Bd); mat_free(&G); mat_free(&Aug); mat_free(&Gi); mat_free(&W); |
| 254 | } |
| 255 | |
| 256 | /* exact zero test and the number of clock levels carrying Psi */ |
| 257 | { |
| 258 | int *owner = at1_xmalloc((size_t)D * sizeof(int)); |
| 259 | for (int i = 0; i < D; i++) owner[i] = -1; |
| 260 | k->y_re = at1_xcalloc((size_t)D, sizeof(double)); k->y_im = at1_xcalloc((size_t)D, sizeof(double)); |
| 261 | int L = 0; |
| 262 | gq y, prod; gq_init(&y); gq_init(&prod); |
| 263 | for (int j = 0; j < N && st == AT1_OK; j++) { |
| 264 | int nonzero = 0; |
| 265 | for (int i = 0; i < D; i++) { |
| 266 | bq_set_i64(&y.re, 0, 1); bq_set_i64(&y.im, 0, 1); |
| 267 | for (int s = 0; s < 2; s++) { |
| 268 | const gq *p = get(&P0, i, 2 * j + s); |
| 269 | if (!p) continue; |
| 270 | gq_mul(&prod, p, &c->psi0[s]); |
| 271 | gq_add(&y, &y, &prod); |
| 272 | } |
| 273 | if (gq_is_zero(&y)) continue; |
| 274 | nonzero = 1; |
| 275 | if (owner[i] >= 0) st = AT1_ERR_INTERNAL; /* supports overlap: not clock-diagonal */ |
| 276 | owner[i] = j; |
| 277 | k->y_re[i] = bq_to_double(&y.re); k->y_im[i] = bq_to_double(&y.im); /* rounded once */ |
| 278 | } |
| 279 | L += nonzero; |
| 280 | } |
| 281 | gq_free(&y); gq_free(&prod); |
| 282 | k->owner = owner; |
| 283 | k->levels_in_psi = L; |
| 284 | k->psi_zero = (L == 0); |
| 285 | } |
| 286 | |
| 287 | /* binary64 copies, each entry correctly rounded from its exact value */ |
| 288 | k->h_re = at1_xcalloc((size_t)D * D, sizeof(double)); k->h_im = at1_xcalloc((size_t)D * D, sizeof(double)); |
| 289 | for (int i = 0; i < D * D; i++) { |
| 290 | if (H.e[i]) { k->h_re[i] = bq_to_double(&H.e[i]->re); k->h_im[i] = bq_to_double(&H.e[i]->im); } |
| 291 | } |
| 292 | /* hmax = max_i sum over columns l carrying Psi of |H_total[i,l]|: the row sums of |H| that act on |
| 293 | * Psi, which is what the rounding of H_total Psi_hat and the error of Psi are multiplied by */ |
| 294 | k->hmax = 0.0; |
| 295 | for (int i = 0; i < D; i++) { |
| 296 | double s = 0.0; |
| 297 | for (int l = 0; l < D; l++) if (k->owner[l] >= 0) s += hypot(k->h_re[i * D + l], k->h_im[i * D + l]); |
| 298 | if (s > k->hmax) k->hmax = s; |
| 299 | } |
| 300 | |
| 301 | free(pivcol); free(is_piv); |
| 302 | mat_free(&HC); mat_free(&IN); mat_free(&I2); mat_free(&X); mat_free(&Y); mat_free(&Z); |
| 303 | mat_free(&HS); mat_free(&H); mat_free(&R); mat_free(&B); mat_free(&P0); |
| 304 | gq_free(&t); |
| 305 | if (st != AT1_OK) at1_kernel_free(k); |
| 306 | return st; |
| 307 | } |