""" diagrams.py -- checks the diagrammatic formulas of section 5. For random undirected real quantum graphs X = (V,d), it computes hom(F,X), by putting the elements mu^*_k(1) at the vertices of F and the bilinear form psi(x d(y)) on the edges, and checks that w_k = hom(P_{k+1},X) and c_k = hom(C_k,X). It then enumerates all the cyclic orders around the vertices of some small graphs F, computes the genus of each drawing, and records the number of distinct complex values of hom(F,X) for each genus, compared with the number of drawings up to relabeling by the automorphisms of F, and also up to complex conjugation, compared with the number of drawings up to relabeling and reflection, and checks that reversing all the cyclic orders conjugates hom(F,X). Finally, for the tree F having a vertex of degree 4 with pendant paths of lengths 1, 1, 2, 2, whose 2 inequivalent plane drawings give psi(a^2 b^2) and psi(abab), with a = d(1) and b = d^2(1), it computes these values exactly, for the graph d(x) = pxp + 2qxq over M_2, and for d = Ad_A + Ad_B over M_3, from section 7, showing that different planar drawings can give different values. It then checks the spider with 6 pendant paths of length 1 and 6 of length 2, whose drawings with cyclic orders 121221122121 and 212112211212 are not related by a relabeling and a reflection, but give the same value over M_2: numerically, for random graphs, exactly for d(x) = pxp + 2qxq, and via the exact polynomial identity Tr(U^2 V U V^2) = Tr(V^2 U V U^2) for 2 x 2 matrices with 8 indeterminate entries, U = ab, V = ba. It checks that this is the only coincidence among the spiders with at most 12 paths of lengths 1, 2, that over M_3 the 2 values differ, exactly for d(x) = pxp + 2qxq + 3rxr, and that a random graph over M_3 separates the 224 drawings of the spiders with 12 such paths. Results are written to diagrams.log. """ import itertools import numpy as np from qspace import QSpace, Logger, random_cp rng = np.random.default_rng(3) LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" def spider(q, k): """mu^*_k(1), as a tensor with k legs""" T = q.one() for j in range(k - 1): T = (T.reshape(-1, q.N) @ q.mu_star().T).reshape(-1) return T.reshape([q.N] * k) def edge_form(q, d): E = np.eye(q.N) return np.array([[q.psi(q.product(E[i], d[:, j])) for j in range(q.N)] for i in range(q.N)]) def hom(q, d, rot): """rot: vertex -> list of edge labels, in cyclic order; each edge label appears twice""" E, subs, ops, legs, c = edge_form(q, d), [], [], {}, 0 scalar = 1 for v, hes in rot.items(): if not hes: # isolated vertex: psi(1) = N scalar *= q.N continue s = "" for he in hes: legs.setdefault(he, []).append(LETTERS[c]) s += LETTERS[c] c += 1 subs.append(s) ops.append(spider(q, len(hes))) for he, (a, b) in legs.items(): subs.append(a + b) ops.append(E) if not subs: return complex(scalar) return scalar * complex(np.einsum(",".join(subs) + "->", *ops, optimize=True)) def genus(rot): ends = {} for v, h in rot.items(): for e in h: ends.setdefault(e, []).append(v) seen, faces = set(), 0 for v, h in rot.items(): for e in h: if (v, e) in seen: continue faces += 1 cur = (v, e) while cur not in seen: seen.add(cur) vv, ee = cur a, b = ends[ee] w = b if a == vv else a hw = rot[w] cur = (w, hw[(hw.index(ee) + 1) % len(hw)]) V, E = len(rot), sum(len(h) for h in rot.values()) // 2 return (2 - V + E - faces) // 2 def count_distinct(values, rel=1e-9): """number of distinct complex numbers, up to a relative tolerance""" reps = [] for v in values: if not any(abs(v - r) <= rel * max(1.0, abs(v), abs(r)) for r in reps): reps.append(v) return len(reps) def all_drawings(adj): vs = list(adj) opts = [[[adj[v][0]] + list(p) for p in itertools.permutations(adj[v][1:])] for v in vs] for combo in itertools.product(*opts): yield dict(zip(vs, combo)) def drawing_orbits(adj, reflections=False): """number of drawings of F, up to relabeling by the automorphisms of F, and also up to reflection, reversing all the cyclic orders, if asked, for each genus""" vs = sorted(adj) ends = {} for v, h in adj.items(): for e in h: ends.setdefault(e, []).append(v) lab = {frozenset(p): e for e, p in ends.items()} auts = [dict(zip(vs, p)) for p in itertools.permutations(vs) if all(frozenset((p[vs.index(a)], p[vs.index(b)])) in lab for a, b in ends.values())] def norm(cyc): i = cyc.index(min(cyc)) return tuple(cyc[i:] + cyc[:i]) orbits = {} for rot in all_drawings(adj): key = None for r in ([rot, mirror(rot)] if reflections else [rot]): for s in auts: new = {s[v]: [lab[frozenset(s[x] for x in ends[e])] for e in r[v]] for v in vs} k = tuple(norm(new[v]) for v in vs) key = k if key is None or k < key else key orbits.setdefault(genus(rot), set()).add(key) return {g: len(o) for g, o in sorted(orbits.items())} def mirror(rot): return {v: [h[0]] + h[1:][::-1] for v, h in rot.items()} def random_undirected_real(q): A = random_cp(q, rng) d = A + q.adjoint(A) return d + q.sharp(d) if __name__ == "__main__": log = Logger("diagrams.log") graphs = { "bowtie": {0: ["a", "b", "c", "d"], 1: ["a", "x"], 2: ["b", "x"], 3: ["c", "y"], 4: ["d", "y"]}, "K4": {0: ["a", "b", "c"], 1: ["a", "z", "x"], 2: ["b", "x", "y"], 3: ["c", "y", "z"]}, "prism": {0: ["a", "b", "p"], 1: ["a", "c", "q"], 2: ["b", "c", "r"], 3: ["p", "d", "e"], 4: ["q", "d", "f"], 5: ["r", "e", "f"]}, "tree": {0: ["a", "b", "c", "d"], 1: ["a"], 2: ["b"], 3: ["c", "x"], 4: ["x"], 5: ["d", "y"], 6: ["y"]}, } for blocks in [[1, 1, 1], [2], [1, 2]]: q = QSpace(blocks) d = random_undirected_real(q) log(f"=== V with blocks {blocks}: undirected and real " f"{np.allclose(q.adjoint(d), d) and np.allclose(q.sharp(d), d)}") for k in range(1, 6): path = {0: ["e0"], k: [f"e{k-1}"]} for i in range(1, k): path[i] = [f"e{i-1}", f"e{i}"] cyc = {i: [f"e{(i-1) % k}", f"e{i}"] for i in range(k)} wk = q.psi(np.linalg.matrix_power(d, k) @ q.one()) ck = np.trace(np.linalg.matrix_power(d, k)) log(f" k = {k}: w_k = hom(P_k+1,X) {np.isclose(hom(q, d, path), wk)}, " f"c_k = hom(C_k,X) {np.isclose(hom(q, d, cyc), ck)}") for name, adj in graphs.items(): vals = {} for rot in all_drawings(adj): vals.setdefault(genus(rot), []).append(hom(q, d, rot)) log(f" {name}: number of distinct complex values of hom(F,X), by genus of the drawing: " f"{ {g: count_distinct(s) for g, s in sorted(vals.items())} }, drawings up to relabeling " f"{drawing_orbits(adj)}") log(f" {name}: values up to complex conjugation " f"{ {g: count_distinct([complex(v.real, abs(v.imag)) for v in s]) for g, s in sorted(vals.items())} }, " f"drawings up to relabeling and reflection {drawing_orbits(adj, True)}") conj = all(np.isclose(hom(q, d, mirror(rot)), np.conj(hom(q, d, rot))) for rot in all_drawings(adj)) log(f" {name}: reversing all the cyclic orders conjugates hom(F,X): {conj}") # isolated vertex, and the 2 plane drawings of the tree, computed exactly q = QSpace([2]) d = random_undirected_real(q) log(f"=== isolated vertex: hom = psi(1) = N {np.isclose(hom(q, d, {0: []}), q.N)}") from fractions import Fraction as Fr def frmat(rows): return [[Fr(x) for x in r] for r in rows] def mul(X, Y): return [[sum(X[i][k] * Y[k][j] for k in range(len(Y))) for j in range(len(Y[0]))] for i in range(len(X))] def add(X, Y, c=1): return [[X[i][j] + c * Y[i][j] for j in range(len(X[0]))] for i in range(len(X))] def tr(X): return sum(X[i][i] for i in range(len(X))) def ad(K, x): # K x K^*, for real K Kt = [list(r) for r in zip(*K)] return mul(mul(K, x), Kt) tree = {0: ["a", "b", "c", "d"], 1: ["a"], 2: ["b"], 3: ["c", "x"], 4: ["x"], 5: ["d", "y"], 6: ["y"]} for name, n, Ks, sq in [("M_2, d(x) = pxp + 2qxq", 2, [(frmat([[1, 0], [0, 0]]), 1), (frmat([[Fr(1, 2), Fr(1, 2)], [Fr(1, 2), Fr(1, 2)]]), 2)], False), ("M_3, d = Ad_A + Ad_B", 3, [(frmat([[1, 0, 0], [0, -1, 0], [0, 0, 0]]), Fr(3, 2)), (frmat([[0, 1, 0], [1, 0, 1], [0, 1, 0]]), Fr(3, 4))], True)]: dmap = lambda x: [[sum(c * ad(K, x)[i][j] for K, c in Ks) for j in range(n)] for i in range(n)] one = frmat(np.eye(n, dtype=int).tolist()) a = dmap(one) b = dmap(a) v1 = n * tr(mul(mul(a, a), mul(b, b))) # psi = n Tr v2 = n * tr(mul(mul(a, b), mul(a, b))) comm = add(mul(a, b), mul(b, a), -1) log(f"=== tree with pendant paths 1, 1, 2, 2, over {name}: psi(a^2 b^2) = {v1}, psi(abab) = {v2}, " f"difference {v1 - v2}, [a,b] = {[[str(x) for x in r] for r in comm]}") W = {} for w in ["121221122121", "212112211212"]: X = one for c in w: X = mul(X, a if c == "1" else b) W[w] = n * tr(X) log(f" spider with 12 arms, cyclic orders 121221122121 and 212112211212: values {W['121221122121']} and " f"{W['212112211212']}, equal {W['121221122121'] == W['212112211212']}") # numerical check of the 2 drawings, with the general contraction code qn = QSpace([n]) Kf = [(np.array(K, dtype=float), float(c)) for K, c in Ks] dn = sum(c * ad_np for ad_np in [None] for K, c in [] ) if False else \ sum(c * qn.matrix_of(lambda x, K=K: [K @ x[0] @ K.T]) for K, c in Kf) h1 = hom(qn, dn, {**tree, 0: ["a", "b", "c", "d"]}) h2 = hom(qn, dn, {**tree, 0: ["a", "c", "b", "d"]}) log(f" contractions of the 2 plane drawings: {h1.real:.6f}, {h2.real:.6f}, " f"matching {np.isclose(h1, float(v1)) and np.isclose(h2, float(v2))}") # spiders: a center with pendant paths of lengths 1 and 2, contracting to psi(word in a, b) log("=== spiders, with a center of degree k and pendant paths of lengths 1, 2") def spider_graph(lengths): rot, c = {0: []}, 1 for i, l in enumerate(lengths): prev, e = 0, f"s{i}_0" rot[0].append(e) for j in range(l): rot[c] = [e] if j == l - 1 else [e, f"s{i}_{j + 1}"] e = f"s{i}_{j + 1}" c += 1 return rot def word_value(q, d, lengths): a = q.to_blocks(d @ q.one())[0] b = q.to_blocks(d @ d @ q.one())[0] X = np.eye(a.shape[0], dtype=complex) for l in lengths: X = X @ (a if l == 1 else b) return q.blocks[0] * np.trace(X) q = QSpace([2]) d = random_undirected_real(q) for lengths in [(1, 2, 1, 1, 2, 2), (1, 1, 2, 1, 2, 2, 2, 1)]: h = hom(q, d, spider_graph(lengths)) log(f" lengths {lengths}: hom(F,X) = psi(word in a, b), read in the cyclic order " f"{np.isclose(h, word_value(q, d, lengths))}") def dihedral_min(w): n = len(w) return min(min(s[i:] + s[:i] for i in range(n)) for s in (w, w[::-1])) w1, w2 = "121221122121", "212112211212" log(f" 12 arms: w1 = {w1}, w2 = {w2}, palindromes {w1 == w1[::-1] and w2 == w2[::-1]}, " f"least rotations or reversals {dihedral_min(w1)}, {dihedral_min(w2)}, inequivalent {dihedral_min(w1) != dihedral_min(w2)}") ok, real = True, True for blocks in [[2]] * 5: q = QSpace(blocks) d = random_undirected_real(q) v1 = word_value(q, d, [int(c) for c in w1]) v2 = word_value(q, d, [int(c) for c in w2]) ok &= np.isclose(v1, v2, rtol=1e-10) real &= abs(v1.imag) < 1e-9 * abs(v1) log(f" over M_2, for 5 random undirected real X: equal values {ok}, real values {real}") # exact symbolic check of Tr(U^2 V U V^2) = Tr(V^2 U V U^2), for 2 x 2 matrices with 8 indeterminates def padd(P, Q): R = dict(P) for m, c in Q.items(): R[m] = R.get(m, 0) + c return {m: c for m, c in R.items() if c} def pmul(P, Q): R = {} for m1, c1 in P.items(): for m2, c2 in Q.items(): m = tuple(x + y for x, y in zip(m1, m2)) R[m] = R.get(m, 0) + c1 * c2 return {m: c for m, c in R.items() if c} def var(i): return {tuple(1 if j == i else 0 for j in range(8)): 1} def mmul(A, B): return [[padd(pmul(A[i][0], B[0][j]), pmul(A[i][1], B[1][j])) for j in range(2)] for i in range(2)] U = [[var(0), var(1)], [var(2), var(3)]] V = [[var(4), var(5)], [var(6), var(7)]] def ptr(word): M = {"U": U, "V": V} X = M[word[0]] for c in word[1:]: X = mmul(X, M[c]) return padd(X[0][0], X[1][1]) t1, t2 = ptr("UUVUVV"), ptr("VVUVUU") log(f" exact polynomial identity Tr(U^2VUV^2) = Tr(V^2UVU^2), for generic 2 x 2 matrices U, V: " f"{t1 == t2}, with {len(t1)} monomials, while Tr(UUVV) = Tr(UVUV) {ptr('UUVV') == ptr('UVUV')}") log(f" the 2 words in a, b: ab ab ba ab ba ba = {'ab ab ba ab ba ba'.replace(' ', '') == w1.replace('1', 'a').replace('2', 'b')}, " f"ba ba ab ba ab ab = {'babaabbaabab' == w2.replace('1', 'a').replace('2', 'b')}") # an exact example over M_3 where the 2 values differ: d(x) = pxp + 2qxq + 3rxr def proj(v): s2 = sum(x * x for x in v) return [[Fr(v[i] * v[j], s2) for j in range(len(v))] for i in range(len(v))] Ps = [proj([1, 0, 0]), proj([1, 1, 1]), proj([1, 2, 0])] one3 = frmat(np.eye(3, dtype=int).tolist()) def d3(x): res = [[Fr(0)] * 3 for _ in range(3)] for k, P in enumerate(Ps): res = add(res, mul(mul(P, x), P), k + 1) return res a3 = d3(one3) b3 = d3(a3) W3 = {} for w in [w1, w2]: X = one3 for c in w: X = mul(X, a3 if c == "1" else b3) W3[w] = 3 * tr(X) log(f" over M_3, d(x) = pxp + 2qxq + 3rxr, with p, q, r the projections onto (1,0,0), (1,1,1), (1,2,0): " f"exact values {W3[w1]} and {W3[w2]}, different {W3[w1] != W3[w2]}") # over M_3, a random quantum graph separates all the drawings of the spiders with 12 arms rng3 = np.random.default_rng(7) q = QSpace([3]) Ms3 = [] for t in range(3): A = random_cp(q, rng3, kraus=1) dd = A + q.adjoint(A) dd = dd + q.sharp(dd) a, b = q.to_blocks(dd @ q.one())[0], q.to_blocks(dd @ dd @ q.one())[0] sc = np.abs(np.linalg.eigvalsh(a)).max() Ms3.append((a / sc, b / sc ** 2)) ws = sorted({dihedral_min("".join(w)) for w in itertools.product("12", repeat=12)}) vecs = [] for w in ws: vec = [] for a, b in Ms3: X = np.eye(3, dtype=complex) for c in w: X = X @ (a if c == "1" else b) vec.append(np.trace(X)) vecs.append(np.array(vec)) gap = min(np.max(np.abs(vecs[i] - vecs[j]) / np.abs(vecs[i])) for i in range(len(ws)) for j in range(i + 1, len(ws)) if ws[i].count("2") == ws[j].count("2")) log(f" over M_3, random undirected real X: the {len(ws)} drawings of the spiders with 12 arms of lengths 1, 2, " f"up to relabeling and reflection, give distinct values, smallest relative difference {gap:.1e}") # all the spiders with at most 12 arms of lengths 1, 2: coincidences between inequivalent drawings for n_arms in range(2, 13): coinc, classes = [], 0 Ms = [] for blocks in [[2]] * 3: q = QSpace(blocks) dd = random_undirected_real(q) Ms.append((q.to_blocks(dd @ q.one())[0], q.to_blocks(dd @ dd @ q.one())[0])) for k in range(0, n_arms + 1): ws = sorted({dihedral_min("".join(w)) for w in itertools.product("12", repeat=n_arms) if w.count("2") == k}) classes += len(ws) vecs = [] for w in ws: vec = [] for a, b in Ms: X = np.eye(2, dtype=complex) for c in w: X = X @ (a if c == "1" else b) vec.append(np.trace(X)) vecs.append(np.array(vec)) for i in range(len(ws)): for j in range(i + 1, len(ws)): if np.allclose(vecs[i], vecs[j], rtol=1e-9, atol=0): coinc.append([ws[i], ws[j]]) if coinc or n_arms in (4, 8, 12): log(f" {n_arms} arms: {classes} drawings up to relabeling and reflection, coincidences of values over M_2: {coinc}") log.save()