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_engine.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 | /* Engine orchestration: builds every quantity of AT0_RESULT_V1 section 3 from |
| 18 | * the case alone, attaches ESTIMATED error bounds (README section "Error |
| 19 | * estimates"), and decides label status with exact integer comparisons. */ |
| 20 | #include "at0_model.h" |
| 21 | #include <math.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #define EPS 1.1102230246251565e-16 /* 2^-53 */ |
| 25 | |
| 26 | static double fmax_d(double a, double b) { return a > b ? a : b; } |
| 27 | |
| 28 | static at0_status bound_from(double est, at0_scaled *out) |
| 29 | { |
| 30 | if (!isfinite(est) || est < 0) return AT0_ERR_NONFINITE; |
| 31 | return at0_scaled_from_double_ceil(est, out); |
| 32 | } |
| 33 | |
| 34 | static at0_status run(const at0_case *c, at0_engine_result *out, int reversed) |
| 35 | { |
| 36 | if (!c || !out) return AT0_ERR_ARGUMENT; |
| 37 | memset(out, 0, sizeof *out); |
| 38 | out->bound_kind = AT0_BOUND_ESTIMATED; |
| 39 | out->label_count = c->label_count; |
| 40 | at0_status st; |
| 41 | |
| 42 | at0_system_hamiltonian hs; |
| 43 | if ((st = at0_hamiltonian_system(c, &hs)) != AT0_OK) return st; |
| 44 | at0_kernel ker; |
| 45 | if ((st = at0_constraint_kernel(c, &hs, &ker)) != AT0_OK) return st; |
| 46 | out->kernel_dim = ker.kernel_dim; |
| 47 | at0_state psi; |
| 48 | if ((st = at0_constraint_physical_state(c, &hs, &ker, &psi)) != AT0_OK) return st; |
| 49 | double psi_norm2; |
| 50 | if ((st = at0_state_norm2(&psi, &psi_norm2)) != AT0_OK) return st; |
| 51 | out->psi_nonzero = psi_norm2 > 0.0; |
| 52 | |
| 53 | double n = (double)c->clock_dim, m = (double)c->label_count; |
| 54 | double w = at0_rat_to_double(c->povm_weight); |
| 55 | double hmax = fmax_d(fabs(at0_rat_to_double(c->clock_energies[0])), |
| 56 | fabs(at0_rat_to_double(c->clock_energies[c->clock_dim - 1]))) |
| 57 | + fabs(at0_rat_to_double(c->h0)) + at0_rat_to_double(c->h_norm); |
| 58 | |
| 59 | if (out->psi_nonzero) { |
| 60 | if ((st = at0_constraint_residual(c, &hs, &psi, &out->constraint_residual)) != AT0_OK) return st; |
| 61 | if ((st = bound_from(32.0 * EPS * n * (hmax + 1.0), &out->constraint_residual_bound)) != AT0_OK) return st; |
| 62 | } else { |
| 63 | out->constraint_residual = 0.0; |
| 64 | out->constraint_residual_bound.n = 0; out->constraint_residual_bound.k = 0; |
| 65 | } |
| 66 | if ((st = at0_povm_residual(c, &out->povm_residual)) != AT0_OK) return st; |
| 67 | if ((st = bound_from(16.0 * EPS * n * (m * w + 1.0), &out->povm_residual_bound)) != AT0_OK) return st; |
| 68 | |
| 69 | for (int i = 0; i < c->label_count; i++) { |
| 70 | int k = reversed ? c->label_count - 1 - i : i; |
| 71 | at0_label_values *lv = &out->label[k]; |
| 72 | memset(lv, 0, sizeof *lv); |
| 73 | if (!out->psi_nonzero) { lv->status = AT0_LABEL_UNDEFINED; continue; } |
| 74 | at0_conditional cond; |
| 75 | if ((st = at0_conditional_compute(c, &psi, psi_norm2, k, &cond)) != AT0_OK) return st; |
| 76 | lv->clock_probability = cond.clock_probability; |
| 77 | if ((st = bound_from(32.0 * EPS * (n + 4.0) * (cond.clock_probability + 1.0), &lv->clock_probability_bound)) != AT0_OK) return st; |
| 78 | int le, gt; |
| 79 | if ((st = at0_exact_prob_status(lv->clock_probability, lv->clock_probability_bound, c->tol_zero_probability, &le, >)) != AT0_OK) return st; |
| 80 | lv->status = le ? AT0_LABEL_UNDEFINED : (gt ? AT0_LABEL_DEFINED : AT0_LABEL_INDETERMINATE); |
| 81 | if (lv->status != AT0_LABEL_DEFINED) continue; |
| 82 | if (!cond.defined_numeric) return AT0_ERR_INTERNAL; /* DEFINED implies ||phi|| > 0 */ |
| 83 | for (int ax = 0; ax < 3; ax++) { |
| 84 | for (int sg = 0; sg < 2; sg++) { |
| 85 | if ((st = at0_observable_probability(&cond.rho, (at0_axis)ax, (at0_sign)sg, &lv->pauli[ax][sg])) != AT0_OK) return st; |
| 86 | if ((st = bound_from(32.0 * EPS * (n + 8.0), &lv->pauli_bound[ax][sg])) != AT0_OK) return st; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return AT0_OK; |
| 91 | } |
| 92 | |
| 93 | at0_status at0_engine_run(const at0_case *c, at0_engine_result *out) { return run(c, out, 0); } |
| 94 | at0_status at0_engine_run_reversed(const at0_case *c, at0_engine_result *out) { return run(c, out, 1); } |