Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/research/atemporal/at1/evaluator/src/model.rs # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / research / atemporal / at1 / evaluator / src / model.rs` 532 lines ยท 17609 bytes Raw Blame History 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 //! Shadow model: the evaluator's own computation of the AT-1 physics from the 17 //! case text (AT1_CASE_V1 section 3, AT1_SPEC Theorems 1 to 5), written from the 18 //! contract and the spec only, never from the oracle's or the engine's code. 19 //! 20 //! Kernel pairs, degenerate levels, level vectors and the zero test are exact 21 //! (rational and Gaussian-rational). The conditional vector uses relative 22 //! phases (Corollary 3.2). When every phase is a quarter turn the whole 23 //! computation stays exact; otherwise it runs in double-double (dd.rs) with a 24 //! stated ESTIMATED bound. The ideal reference rotates the Bloch vector of psi_0 25 //! about h by 2|h|(t_k - t_r) (AT1_SPEC Definition 5.2), exact when |h| is 26 //! rational and the angle is a quarter turn. 27 28 use crate::big::Int; 29 use crate::case::Case; 30 use crate::dd::{cos_sin_turns, cos_sin_turns_q, Dd}; 31 use crate::rat::{Cq, Q}; 32 33 /// Claimed absolute error of every inexact shadow value (double-double, ~1e-30 actual). 34 pub fn dd_bound() -> Q { 35 Q::new(Int::from_u64(1), Int::pow10(24)) 36 } 37 38 #[derive(Clone, Debug)] 39 pub struct Val { 40 pub q: Q, 41 pub exact: bool, 42 } 43 impl Val { 44 pub fn exact(q: Q) -> Val { 45 Val { q, exact: true } 46 } 47 pub fn approx(d: Dd) -> Val { 48 Val { q: d.to_q(), exact: false } 49 } 50 pub fn bound(&self) -> Q { 51 if self.exact { Q::zero() } else { dd_bound() } 52 } 53 pub fn f64(&self) -> f64 { 54 Dd::from_q(&self.q).hi 55 } 56 } 57 58 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 59 pub enum Status { 60 Defined, 61 Undefined, 62 Indeterminate, 63 } 64 impl Status { 65 pub fn text(self) -> &'static str { 66 match self { 67 Status::Defined => "DEFINED", 68 Status::Undefined => "UNDEFINED", 69 Status::Indeterminate => "INDETERMINATE", 70 } 71 } 72 pub fn parse(s: &str) -> Option { 73 match s { 74 "DEFINED" => Some(Status::Defined), 75 "UNDEFINED" => Some(Status::Undefined), 76 "INDETERMINATE" => Some(Status::Indeterminate), 77 _ => None, 78 } 79 } 80 } 81 82 /// The contract's status rule: UNDEFINED if p + b <= tol; DEFINED if p - b > tol; else INDETERMINATE. 83 pub fn status_rule(p: &Q, b: &Q, tol: &Q) -> Status { 84 if p.add(b).cmp(tol) != std::cmp::Ordering::Greater { 85 Status::Undefined 86 } else if p.sub(b).cmp(tol) == std::cmp::Ordering::Greater { 87 Status::Defined 88 } else { 89 Status::Indeterminate 90 } 91 } 92 93 /// Physics-level defects a mutant engine or oracle can carry (all false = honest). 94 #[derive(Clone, Copy, Debug, Default)] 95 pub struct Mods { 96 pub drop_v: bool, 97 pub flip_v: bool, 98 pub shift_level: bool, 99 pub phase_minus: bool, 100 } 101 102 #[derive(Clone, Debug)] 103 pub struct Shadow { 104 pub kernel_dim: usize, 105 pub trivial: bool, 106 pub interacting_exact: bool, 107 pub p: Vec, 108 pub status: Vec, 109 /// [k][axis X,Y,Z][PLUS, MINUS]; None for a label that is not DEFINED 110 pub pauli: Vec>, 111 pub ideal: Vec<[[Val; 2]; 3]>, 112 pub ideal_exact: bool, 113 pub povm_residual: Val, 114 } 115 116 trait F: Clone { 117 fn from_q(q: &Q) -> Self; 118 fn add(&self, o: &Self) -> Self; 119 fn sub(&self, o: &Self) -> Self; 120 fn mul(&self, o: &Self) -> Self; 121 fn div(&self, o: &Self) -> Self; 122 fn is_zero_exact(&self) -> bool; 123 /// (cos, sin) of 2 pi turns, or None when this field cannot represent it. 124 fn phase(turns: &Q) -> Option<(Self, Self)>; 125 fn val(&self) -> Val; 126 } 127 128 impl F for Q { 129 fn from_q(q: &Q) -> Q { 130 q.clone() 131 } 132 fn add(&self, o: &Q) -> Q { 133 Q::add(self, o) 134 } 135 fn sub(&self, o: &Q) -> Q { 136 Q::sub(self, o) 137 } 138 fn mul(&self, o: &Q) -> Q { 139 Q::mul(self, o) 140 } 141 fn div(&self, o: &Q) -> Q { 142 Q::div(self, o) 143 } 144 fn is_zero_exact(&self) -> bool { 145 self.is_zero() 146 } 147 fn phase(turns: &Q) -> Option<(Q, Q)> { 148 let f4 = turns.mod1().mul(&Q::int(4)); 149 if !f4.is_int() { 150 return None; 151 } 152 Some(match f4.n.low_u64() { 153 0 => (Q::one(), Q::zero()), 154 1 => (Q::zero(), Q::one()), 155 2 => (Q::int(-1), Q::zero()), 156 _ => (Q::zero(), Q::int(-1)), 157 }) 158 } 159 fn val(&self) -> Val { 160 Val::exact(self.clone()) 161 } 162 } 163 164 impl F for Dd { 165 fn from_q(q: &Q) -> Dd { 166 Dd::from_q(q) 167 } 168 fn add(&self, o: &Dd) -> Dd { 169 Dd::add(*self, *o) 170 } 171 fn sub(&self, o: &Dd) -> Dd { 172 Dd::sub(*self, *o) 173 } 174 fn mul(&self, o: &Dd) -> Dd { 175 Dd::mul(*self, *o) 176 } 177 fn div(&self, o: &Dd) -> Dd { 178 Dd::div(*self, *o) 179 } 180 fn is_zero_exact(&self) -> bool { 181 self.hi == 0.0 && self.lo == 0.0 182 } 183 fn phase(turns: &Q) -> Option<(Dd, Dd)> { 184 Some(cos_sin_turns_q(turns)) 185 } 186 fn val(&self) -> Val { 187 Val::approx(*self) 188 } 189 } 190 191 #[derive(Clone)] 192 struct C { 193 re: T, 194 im: T, 195 } 196 impl C { 197 fn from_cq(z: &Cq) -> C { 198 C { re: T::from_q(&z.re), im: T::from_q(&z.im) } 199 } 200 fn add(&self, o: &C) -> C { 201 C { re: self.re.add(&o.re), im: self.im.add(&o.im) } 202 } 203 fn mul(&self, o: &C) -> C { 204 C { re: self.re.mul(&o.re).sub(&self.im.mul(&o.im)), im: self.re.mul(&o.im).add(&self.im.mul(&o.re)) } 205 } 206 fn norm2(&self) -> T { 207 self.re.mul(&self.re).add(&self.im.mul(&self.im)) 208 } 209 } 210 211 /// Pauli outcome probabilities of the pure state chi (unnormalised), each computed directly. 212 fn pauli_of(c0: &C, c1: &C) -> [[T; 2]; 3] { 213 let n0 = c0.norm2(); 214 let n1 = c1.norm2(); 215 let nn = n0.add(&n1); 216 // conj(c0) c1 217 let xr = c0.re.mul(&c1.re).add(&c0.im.mul(&c1.im)); 218 let xi = c0.re.mul(&c1.im).sub(&c0.im.mul(&c1.re)); 219 let half = T::from_q(&Q::frac(1, 2)); 220 let q = |a: &T| a.div(&nn); 221 [ 222 [half.add(&q(&xr)), half.sub(&q(&xr))], 223 [half.add(&q(&xi)), half.sub(&q(&xi))], 224 [q(&n0), q(&n1)], 225 ] 226 } 227 228 fn val3(a: &[[T; 2]; 3]) -> [[Val; 2]; 3] { 229 [[a[0][0].val(), a[0][1].val()], [a[1][0].val(), a[1][1].val()], [a[2][0].val(), a[2][1].val()]] 230 } 231 232 /// Level structure: per level, the exact level vector u_j (zero when the level is not in the kernel). 233 pub struct Levels { 234 pub kernel_dim: usize, 235 pub u: Vec<[Cq; 2]>, 236 pub s: Q, 237 } 238 239 pub fn levels(c: &Case, m: &Mods) -> Levels { 240 let n = c.n; 241 let mut kernel_dim = 0; 242 let mut u = Vec::new(); 243 for j in 0..n { 244 let src = if m.shift_level { (j + n - 1) % n } else { j }; 245 let mut v = c.v[src].clone(); 246 if m.drop_v { 247 v = [Q::zero(), Q::zero(), Q::zero()]; 248 } 249 if m.flip_v { 250 v = [v[0].neg(), v[1].neg(), v[2].neg()]; 251 } 252 let nx = c.h[0].add(&v[0]); 253 let ny = c.h[1].add(&v[1]); 254 let nz = c.h[2].add(&v[2]); 255 let r2 = nx.mul(&nx).add(&ny.mul(&ny)).add(&nz.mul(&nz)); 256 let base = c.e[j].add(&c.h0); 257 let zero2 = [Cq::zero(), Cq::zero()]; 258 let r = match r2.sqrt_exact() { 259 Some(r) => r, 260 None => { 261 // irrational level norm (only reachable by a mutant): never matches a rational energy 262 u.push(zero2); 263 continue; 264 } 265 }; 266 if r.is_zero() { 267 if base.is_zero() { 268 kernel_dim += 2; 269 u.push(c.psi.clone()); 270 } else { 271 u.push(zero2); 272 } 273 continue; 274 } 275 let mut found = None; 276 for s in [1i64, -1] { 277 let sr = r.mul(&Q::int(s)); 278 if base.add(&sr).is_zero() { 279 found = Some(sr); 280 } 281 } 282 match found { 283 None => u.push(zero2), 284 Some(sr) => { 285 kernel_dim += 1; 286 // contract eigenvector (s|n| + n_z, n_x + i n_y), else (n_x - i n_y, s|n| - n_z) 287 let mut f = [Cq::real(sr.add(&nz)), Cq::new(nx.clone(), ny.clone())]; 288 if f[0].is_zero() && f[1].is_zero() { 289 f = [Cq::new(nx.clone(), ny.neg()), Cq::real(sr.sub(&nz))]; 290 } 291 let ff = f[0].norm2().add(&f[1].norm2()); 292 let ip = f[0].conj().mul(&c.psi[0]).add(&f[1].conj().mul(&c.psi[1])); 293 let coef = ip.scale(&Q::one().div(&ff)); 294 u.push([f[0].mul(&coef), f[1].mul(&coef)]); 295 } 296 } 297 } 298 let mut s = Q::zero(); 299 for uj in &u { 300 s = s.add(&uj[0].norm2()).add(&uj[1].norm2()); 301 } 302 Levels { kernel_dim, u, s } 303 } 304 305 struct Inter { 306 p: Vec, 307 pauli: Vec>, 308 zero: Vec, 309 } 310 311 fn interacting(c: &Case, lv: &Levels, m: &Mods) -> Option { 312 let a = lv.u.iter().position(|x| !(x[0].is_zero() && x[1].is_zero()))?; 313 let n_q = Q::int(c.n as i64); 314 let ns = T::from_q(&n_q.mul(&lv.s)); 315 let w = T::from_q(&c.w); 316 let mut out = Inter { p: Vec::new(), pauli: Vec::new(), zero: Vec::new() }; 317 for k in 0..c.m { 318 let dk = Q::int(k as i64 - c.ref_index as i64); 319 let mut chi0 = C { re: T::from_q(&Q::zero()), im: T::from_q(&Q::zero()) }; 320 let mut chi1 = chi0.clone(); 321 for (j, uj) in lv.u.iter().enumerate() { 322 if uj[0].is_zero() && uj[1].is_zero() { 323 continue; 324 } 325 let mut turns = c.e[j].sub(&c.e[a]).mul(&dk).mul(&c.tau); 326 if m.phase_minus { 327 turns = turns.neg(); 328 } 329 let (cs, sn) = T::phase(&turns)?; 330 let ph = C { re: cs, im: sn }; 331 chi0 = chi0.add(&ph.mul(&C::from_cq(&uj[0]))); 332 chi1 = chi1.add(&ph.mul(&C::from_cq(&uj[1]))); 333 } 334 let nn = chi0.norm2().add(&chi1.norm2()); 335 let p = w.mul(&nn).div(&ns); 336 out.zero.push(nn.is_zero_exact()); 337 out.p.push(p.val()); 338 if nn.is_zero_exact() { 339 out.pauli.push(None); 340 } else { 341 out.pauli.push(Some(val3(&pauli_of(&chi0, &chi1)))); 342 } 343 } 344 Some(out) 345 } 346 347 fn bloch(psi: &[Cq; 2]) -> [Q; 3] { 348 let n = psi[0].norm2().add(&psi[1].norm2()); 349 let x = psi[0].conj().mul(&psi[1]); 350 let two = Q::int(2); 351 [x.re.mul(&two).div(&n), x.im.mul(&two).div(&n), psi[0].norm2().sub(&psi[1].norm2()).div(&n)] 352 } 353 354 fn rotate(b: &[T; 3], nh: &[T; 3], cs: &T, sn: &T) -> [T; 3] { 355 let one = T::from_q(&Q::one()); 356 let dot = nh[0].mul(&b[0]).add(&nh[1].mul(&b[1])).add(&nh[2].mul(&b[2])); 357 let cross = [ 358 nh[1].mul(&b[2]).sub(&nh[2].mul(&b[1])), 359 nh[2].mul(&b[0]).sub(&nh[0].mul(&b[2])), 360 nh[0].mul(&b[1]).sub(&nh[1].mul(&b[0])), 361 ]; 362 let omc = one.sub(cs); 363 let mut r = b.clone(); 364 for i in 0..3 { 365 r[i] = b[i].mul(cs).add(&cross[i].mul(sn)).add(&nh[i].mul(&dot).mul(&omc)); 366 } 367 r 368 } 369 370 fn probs_from_bloch(b: &[T; 3]) -> [[Val; 2]; 3] { 371 let half = T::from_q(&Q::frac(1, 2)); 372 let mut out: Vec<[Val; 2]> = Vec::new(); 373 for i in 0..3 { 374 let hb = b[i].mul(&half); 375 out.push([half.add(&hb).val(), half.sub(&hb).val()]); 376 } 377 [out[0].clone(), out[1].clone(), out[2].clone()] 378 } 379 380 /// Ideal reference for every label: (values, all exact?). 381 fn ideal(c: &Case, drop_h: bool) -> (Vec<[[Val; 2]; 3]>, bool) { 382 let b = bloch(&c.psi); 383 let h2 = c.h[0].mul(&c.h[0]).add(&c.h[1].mul(&c.h[1])).add(&c.h[2].mul(&c.h[2])); 384 let mut out = Vec::new(); 385 let mut all_exact = true; 386 for k in 0..c.m { 387 if h2.is_zero() || drop_h { 388 out.push(probs_from_bloch::(&b)); 389 continue; 390 } 391 let dk = Q::int(k as i64 - c.ref_index as i64); 392 if let Some(hn) = h2.sqrt_exact() { 393 let turns = Q::int(2).mul(&hn).mul(&dk).mul(&c.tau); 394 let nh = [c.h[0].div(&hn), c.h[1].div(&hn), c.h[2].div(&hn)]; 395 if let Some((cs, sn)) = ::phase(&turns) { 396 out.push(probs_from_bloch::(&rotate::(&b, &nh, &cs, &sn))); 397 continue; 398 } 399 all_exact = false; 400 let (cs, sn) = cos_sin_turns_q(&turns); 401 let bd = [Dd::from_q(&b[0]), Dd::from_q(&b[1]), Dd::from_q(&b[2])]; 402 let nd = [Dd::from_q(&nh[0]), Dd::from_q(&nh[1]), Dd::from_q(&nh[2])]; 403 out.push(probs_from_bloch::
(&rotate::
(&bd, &nd, &cs, &sn))); 404 } else { 405 all_exact = false; 406 let hn = Dd::from_q(&h2).sqrt(); 407 let turns = hn.mul(Dd::from_q(&Q::int(2).mul(&dk).mul(&c.tau))); 408 let (cs, sn) = cos_sin_turns(turns); 409 let bd = [Dd::from_q(&b[0]), Dd::from_q(&b[1]), Dd::from_q(&b[2])]; 410 let nd = [Dd::from_q(&c.h[0]).div(hn), Dd::from_q(&c.h[1]).div(hn), Dd::from_q(&c.h[2]).div(hn)]; 411 out.push(probs_from_bloch::
(&rotate::
(&bd, &nd, &cs, &sn))); 412 } 413 } 414 (out, all_exact) 415 } 416 417 /// || sum_k F_k - I ||_F (AT1_SPEC Theorem 2), exact when every gap sum is exact. 418 fn povm_residual(c: &Case) -> Val { 419 let n = c.n; 420 let mq = Q::int(c.m as i64); 421 let wn = c.w.div(&Q::int(n as i64)); 422 // diagonal: (w M / N - 1)^2 per level 423 let diag = wn.mul(&mq).sub(&Q::one()); 424 let mut exact_sum = diag.mul(&diag).mul(&Q::int(n as i64)); 425 let mut dd_extra = Dd::zero(); 426 let mut exact = true; 427 for j in 0..n { 428 for jp in (j + 1)..n { 429 let dtau = c.e[j].sub(&c.e[jp]).mul(&c.tau); 430 let s2: Option = if dtau.is_int() { 431 Some(mq.mul(&mq)) 432 } else if dtau.mul(&mq).is_int() { 433 Some(Q::zero()) 434 } else { 435 // direct sum of exp(-2 pi i D k tau), k = 0..M-1 436 let mut ex = Some((Q::zero(), Q::zero())); 437 let mut re = Dd::zero(); 438 let mut im = Dd::zero(); 439 for k in 0..c.m { 440 let t = dtau.mul(&Q::int(k as i64)).neg(); 441 if let Some((a, b)) = &mut ex { 442 match ::phase(&t) { 443 Some((cs, sn)) => { 444 *a = a.add(&cs); 445 *b = b.add(&sn); 446 } 447 None => {} 448 } 449 } 450 if ::phase(&t).is_none() { 451 ex = None; 452 } 453 let (cs, sn) = cos_sin_turns_q(&t); 454 re = re.add(cs); 455 im = im.add(sn); 456 } 457 match ex { 458 Some((a, b)) => Some(a.mul(&a).add(&b.mul(&b))), 459 None => { 460 exact = false; 461 dd_extra = dd_extra.add(re.mul(re).add(im.mul(im)).mul(Dd::from_q(&wn.mul(&wn))).mul_f(2.0)); 462 None 463 } 464 } 465 }; 466 if let Some(s2) = s2 { 467 exact_sum = exact_sum.add(&s2.mul(&wn).mul(&wn).mul(&Q::int(2))); 468 } 469 } 470 } 471 if exact { 472 if let Some(r) = exact_sum.sqrt_exact() { 473 return Val::exact(r); 474 } 475 } 476 Val::approx(Dd::from_q(&exact_sum).add(dd_extra).sqrt()) 477 } 478 479 pub fn shadow(c: &Case, m: &Mods, force_dd: bool) -> Shadow { 480 let lv = levels(c, m); 481 let trivial = lv.kernel_dim == 0 || lv.s.is_zero(); 482 let tolz = Q::from_scaled(&c.tol_zero); 483 let (ideal_v, ideal_exact) = ideal(c, false); 484 let povm = povm_residual(c); 485 let mut sh = Shadow { 486 kernel_dim: lv.kernel_dim, 487 trivial, 488 interacting_exact: true, 489 p: Vec::new(), 490 status: vec![Status::Undefined; c.m], 491 pauli: vec![None; c.m], 492 ideal: ideal_v, 493 ideal_exact, 494 povm_residual: povm, 495 }; 496 if trivial { 497 return sh; 498 } 499 let inter = if force_dd { None } else { interacting::(c, &lv, m) }; 500 let inter = match inter { 501 Some(i) => i, 502 None => { 503 sh.interacting_exact = false; 504 interacting::
(c, &lv, m).expect("double-double path always succeeds") 505 } 506 }; 507 for k in 0..c.m { 508 let pv = &inter.p[k]; 509 let st = if inter.zero[k] && pv.exact { Status::Undefined } else { status_rule(&pv.q, &pv.bound(), &tolz) }; 510 sh.status[k] = st; 511 if st == Status::Defined { 512 sh.pauli[k] = inter.pauli[k].clone(); 513 } 514 } 515 sh.p = inter.p; 516 sh 517 } 518 519 /// Shadow for the ideal model of the same case (V dropped): used by mutants that write the ideal marginal. 520 pub fn ideal_marginal(c: &Case) -> Q { 521 c.w.div(&Q::int(c.n as i64)) 522 } 523 524 /// Ideal reference values (Definition 5.2), optionally without H_S (oracle mutant). 525 pub fn ideal_values(c: &Case, drop_h: bool) -> Vec<[[Val; 2]; 3]> { 526 ideal(c, drop_h).0 527 } 528 529 /// Display form: exact rational, or a decimal marked `~` for a double-double value. 530 pub fn show(v: &Val) -> String { 531 if v.exact { v.q.text() } else { format!("~{:.17e}", v.q.to_f64()) } 532 }