import numpy as np

from qsim.etape1_un_qubit import ZERO
from qsim.etape2_portes import H, apply, is_unitary

# Exercice 2 : une porte conserve le produit scalaire ; une copie l'élèverait au carré
plus = apply(ZERO, H)
print("⟨0|+⟩       =", round(float(np.vdot(ZERO, plus).real), 3))
print("⟨0 0|+ +⟩   =", round(float(np.vdot(np.kron(ZERO, ZERO), np.kron(plus, plus)).real), 3))

# Une porte de deux qubits tirée au hasard
rng = np.random.default_rng(42)
gate, _ = np.linalg.qr(rng.normal(size=(4, 4)) + 1j * rng.normal(size=(4, 4)))
print("porte unitaire :", is_unitary(gate))

before = np.vdot(np.kron(ZERO, ZERO), np.kron(plus, ZERO))
after = np.vdot(gate @ np.kron(ZERO, ZERO), gate @ np.kron(plus, ZERO))
print("produit scalaire avant la porte :", round(float(abs(before)), 3))
print("produit scalaire après la porte :", round(float(abs(after)), 3))
