import numpy as np

from qsim import H, ONE, ZERO, apply, make_state, measure

rng = np.random.default_rng(42)
PLUS = make_state(1 / np.sqrt(2), 1 / np.sqrt(2))


def source_plus():
    """Source A : prépare |+> à chaque fois."""
    return PLUS


def source_coin():
    """Source B : tire à pile ou face, puis prépare |0> ou |1>."""
    return ZERO if rng.random() < 0.5 else ONE


def run(source, gate=None, shots=1000):
    counts = {"0": 0, "1": 0}
    for _ in range(shots):
        state = source()
        if gate is not None:
            state = apply(state, gate)
        outcome, _ = measure(state, rng)
        counts[str(outcome)] += 1
    return counts


print("mesure directe   A :", run(source_plus), " B :", run(source_coin))
print("H, puis mesure   A :", run(source_plus, H), " B :", run(source_coin, H))
