import numpy as np

from common import alice_gates, branch, rounded, share_pair
from qsim import H, apply, make_state

states = {
    "|0⟩": make_state(1, 0),
    "|1⟩": make_state(0, 1),
    "0,6|0⟩ + 0,8i|1⟩": make_state(0.6, 0.8j),
    "|+⟩": make_state(1 / np.sqrt(2), 1 / np.sqrt(2)),
}
for name, psi in states.items():
    state = alice_gates(share_pair(psi))
    p_bits, p0_z, p0_x = [], 0.0, 0.0
    for m0 in (0, 1):
        for m1 in (0, 1):
            p, bob = branch(state, m0, m1)
            p_bits.append(rounded(p))
            # Bob mesure tout de suite, sans connaître m0 m1 : on moyenne sur les résultats d'Alice
            p0_z += p * abs(bob[0]) ** 2  # lecture directe
            p0_x += p * abs(apply(bob, H)[0]) ** 2  # lecture après une H
    print(f"{name:<17} | P(00, 01, 10, 11) chez Alice = {p_bits} | "
          f"Bob lit 0 : {rounded(p0_z):.3f} (direct), {rounded(p0_x):.3f} (après H)")
