import numpy as np
from common import bob_zero
from qsim.etape2_portes import H, I, X, Z

CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
bell = np.array([1, 0, 0, 1]) / np.sqrt(2)
decode = np.kron(H, I) @ CNOT                  # CNOT, puis H sur le qubit 0 (leçon 5.6)

for bit, gate in [(0, I), (1, Z)]:
    shared = np.kron(gate, I) @ bell           # Alice code son bit sur son qubit
    # 1. Bob n'a que son qubit
    alone = round(bob_zero(shared, False), 3)
    # 2. Alice envoie son qubit à Bob, qui a alors les deux
    decoded = decode @ shared
    reading = format(int(np.argmax(np.abs(decoded))), "02b")
    print(f"bit {bit} : Bob seul lit 0 avec la probabilité {alone} ; avec les deux qubits, il lit {reading}")

# Avec I, X, Z ou X puis Z, Alice code deux bits : quatre états de Bell, quatre lectures
for name, gate in [("I", I), ("X", X), ("Z", Z), ("X puis Z", Z @ X)]:
    reading = format(int(np.argmax(np.abs(decode @ np.kron(gate, I) @ bell))), "02b")
    print(f"Alice applique {name:<9}: avec les deux qubits, Bob lit {reading}")
