""" qspace.py -- finite quantum spaces V, with C(V) = M_{n_1}(C) + ... + M_{n_k}(C), taken with their multimatrix basis e^r_{ab}, written in single indices i = (r,a,b), and with the canonical trace psi = sum_r n_r Tr (so that psi(1) = N = |V|). A quantum graph over V is an arbitrary matrix d in M_N(C), regarded as a linear map C(V) -> C(V) in the multimatrix basis: d(e_j) = sum_i d[i,j] e_i. The module provides the 4 basic operations on such matrices: composition (matrix product), adjoint (with respect to = psi(y^*x)), the Hadamard product A.B = mu(A x B)mu^*, and the sharp operation A#(x) = A(x^*)^*. """ import numpy as np class QSpace: def __init__(self, blocks): self.blocks = list(blocks) self.index = [] # i -> (r, a, b) for r, n in enumerate(self.blocks): for a in range(n): for b in range(n): self.index.append((r, a, b)) self.N = len(self.index) self.pos = {t: i for i, t in enumerate(self.index)} self.size = np.array([self.blocks[r] for (r, a, b) in self.index], dtype=float) self.G = np.diag(self.size) # Gram matrix: = n_i delta_ij self.Ginv = np.diag(1 / self.size) # index calculus def mult(self, i, j): r, a, b = self.index[i] s, c, d = self.index[j] if r == s and b == c: return self.pos[(r, a, d)] return None def bar(self, i): r, a, b = self.index[i] return self.pos[(r, b, a)] # elements of C(V) as coordinate vectors, and back to block lists def to_blocks(self, v): out = [np.zeros((n, n), dtype=complex) for n in self.blocks] for i, (r, a, b) in enumerate(self.index): out[r][a, b] = v[i] return out def to_vec(self, bl): return np.array([bl[r][a, b] for (r, a, b) in self.index], dtype=complex) def one(self): return self.to_vec([np.eye(n) for n in self.blocks]) def psi(self, v): return sum(self.blocks[r] * np.trace(m) for r, m in enumerate(self.to_blocks(v))) def inner(self, x, y): # = psi(y^* x), linear at left return np.vdot(y, self.G @ x) def product(self, x, y): bx, by = self.to_blocks(x), self.to_blocks(y) return self.to_vec([bx[r] @ by[r] for r in range(len(self.blocks))]) def star(self, x): return self.to_vec([m.conj().T for m in self.to_blocks(x)]) # linear maps C(V) -> C(V) given by a function on block lists def matrix_of(self, f): M = np.zeros((self.N, self.N), dtype=complex) for j in range(self.N): e = np.zeros(self.N, dtype=complex) e[j] = 1 M[:, j] = self.to_vec(f(self.to_blocks(e))) return M # multiplication mu : C(V) x C(V) -> C(V), and its adjoint def mu(self): M = np.zeros((self.N, self.N * self.N), dtype=complex) for i in range(self.N): for j in range(self.N): k = self.mult(i, j) if k is not None: M[k, i * self.N + j] = 1 return M def mu_star(self): G2 = np.kron(self.G, self.G) return np.linalg.inv(G2) @ self.mu().conj().T @ self.G # the 4 operations def adjoint(self, A): return self.Ginv @ A.conj().T @ self.G def schur(self, A, B): C = np.zeros((self.N, self.N), dtype=complex) for m in range(self.N): for p in range(self.N): s = 0 for i in range(self.N): for j in range(self.N): if self.mult(i, j) != m: continue for k in range(self.N): for l in range(self.N): if self.mult(k, l) == p: s += A[i, k] * B[j, l] C[m, p] = s / self.size[p] return C def schur_fast(self, A, B): return self.mu() @ np.kron(A, B) @ self.mu_star() def sharp(self, A): S = np.zeros_like(A) for m in range(self.N): for j in range(self.N): S[m, j] = np.conj(A[self.bar(m), self.bar(j)]) return S # special quantum graphs def J(self): # J(x) = psi(x) 1, unit of the Hadamard product one = self.one() psi_e = np.array([self.psi(np.eye(self.N)[j]) for j in range(self.N)]) return np.outer(one, psi_e) def identity(self): return np.eye(self.N, dtype=complex) def simplex(self): return self.J() - self.identity() # degree, Laplacian (Lindblad form) def degree(self, d): return d @ self.one() def laplacian(self, d): deg = self.to_blocks(self.degree(d)) Lmul = self.matrix_of(lambda x: [deg[r] @ x[r] for r in range(len(x))]) Rmul = self.matrix_of(lambda x: [x[r] @ deg[r] for r in range(len(x))]) return (Lmul + Rmul) / 2 - d # --------------------------------------------------------------------------- # helpers shared by the programs planar.py, simplex.py, weyl.py, products.py, # rigidity.py, laplacian.py and walks.py def close(A, B, tol=1e-9): return np.max(np.abs(A - B)) < tol class Logger: """prints and records lines, then writes them to a log file""" def __init__(self, name): self.name, self.lines = name, [] def __call__(self, s): print(s) self.lines.append(s) def save(self): open(self.name, "w").write("\n".join(self.lines) + "\n") def random_cp(qs, rng, kraus=2): """random completely positive map on C(V), d(x)_s = sum K^* x_r K""" n = qs.blocks Ks = {(r, s, k): rng.normal(size=(n[r], n[s])) + 1j * rng.normal(size=(n[r], n[s])) for r in range(len(n)) for s in range(len(n)) for k in range(kraus)} def g(x): res = [np.zeros((n[s], n[s]), dtype=complex) for s in range(len(n))] for (r, s, k), K in Ks.items(): res[s] += K.conj().T @ x[r] @ K return res return qs.matrix_of(g) def random_matrix(qs, rng): return rng.normal(size=(qs.N, qs.N)) + 1j * rng.normal(size=(qs.N, qs.N)) def weyl(n, g): """Weyl matrix U_g = U^{g_1} Lambda^{g_2}, with U the shift and Lambda the clock""" U = np.roll(np.eye(n), 1, axis=0) # U e_a = e_{a+1} Lam = np.diag(np.exp(2j * np.pi * np.arange(n) / n)) # Lambda e_a = omega^a e_a return np.linalg.matrix_power(U, g[0] % n) @ np.linalg.matrix_power(Lam, g[1] % n) def ad(qs, X, Y=None): """the map x -> X x Y^* on C(V) = M_n(C)""" Y = X if Y is None else Y return qs.matrix_of(lambda x: [X @ x[0] @ Y.conj().T]) def to_tensor(qs, d): """the isomorphism (M_N(C), o, #) -> C(V) x C(V)^op, theta_{a,b} -> a x b^*, with C(V)^op realized via the transpose, as block matrices indexed by pairs of blocks (r, s)""" T = {(r, s): np.zeros((nr * ns, nr * ns), dtype=complex) for r, nr in enumerate(qs.blocks) for s, ns in enumerate(qs.blocks)} for i, (r, a, b) in enumerate(qs.index): for j, (s, c, e) in enumerate(qs.index): ns = qs.blocks[s] T[r, s][a * ns + c, b * ns + e] += d[i, j] / ns return T def from_tensor(qs, T): d = np.zeros((qs.N, qs.N), dtype=complex) for i, (r, a, b) in enumerate(qs.index): for j, (s, c, e) in enumerate(qs.index): ns = qs.blocks[s] d[i, j] = ns * T[r, s][a * ns + c, b * ns + e] return d