//! Filter-function control + noise spectroscopy in the browser (`wai.quantum.cal.*`, //! frequency domain). The SAME deterministic fixed-point DSP a native sink runs, //! compiled to wasm32: reconstruct a *hidden* noise PSD from a dynamical-decoupling //! filter bank, and reshape a DD sequence so its filter's stopband sits over the //! noise — byte-exact, each sealing a `wai.quantum.calibration` receipt. The //! matched pair that extends the calibration suite to correlated / 1-f noise. use ed25519_dalek::SigningKey; use wai::quantum_control::{ dd_cpmg, optimize_dd, psd_model, reconstruct_psd, NoiseGrid, RobustResult, SpectroResult, }; use wai::quantum_ops::GrantRef; use wasm_bindgen::prelude::*; const ONE: f64 = (1u64 << 20) as f64; fn f(v: i64) -> f64 { v as f64 / ONE } fn signer() -> SigningKey { SigningKey::from_bytes(&[0x51; 32]) } fn grant() -> GrantRef { GrantRef::unbounded("quantum.calibrate") } fn base_grid() -> NoiseGrid { NoiseGrid::linear(0.5, 90.0, 180) } // =========================================================================== // 1. Noise spectroscopy — recover a hidden PSD from a DD filter bank // =========================================================================== #[wasm_bindgen] pub struct SpectroDemo { g: NoiseGrid, res: SpectroResult, } #[wasm_bindgen] impl SpectroDemo { /// Reconstruct a hidden `1/f + Lorentzian-bump` PSD from a bank of CPMG /// filters, each a narrow-band probe. `seed` varies the sampling noise. #[wasm_bindgen(constructor)] pub fn new(seed: f64) -> SpectroDemo { let g = base_grid(); let psd = psd_model(&g, 0.02, 0.6, 25.0, 5.0); let res = reconstruct_psd(&g, &psd, 20, 40_000, 40, seed as u64); SpectroDemo { g, res } } pub fn omega(&self) -> Vec { self.g.omega.iter().map(|&w| f(w)).collect() } pub fn psd_true(&self) -> Vec { self.res.psd_true.iter().map(|&v| f(v)).collect() } pub fn psd_est(&self) -> Vec { self.res.psd_est.iter().map(|&v| f(v)).collect() } pub fn n_sequences(&self) -> u32 { self.res.n_sequences as u32 } pub fn error(&self) -> f64 { f(self.res.error_fx) } pub fn mean_psd(&self) -> f64 { f(self.res.psd_true.iter().sum::() / self.res.psd_true.len().max(1) as i64) } pub fn receipt_json(&self) -> String { self.res.seal(&signer(), "did:key:demo-lab", 1_000_000, grant()).to_json() } pub fn receipt_verifies(&self) -> bool { self.res.seal(&signer(), "did:key:demo-lab", 1_000_000, grant()).verify() } } // =========================================================================== // 2. Filter-function robust control — reshape a DD sequence to dodge the PSD // =========================================================================== #[wasm_bindgen] pub struct RobustFFDemo { g: NoiseGrid, psd: Vec, res: RobustResult, n: u32, } #[wasm_bindgen] impl RobustFFDemo { /// Reshape an `n`-pulse DD sequence to minimise the filter overlap against a /// `1/f + bump` PSD whose bump sits right under equal-spaced CPMG's passband. #[wasm_bindgen(constructor)] pub fn new(n: u32, seed: f64) -> RobustFFDemo { let g = base_grid(); // bump location jittered by seed so "new spectrum" reshapes differently let w0 = 22.0 + (seed as u64 % 12) as f64; let psd = psd_model(&g, 0.02, 0.6, w0, 5.0); let res = optimize_dd(&g, &psd, n as usize, 40); RobustFFDemo { g, psd, res, n } } pub fn omega(&self) -> Vec { self.g.omega.iter().map(|&w| f(w)).collect() } pub fn psd(&self) -> Vec { self.psd.iter().map(|&v| f(v)).collect() } pub fn kern_cpmg(&self) -> Vec { self.res.kern_cpmg.iter().map(|&v| f(v)).collect() } pub fn kern_opt(&self) -> Vec { self.res.kern_opt.iter().map(|&v| f(v)).collect() } pub fn chi_cpmg(&self) -> f64 { f(self.res.chi_cpmg) } pub fn chi_opt(&self) -> f64 { f(self.res.chi_opt) } pub fn coh_cpmg(&self) -> f64 { f(self.res.coh_cpmg) } pub fn coh_opt(&self) -> f64 { f(self.res.coh_opt) } pub fn improvement(&self) -> f64 { f(self.res.improvement_fx()) } pub fn n_pulses(&self) -> u32 { self.n } /// Equal-spaced CPMG pulse positions (in [0,1]). pub fn pulses_cpmg(&self) -> Vec { dd_cpmg(self.n as usize).iter().map(|&p| f(p)).collect() } /// The optimised (reshaped) pulse positions. pub fn pulses_opt(&self) -> Vec { self.res.pulses.iter().map(|&p| f(p)).collect() } pub fn receipt_json(&self) -> String { self.res.seal(&signer(), "did:key:demo-lab", 1_000_000, grant()).to_json() } pub fn receipt_verifies(&self) -> bool { self.res.seal(&signer(), "did:key:demo-lab", 1_000_000, grant()).verify() } }