""" distance.py -- checks the Kuperberg-Weaver formula for the distance of section 5. For random positively weighted quantum graphs over V = M_2 + C + M_3, given by random sparse Kraus operators K in the blocks of B(H), H = C^2 + C + C^3, and random projections p,q in C(V), it compares the smallest k with q d^k(p) nonzero, and the smallest k with p V^k q nonzero, where V is the span of the Kraus operators. It also checks the symmetry of the distance, for undirected graphs, and that positive weights are needed, the signed 4-cycle with one edge of weight -1 having d^2 = 2, so that opposite vertices are at infinite distance. Results are written to distance.log. """ import numpy as np from qspace import QSpace, Logger log = Logger("distance.log") rng = np.random.default_rng(11) blocks = [2, 1, 3] q = QSpace(blocks) off = np.cumsum([0] + blocks) H = off[-1] def haar(n): z = rng.normal(size=(n, n)) + 1j * rng.normal(size=(n, n)) Q, R = np.linalg.qr(z) return Q * (np.diag(R) / abs(np.diag(R))) def random_projection(n): k = rng.integers(0, n + 1) W = haar(n)[:, :k] return W @ W.conj().T def block_diag(bl): M = np.zeros((H, H), dtype=complex) for r, x in enumerate(bl): M[off[r]:off[r + 1], off[r]:off[r + 1]] = x return M def random_graph(undirected): Ks = [] for r in range(3): for s in range(3): if rng.random() < 0.4: K = rng.normal(size=(blocks[r], blocks[s])) + 1j * rng.normal(size=(blocks[r], blocks[s])) K[rng.random(K.shape) < 0.5] = 0 M = np.zeros((H, H), dtype=complex) M[off[r]:off[r + 1], off[s]:off[s + 1]] = K Ks.append(M) if undirected: Ks.append(np.sqrt(blocks[s] / blocks[r]) * M.conj().T) def dmap(x): X = block_diag(x) Y = sum(K.conj().T @ X @ K for K in Ks) return [Y[off[r]:off[r + 1], off[r]:off[r + 1]] for r in range(3)] return q.matrix_of(dmap), Ks def dist_walks(D, p, qq): x = q.to_vec(p) for k in range(15): if np.linalg.norm(q.product(q.to_vec(qq), x)) > 1e-9: return k x = D @ x return None def dist_kw(Ks, p, qq): P, Q, words = block_diag(p), block_diag(qq), [np.eye(H)] for k in range(15): if any(np.linalg.norm(P @ W @ Q) > 1e-9 for W in words): return k M = np.array([(W @ K).flatten() for W in words for K in Ks]) if len(M) == 0: return None u, sv, vh = np.linalg.svd(M, full_matrices=False) words = [vh[i].reshape(H, H) for i in range(len(sv)) if sv[i] > 1e-9] return None for undirected in [False, True]: agree, sym, tested, hist = True, True, 0, {} for g in range(10): D, Ks = random_graph(undirected) for t in range(30): p = [random_projection(n) if rng.random() < 0.6 else np.zeros((n, n)) for n in blocks] qq = [random_projection(n) if rng.random() < 0.6 else np.zeros((n, n)) for n in blocks] if all(np.allclose(x, 0) for x in p) or all(np.allclose(x, 0) for x in qq): continue a, b = dist_walks(D, p, qq), dist_kw(Ks, p, qq) agree &= (a == b) if undirected: sym &= (a == dist_walks(D, qq, p)) hist[a] = hist.get(a, 0) + 1 tested += 1 log(f"{'undirected' if undirected else 'arbitrary'} graphs: {tested} pairs, formulas agree {agree}" + (f", distance symmetric {sym}" if undirected else "") + f", distances found {dict(sorted(hist.items(), key=str))}") # positivity is needed: the 4-cycle with 3 edges of weight 1 and one of weight -1 d = np.array([[0, 1, 0, -1], [1, 0, 1, 0], [0, 1, 0, 1], [-1, 0, 1, 0]]) pw = [np.linalg.matrix_power(d, k) for k in range(1, 9)] log(f"signed 4-cycle, with d_14 = -1: d^2 = 2 {np.array_equal(pw[1], 2 * np.eye(4, dtype=int))}, " f"(d^k)_13 = 0 for k = 1..8 {all(P[0, 2] == 0 for P in pw)}, so dist(e_1,e_3) = infinity, " f"while the support graph is the 4-cycle, with distance 2") log.save()