"""Check one Wilson plaquette in the defining representation of SU(2).

Run with Python 3. The rational rotations form an SO(2) subgroup of SU(2).
Three identity links leave one variable rotation around an oriented face.
"""
from fractions import Fraction as Q
import json


def compare(name, cosine, sine):
    # The holonomy has rows (cosine, -sine), (sine, cosine).
    assert cosine**2 + sine**2 == 1
    trace = 2 * cosine
    beta_lgt = Q(3, 2)
    beta_sub = 2 * beta_lgt
    normalized_cost = 1 - trace / 2
    source_action = beta_sub * normalized_cost
    comparison_action = beta_lgt * (2 - trace)
    assert source_action == comparison_action
    return {
        "holonomy": name,
        "trace": str(trace),
        "action": str(source_action),
        "unconvertedAction": str(beta_lgt * normalized_cost),
    }


rows = [
    compare("Identity", Q(1), Q(0)),
    compare("Rational rotation", Q(3, 5), Q(4, 5)),
    compare("Inverse rotation", Q(3, 5), Q(-4, 5)),
    compare("Negative identity", Q(-1), Q(0)),
]
assert [row["action"] for row in rows] == ["0", "6/5", "6/5", "6"]
assert rows[1]["unconvertedAction"] == "3/5"
print(json.dumps(rows, indent=2))
