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 / evaluator / src / judge.rs
| 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 | //! Exact re-derivation of the AT1_RESULT_V1 section 4 checks and the section 5 |
| 17 | //! outcome, codes and expectation from the written values. Every comparison is |
| 18 | //! on exact rationals (a binary64 value and a scaled decimal are both exact). |
| 19 | |
| 20 | use crate::case::Case; |
| 21 | use crate::model::Status; |
| 22 | use crate::rat::Q; |
| 23 | use crate::result::{Ck, Kind, RVal, ResultFile, Six, Verdict}; |
| 24 | use std::cmp::Ordering; |
| 25 | |
| 26 | pub const CHECK_CODES: [&str; 12] = [ |
| 27 | "BOUND_KIND_INSUFFICIENT", |
| 28 | "NONFINITE_VALUE", |
| 29 | "TRIVIAL_PHYSICAL_STATE", |
| 30 | "CONSTRAINT_RESIDUAL_EXCEEDED", |
| 31 | "POVM_NORMALIZATION_EXCEEDED", |
| 32 | "PROBABILITY_SUM_EXCEEDED", |
| 33 | "PROBABILITY_OUT_OF_RANGE", |
| 34 | "PROBABILITY_SUM_EXCEEDED", |
| 35 | "CONDITIONAL_UNDEFINED", |
| 36 | "", // check 10: depends on prediction_target |
| 37 | "ORACLE_DISAGREEMENT", |
| 38 | "ORACLE_DISAGREEMENT", |
| 39 | ]; |
| 40 | |
| 41 | /// Absolute form: PASS if |v|+b <= tol; FAIL if |v|-b > tol; else INDETERMINATE. |
| 42 | pub fn tri_abs(v: &Q, b: &Q, tol: &Q) -> Ck { |
| 43 | let a = v.abs(); |
| 44 | if a.add(b).cmp(tol) != Ordering::Greater { |
| 45 | Ck::Pass |
| 46 | } else if a.sub(b).cmp(tol) == Ordering::Greater { |
| 47 | Ck::Fail |
| 48 | } else { |
| 49 | Ck::Indet |
| 50 | } |
| 51 | } |
| 52 | /// Signed form (range limits): PASS if v+b <= tol; FAIL if v-b > tol; else INDETERMINATE. |
| 53 | pub fn tri_signed(v: &Q, b: &Q, tol: &Q) -> Ck { |
| 54 | if v.add(b).cmp(tol) != Ordering::Greater { |
| 55 | Ck::Pass |
| 56 | } else if v.sub(b).cmp(tol) == Ordering::Greater { |
| 57 | Ck::Fail |
| 58 | } else { |
| 59 | Ck::Indet |
| 60 | } |
| 61 | } |
| 62 | /// FAIL > INDETERMINATE > PASS; NOT_EVALUATED is neutral. |
| 63 | pub fn worst(a: Ck, b: Ck) -> Ck { |
| 64 | let r = |c: Ck| match c { |
| 65 | Ck::NotEval => 0, |
| 66 | Ck::Pass => 1, |
| 67 | Ck::Indet => 2, |
| 68 | Ck::Fail => 3, |
| 69 | }; |
| 70 | if r(a) >= r(b) { a } else { b } |
| 71 | } |
| 72 | |
| 73 | /// Value and bound of a written token, None for nonfinite or undefined. |
| 74 | fn qv(v: &RVal) -> Option<(Q, Q)> { |
| 75 | v.q().map(|q| (q, v.bq())) |
| 76 | } |
| 77 | |
| 78 | /// Status rule of section 3 applied to a written clock probability. |
| 79 | pub fn label_from(v: &RVal, tol_zero: &Q) -> Status { |
| 80 | match qv(v) { |
| 81 | None => Status::Undefined, |
| 82 | Some((p, b)) => crate::model::status_rule(&p, &b, tol_zero), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn dev_six(a: &Six, b: &Six, tol: &Q, st: &mut Ck) { |
| 87 | for ax in 0..3 { |
| 88 | for g in 0..2 { |
| 89 | if let (Some((x, bx)), Some((y, by))) = (qv(&a[ax][g]), qv(&b[ax][g])) { |
| 90 | *st = worst(*st, tri_abs(&x.sub(&y), &bx.add(&by), tol)); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn all_values(r: &ResultFile) -> Vec<&RVal> { |
| 97 | let mut v: Vec<&RVal> = vec![&r.constraint, &r.povm]; |
| 98 | for k in 0..r.case.m { |
| 99 | v.push(&r.clock[k]); |
| 100 | v.push(&r.ref_clock[k]); |
| 101 | for f in [&r.pauli[k], &r.ref_ideal[k], &r.ref_inter[k]] { |
| 102 | for ax in 0..3 { |
| 103 | for g in 0..2 { |
| 104 | v.push(&f[ax][g]); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | v |
| 110 | } |
| 111 | |
| 112 | /// Re-derives the verdict block of a completed run. `nontrivial` is the exact |
| 113 | /// decision of check 3 (the caller supplies it: from the shadow when verifying, |
| 114 | /// from the engine's own claim when splicing as the runner). |
| 115 | pub fn rederive(r: &ResultFile, nontrivial: bool) -> Verdict { |
| 116 | let c: &Case = &r.case; |
| 117 | let tol_prob = Q::from_scaled(&c.tol_prob); |
| 118 | let tol_schro = Q::from_scaled(&c.tol_schro); |
| 119 | let one = Q::one(); |
| 120 | let mut ck = [Ck::NotEval; 12]; |
| 121 | ck[0] = if r.bound_kind >= c.min_bound_kind { Ck::Pass } else { Ck::Fail }; |
| 122 | ck[1] = if all_values(r).iter().any(|v| v.kind == Kind::Nonfinite) { Ck::Fail } else { Ck::Pass }; |
| 123 | ck[2] = if nontrivial { Ck::Pass } else { Ck::Fail }; |
| 124 | if let Some((v, b)) = qv(&r.povm) { |
| 125 | ck[4] = tri_abs(&v, &b, &Q::from_scaled(&c.tol_povm)); |
| 126 | } |
| 127 | if nontrivial { |
| 128 | if let Some((v, b)) = qv(&r.constraint) { |
| 129 | ck[3] = tri_abs(&v, &b, &Q::from_scaled(&c.tol_constraint)); |
| 130 | } |
| 131 | // 6: sum of every clock probability minus 1, summed bounds |
| 132 | let mut sum = Q::zero(); |
| 133 | let mut bs = Q::zero(); |
| 134 | let mut ok = true; |
| 135 | for k in 0..c.m { |
| 136 | match qv(&r.clock[k]) { |
| 137 | Some((v, b)) => { |
| 138 | sum = sum.add(&v); |
| 139 | bs = bs.add(&b); |
| 140 | } |
| 141 | None => ok = false, |
| 142 | } |
| 143 | } |
| 144 | if ok { |
| 145 | ck[5] = tri_abs(&sum.sub(&one), &bs, &tol_prob); |
| 146 | } |
| 147 | // 7: every engine probability, signed form, both limits (reference lines excluded) |
| 148 | let mut st = Ck::NotEval; |
| 149 | for k in 0..c.m { |
| 150 | let mut items: Vec<&RVal> = vec![&r.clock[k]]; |
| 151 | for ax in 0..3 { |
| 152 | for g in 0..2 { |
| 153 | items.push(&r.pauli[k][ax][g]); |
| 154 | } |
| 155 | } |
| 156 | for it in items { |
| 157 | if let Some((v, b)) = qv(it) { |
| 158 | st = worst(st, tri_signed(&v.neg(), &b, &tol_prob)); |
| 159 | st = worst(st, tri_signed(&v.sub(&one), &b, &tol_prob)); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | ck[6] = st; |
| 164 | // 8: PLUS + MINUS - 1 per defined label and axis |
| 165 | let mut st = Ck::NotEval; |
| 166 | for k in 0..c.m { |
| 167 | if r.label[k] != Status::Defined { |
| 168 | continue; |
| 169 | } |
| 170 | for ax in 0..3 { |
| 171 | if let (Some((p, bp)), Some((q, bq))) = (qv(&r.pauli[k][ax][0]), qv(&r.pauli[k][ax][1])) { |
| 172 | st = worst(st, tri_abs(&p.add(&q).sub(&one), &bp.add(&bq), &tol_prob)); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | ck[7] = st; |
| 177 | // 9: no label UNDEFINED |
| 178 | ck[8] = if r.label.iter().any(|s| *s == Status::Undefined) { Ck::Fail } else { Ck::Pass }; |
| 179 | // 10 and 11: labels DEFINED on both sides only |
| 180 | let wn = c.w.div(&Q::int(c.n as i64)); |
| 181 | let mut st10 = Ck::NotEval; |
| 182 | let mut st11 = Ck::NotEval; |
| 183 | for k in 0..c.m { |
| 184 | if r.label[k] != Status::Defined || r.ref_label[k] != Status::Defined { |
| 185 | continue; |
| 186 | } |
| 187 | let target = if c.target_ideal { &r.ref_ideal[k] } else { &r.ref_inter[k] }; |
| 188 | dev_six(&r.pauli[k], target, &tol_schro, &mut st10); |
| 189 | if let Some((p, bp)) = qv(&r.clock[k]) { |
| 190 | if c.target_ideal { |
| 191 | st10 = worst(st10, tri_abs(&p.sub(&wn), &bp, &tol_prob)); |
| 192 | } else if let Some((m, bm)) = qv(&r.ref_clock[k]) { |
| 193 | st10 = worst(st10, tri_abs(&p.sub(&m), &bp.add(&bm), &tol_prob)); |
| 194 | } |
| 195 | } |
| 196 | if c.target_ideal { |
| 197 | dev_six(&r.pauli[k], &r.ref_inter[k], &tol_schro, &mut st11); |
| 198 | if let (Some((p, bp)), Some((m, bm))) = (qv(&r.clock[k]), qv(&r.ref_clock[k])) { |
| 199 | st11 = worst(st11, tri_abs(&p.sub(&m), &bp.add(&bm), &tol_prob)); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | ck[9] = st10; |
| 204 | ck[10] = if c.target_ideal { st11 } else { Ck::NotEval }; |
| 205 | // 12: label status agreement |
| 206 | let mut st = Ck::NotEval; |
| 207 | for k in 0..c.m { |
| 208 | let (a, b) = (r.label[k], r.ref_label[k]); |
| 209 | let one = if a == Status::Indeterminate || b == Status::Indeterminate { |
| 210 | Ck::Indet |
| 211 | } else if a != b { |
| 212 | Ck::Fail |
| 213 | } else { |
| 214 | Ck::Pass |
| 215 | }; |
| 216 | st = worst(st, one); |
| 217 | } |
| 218 | ck[11] = st; |
| 219 | } |
| 220 | let mut codes: Vec<String> = Vec::new(); |
| 221 | let mut add = |s: &str| { |
| 222 | if !codes.iter().any(|x| x == s) { |
| 223 | codes.push(s.to_string()); |
| 224 | } |
| 225 | }; |
| 226 | for i in 0..12 { |
| 227 | if ck[i] == Ck::Fail { |
| 228 | if i == 9 { |
| 229 | add(if c.target_ideal { "SCHRODINGER_DEVIATION_EXCEEDED" } else { "INTERACTING_DEVIATION_EXCEEDED" }); |
| 230 | } else { |
| 231 | add(CHECK_CODES[i]); |
| 232 | } |
| 233 | } |
| 234 | if ck[i] == Ck::Indet { |
| 235 | add("PRECISION_INSUFFICIENT"); |
| 236 | } |
| 237 | } |
| 238 | if r.label.iter().any(|s| *s == Status::Indeterminate) { |
| 239 | add("PRECISION_INSUFFICIENT"); |
| 240 | } |
| 241 | codes.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes())); |
| 242 | let outcome = if codes.is_empty() { "PASS" } else { "FAIL" }; |
| 243 | let exp_outcome = if c.expected_pass { "PASS" } else { "FAIL" }; |
| 244 | let met = outcome == exp_outcome && codes == c.expected_codes; |
| 245 | Verdict { |
| 246 | checks: ck, |
| 247 | outcome: outcome.into(), |
| 248 | failure_codes: codes, |
| 249 | error_code: "none".into(), |
| 250 | expectation: if met { "YES".into() } else { "NO".into() }, |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /// The ERROR / NOT_RUN verdict shape. |
| 255 | pub fn placeholder_verdict(outcome: &str, error_code: &str) -> Verdict { |
| 256 | Verdict { |
| 257 | checks: [Ck::NotEval; 12], |
| 258 | outcome: outcome.into(), |
| 259 | failure_codes: Vec::new(), |
| 260 | error_code: error_code.into(), |
| 261 | expectation: "NOT_APPLICABLE".into(), |
| 262 | } |
| 263 | } |