#!/usr/bin/env python3
"""Supergeometry preprint, Sec. 3: graded quantumness of a tetrahedron in an octahedral cavity.

Eight equivalent positions = the 8 faces of the cavity, labelled by sign triples s in {+1,-1}^3.
Hopping amplitude t = 1 between positions on adjacent faces (sign triples differing in one sign),
so the hopping graph is a cube. An external influence adds an on-site energy e_s:
  field    e_s = -f (s.d)        uniform field along unit vector d
  stretch  e_s = -f (s.d)^2      axial stretch (favours the two polar positions)
  squeeze  e_s = +f (s.d)^2      axial squeeze (favours the equatorial belt)
The ground state of H = -A + diag(e) gives probabilities p_k = |c_k|^2.
Participation ratio PR = 1 / sum p_k^2: PR = 8 all positions equally, PR = 1 a single position.

Run:  python3 supergeometry_table.py        (needs numpy only)
"""
import itertools
import numpy as np

S = np.array(list(itertools.product([1, -1], repeat=3)), float)
A = np.array([[1.0 if np.sum(a != b) == 1 else 0.0 for b in S] for a in S])  # cube graph
STRENGTHS = [0, 0.3, 1, 2, 4, 10, 30]


def participation_ratio(kind, d, f):
    d = np.asarray(d, float)
    d = d / np.linalg.norm(d)
    sd = S @ d
    e = {"field": -f * sd, "stretch": -f * sd**2, "squeeze": f * sd**2}[kind]
    w, v = np.linalg.eigh(-A + np.diag(e))
    p = v[:, 0] ** 2
    return 1.0 / np.sum(p**2)


ROWS = [
    ("field along a body diagonal", "field", (1, 1, 1)),
    ("field, generic direction", "field", (1, 0.4, 0.15)),
    ("field along a cube axis", "field", (0, 0, 1)),
    ("axial stretch along a body diagonal", "stretch", (1, 1, 1)),
    ("axial squeeze along a body diagonal", "squeeze", (1, 1, 1)),
]

if __name__ == "__main__":
    print("adjacency spectrum of the cube graph:", np.round(np.linalg.eigvalsh(A), 6))
    print(f"{'influence':40s}" + "".join(f"{s:>7}t" for s in STRENGTHS))
    for name, kind, d in ROWS:
        print(f"{name:40s}" + "".join(f"{participation_ratio(kind, d, f):8.1f}" for f in STRENGTHS))
