use super::*;
use ndarray::{Array2, ArrayView1};
fn lcg_u01(state: &mut u64) -> f64 {
*state = state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
(((*state >> 11) as f64) + 1.0) / ((1u64 << 53) as f64 + 1.0)
}
fn gaussian(state: &mut u64) -> f64 {
let u1 = lcg_u01(state);
let u2 = lcg_u01(state);
(-2.0 * u1.ln()).sqrt() * (std::f64::consts::TAU * u2).cos()
}
fn unit_rows(rows: usize, p: usize, seed: u64) -> Array2<f32> {
let mut s = seed;
let mut m = Array2::<f32>::zeros((rows, p));
for i in 0..rows {
let mut norm2 = 0.0f64;
for c in 0..p {
let v = gaussian(&mut s);
m[[i, c]] = v as f32;
norm2 += v * v;
}
let inv = 1.0 / norm2.sqrt().max(1.0e-30);
for c in 0..p {
m[[i, c]] = (m[[i, c]] as f64 * inv) as f32;
}
}
m
}
fn max_cross_gate(row: ArrayView1<'_, f32>, decoder: &Array2<f32>, block_size: usize) -> f64 {
let n_blocks = decoder.nrows() / block_size;
let mut norm2 = 0.0f64;
for &v in row.iter() {
norm2 += v as f64 * v as f64;
}
let norm = norm2.sqrt();
let mut best = 0.0f64;
for g in 0..n_blocks {
let mut energy = 0.0f64;
for r in 0..block_size {
let atom = decoder.row(g * block_size + r);
let mut dot = 0.0f64;
for (rv, av) in row.iter().zip(atom.iter()) {
dot += *rv as f64 * *av as f64;
}
energy += dot * dot;
}
best = best.max(energy.sqrt());
}
best / norm.max(1.0e-30)
}
#[test]
fn closed_form_matches_derivation() {
let p = 256usize;
let k = 2000usize;
let f = routability_floor(p, k, 1, 1.0);
let expect = (1.0f64 / p as f64).sqrt() + (2.0 * (k as f64).ln() / p as f64).sqrt();
assert!(
(f.floor - expect).abs() < 1.0e-12,
"δ=1 floor {} != derivation {expect}",
f.floor
);
let f01 = routability_floor(p, k, 1, 0.01);
let widen = (2.0 * (1.0f64 / 0.01).ln() / p as f64).sqrt();
let union_1 = (2.0 * (k as f64).ln() / p as f64).sqrt();
let union_01 = (2.0 * ((k as f64) / 0.01).ln() / p as f64).sqrt();
assert!(f01.floor > f.floor, "smaller δ must widen the floor");
assert!(
((union_01 * union_01) - (union_1 * union_1) - (widen * widen)).abs() < 1.0e-12,
"union term must grow by ln(1/δ) in the squared scale"
);
let e = minimum_routable_energy(&f);
assert!((0.0..1.0).contains(&e), "energy fraction out of (0,1): {e}");
assert!(
(e - f.floor * f.floor / (1.0 + f.floor * f.floor)).abs() < 1.0e-12,
"minimum_routable_energy must equal floor²/(1+floor²)"
);
}
#[test]
fn monte_carlo_floor_bounds_and_tightness() {
let (p, k, n) = (256usize, 2000usize, 4000usize);
let decoder = unit_rows(k, p, 0xF100_D001);
let residuals = unit_rows(n, p, 0x0FF5_E7ED);
let mut gates: Vec<f64> = residuals
.outer_iter()
.map(|r| max_cross_gate(r, &decoder, 1))
.collect();
gates.sort_by(|a, b| a.partial_cmp(b).unwrap());
let f01 = routability_floor(p, k, 1, 0.01).floor;
let f5 = routability_floor(p, k, 1, 0.5).floor;
let frac_below = gates.iter().filter(|&&g| g <= f01).count() as f64 / n as f64;
assert!(
frac_below >= 0.99,
"δ=0.01 floor {f01:.4} must bound ≥99% of rows; got {frac_below:.4}"
);
let q99 = gates[((0.99 * (n - 1) as f64).round() as usize).min(n - 1)];
let ratio = q99 / f5;
assert!(
(0.5..2.0).contains(&ratio),
"δ=0.5 floor {f5:.4} must be within a factor of 2 of the empirical q99 {q99:.4} \
(ratio {ratio:.3})"
);
let audit = routability_audit(decoder.view(), residuals.view(), 1, 0.5, &[0.5, 0.99]).unwrap();
assert_eq!(audit.n_rows, n);
let median = gates[((0.5 * (n - 1) as f64).round() as usize).min(n - 1)];
assert!(
(audit.coherence_excess - median / f5).abs() < 1.0e-9,
"audit coherence_excess {} must equal (median max-cross-gate)/floor {}",
audit.coherence_excess,
median / f5
);
assert!(
(0.5..2.0).contains(&audit.coherence_excess),
"random dictionary must sit near the generic-position floor (excess {:.3})",
audit.coherence_excess
);
}
#[test]
fn phase_transition_at_floor() {
let (p, k) = (256usize, 2000usize);
let f = routability_floor(p, k, 1, 0.5).floor;
let trials = 300usize;
let run = |a: f64, seed0: u64| -> f64 {
let mut successes = 0usize;
for t in 0..trials {
let seed = seed0.wrapping_add((t as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15));
let decoder = unit_rows(k, p, seed);
let target = unit_rows(1, p, seed ^ 0xA5A5_A5A5);
let interference = unit_rows(1, p, seed ^ 0x5A5A_5A5A);
let amp = a * f; let mut x = Array2::<f32>::zeros((1, p));
for c in 0..p {
x[[0, c]] = amp as f32 * target[[0, c]] + interference[[0, c]];
}
let g_target = {
let mut d = 0.0f64;
for c in 0..p {
d += x[[0, c]] as f64 * target[[0, c]] as f64;
}
d.abs()
};
let g_off = max_cross_gate(x.row(0), &decoder, 1) * {
let mut n2 = 0.0f64;
for c in 0..p {
n2 += x[[0, c]] as f64 * x[[0, c]] as f64;
}
n2.sqrt()
};
if g_target > g_off {
successes += 1;
}
}
successes as f64 / trials as f64
};
let below = run(0.5, 0x1234_5678);
let above = run(2.0, 0x8765_4321);
assert!(
below < 0.5,
"at 0.5× the floor routing should fail mostly; success frac {below:.3}"
);
assert!(
above > 0.9,
"at 2× the floor routing should succeed mostly; success frac {above:.3}"
);
}
#[test]
fn perfect_reconstruction_zero_residual_is_a_defined_audit_not_an_error() {
let (p, k, n) = (4usize, 6usize, 10usize);
let decoder = unit_rows(k, p, 0xDEAD_BEEF);
let residuals = Array2::<f32>::zeros((n, p));
let audit = routability_audit(decoder.view(), residuals.view(), 1, 0.05, &[0.5, 0.9])
.expect("all-zero residual is a valid perfect-reconstruction audit, not an error");
assert_eq!(audit.n_rows, 0);
let expected_floor = routability_floor(p, k, 1, 0.05);
assert!((audit.floor.floor - expected_floor.floor).abs() < 1.0e-12);
assert_eq!(audit.empirical_mean, 0.0);
assert_eq!(audit.empirical_max, 0.0);
assert_eq!(audit.confidence_quantile, 0.0);
assert_eq!(audit.coherence_excess, 0.0);
assert_eq!(audit.fraction_below_floor, 1.0);
for &(level, value) in &audit.quantiles {
assert_eq!(
value, 0.0,
"quantile at level {level} must be 0 with no residual"
);
}
assert_eq!(audit.quantiles.len(), 2);
let tiny = Array2::<f32>::from_elem((n, p), 1.0e-13_f32);
let tiny_audit = routability_audit(decoder.view(), tiny.view(), 1, 0.05, &[0.5, 0.9])
.expect("sub-threshold residual is still a defined audit");
assert_eq!(tiny_audit.n_rows, 0);
assert_eq!(tiny_audit.fraction_below_floor, 1.0);
}
#[test]
fn block_variant_floor_and_audit() {
let (p, g, b, n) = (256usize, 1000usize, 2usize, 3000usize);
let decoder = block_frames(g, b, p, 0xB10C_C0DE);
let residuals = unit_rows(n, p, 0x0FF5_B10C);
let mut gates: Vec<f64> = residuals
.outer_iter()
.map(|r| max_cross_gate(r, &decoder, b))
.collect();
gates.sort_by(|a, x| a.partial_cmp(x).unwrap());
let f01 = routability_floor(p, g, b, 0.01);
let f01_b1 = routability_floor(p, g, 1, 0.01);
assert!(
f01.floor > f01_b1.floor,
"b=2 floor {} must exceed b=1 floor {}",
f01.floor,
f01_b1.floor
);
let frac_below = gates.iter().filter(|&&x| x <= f01.floor).count() as f64 / n as f64;
assert!(
frac_below >= 0.99,
"b=2 δ=0.01 floor {:.4} must bound ≥99% of rows; got {frac_below:.4}",
f01.floor
);
let audit = routability_audit(decoder.view(), residuals.view(), b, 0.5, &[0.5, 0.99]).unwrap();
assert_eq!(audit.floor.b_max, b);
assert_eq!(audit.floor.n_blocks, g);
assert!(
(0.5..2.0).contains(&audit.coherence_excess),
"random block frames must sit near the generic-position floor (excess {:.3})",
audit.coherence_excess
);
for gg in 0..3 {
for r1 in 0..b {
for r2 in 0..b {
let mut dot = 0.0f64;
for c in 0..p {
dot += decoder[[gg * b + r1, c]] as f64 * decoder[[gg * b + r2, c]] as f64;
}
let want = if r1 == r2 { 1.0 } else { 0.0 };
assert!(
(dot - want).abs() < 1.0e-4,
"block {gg} frame not orthonormal: <{r1},{r2}> = {dot}"
);
}
}
}
}
fn block_frames(g: usize, b: usize, p: usize, seed: u64) -> Array2<f32> {
let mut s = seed;
let mut d = Array2::<f32>::zeros((g * b, p));
for block in 0..g {
let mut basis: Vec<Vec<f64>> = Vec::with_capacity(b);
for _ in 0..b {
let mut v: Vec<f64> = (0..p).map(|_| gaussian(&mut s)).collect();
for u in basis.iter() {
let dot: f64 = v.iter().zip(u).map(|(a, x)| a * x).sum();
for (vc, uc) in v.iter_mut().zip(u) {
*vc -= dot * uc;
}
}
let norm = v.iter().map(|x| x * x).sum::<f64>().sqrt().max(1.0e-30);
for vc in v.iter_mut() {
*vc /= norm;
}
basis.push(v);
}
for (r, v) in basis.iter().enumerate() {
for c in 0..p {
d[[block * b + r, c]] = v[c] as f32;
}
}
}
d
}