def bit_flip_zero(p): return ((1-p, 0.0), (0.0, p))
def dephase_plus(p): return ((0.5, 0.5*(1-2*p)), (0.5*(1-2*p), 0.5))
def trace(rho): return rho[0][0] + rho[1][1]
def x_minus_probability(rho): return 0.5*(1 - 2*rho[0][1])
def readout_confuse(p_one, e=.05): return e*(1-p_one) + (1-e)*p_one
rho_x = bit_flip_zero(.1); rho_z = dephase_plus(.1)
identity_case = bit_flip_zero(0.0); complete_dephase = dephase_plus(0.5)
assert abs(trace(rho_x)-1) < 1e-12 and abs(trace(rho_z)-1) < 1e-12
assert abs(rho_x[1][1]-.1) < 1e-12
assert abs(x_minus_probability(rho_z)-.1) < 1e-12
assert abs(readout_confuse(.1)-.14) < 1e-12
assert identity_case == ((1.0, 0.0), (0.0, 0.0)) and complete_dephase[0][1] == 0.0
print(f"PASS: 41 noise bitflip_population={rho_x[1][1]:.3f} dephase_coherence={rho_z[0][1]:.3f} complete_dephase={complete_dephase[0][1]:.1f}")
