def main(): if argv.code == "RM14": s = """ 1111111111111111 .1.1.1.1.1.1.1.1 ..11..11..11..11 ....1111....1111 ........11111111 """ else: s = argv.get("G") if s is None: G = parse("111") #G = parse("1. .1") #G = parse("11. .11") #G = parse("1.. .11") else: G = parse(s) print(G) w1 = genus_enum1(G) w2 = genus_enum2(G) #print(w1) print("w1 =") print(w1.flatstr()) #print(w2) print("w2 =") print(w2.flatstr()) items = get_phi_2(w2) #for w in items: # print("\t", w.flatstr()) w = sum(items) print("sum(items) =") print(w.flatstr()) print("diff:") print((w2 - w).flatstr()) w3 = genus_enum3(G) print("w3 =") print(w3.flatstr()) #for w in items: # print("\t", w.flatstr(), w.flatstr()==w2.flatstr()) items = get_phi_3(w3, w2) #w = sum(items) #print("diff:") #print((w3 - w).flatstr()) if 0: w4 = genus_enum4(G) print("w4 =") print(w4.flatstr()) #for w in items: # print("\t", w.flatstr(), w.flatstr()==w2.flatstr()) items = get_phi_4(w4, w3)
def test_hamming(): G = parse(""" 1....111 .1..1.11 ..1.11.1 ...1111. """) assert strong_morthogonal(G, 2)
def test(): G = parse("11 ..") test_enum(G) G = parse("11. .11") #test_enum(G, verbose=True) #print(p.flatstr()) #return #G = parse("11.. .11. 1..1") #test_enum(G) for m in range(1, 5): for n in range(m, 6): for G in all_codes(m, n): test_enum(G) print("OK")
def parse(self, decl): A = solve.parse(decl) decl = decl.replace('.', '0') rows = decl.split() rows = [l.strip() for l in rows if l.strip()] promote = self.ring.promote rows = [list(promote(int(c)) for c in l) for l in rows] assert rows n = len(rows[0]) for row in rows: assert len(row) == n, "rows have varying lengths" A = numpy.array(rows, dtype=object) m, n = A.shape src = Space(self.ring, n) tgt = Space(self.ring, m) return Lin(tgt, src, A)
def main_torus(): n = 8 # ZZZZZZZZ|XXXXXXXX # 12345678|12345678 H = parse(""" 111..1..|........ 1..1....|1.1..... ........|11.11... .1..1...|.1...1.. ..1...1.|...1..1. ...11.11|........ .....1.1|....1..1 ........|..1..111 """.replace("|", "")) print() print("H=") print(shortstr(H)) F = symplectic(n) C = dot2(H, dot2(F, H.transpose())) for i in range(n): for j in range(i + 1, n): if C[i, j]: print("fail:", i + 1, j + 1) print(rank(H)) H = linear_independent(H) print("H=") print(shortstr(H)) HF = dot2(H, F) K = array2(find_kernel(HF)) print("K=") print(shortstr(K)) HK = numpy.concatenate((H, K)) L = linear_independent(HK) print() print(shortstr(L))
def test_main(): for ring in [element.Q, element.FiniteField(2), element.FiniteField(7)]: #for ring in [element.FiniteField(2)]: cx = Assembly.build_tetrahedron(ring) chain = cx.get_chain() #chain.dump() test_chain(chain) M = parse(""" 1.1..1 11.1.. .11.1. ...111 """) chain = Chain.from_array(M, ring) test_chain(chain) #cx = Assembly.build_torus(ring, 2, 2) cx = Assembly.build_surface(ring, (0, 0), (2, 2)) chain = cx.get_chain() test_chain(chain)
continue x, y = layout[key] cvs.stroke(path.circle(x, y, r), st_thick + [orange]) # --------------------------------------------------------- ring = element.FiniteField(2) if 0: # ------------------------------------ M = parse(""" 11..... 1.1.... .1.11.. ..11.1. ....1.1 .....11 """) #print(M) M = numpy.array([[1, 1, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 1, 0], [0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1]]) chain = Chain.fromnumpy(M, ring) verts = chain.cells[0] for i, v in enumerate(verts): x = (i // 2) y = -(i % 2) v.pos = x, y
def parse(self, decl): ring = self.ring A = solve.parse(decl) B = self.zeros(*A.shape) B = B + A return B
def test_dual(): from vec import Space, Hom, Map import element ring = element.Z #ring = element.Q one = ring.one space = Space(2, ring) hom = Hom(space, space) I = Map.from_array([[1, 0], [0, 1]], hom) X = Map.from_array([[0, 1], [1, 0]], hom) Z = Map.from_array([[1, 0], [0, -1]], hom) assert X+X == (2*X) assert X*Z == -Z*X assert (X@X) * (Z@Z) == (Z@Z) * (X@X) assert (I@X@X) * (I@Z@Z) == (I@Z@Z) * (I@X@X) if argv.code == "repitition": G = parse("111") H = parse(""" 11. .11""") elif argv.code == "steane": G = parse(""" 1111... .1.11.1 ..11.11 """) else: return code = Code(G) if argv.dual: code = code.get_dual() dual = code.get_dual() code.dump() #W = lambda A, B : (A@A@A + B@B@B) #WD = lambda A, B : (A@A@A + B@B@A + B@A@B + A@B@B) W = code.tensor_enum WD = dual.tensor_enum a = 2**len(code.G) b = 2**len(dual.G) A = WD(I, X) B = W(I, Z) assert A*B == B*A AA = W(I+X, I-X) BB = WD(I+Z, I-Z) assert AA == a*A assert BB == b*B assert AA*BB == a*b*A*B #print(W(I+X, I-X)) #print(WD(I, X)) #print(W(I, Z)) src = Space(2, ring) tgt = Space(3, ring) hom = Hom(src, tgt) A = Map.from_array([ [1, 2], [5, 6], [7, 8]], hom) B = Map.from_array([ [7, 2], [6, 3], [5, 4]], hom) assert W(A+B, A-B) == a*WD(A, B) assert WD(A+B, A-B) == b*W(A, B)
def get_code(): name = argv.get("code") code = None if name == "toric": G = parse(""" 1.1..... .1...1.. 11.11... .111..1. 1...11.1 """) # l=2 toric code X logops + X stabs elif name == "toric3": G = parse(""" .....1.....1.....1 ............1.1.1. .111..........1... ...111..........1. 1.....11...1...... ..1....111........ ....1....111...... ......1.....11...1 ........1....111.. ..........1....111 """) # l=3 toric code X logops + X stabs elif name == "steane": G = parse(""" 1111111 1111... .1.11.1 ..11.11 """) elif name == "haah": # triorthogonal matrix G = parse(""" 1111111....... .......1111111 1.1.1.11.1.1.1 .11..11.11..11 ...1111...1111 """) G = parse(""" 1.1.1.11.1.1.1 .11..11.11..11 ...1111...1111 """) G = parse(""" ..............1111111111111111 ......11111111........11111111 ..1111....1111....1111....1111 .1..11..11..11..11..11..11..11 11.1.1.1.1.1.1.1.1.1.1.1.1.1.1 """) elif name == "rm": r = argv.get("r", 1) m = argv.get("m", 3) code = reed_muller(r, m) if argv.puncture: code = code.puncture(0) elif name == "rand": n = argv.get("n", 8) m = argv.get("m", 4) G = rand2(m, n) code = Code(G) else: assert 0, "no code chosen" if code is None: code = Code(G) if argv.dual: code = code.get_dual() return code
def search_extend(): # Extend the checks of a random code to make it triorthogonal. # Based on the search function above. verbose = argv.get("verbose") m = argv.get("m", 6) n = argv.get("n", m+2) k = argv.get("k") # odd _numbered rows ( logical operators) code = argv.get("code", "rand") if code == "rand": while 1: G0 = rand2(m, n) counts = G0.sum(0) if min(counts)==2 and rank(G0) == m: cols = set() for i in range(n): cols.add(tuple(G0[:, i])) if len(cols) == n: # no repeated cols break elif code == "toric": G0 = parse(""" 11.11... .111..1. 1...11.1 """) # l=2 toric code X logops + X stabs l = argv.get("l", 3) G0 = build_toric(l) m, n = G0.shape else: return code = Code(G0, check=False) print(shortstr(G0)) print("is_triorthogonal:", code.is_triorthogonal()) # these are the variables N_x xs = list(cross([(0, 1)]*m)) N = len(xs) lookup = {} for i, x in enumerate(xs): lookup[x] = i lhs = [] rhs = [] taken = set() for i in range(n): x = G0[:, i] idx = lookup[tuple(x)] assert idx not in taken taken.add(idx) if verbose: for idx in range(N): print(idx, xs[idx], "*" if idx in taken else "") for idx in taken: v = zeros2(N) v[idx] = 1 lhs.append(v) rhs.append(1) # bi-orthogonality for a in range(m): for b in range(a+1, m): v = zeros2(N) for i, x in enumerate(xs): if x[a] == x[b] == 1: v[i] += 1 assert v.sum() lhs.append(v) rhs.append(0) # tri-orthogonality for a in range(m): for b in range(a+1, m): for c in range(b+1, m): v = zeros2(N) for i, x in enumerate(xs): if x[a] == x[b] == x[c] == 1: v[i] += 1 assert v.sum() lhs.append(v) rhs.append(0) # dissallow columns with weight <= 1 for i, x in enumerate(xs): if sum(x)<=1: v = zeros2(N) v[i] = 1 lhs.append(v) rhs.append(0) if k is not None: # constrain to k _number of odd-weight rows assert 0<=k<m for a in range(m): v = zeros2(N) for i, x in enumerate(xs): if x[a] == 1: v[i] = 1 lhs.append(v) if a<k: rhs.append(1) else: rhs.append(0) A = array2(lhs) rhs = array2(rhs) if verbose: print("lhs:") print(shortstr(A)) print("rhs:") print(shortstr(rhs)) B = pseudo_inverse(A) soln = dot2(B, rhs) if not eq2(dot2(A, soln), rhs): print("no solution") return if verbose: print("soln:") print(shortstr(soln)) soln.shape = (N, 1) rhs.shape = A.shape[0], 1 K = array2(list(find_kernel(A))) best = None density = 1.0 size = 9999*n trials = argv.get("trials", 1024) count = 0 for trial in range(trials): u = rand2(len(K), 1) v = dot2(K.transpose(), u) #print(v) assert dot2(A, v).sum()==0 #if v.sum() != n: # continue assert v[0]==0 v = (v+soln)%2 assert eq2(dot2(A, v), rhs) Gt = list(G0.transpose()) for i, x in enumerate(xs): if v[i] and not i in taken: Gt.append(x) if not Gt: continue Gt = array2(Gt) G = Gt.transpose() if verbose: print("G0") print(shortstr(G0)) print("solution:") print(shortstr(G)) assert is_morthogonal(G, 3) if G.shape[1]<m: continue if 0 in G.sum(1): continue #print(shortstr(G)) # for g in G: # print(shortstr(g), g.sum()) # print() _density = float(G.sum()) / (G.shape[0]*G.shape[1]) #if best is None or _density < density: if best is None or G.shape[1] < size: best = G density = _density size = G.shape[1] if 0: #sols.append(G) Gx = even_rows(G) assert is_morthogonal(Gx, 3) if len(Gx)==0: continue GGx = array2(list(span(Gx))) assert is_morthogonal(GGx, 3) count += 1 print("found %d solutions" % count) G = best #print(shortstr(G)) for g in G: print(shortstr(g), g.sum()) print() print("density:", density)
def test_matrix(ring): one = ring.one zero = ring.zero rows = cols = [Cell(0, i) for i in [0, 1]] I = Matrix.from_array([[1, 0], [0, 1]], ring, rows, cols) X = Matrix.from_array([[0, 1], [1, 0]], ring, rows, cols) Z = Matrix.from_array([[1, 0], [0, -1]], ring, rows, cols) assert I * X == X assert X * X == I assert Z * Z == I assert ring.one == -ring.one or X * Z != Z * X assert X * Z == -Z * X assert (-1) * I == -I assert (I + -I).is_zero() II = I @ I XI = X @ I IZ = I @ Z assert XI * XI == II assert XI * IZ == X @ Z M = parse(""" 1.1..1 11.1.. .11.1. ...111 """) A = Matrix.from_array(M, ring) #print(A) A.check() if 0: print("kernel:") print(A.kernel()) print("image:") print(A.image()) print("cokernel:") print(A.cokernel()) At = A.dual() #print(A*At) At.check() M = [[1, 0, 1], [1, 1, 0]] C = Chain.from_array(M, ring) Ct = C.dual() #Ct.dump() #print(Ct.cells) #print(Ct.get_cells(-1)) #M = Ct.get_bdymap(-1) #print(M.cols, M.rows) Ct.check() #C.dump() #Ct.dump() A = C @ Ct #test_chain(A) A.check()
def main(): ring = element.Q #ring = element.FiniteField(2) # tetra-hedron face map: M = parse(""" 1.1..1 11.1.. .11.1. ...111 """) chain = Chain.from_array(M, ring) chain.check() M = chain.get_bdymap(1) print(M) rows, cols = M.rows, M.cols flow = Flow(chain) #flow.build() # match goes from src:grade-1 --> tgt:grade # rows are faces, cols are edges assert flow.accept(rows[1], cols[0]) flow.add(rows[1], cols[0]) assert flow.accept(rows[2], cols[1]) flow.add(rows[2], cols[1]) assert flow.accept(rows[3], cols[4]) flow.add(rows[3], cols[4]) print(flow) A = flow.get_bdymap(2) # 2 --> 1 B = flow.get_bdymap(1) # 1 --> 0 print(A, A.shape) print(B, B.shape) print(B.cols, B.rows) C = B * A assert C.is_zero() f0 = flow.get_f(0) f1 = flow.get_f(1) f2 = flow.get_f(2) assert f0 * chain.get_bdymap(1) == B * f1 assert f1 * chain.get_bdymap(2) == A * f2 g0 = flow.get_g(0) g1 = flow.get_g(1) g2 = flow.get_g(2) assert chain.get_bdymap(1) * g1 == g0 * B assert chain.get_bdymap(2) * g2 == g1 * A fs = [f0, f1, f2] gs = [g0, g1, g2] chi0 = flow.get_homotopy(0) chi1 = flow.get_homotopy(1) chi2 = flow.get_homotopy(2) chis = [chi0, chi1, chi2] for i in [1, 2]: cells = chain.get_cells(i) I = Matrix.identity(cells, ring) lhs = gs[i] * fs[i] - I rhs = chain.get_bdymap(i + 1) * chis[i] + chis[i - 1] * chain.get_bdymap(i) assert lhs == rhs #print(lhs) cells = flow.get_critical(i) I = Matrix.identity(cells, ring) lhs = fs[i] * gs[i] #print(lhs) #print(I) assert lhs == I print() for grade in range(2): print("grade =", grade) print("f =") print(fs[grade]) print("g =") print(gs[grade]) print("chi =") print(chis[grade]) #print("rows:", chis[grade].rows) #print("cols:", chis[grade].cols) print("fg =") print(fs[grade] * gs[grade]) print("gf =") print(gs[grade] * fs[grade]) print() print(flow.get_bdymap(1))