File size: 3,547 Bytes
6e99a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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()