from math import exp
def channel(delay_s, t1_s, t2_s, state):
    if min(delay_s, t1_s, t2_s) < 0 or not t1_s or not t2_s:
        raise ValueError("times must be positive")
    p0, p1, coherence = state
    survival, contrast = exp(-delay_s / t1_s), exp(-delay_s / t2_s)
    return p0 + p1 * (1 - survival), p1 * survival, coherence * contrast, coherence.conjugate() * contrast
state = (.2, .8, .3 + .1j)
boundary = channel(0.0, 30e-6, 20e-6, state)
baseline = channel(20e-6, 30e-6, 20e-6, state)
counterfactual = channel(1e-3, 30e-6, 20e-6, state)
assert boundary == (.2, .8, .3 + .1j, .3 - .1j)
assert all(abs(row[0] + row[1] - 1) < 1e-15 and row[2] == row[3].conjugate() for row in (boundary, baseline, counterfactual))
assert counterfactual[1] < 1e-14 and abs(counterfactual[2]) < 1e-20
print(f"PASS: 45 channel evidence population={baseline[1]:.6f} coherence={abs(baseline[2]):.6f} long_population={counterfactual[1]:.3e} trace={baseline[0]+baseline[1]:.1f}")
