from fractions import Fraction as Q
from komorebi_browser import show_plot

a, b = Q(3, 5), Q(4, 5)
if a*a+b*b != 1:
    raise ValueError("Choose real a and b with a*a+b*b = 1.")
coordinate = (a*a, b*b)
# H(a, ib) = ((a+ib)/sqrt(2), (a-ib)/sqrt(2)).
phase_i = ((a*a+b*b)/2, (a*a+b*b)/2)
phase_1 = ((a+b)**2/2, (a-b)**2/2)
purity = sum(p*p for p in coordinate)
print(f"Coordinate probabilities: {coordinate[0]}, {coordinate[1]}")
print(f"Hadamard probabilities with phase i: {phase_i[0]}, {phase_i[1]}")
print(f"Hadamard probabilities with phase 1: {phase_1[0]}, {phase_1[1]}")
print("Purity after coordinate dephasing:", purity)
show_plot([
    {"type": "bar", "x": ["Outcome 0", "Outcome 1"], "y": list(map(float, coordinate)), "name": "Coordinate basis, either state"},
    {"type": "bar", "x": ["Outcome 0", "Outcome 1"], "y": list(map(float, phase_i)), "name": "Hadamard basis, phase i"},
    {"type": "bar", "x": ["Outcome 0", "Outcome 1"], "y": list(map(float, phase_1)), "name": "Hadamard basis, phase 1"},
], {"title": {"text": "Relative phase changes interference"}, "barmode": "group",
    "yaxis": {"title": {"text": "Probability"}, "range": [0, 1]}},
    description="Both states have coordinate probabilities 9/25 and 16/25. After a Hadamard transformation, the phase-i state has probabilities one half and one half; the real state has probabilities 49/50 and 1/50. Printed fractions are exact; bar heights use floating-point conversion.")
