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_model.h
| 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 | /* AT-0 candidate relational quantum engine (Agent 3, research model). |
| 18 | * |
| 19 | * Implements the finite Page-Wootters universe of AT0_CASE_V1 / AT0_RESULT_V1 |
| 20 | * (aien-architecture docs/plans/atemporal/ at commit 044c9d1): a clock of |
| 21 | * dimension N, one qubit, the constraint H_total = H_C (x) I + I (x) H_S, a |
| 22 | * covariant discrete clock POVM, and conditional qubit statistics read off by |
| 23 | * the Born rule after a clock measurement. |
| 24 | * |
| 25 | * Rules this header enforces by construction: |
| 26 | * - no wall-clock, CPU time, GPU, random or scheduling input anywhere; |
| 27 | * - the clock phase is a declared measurement parameter (label k, tau); |
| 28 | * - no hidden mutable global state: every function takes explicit objects; |
| 29 | * - every failure is an explicit at0_status, never a silent default; |
| 30 | * - every array index is bounds-checked against the case limits below. |
| 31 | * |
| 32 | * Portable C11 (plus __int128 through __extension__ for exact rationals). |
| 33 | */ |
| 34 | #ifndef AT0_MODEL_H |
| 35 | #define AT0_MODEL_H |
| 36 | |
| 37 | #include <complex.h> |
| 38 | #include <stddef.h> |
| 39 | #include <stdint.h> |
| 40 | #include <stdio.h> |
| 41 | |
| 42 | /* ---- contract limits (AT0_CASE_V1 sections 2 and 3) ---------------------- */ |
| 43 | #define AT0_CLOCK_DIM_MIN 2 |
| 44 | #define AT0_CLOCK_DIM_MAX 64 |
| 45 | #define AT0_LABEL_MAX 256 |
| 46 | #define AT0_SYSTEM_DIM 2 |
| 47 | #define AT0_RATIONAL_LIMIT 1048576LL /* contract limit on written tokens */ |
| 48 | #define AT0_RAT_REPR_LIMIT 4611686018427387904LL /* 2^62: representability of derived rationals */ |
| 49 | #define AT0_SCALED_K_MAX 40 |
| 50 | #define AT0_INT_SATURATED 999999999999999999LL /* parsed value of any token longer than 18 digits */ |
| 51 | #define AT0_LABEL_LEN 32 |
| 52 | #define AT0_NAME_LEN 64 |
| 53 | #define AT0_DIGEST_HEX 64 |
| 54 | #define AT0_LINE_MAX 4096 |
| 55 | |
| 56 | /* ---- status codes --------------------------------------------------------- */ |
| 57 | typedef enum { |
| 58 | AT0_OK = 0, |
| 59 | /* case refusal codes, AT0_RESULT_V1 section 5, in contract order */ |
| 60 | AT0_CASE_PARSE_ERROR, |
| 61 | AT0_CASE_NONCANONICAL, |
| 62 | AT0_CASE_UNSUPPORTED_VERSION, |
| 63 | AT0_CASE_INVALID_PARAMETER, |
| 64 | AT0_CASE_IRRATIONAL_SPECTRUM, |
| 65 | AT0_CASE_ID_MISMATCH, |
| 66 | /* engine errors (never silent) */ |
| 67 | AT0_ERR_ARGUMENT, /* NULL pointer, index out of range, bad enum */ |
| 68 | AT0_ERR_OVERFLOW, /* exact arithmetic would overflow its limits */ |
| 69 | AT0_ERR_NONFINITE, /* a computed binary64 is NaN or infinite */ |
| 70 | AT0_ERR_IO, /* read or write failure */ |
| 71 | AT0_ERR_INTERNAL |
| 72 | } at0_status; |
| 73 | |
| 74 | const char *at0_status_name(at0_status s); |
| 75 | int at0_status_is_refusal(at0_status s); |
| 76 | |
| 77 | /* ---- exact arithmetic ----------------------------------------------------- */ |
| 78 | __extension__ typedef __int128 at0_i128; |
| 79 | |
| 80 | typedef struct { int64_t n; int64_t d; } at0_rat; /* reduced, d >= 1 */ |
| 81 | typedef struct { at0_rat re, im; } at0_crat; /* complex rational */ |
| 82 | typedef struct { uint64_t n; int k; } at0_scaled; /* n / 10^k */ |
| 83 | |
| 84 | at0_status at0_rat_make(int64_t n, int64_t d, at0_rat *out); /* reduces, checks limits */ |
| 85 | at0_status at0_rat_add(at0_rat a, at0_rat b, at0_rat *out); |
| 86 | at0_status at0_rat_sub(at0_rat a, at0_rat b, at0_rat *out); |
| 87 | at0_status at0_rat_mul(at0_rat a, at0_rat b, at0_rat *out); |
| 88 | int at0_rat_cmp(at0_rat a, at0_rat b); /* exact sign of a-b */ |
| 89 | int at0_rat_is_zero(at0_rat a); |
| 90 | int at0_rat_in_limits(at0_rat a); /* contract limit |n|, d <= 2^20 */ |
| 91 | int at0_scaled_in_limits(at0_scaled s); /* contract limit k <= 40 */ |
| 92 | double at0_rat_to_double(at0_rat a); /* one correctly-rounded division */ |
| 93 | /* exact test: is a a square of a rational? if yes, *root is the nonnegative root */ |
| 94 | at0_status at0_rat_sqrt_exact(at0_rat a, at0_rat *root, int *is_square); |
| 95 | /* overflow-free exact helpers for in-limit case data (see at0_exact.c): */ |
| 96 | int at0_i128_mul(at0_i128 a, at0_i128 b, at0_i128 *out); /* 1 on success, 0 on overflow */ |
| 97 | int at0_i128_add(at0_i128 a, at0_i128 b, at0_i128 *out); |
| 98 | at0_status at0_rat_norm_exact(at0_rat hx, at0_rat hy, at0_rat hz, at0_rat *norm, int *is_square); /* |h| */ |
| 99 | at0_status at0_exact_norm_plus_hz(at0_rat hx, at0_rat hy, at0_rat hz, at0_rat h_norm, |
| 100 | at0_i128 *A, at0_i128 *D); /* |h| + hz = A / D, D = dx dy dz */ |
| 101 | at0_status at0_exact_sum_is_zero(int count, const at0_i128 *num, const at0_i128 *den, int *zero); |
| 102 | /* reduce x into [-1/2, 1/2) exactly: x - round(x) */ |
| 103 | at0_status at0_rat_reduce_turn(at0_rat x, at0_rat *out); |
| 104 | |
| 105 | /* parse / print canonical tokens; parsing refuses non-canonical text */ |
| 106 | at0_status at0_rat_parse(const char *tok, at0_rat *out); |
| 107 | at0_status at0_crat_parse(const char *tok, at0_crat *out); |
| 108 | at0_status at0_scaled_parse(const char *tok, at0_scaled *out); |
| 109 | int at0_rat_format(at0_rat a, char *buf, size_t cap); |
| 110 | int at0_crat_format(at0_crat a, char *buf, size_t cap); |
| 111 | int at0_scaled_format(at0_scaled s, char *buf, size_t cap); |
| 112 | |
| 113 | /* exact comparison of a finite nonnegative binary64 p against scaled decimals: |
| 114 | * sets *le = (p + b <= tol), *gt = (p - b > tol), computed with exact integers */ |
| 115 | at0_status at0_exact_prob_status(double p, at0_scaled b, at0_scaled tol, int *le, int *gt); |
| 116 | |
| 117 | /* binary64 -> smallest-ish scaled decimal >= x (x finite, >= 0); ESTIMATED use only */ |
| 118 | at0_status at0_scaled_from_double_ceil(double x, at0_scaled *out); |
| 119 | |
| 120 | /* ---- case (versioned input) ----------------------------------------------- */ |
| 121 | typedef enum { AT0_CTRL_POSITIVE, AT0_CTRL_NEGATIVE } at0_control_kind; |
| 122 | typedef enum { AT0_EXPECT_PASS, AT0_EXPECT_FAIL } at0_expected_outcome; |
| 123 | typedef enum { AT0_BOUND_NONE = 0, AT0_BOUND_ESTIMATED = 1, AT0_BOUND_RIGOROUS = 2 } at0_bound_kind; |
| 124 | |
| 125 | #define AT0_FAILURE_CODE_COUNT 10 |
| 126 | extern const char *const at0_failure_codes[AT0_FAILURE_CODE_COUNT]; /* sorted bytewise */ |
| 127 | |
| 128 | typedef struct { |
| 129 | char name[AT0_NAME_LEN + 1]; |
| 130 | /* semantic block */ |
| 131 | int clock_dim; /* N */ |
| 132 | at0_rat clock_energies[AT0_CLOCK_DIM_MAX]; /* strictly increasing */ |
| 133 | at0_rat h0, hx, hy, hz; |
| 134 | char reference_clock_label[AT0_LABEL_LEN + 1]; |
| 135 | at0_crat psi0[AT0_SYSTEM_DIM]; |
| 136 | at0_rat povm_tau_turns; |
| 137 | at0_rat povm_weight; |
| 138 | int label_count; /* M */ |
| 139 | char labels[AT0_LABEL_MAX][AT0_LABEL_LEN + 1]; |
| 140 | /* acceptance block */ |
| 141 | at0_control_kind control_kind; |
| 142 | at0_expected_outcome expected_outcome; |
| 143 | int expected_failure_count; /* 0 means token "none" */ |
| 144 | int expected_failure_idx[AT0_FAILURE_CODE_COUNT]; |
| 145 | at0_bound_kind min_bound_kind; |
| 146 | at0_scaled tol_constraint_residual, tol_povm_residual, tol_probability, |
| 147 | tol_zero_probability, tol_schrodinger; |
| 148 | /* identities as read from the file, and recomputed */ |
| 149 | char case_id[AT0_DIGEST_HEX + 1]; |
| 150 | char acceptance_id[AT0_DIGEST_HEX + 1]; |
| 151 | char case_file_sha256[AT0_DIGEST_HEX + 1]; |
| 152 | /* derived exact data filled by validation */ |
| 153 | int reference_index; /* index r of reference label */ |
| 154 | at0_rat h_norm; /* |h| rational */ |
| 155 | } at0_case; |
| 156 | |
| 157 | /* Parse and validate AT0_CASE_V1 bytes (rules 1..6 in order). On refusal returns |
| 158 | * the refusal status and leaves *out unspecified. */ |
| 159 | at0_status at0_case_parse(const uint8_t *bytes, size_t len, at0_case *out); |
| 160 | /* Emit the canonical case text (byte-exact round trip). Returns bytes written or -1. */ |
| 161 | long at0_case_emit(const at0_case *c, char *buf, size_t cap); |
| 162 | /* Compute case_id / acceptance_id from a parsed case into 64-hex buffers. */ |
| 163 | at0_status at0_case_identities(const at0_case *c, char case_id[AT0_DIGEST_HEX + 1], |
| 164 | char acceptance_id[AT0_DIGEST_HEX + 1]); |
| 165 | at0_status at0_case_read_file(const char *path, at0_case *out); |
| 166 | |
| 167 | /* ---- quantum state representation ---------------------------------------- */ |
| 168 | /* The combined Hilbert space is C^N (x) C^2 with clock factor first: |
| 169 | * component index (j, s) -> j * 2 + s. */ |
| 170 | #define AT0_TOTAL_DIM_MAX (AT0_CLOCK_DIM_MAX * AT0_SYSTEM_DIM) |
| 171 | |
| 172 | typedef struct { |
| 173 | int clock_dim; |
| 174 | double complex amp[AT0_TOTAL_DIM_MAX]; |
| 175 | } at0_state; |
| 176 | |
| 177 | typedef struct { double complex v[AT0_SYSTEM_DIM]; } at0_qubit; |
| 178 | typedef struct { double complex m[AT0_SYSTEM_DIM][AT0_SYSTEM_DIM]; } at0_qubit_op; |
| 179 | typedef struct { int clock_dim; double complex v[AT0_CLOCK_DIM_MAX]; } at0_clock_vec; |
| 180 | |
| 181 | at0_status at0_state_init(at0_state *s, int clock_dim); /* zero state */ |
| 182 | at0_status at0_state_get(const at0_state *s, int j, int sys, double complex *out); |
| 183 | at0_status at0_state_set(at0_state *s, int j, int sys, double complex val); |
| 184 | at0_status at0_state_norm2(const at0_state *s, double *out); /* <s|s> */ |
| 185 | at0_status at0_state_inner(const at0_state *a, const at0_state *b, double complex *out); |
| 186 | at0_status at0_state_scale(at0_state *s, double complex z); |
| 187 | at0_status at0_state_check_finite(const at0_state *s); |
| 188 | |
| 189 | /* ---- Hamiltonian construction --------------------------------------------- */ |
| 190 | typedef struct { |
| 191 | double eig[AT0_SYSTEM_DIM]; /* binary64 values of e_0 = h0 - |h|, e_1 = h0 + |h|; the exact |
| 192 | spectrum lives in the case (h0, h_norm) and is what the kernel test uses */ |
| 193 | at0_qubit vec[AT0_SYSTEM_DIM]; /* orthonormal eigenvectors (binary64) */ |
| 194 | int degenerate; /* |h| == 0 */ |
| 195 | at0_qubit_op hs; /* H_S matrix in computational basis */ |
| 196 | } at0_system_hamiltonian; |
| 197 | |
| 198 | at0_status at0_hamiltonian_system(const at0_case *c, at0_system_hamiltonian *out); |
| 199 | /* apply H_total = H_C (x) I + I (x) H_S to a state */ |
| 200 | at0_status at0_hamiltonian_apply_total(const at0_case *c, const at0_system_hamiltonian *hs, |
| 201 | const at0_state *in, at0_state *out); |
| 202 | |
| 203 | /* ---- constraint: kernel projection and verification ----------------------- */ |
| 204 | typedef struct { |
| 205 | int kernel_dim; /* exact count of (j, s) with E_j + e_s = 0 */ |
| 206 | int match_j[AT0_TOTAL_DIM_MAX]; /* matched clock index */ |
| 207 | int match_s[AT0_TOTAL_DIM_MAX]; /* matched system eigen index */ |
| 208 | } at0_kernel; |
| 209 | |
| 210 | at0_status at0_constraint_kernel(const at0_case *c, const at0_system_hamiltonian *hs, at0_kernel *out); |
| 211 | /* |Psi> = P_0 (|t_r> (x) |psi_0>) */ |
| 212 | at0_status at0_constraint_physical_state(const at0_case *c, const at0_system_hamiltonian *hs, |
| 213 | const at0_kernel *k, at0_state *psi); |
| 214 | /* || H_total Psi_hat ||_2 ; requires Psi nonzero */ |
| 215 | at0_status at0_constraint_residual(const at0_case *c, const at0_system_hamiltonian *hs, |
| 216 | const at0_state *psi, double *out); |
| 217 | |
| 218 | /* ---- clock POVM ----------------------------------------------------------- */ |
| 219 | /* |t_k> = N^(-1/2) sum_j exp(-2 pi i E_j k tau) |E_j>, phase reduced exactly mod one turn */ |
| 220 | at0_status at0_povm_clock_state(const at0_case *c, int k, at0_clock_vec *out); |
| 221 | /* || sum_k w |t_k><t_k| - I ||_F */ |
| 222 | at0_status at0_povm_residual(const at0_case *c, double *out); |
| 223 | |
| 224 | /* ---- conditional subsystem state ------------------------------------------ */ |
| 225 | typedef struct { |
| 226 | at0_qubit phi; /* (<t_k| (x) I) Psi, unnormalized */ |
| 227 | double phi_norm2; /* ||phi||^2 */ |
| 228 | double clock_probability; /* w ||phi||^2 / ||Psi||^2 */ |
| 229 | int defined_numeric; /* phi_norm2 > 0 so rho exists numerically */ |
| 230 | at0_qubit_op rho; /* phi phi^dagger / ||phi||^2 when defined */ |
| 231 | } at0_conditional; |
| 232 | |
| 233 | at0_status at0_conditional_compute(const at0_case *c, const at0_state *psi, double psi_norm2, |
| 234 | int k, at0_conditional *out); |
| 235 | |
| 236 | /* ---- observables ---------------------------------------------------------- */ |
| 237 | typedef enum { AT0_AXIS_X = 0, AT0_AXIS_Y = 1, AT0_AXIS_Z = 2 } at0_axis; |
| 238 | typedef enum { AT0_SIGN_PLUS = 0, AT0_SIGN_MINUS = 1 } at0_sign; |
| 239 | |
| 240 | at0_status at0_pauli_matrix(at0_axis axis, at0_qubit_op *out); |
| 241 | /* P(sigma = +/-1) = Tr(rho (I +/- sigma)/2), computed directly, not as 1 - other */ |
| 242 | at0_status at0_observable_probability(const at0_qubit_op *rho, at0_axis axis, at0_sign sign, double *out); |
| 243 | |
| 244 | /* ---- engine run and versioned output -------------------------------------- */ |
| 245 | typedef enum { AT0_LABEL_DEFINED, AT0_LABEL_UNDEFINED, AT0_LABEL_INDETERMINATE } at0_label_status; |
| 246 | |
| 247 | typedef struct { |
| 248 | at0_label_status status; |
| 249 | double clock_probability; at0_scaled clock_probability_bound; |
| 250 | double pauli[3][2]; at0_scaled pauli_bound[3][2]; |
| 251 | } at0_label_values; |
| 252 | |
| 253 | typedef struct { |
| 254 | int kernel_dim; |
| 255 | int psi_nonzero; |
| 256 | double constraint_residual; at0_scaled constraint_residual_bound; |
| 257 | double povm_residual; at0_scaled povm_residual_bound; |
| 258 | int label_count; |
| 259 | at0_label_values label[AT0_LABEL_MAX]; |
| 260 | at0_bound_kind bound_kind; /* always ESTIMATED in this engine */ |
| 261 | } at0_engine_result; |
| 262 | |
| 263 | at0_status at0_engine_run(const at0_case *c, at0_engine_result *out); |
| 264 | /* test-only: evaluate the labels in reverse order; must give bit-identical values */ |
| 265 | at0_status at0_engine_run_reversed(const at0_case *c, at0_engine_result *out); |
| 266 | |
| 267 | /* OMEGA-AT0-ENGINE v1 writer (values block identical to AT0_RESULT_V1 section 1 |
| 268 | * minus the oracle's reference lines). Returns bytes written or -1. */ |
| 269 | long at0_engine_emit(const at0_case *c, const at0_engine_result *r, char *buf, size_t cap); |
| 270 | int at0_f64_format(double x, char *buf, size_t cap); /* f64:<16 hex>, -0 folded, nonfinite */ |
| 271 | |
| 272 | #endif /* AT0_MODEL_H */ |