# Données de la figure « la phase relative, réglée pas à pas » : jamais affiché comme extrait.
# Même calcul que 04-relative-phase.py, sur huit réglages de φ (un huitième de tour) et une courbe de 65 points.
import cmath
import json

import numpy as np
from common import h
from qsim.etape1_un_qubit import make_state, probabilities

s = 1 / np.sqrt(2)
LABELS = ["0", "π/4", "π/2", "3π/4", "π", "5π/4", "3π/2", "7π/4"]


def reading(phi):
    state = make_state(s, s * cmath.exp(1j * phi))
    a, b = state
    return {
        "a": [round(float(a.real), 3) + 0.0, round(float(a.imag), 3) + 0.0],
        "b": [round(float(b.real), 3) + 0.0, round(float(b.imag), 3) + 0.0],
        "direct": [round(float(p), 3) for p in probabilities(state)],
        "after_h": [round(float(p), 3) + 0.0 for p in probabilities(h(state))],
    }


settings = [{"label": label, "phi": round(k * np.pi / 4, 6), **reading(k * np.pi / 4)} for k, label in enumerate(LABELS)]
curve = [[round(phi, 4), round(float(probabilities(h(make_state(s, s * cmath.exp(1j * phi))))[0]), 4) + 0.0]
         for phi in np.linspace(0, 2 * np.pi, 65)]
print(json.dumps({"settings": settings, "curve": curve}, ensure_ascii=False))
