import numpy as np

from qsim.etape2_portes import H, ry, rz

# Exercice 2 : des portes à un qubit, même nombreuses, ne créent pas d'intrication
rng = np.random.default_rng(42)


def random_single():
    """Une porte à un qubit au hasard : RZ · RY · RZ, angles tirés au hasard."""
    a, b, c = rng.uniform(0, 2 * np.pi, size=3)
    return rz(a) @ ry(b) @ rz(c)


def entanglement_test(state):
    """|ad − bc| pour a|00⟩ + b|01⟩ + c|10⟩ + d|11⟩ : 0 si et seulement si l'état est séparable."""
    a, b, c, d = state
    return abs(a * d - b * c)


state = np.array([1, 0, 0, 0], dtype=complex)  # |00⟩
for _ in range(10):
    state = np.kron(random_single(), random_single()) @ state
print("10 couches de portes à un qubit : |ad − bc| =", round(entanglement_test(state), 3))

CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
bell = CNOT @ np.kron(H, np.eye(2)) @ np.array([1, 0, 0, 0])
print("H puis CNOT               : |ad − bc| =", round(entanglement_test(bell), 3))
