Download example.py from zeechimp/hv-tctn-universal: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/zeechimp/hv-tctn-universal/resolve/main/example.py
- Command line
-
hf download hf://zeechimp/hv-tctn-universal/example.py
-
curl -L -o example.py https://huggingface.co/zeechimp/hv-tctn-universal/resolve/main/example.py
3.55 kB
| #!/usr/bin/env python3 | |
| """ | |
| example.py — Run the universal TCTN on the four substrates. | |
| NumPy only. | |
| """ | |
| import numpy as np | |
| from hv_tctn_universal import ( | |
| substrate, measure_velocity, choose_delays, | |
| UniversalTCTN, make_drive, | |
| ) | |
| def demo_single_velocity(): | |
| print("=" * 72) | |
| print("Demo 1 — one velocity, one substrate") | |
| print("=" * 72) | |
| print() | |
| spec = substrate('qubit_8') | |
| v_true = 2.0 | |
| v_hat, conf = measure_velocity(spec, v_true, noise=0.01) | |
| print(f" substrate: qubit_8") | |
| print(f" v_true: {v_true:.2f}") | |
| print(f" v_hat: {v_hat:.3f}") | |
| print(f" rel error: {abs(v_hat - v_true) / v_true * 100:.1f}%") | |
| print(f" confidence: {conf:.4f}") | |
| print() | |
| def demo_velocity_sweep(): | |
| print("=" * 72) | |
| print("Demo 2 — velocity sweep, one substrate") | |
| print("=" * 72) | |
| print() | |
| spec = substrate('spaced_16') | |
| print(f" {'v_true':>8s} {'v_hat':>8s} {'rel_err':>9s}") | |
| print(" " + "-" * 30) | |
| for v in [0.5, 1.0, 2.0, 4.0, 8.0, -2.0, -4.0]: | |
| v_hat, _ = measure_velocity(spec, v) | |
| err = abs(v_hat - v) / abs(v) | |
| print(f" {v:>8.2f} {v_hat:>8.3f} {err:>8.1%}") | |
| print() | |
| def demo_manual_pipeline(): | |
| print("=" * 72) | |
| print("Demo 3 — manual pipeline (step-by-step)") | |
| print("=" * 72) | |
| print() | |
| positions = np.linspace(0.0, 1.0, 8) | |
| response_tau = np.full(8, 0.1) | |
| delays = choose_delays(positions, v_min=0.5, v_max=8.0, n_delays=30) | |
| v_true = 2.0 | |
| dt = 0.02 | |
| n_steps = 200 | |
| # Generate drive manually | |
| drive = make_drive(positions, response_tau, v_true, | |
| t_center=1.0, n_steps=n_steps, dt=dt, mode='pulse') | |
| # Feed into TCTN one step at a time | |
| tctn = UniversalTCTN(positions, delays, dt, differentiate=False) | |
| tctn.reset() | |
| for t in range(n_steps): | |
| tctn.step(drive[t]) | |
| v_hat, conf = tctn.classify(k_top=3) | |
| print(f" v_true: {v_true:.2f}") | |
| print(f" v_hat: {v_hat:.3f}") | |
| print(f" rel error: {abs(v_hat - v_true) / v_true * 100:.1f}%") | |
| print(f" confidence: {conf:.4f}") | |
| print(f" cell peak max: {tctn.cell_peak.max():.6f}") | |
| print() | |
| def demo_custom_substrate(): | |
| print("=" * 72) | |
| print("Demo 4 — custom substrate (12 sensors, mixed τ)") | |
| print("=" * 72) | |
| print() | |
| spec = { | |
| 'positions': np.linspace(0.0, 1.0, 12), | |
| 'response_tau': np.geomspace(0.02, 0.4, 12), | |
| 'mode': 'step', | |
| 'differentiate': True, | |
| 'description': 'custom 12-sensor photodiode array', | |
| } | |
| print(f" {spec['description']}") | |
| print(f" {'v_true':>8s} {'v_hat':>8s} {'rel_err':>9s}") | |
| print(" " + "-" * 30) | |
| for v in [1.0, 2.0, 4.0, -1.0, -2.0, -4.0]: | |
| v_hat, _ = measure_velocity(spec, v) | |
| err = abs(v_hat - v) / abs(v) | |
| print(f" {v:>8.2f} {v_hat:>8.3f} {err:>8.1%}") | |
| print() | |
| def demo_noise_ladder(): | |
| print("=" * 72) | |
| print("Demo 5 — noise ladder") | |
| print("=" * 72) | |
| print() | |
| spec = substrate('spaced_8') | |
| print(f" {'noise':>8s} {'v_hat':>8s} {'rel_err':>9s}") | |
| print(" " + "-" * 30) | |
| for noise in [0.0, 0.01, 0.02, 0.05, 0.10, 0.20]: | |
| v_hat, _ = measure_velocity(spec, 2.0, noise=noise, seed=0) | |
| err = abs(v_hat - 2.0) / 2.0 | |
| print(f" {noise:>8.3f} {v_hat:>8.3f} {err:>8.1%}") | |
| print() | |
| def main(): | |
| demo_single_velocity() | |
| demo_velocity_sweep() | |
| demo_manual_pipeline() | |
| demo_custom_substrate() | |
| demo_noise_ladder() | |
| if __name__ == '__main__': | |
| main() |