#!/usr/bin/env python3 """test_lens_kernel.py — Cross-validate C lens kernel against Python.""" import numpy as np import subprocess # Generate a known input np.random.seed(42) test_input = np.random.randn(16).astype(np.float32) * 2.0 test_input.tofile('/home/compunerd/agent_framework/bqsm_assist/test_input_16.bin') # C kernel output: read and compare to Python lens N_RING = 16 N_HARM = 15 LENS_SITE = 0 LENS_DELTA = 0.2 def deriv(theta, omega): d = np.zeros(N_RING) for i in range(N_RING): ip = (i + 1) & (N_RING - 1) im = (i - 1) & (N_RING - 1) d[i] = omega[i] + np.sin(theta[ip] - theta[i]) + np.sin(theta[im] - theta[i]) return d def rk4_step(theta, omega, dt=0.5): k1 = deriv(theta, omega) k2 = deriv(theta + 0.5*dt*k1, omega) k3 = deriv(theta + 0.5*dt*k2, omega) k4 = deriv(theta + dt*k3, omega) return theta + (dt/6.0)*(k1 + 2*k2 + 2*k3 + k4) def settle(theta, omega, steps=60): for _ in range(steps): theta = rk4_step(theta, omega) return theta def winding(theta): d = np.diff(theta) d = np.where(d > np.pi, d - 2*np.pi, d) d = np.where(d < -np.pi, d + 2*np.pi, d) q = int(round(np.sum(d) / (2 * np.pi))) return max(-3, min(3, q)) # Python lens projection omega = np.zeros(N_RING) omega[LENS_SITE] = LENS_DELTA theta_py = test_input.astype(np.float64) theta_settled = settle(theta_py, omega) q_py = winding(theta_settled) print("Test input (16 values):") for i in range(16): print(f" [{i:2d}] = {test_input[i]:+.4f} θ={theta_py[i]:.4f} θ'={theta_settled[i]:.4f}") print(f"\nPython winding q = {q_py}") # Compile and run C test c_code = ''' #define _GNU_SOURCE #include #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define N_RING 16 #define LENS_SITE 0 #define LENS_DELTA 0.2 #define K_COUPL 1.0 #define DT 0.5 #define SETTLE_STEPS 60 double lens_omega[N_RING]; void init_lens() { memset(lens_omega, 0, sizeof(lens_omega)); lens_omega[LENS_SITE] = LENS_DELTA; } void deriv(double *theta, double *out) { for (int j = 0; j < N_RING; j++) { double jp = theta[(j + 1) & 15]; double jm = theta[(j - 1) & 15]; out[j] = lens_omega[j] + K_COUPL * (sin(jp - theta[j]) + sin(jm - theta[j])); } } void rk4_step(double *theta) { double k1[N_RING], k2[N_RING], k3[N_RING], k4[N_RING], tmp[N_RING]; deriv(theta, k1); for (int j = 0; j < N_RING; j++) tmp[j] = theta[j] + 0.5*DT*k1[j]; deriv(tmp, k2); for (int j = 0; j < N_RING; j++) tmp[j] = theta[j] + 0.5*DT*k2[j]; deriv(tmp, k3); for (int j = 0; j < N_RING; j++) tmp[j] = theta[j] + DT*k3[j]; deriv(tmp, k4); for (int j = 0; j < N_RING; j++) theta[j] += (DT/6.0)*(k1[j] + 2*k2[j] + 2*k3[j] + k4[j]); } int ring_winding(double *theta) { double sum = 0; for (int j = 0; j < N_RING - 1; j++) { double d = theta[j+1] - theta[j]; if (d > M_PI) d -= 2*M_PI; if (d < -M_PI) d += 2*M_PI; sum += d; } return (int)lround(sum / (2*M_PI)); } int main() { init_lens(); float input[16]; FILE *f = fopen("/home/compunerd/agent_framework/bqsm_assist/test_input_16.bin", "rb"); fread(input, sizeof(float), 16, f); fclose(f); double theta[16]; for (int j = 0; j < 16; j++) { theta[j] = (double)input[j]; printf("[%2d] in=%.4f theta0=%.4f\\n", j, input[j], theta[j]); } for (int s = 0; s < SETTLE_STEPS; s++) rk4_step(theta); int q = ring_winding(theta); printf("\\nC winding q = %d\\n", q); for (int j = 0; j < 16; j++) printf("[%2d] theta_final=%.6f\\n", j, theta[j]); return 0; } ''' with open('/tmp/test_lens.c', 'w') as f: f.write(c_code) result = subprocess.run(['cc', '-O3', '-std=c11', '-lm', '/tmp/test_lens.c', '-o', '/tmp/test_lens'], capture_output=True, text=True) print(f"\nCompiling C lens: {result.returncode == 0}") result = subprocess.run(['/tmp/test_lens'], capture_output=True, text=True) print("\nC output:") print(result.stdout) # Compare print("=" * 50) print("CROSS-VALIDATION") print("=" * 50)