# Données de la figure « erreur cohérente contre erreur aléatoire » : jamais affiché comme extrait.
# Mêmes formules que 07-exercise-3.py (ε = 0,05 rad, même probabilité d'erreur par porte), tracées de 0 à 60 portes :
# la courbe cohérente est un sin², qui atteint son maximum vers 63 portes puis redescendrait. On s'arrête avant.
import json

import numpy as np

EPSILON = 0.05
p_flip = np.sin(EPSILON / 2) ** 2
LAST = 60


def coherent(n):
    return np.sin(n * EPSILON / 2) ** 2


def pauli(n):
    return (1 - (1 - 2 * p_flip) ** n) / 2


def first_above(error, level=0.10):
    n = 1
    while error(n) <= level:
        n += 1
    return n


n_coherent = first_above(coherent)
n_pauli = first_above(pauli)
print(json.dumps({
    "epsilon": EPSILON,
    "p_flip": round(float(p_flip), 5),
    "last": LAST,
    "peak": round(float(np.pi / EPSILON), 1),  # n ε / 2 = π / 2 : maximum de la courbe cohérente
    "coherent": [[n, round(float(coherent(n)), 4) + 0.0] for n in range(LAST + 1)],
    "pauli": [[n, round(float(pauli(n)), 4) + 0.0] for n in range(LAST + 1)],
    "level": 0.10,
    "crossing": {
        "coherent": {"n": n_coherent, "value": round(float(coherent(n_coherent)), 3)},
        "pauli": {"n": n_pauli, "value": round(float(pauli(n_pauli)), 3)},
    },
}, ensure_ascii=False))
