""" cycles.py -- checks the results on trees and cycles from section 7. Over V = M_n, it checks that d = sum_i Ad_{W_i}, with W_i an orthonormal basis of a self-adjoint traceless subspace S, is simple, undirected, positively weighted and without loops, with E = n^2 dim(S)/2 edges, and ker L equal to the commutant of S, as well as Tr L = 2E and rank L = N - c. It checks the quantum square, the explicit connected non-regular graph over M_3 with E = N, and that random pairs A, B over M_3, M_4 never give A^2 + B^2 scalar. It then checks the complete bipartite quantum graphs K_{V,W}, the quantum stars, and the examples showing that the classical characterizations of trees diverge: a connected graph over M_2 + M_2 with E = N - 2, and tau = 4248/15625 computed exactly, via the principal minors of L, and Ad_h over M_3 with negative cyclomatic number. Results are written to cycles.log. """ import numpy as np from scipy.linalg import null_space from qspace import QSpace, Logger, close, ad, weyl log = Logger("cycles.log") rng = np.random.default_rng(8) def blockmap(q, r, s, K): """x -> K^* x_r K, from the block r to the block s, normalized to be a Hadamard projection""" def f(x, c=1.0): out = [np.zeros((n, n), dtype=complex) for n in q.blocks] out[s] = c * K.conj().T @ x[r] @ K return out A = q.matrix_of(f) B = q.schur_fast(A, A) lam = np.vdot(A.flatten(), B.flatten()) / np.vdot(A.flatten(), A.flatten()) return A / lam def invariants(q, d): L = q.laplacian(d) S = np.sqrt(q.G) @ L @ np.sqrt(q.Ginv) ev = np.linalg.eigvalsh((S + S.conj().T) / 2) pi0 = int(np.sum(np.abs(ev) < 1e-8)) E = q.psi(d @ q.one()).real / 2 tau = np.prod(ev[np.abs(ev) > 1e-8]) / q.N return E, pi0, tau, ev def props(q, d): return bool(close(q.schur_fast(d, d), d) and close(q.adjoint(d), d) and close(q.sharp(d), d) and close(q.schur_fast(d, q.identity()), 0 * d)) def sa_orthonormal(n, k): """k random self-adjoint traceless matrices, orthonormal for Tr(XY)/n""" vs = [] for i in range(k): X = rng.normal(size=(n, n)) + 1j * rng.normal(size=(n, n)) X = X + X.conj().T vs.append(X - np.trace(X) / n * np.eye(n)) R = np.array([np.concatenate([v.real.flatten(), v.imag.flatten()]) for v in vs]).T Q, _ = np.linalg.qr(R) return [np.sqrt(n) * (Q[:n * n, i] + 1j * Q[n * n:, i]).reshape(n, n) for i in range(k)] for n in [2, 3, 4]: q = QSpace([n]) ok, trL = True, True for k in [1, 2, 3]: W = sa_orthonormal(n, k) d = sum(ad(q, w) for w in W) E, pi0, tau, ev = invariants(q, d) alg = [np.eye(n)] + W comm = null_space(np.array([np.kron(w, np.eye(n)) - np.kron(np.eye(n), w.T) for w in W]).reshape(-1, n * n)) ok &= props(q, d) and np.isclose(E, n * n * k / 2) and pi0 == comm.shape[1] trL &= np.isclose(np.trace(q.laplacian(d)).real, 2 * E) and np.linalg.matrix_rank(q.laplacian(d), tol=1e-8) == q.N - pi0 log(f"M_{n}: sum of Ad_W over orthonormal self-adjoint traceless W is simple, undirected, real, " f"loopless, with E = n^2 k/2 and ker L = commutant: {ok}, and Tr L = 2E, rank L = N - c: {trL}") q = QSpace([2]) d = ad(q, weyl(2, (1, 0))) + ad(q, weyl(2, (0, 1))) E, pi0, tau, ev = invariants(q, d) log(f"quantum square: simple, undirected, real, loopless {props(q, d)}, E = {E:.1f}, |pi_0| = {pi0}, regular of valence 2 " f"{close(d @ q.one(), 2 * q.one())}, tau = {tau:.4f}, L spectrum {np.round(ev, 4)}") W = sa_orthonormal(2, 2) # conjugating a random 2-dimensional S over M_2 to span(sigma_x, sigma_z) a = np.array([np.trace(W[0] @ s).real / 2 for s in [weyl(2, (1, 0)), np.array([[0, -1j], [1j, 0]]), weyl(2, (0, 1))]]) b = np.array([np.trace(W[1] @ s).real / 2 for s in [weyl(2, (1, 0)), np.array([[0, -1j], [1j, 0]]), weyl(2, (0, 1))]]) log(f"random 2-dimensional S over M_2: W_i^2 = 1 {all(close(w @ w, np.eye(2)) for w in W)}, " f"Pauli vectors orthonormal {np.isclose(a @ a, 1) and np.isclose(b @ b, 1) and np.isclose(a @ b, 0)}") q = QSpace([3]) A = np.sqrt(1.5) * np.diag([1, -1, 0]) B = np.sqrt(3) / 2 * np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) d = ad(q, A) + ad(q, B) E, pi0, tau, ev = invariants(q, d) log(f"M_3 example A = sqrt(3/2) diag(1,-1,0), B = sqrt(3)/2 (e12+e21+e23+e32): simple, undirected, real, loopless {props(q, d)}, " f"E = {E:.1f} = N, connected {pi0 == 1}, regular {close(d @ q.one(), (d @ q.one())[0] * q.one())}") for n in [3, 4]: best = np.inf for t in range(2000): W = sa_orthonormal(n, 2) M = W[0] @ W[0] + W[1] @ W[1] best = min(best, np.linalg.norm(M - np.trace(M) / n * np.eye(n))) log(f"M_{n}: over 2000 random pairs, min distance from A^2 + B^2 to the scalars = {best:.4f}") def complete_bipartite(bv, bw): q = QSpace(bv + bw) r0 = len(bv) def f(x): out = [] pv = sum(q.blocks[r] * np.trace(x[r]) for r in range(r0)) pw = sum(q.blocks[r] * np.trace(x[r]) for r in range(r0, len(q.blocks))) for r, n in enumerate(q.blocks): out.append((pw if r < r0 else pv) * np.eye(n)) return out return q, q.matrix_of(f) for bv, bw in [([1], [2]), ([1], [3]), ([2], [2]), ([1, 1], [2]), ([2], [1, 2])]: q, d = complete_bipartite(bv, bw) E, pi0, tau, ev = invariants(q, d) p, r = sum(n * n for n in bv), sum(n * n for n in bw) spec = sorted([0.0] + [p + r] + [float(r)] * (p - 1) + [float(p)] * (r - 1)) log(f"K_V,W with V blocks {bv}, W blocks {bw}: simple, undirected, real, loopless {props(q, d)}, connected {pi0 == 1}, " f"E = {E:.0f} = N_V N_W {np.isclose(E, p * r)}, Laplacian spectrum of K_{p},{r} {np.allclose(ev, spec)}, " f"tau = p^(r-1) r^(p-1) {np.isclose(tau, p ** (r - 1) * r ** (p - 1))}") q = QSpace([2, 2]) K = np.diag([1.0, 2.0]) d = blockmap(q, 0, 1, K) + blockmap(q, 1, 0, K.conj().T) + blockmap(q, 0, 0, weyl(2, (1, 0))) E, pi0, tau, ev = invariants(q, d) log(f"M_2 + M_2 example, K = diag(1,2) between the blocks, sigma_x on the first block: simple, undirected, real, loopless {props(q, d)}, " f"positively weighted, E = {E:.1f}, N = {q.N}, connected {pi0 == 1}, tau = {tau:.4f}") # exact value: the Laplacian has rational entries, and tau = (1/N) sum of the principal (N-1)-minors from fractions import Fraction def det_exact(M): M = [row[:] for row in M] n, det = len(M), Fraction(1) for c in range(n): p = next((r for r in range(c, n) if M[r][c] != 0), None) if p is None: return Fraction(0) if p != c: M[c], M[p] = M[p], M[c] det = -det det *= M[c][c] for r in range(c + 1, n): f = M[r][c] / M[c][c] M[r] = [M[r][k] - f * M[c][k] for k in range(n)] return det Lq = q.laplacian(d) Lf = [[Fraction(x.real).limit_denominator(10 ** 4) for x in row] for row in Lq] rational = np.allclose(np.array([[float(x) for x in r] for r in Lf]), Lq, atol=1e-12) minors = sum(det_exact([[Lf[i][j] for j in range(q.N) if j != k] for i in range(q.N) if i != k]) for k in range(q.N)) tau_exact = minors / q.N log(f" exact: Laplacian rational {rational}, tau = {tau_exact} = {float(tau_exact)}, equal to 4248/15625 " f"{tau_exact == Fraction(4248, 15625)}, matching the eigenvalue product {np.isclose(float(tau_exact), tau)}") q = QSpace([3]) h = np.sqrt(1.5) * np.diag([1, -1, 0]) d = ad(q, h) E, pi0, tau, ev = invariants(q, d) log(f"Ad_h over M_3, h = sqrt(3/2) diag(1,-1,0): simple, undirected, real, loopless {props(q, d)}, E = {E:.1f}, N = 9, " f"|pi_0| = {pi0}, cyclomatic number E - N + |pi_0| = {E - 9 + pi0:.1f}") log.save()