import numpy as np

from common import diffusion, phase_oracle, rounded, uniform_state
from qsim import apply

np.set_printoptions(precision=3, suppress=True, floatmode="fixed")

n, marked = 3, ["101"]
state = apply(uniform_state(n), phase_oracle(marked, n))
print("après l'oracle    :", rounded(state))
print("moyenne           :", rounded(state.mean()))

# À la main : chaque amplitude a devient 2 · moyenne − a
by_hand = 2 * state.mean() - state
print("2 · moyenne − a   :", rounded(by_hand))

# Avec la matrice de diffusion, construite à partir de portes H
print("même résultat que la diffusion :", np.allclose(apply(state, diffusion(n)), by_hand))

# La même diffusion, écrite D = 2|u⟩⟨u| − I, avec |u⟩ la superposition uniforme
u = uniform_state(n)
print("D = 2|u⟩⟨u| − I :", np.allclose(diffusion(n), 2 * np.outer(u, u) - np.eye(2**n)))
