import numpy as np


def h(state):
    """Transforme (a, b) en ((a + b)/√2, (a − b)/√2) : la porte H, vue en détail en 4.4."""
    a, b = state
    return np.array([a + b, a - b]) / np.sqrt(2)


def read(state, shots, rng):
    zeros = int(np.sum(rng.random(shots) < state[0] ** 2))
    return {"0": zeros, "1": shots - zeros}


plus = np.array([1.0, 1.0]) / np.sqrt(2)
minus = np.array([1.0, -1.0]) / np.sqrt(2)

rng = np.random.default_rng(42)
for name, state in [("|+⟩", plus), ("|−⟩", minus)]:
    after_h = h(state)
    print(name, "après h :", np.round(after_h, 3), " 1 000 lectures :", read(after_h, 1000, rng))
