SilverSight/tests/test_nuvmap_equivalence.py
allaun 85141a4b94 feat(nuvmap,braid): NUVMAP port + Rossby/Kelvin braid correspondence
- Port NUVMAP projection engine from Research Stack to SilverSight
  with Q16_16 fixed-point (zero Float) and CBOR serialization
- Add Rotational Wave — Braid Correspondence formalization at boundary
  (ChiralLabel, RossbyDrift, rossby_convergence_bound stubbed,
   kelvin_wave_eigensolid proven)
- Add auto-pipeline CI workflow, webhook receiver, Forgejo MCP server
- Add SOPS/Age encryption config
- Add stack compose for portable deployment
- Add rotational wave design doc
2026-06-30 16:38:11 -05:00

331 lines
No EOL
10 KiB
Python

#!/usr/bin/env python3
"""
NUVMAP Equivalence Test — SilverSight vs Research Stack Archive
Compares the Q16_16 SilverSight port against the original float-based
Research Stack implementation to verify numerical equivalence within
Q16_16 quantization tolerance.
Run: python -m tests.test_nuvmap_equivalence
"""
import sys
sys.path.insert(0, "/home/allaun/Research Stack/5-Applications/cff")
sys.path.insert(0, "/home/allaun/SilverSight/python")
# Import original (float) from Research Stack
import importlib.util
spec_orig = importlib.util.spec_from_file_location(
"original_nuvmap",
"/home/allaun/Research Stack/5-Applications/cff/nuvmap/projection_engine.py"
)
original_module = importlib.util.module_from_spec(spec_orig)
spec_orig.loader.exec_module(original_module)
OriginalEngine = original_module.NUVMAPProjectionEngine
OriginalCell = original_module.NUVMAPCell
OriginalSurface = original_module.NUVMAPSurface
original_build = original_module.build_nuvmap_from_eigenmass
# Import ported (Q16_16) from SilverSight
spec_ported = importlib.util.spec_from_file_location(
"ported_nuvmap",
"/home/allaun/SilverSight/python/nuvmap/projection_engine.py"
)
ported_module = importlib.util.module_from_spec(spec_ported)
spec_ported.loader.exec_module(ported_module)
PortedEngine = ported_module.NUVMAPProjectionEngine
PortedCell = ported_module.NUVMAPCell
PortedSurface = ported_module.NUVMAPSurface
ported_build = ported_module.build_nuvmap_from_eigenmass
# Q16_16 constants from ported module
Q16_ONE = ported_module.Q16_ONE
Q16_HALF = ported_module.Q16_HALF
Q16_PCT1 = ported_module.Q16_PCT1
Q16_PCT70 = ported_module.Q16_PCT70
Q16_PCT30 = ported_module.Q16_PCT30
Q16_150PCT = ported_module.Q16_150PCT
SCALE = ported_module.SCALE
def f2q(f: float) -> int:
"""Float → Q16_16 raw (external boundary only)."""
import math
if math.isnan(f) or math.isinf(f):
return 0
return max(-2147483648, min(2147483647, round(f * SCALE)))
def q2f(q: int) -> float:
"""Q16_16 raw → float (for comparison only)."""
return q / SCALE
# Test data — representative eigenmass inputs
TEST_DATA = [
{
"equation_id": 1,
"amvr": 0.8, "avmr": 0.75, "chiral_residual": 0.05,
"chiral_state": "achiral_stable",
},
{
"equation_id": 2,
"amvr": 0.3, "avmr": 0.4, "chiral_residual": 0.2,
"chiral_state": "left_handed_mass_bias",
},
{
"equation_id": 3,
"amvr": 0.1, "avmr": 0.15, "chiral_residual": 0.6,
"chiral_state": "chiral_scarred",
},
{
"equation_id": 4,
"amvr": 0.5, "avmr": 0.5, "chiral_residual": 0.1,
"chiral_state": "right_handed_vector_bias",
},
{
"equation_id": 5,
"amvr": 0.9, "avmr": 0.85, "chiral_residual": 0.02,
"chiral_state": "achiral_stable",
},
]
def convert_to_q16(data: list) -> list:
"""Convert float test data to Q16_16 for SilverSight engine."""
return [
{
"equation_id": d["equation_id"],
"amvr_q16": f2q(d["amvr"]),
"avmr_q16": f2q(d["avmr"]),
"chiral_residual_q16": f2q(d["chiral_residual"]),
"chiral_state": d["chiral_state"],
}
for d in data
]
def compare_cells(orig: OriginalCell, ported: PortedCell, tol_q16: int = 2) -> bool:
"""Compare original float cell with ported Q16_16 cell."""
# Compare E_i
e_diff = abs(orig.E_i - q2f(ported.E_i))
e_tol = tol_q16 / SCALE
if e_diff > e_tol:
print(f" E_i mismatch: orig={orig.E_i:.6f}, ported={q2f(ported.E_i):.6f}, diff={e_diff:.6f} > {e_tol:.6f}")
return False
# Compare R_i
r_diff = abs(orig.R_i - q2f(ported.R_i))
if r_diff > e_tol:
print(f" R_i mismatch: orig={orig.R_i:.6f}, ported={q2f(ported.R_i):.6f}, diff={r_diff:.6f}")
return False
# Compare chi_i
chi_diff = abs(orig.chi_i - q2f(ported.chi_i))
if chi_diff > e_tol:
print(f" chi_i mismatch: orig={orig.chi_i:.6f}, ported={q2f(ported.chi_i):.6f}, diff={chi_diff:.6f}")
return False
# Compare S_i
s_diff = abs(orig.S_i - q2f(ported.S_i))
if s_diff > e_tol:
print(f" S_i mismatch: orig={orig.S_i:.6f}, ported={q2f(ported.S_i):.6f}, diff={s_diff:.6f}")
return False
# Compare L_i
l_diff = abs(orig.L_i - q2f(ported.L_i))
if l_diff > e_tol:
print(f" L_i mismatch: orig={orig.L_i:.6f}, ported={q2f(ported.L_i):.6f}, diff={l_diff:.6f}")
return False
# Compare q_i (exact integer)
if orig.q_i != ported.q_i:
print(f" q_i mismatch: orig={orig.q_i}, ported={ported.q_i}")
return False
# Compare admissible (exact boolean)
if orig.admissible != ported.admissible:
print(f" admissible mismatch: orig={orig.admissible}, ported={ported.admissible}")
return False
return True
def compare_surfaces(orig: OriginalSurface, ported: PortedSurface, tol_q16: int = 2) -> bool:
"""Compare original and ported surfaces."""
if len(orig.cells) != len(ported.cells):
print(f"Cell count mismatch: orig={len(orig.cells)}, ported={len(ported.cells)}")
return False
ok = True
for i, (o, p) in enumerate(zip(orig.cells, ported.cells)):
if not compare_cells(o, p, tol_q16):
print(f"Cell {i} failed")
ok = False
# Compare surface-level metrics
if orig.total_qubits != ported.total_qubits:
print(f"total_qubits mismatch: orig={orig.total_qubits}, ported={ported.total_qubits}")
ok = False
# Bekenstein bound
b_diff = abs(orig.bekenstein_bound - q2f(ported.bekenstein_bound))
if b_diff > tol_q16 / SCALE:
print(f"bekenstein_bound mismatch: orig={orig.bekenstein_bound:.6f}, ported={q2f(ported.bekenstein_bound):.6f}")
ok = False
# Area utilization
au_diff = abs(orig.area_utilization - q2f(ported.area_utilization))
if au_diff > tol_q16 / SCALE:
print(f"area_utilization mismatch: orig={orig.area_utilization:.6f}, ported={q2f(ported.area_utilization):.6f}")
ok = False
return ok
def test_equivalence():
"""Main equivalence test."""
print("=" * 60)
print("NUVMAP Equivalence Test: Original (float) vs SilverSight (Q16_16)")
print("=" * 60)
# Convert test data
test_data_q16 = convert_to_q16(TEST_DATA)
# Run original (float) engine
print("\nRunning original Research Stack engine (float)...")
orig_engine = OriginalEngine(
total_qubit_budget=0,
chi_max=0.5,
R_max=0.5,
landauer_threshold=0.1,
)
orig_surface = orig_engine.project(TEST_DATA)
print(f" Cells: {len(orig_surface.cells)}, Qubits: {orig_surface.total_qubits}")
print(f" Bekenstein: {orig_surface.bekenstein_bound:.6f}, Area Util: {orig_surface.area_utilization:.6f}")
# Run ported (Q16_16) engine
print("\nRunning SilverSight engine (Q16_16)...")
ported_engine = PortedEngine(
total_qubit_budget=0,
chi_max_q16=Q16_HALF, # 0.5
R_max_q16=Q16_HALF, # 0.5
landauer_threshold_q16=Q16_ONE // 10, # 0.1
)
ported_surface = ported_engine.project(test_data_q16)
print(f" Cells: {len(ported_surface.cells)}, Qubits: {ported_surface.total_qubits}")
print(f" Bekenstein: {q2f(ported_surface.bekenstein_bound):.6f}, Area Util: {q2f(ported_surface.area_utilization):.6f}")
# Compare
print("\nComparing results...")
ok = compare_surfaces(orig_surface, ported_surface, tol_q16=2)
# Summary
print("\n" + "=" * 60)
if ok:
print("✓ PASSED: SilverSight Q16_16 port matches original float results")
print(" (within ±2 Q16_16 steps = ±0.00003 float)")
else:
print("✗ FAILED: Significant numerical divergence detected")
print("=" * 60)
return ok
def test_cbor_roundtrip():
"""Test CBOR serialization round-trip."""
print("\nTesting CBOR serialization round-trip...")
test_data_q16 = convert_to_q16(TEST_DATA)
engine = PortedEngine(total_qubit_budget=100)
surface = engine.project(test_data_q16)
# Serialize
cbor_bytes = surface.to_cbor()
print(f" CBOR size: {len(cbor_bytes)} bytes")
# Deserialize
surface2 = PortedSurface.from_cbor(cbor_bytes)
# Compare
ok = compare_surfaces(surface, surface2, tol_q16=0)
if ok:
print(" ✓ CBOR round-trip successful")
else:
print(" ✗ CBOR round-trip failed")
return ok
def test_file_roundtrip():
"""Test file I/O round-trip."""
import tempfile
import os
print("\nTesting file I/O round-trip...")
test_data_q16 = convert_to_q16(TEST_DATA)
engine = PortedEngine(total_qubit_budget=100)
surface = engine.project(test_data_q16)
with tempfile.NamedTemporaryFile(suffix='.cbor', delete=False) as f:
path = f.name
try:
surface.to_file(path)
surface2 = PortedSurface.from_file(path)
ok = compare_surfaces(surface, surface2, tol_q16=0)
if ok:
print(" ✓ File round-trip successful")
else:
print(" ✗ File round-trip failed")
return ok
finally:
os.unlink(path)
def test_admissibility_gate():
"""Test the quantum_storage_admissible gate matches."""
print("\nTesting quantum_storage_admissible gate...")
test_data_q16 = convert_to_q16(TEST_DATA)
orig_engine = OriginalEngine(chi_max=0.5, R_max=0.5, landauer_threshold=0.1)
orig_surface = orig_engine.project(TEST_DATA)
ported_engine = PortedEngine(
chi_max_q16=Q16_HALF,
R_max_q16=Q16_HALF,
landauer_threshold_q16=Q16_ONE // 10,
)
ported_surface = ported_engine.project(test_data_q16)
ok = True
for i in range(len(TEST_DATA)):
# Test with tau = 1.0
orig_result = orig_engine.quantum_storage_admissible(i, 1.0)
ported_result = ported_engine.quantum_storage_admissible(i, Q16_ONE)
if orig_result != ported_result:
print(f" Gate mismatch at cell {i}: orig={orig_result}, ported={ported_result}")
ok = False
if ok:
print(" ✓ Admissibility gate matches")
return ok
if __name__ == "__main__":
all_ok = True
all_ok &= test_equivalence()
all_ok &= test_cbor_roundtrip()
all_ok &= test_file_roundtrip()
all_ok &= test_admissibility_gate()
print("\n" + "=" * 60)
if all_ok:
print("ALL TESTS PASSED ✓")
sys.exit(0)
else:
print("SOME TESTS FAILED ✗")
sys.exit(1)