import numpy as np

from qsim import H, apply_cnot, apply_single, zero_state

np.set_printoptions(precision=3, suppress=True)


def reduced_first(state):
    """Matrice densité du qubit 0 seul, d'un état de 2 qubits (trace partielle sur le qubit 1)."""
    m = state.reshape(2, 2)  # ligne = qubit 0, colonne = qubit 1
    return m @ m.conj().T


bell = apply_cnot(apply_single(zero_state(2), H, 0), 0, 1)
product = apply_single(zero_state(2), H, 0)  # |+>|0>, sans intrication

for name, state in (("Bell", bell), ("|+>|0>", product)):
    rho = reduced_first(state)
    print(f"{name} : qubit 0 seul =")
    print(rho)
    print("    tr(ρ²) =", round(float(np.trace(rho @ rho).real), 3))
