import numpy as np

from common import MINUS, PLUS, bit_oracle, input_register, rounded
from qsim import apply, kron


def logical_xor(x0, x1):
    return x0 ^ x1


# Entrée en superposition uniforme sur 2 qubits, cible |−⟩
state = apply(kron(PLUS, PLUS, MINUS), bit_oracle(logical_xor, 2))

register = input_register(state)
print("la cible est toujours |−⟩ :", np.allclose(state, kron(register, MINUS)))
for x, amplitude in enumerate(register):
    x0, x1 = x >> 1, x & 1
    print(f"|{x0}{x1}⟩ : f(x) = {logical_xor(x0, x1)}, amplitude {rounded(amplitude):+.3f}")
