def subtype_testdata(): yield XLeech2(~PLoop()) yield XLeech2(Cocode([0, 1, 2, 3])) yield XLeech2(~PLoop(list(range(8)))) yield XLeech2(~PLoop(list(range(8))), Cocode([8, 9])) for i in range(50): yield XLeech2('r', 4)
def iter_Q_x0(): yield G() yield G("x", 0x1000) yield G("x", PLoop(range(8))) yield G("x", PLoop([ 0, 4, 8, 12, 16, 20, 2, 7, 10, ])) yield G("x", 0x800)
def calc_conjugate_to_opp(i0, i1): coc = Cocode([i0, i1]) g = G('d', coc) v = V15('I', i0, i1) coc_value = coc.ord x = PLoop(coc_value & -coc_value) return G('x', x)
def test_parity(): odd, even = Parity(3), Parity(-4) assert odd != even assert Parity(-311) == odd assert int(odd) == 1 assert int(even) == 0 assert (-1)**odd == -1 assert (-1)**even == 1 assert 1**odd == 1**even == 1 assert odd + 1 == odd - 1 == 1 + odd == odd + odd == 7 - odd == even assert odd * odd == -odd == odd assert odd * even == even * odd == 2 * odd == even * 3 == even print("\nPossible parities are:", odd, ",", even) with pytest.raises(ValueError): 2**even with pytest.raises(TypeError): even & even cc1, cc0 = Cocode(0x823), Cocode(0x7ef) assert cc1 + even == even + cc1 == cc0 + odd == odd + cc0 == odd assert cc1 + odd == odd + cc1 == cc0 + even == even + cc0 == even assert odd * cc1 == cc1 * odd == cc1 ccn = Cocode(0) assert even * cc1 == cc1 * even == even * cc0 == cc0 * even == ccn pl = PLoop(0x1234) gc = GCode(pl) assert even * pl == even * gc == GCode(0) assert odd * pl == odd * gc == gc aut = AutPL('e', 'r') assert odd * aut == odd assert even * aut == even
def test_cocode(): print("") for i in range(200): # Test power map, inveriosn, sign, theta, and conversion to GCode n1 = randint(0, 0x1fff) p1 = PLoop(n1) ccvector = randint(0, 0xffffff) coc = mat24.vect_to_cocode(ccvector) cclist = [i for i in range(24) if (ccvector >> i) & 1] cc1 = Cocode(cclist) cc2 = Cocode(coc) if i < 1: print("\nTesting", GcVector(ccvector), ", cocode =", cc1) assert cc1 == cc2 u = Parity(mat24.scalar_prod(p1.value, cc1.value)) assert p1 & cc1 == u == cc1 & p1 == u * 1 == u + 0 par = Parity(randint(0, 1)) assert cc1 + par == par + cc1 == cc1.value // 0x800 + par assert cc1 % 2 == Parity(cc1) assert len(cc1) == mat24.cocode_weight(cc1.value) if len(cc1) < 4: syndrome = mat24.cocode_syndrome(cc1.value) assert cc1.syndrome().value == syndrome syn_from_list = sum(1 << i for i in GcVector(ccvector).syndrome_list()) assert syn_from_list == syndrome i = randint(0, 23) assert cc1.syndrome(i).value == mat24.cocode_syndrome(cc1.value, i) syndrome_list = cc1.syndrome(i).bit_list assert len(cc1) == len(syndrome_list) assert syndrome_list == mat24.cocode_to_bit_list(cc1.value, i)
def test_xyz(n_cases): g1 = group() for i in range(n_cases): v = randint(0, 0x1fff) for tag in "xyz": g = group(tag, v) assert g == group(tag, PLoop(v)) group('x', v) * group('y', v) * group('z', v) == g1
def display_leech_vector(x): """Display a vector in the Leech lattice mod 2 Here parameter ``x`` is a vector in the Leech lattice mod 2 in Leech lattice encoding. """ gcode = PLoop(x >> 12) bl = gcode.bit_list print("GCode:\n", bl) if len(bl) == 16: l = [x for x in range(24) if not x in bl] pos = bl[0] elif len(gcode): pos = bl[0] else: pos = 0 cocode = Cocode(x) + gcode.theta() print("Cocode:", cocode.syndrome_list(pos))
def test_octads(): print("") OMEGA = ~PLoop(0) for i in range(200): no = randint(0, 758) o = Octad(no) assert o == PLoop(mat24.octad_to_gcode(no)) assert o == Octad(sample(o.bit_list, 5)) assert o.octad == no o_signbit, o_cpl = randint(0, 1), randint(0, 1) signed_o = PLoopZ(o_signbit, o_cpl) * o assert signed_o.octad == no assert signed_o.sign == (-1)**o_signbit assert o.gcode == mat24.octad_to_gcode(no) assert signed_o.gcode == (o * PLoopZ(0, o_cpl)).gcode assert signed_o.split_octad() == (o_signbit, o_cpl, o) nsub = randint(0, 63) sub = (-1)**o_signbit * SubOctad(no, nsub) sub1 = (-1)**o_signbit * SubOctad(o, nsub) sub_weight = mat24.suboctad_weight(nsub) assert sub == sub1 assert o == (-1)**o_signbit * Octad(sub) * OMEGA**sub_weight assert sub.octad_number() == no ploop, cocode = sub.isplit() sign, gcode = (-1)**(ploop >> 12), ploop & 0xfff assert gcode == o.gcode ^ 0x800 * sub_weight #assert sub.suboctad == nsub assert sign == signed_o.sign == (-1)**o_signbit coc = mat24.suboctad_to_cocode(nsub, o.gcode) assert cocode == coc assert Cocode(sub) == Cocode(coc) assert sub == SubOctad(sub.sign * o, Cocode(coc)) assert sub == sub.sign * SubOctad(no, Cocode(coc)) o2 = Octad(randint(0, 758)) sub2 = SubOctad(o, o2) assert Cocode(sub2) == Cocode(o & o2) assert len(Cocode(sub2)) // 2 & 1 == int((o & o2) / 2) t_sign, t_tag, t_i0, t_i1 = sub.vector_tuple() assert t_tag == 'T' assert t_sign == sign, (hex(sub.value), t_sign, o_signbit) assert t_i0 == no assert t_i1 == nsub
def test_group_ploop(n_cases): print("") for i, (p, g) in enumerate( zip(ploop_testvectors(n_cases), autpl_testwords(n_cases))): pg = p * g if i < 1: print(p, "*", g, "=", pg) pg_ref = PLoop(mat24.op_ploop_autpl(p.ord, g.rep)) assert pg == pg_ref assert p / 4 == pg / 4 == parity(len(p) / 4)
def gen_ploop_element(tag, r): if isinstance(r, Integral): return r & 0x1fff elif isinstance(r, str): r_num = randint(r == 'n', 0x1fff) if len(r) == 1 and r in "rn": return r_num raise ValueError(ERR_ATOM_VALUE % tag) else: try: return PLoop(r).ord except: raise TypeError(ERR_ATOM_TYPE % (type(r), tag))
def test_subtype(verbose=0): OMEGA = XLeech2(~PLoop()) if verbose: print("OMEGA = ", OMEGA) types = set() for v in subtype_testdata(): g = MM('c', v) v2 = Xsp2_Co1(g) v2_subtype = v2.subtype v2ref = (OMEGA * g) v2ref_subtype = v2ref.subtype if verbose: print("v = ", v) print("g = ", g) print("v2 = ", v2, ", subtype =", v2_subtype) print("v2ref = ", v2ref, ", subtype =", v2ref_subtype) assert v2_subtype == v2ref_subtype, (v2_subtype, v2ref_subtype) types.add(v2_subtype) assert len(types) == 6
def test_group_from_perm(n_cases): for i in range(n_cases): h1 = rand_u7() h2 = rand_u7() autp = AutPL(0, zip(h1, h2)) assert autp == AutPL(0, dict(zip(h1, h2))) assert autp.perm == mat24.perm_from_heptads(h1, h2) assert autp.cocode == 0 perm_num = autp.perm_num assert perm_num == mat24.perm_to_m24num(autp.perm) assert autp == AutPL(0, mat24.perm_from_heptads(h1, h2)) assert autp == AutPL(autp) assert autp == AutPL(0, autp.perm_num) assert autp == AutPL(0, zip(h1, h2)) coc_num = randint(1, 0xfff) coc = Cocode(coc_num) assert coc != Cocode(0) assert coc.cocode == coc_num im_coc = coc * autp assert type(im_coc) == type(coc) assert AutPL(im_coc) == AutPL(coc)**autp aut_cp = AutPL(coc) * autp assert aut_cp == AutPL(coc_num, perm_num) if coc_num and perm_num: assert aut_cp.as_tuples() == [('d', coc_num), ('p', perm_num)] assert autp * AutPL(im_coc) == aut_cp assert type(aut_cp) == type(autp) assert aut_cp.perm_num == perm_num assert aut_cp.cocode == coc_num assert Parity(aut_cp) == Parity(coc) assert aut_cp.parity == coc.parity == Parity(coc).ord assert autp == AutPL() * autp == autp * AutPL() with pytest.raises(TypeError): autp * coc with pytest.raises(TypeError): autp * Parity(randint(0, 9)) with pytest.raises(TypeError): autp * randint(2, 9) with pytest.raises(TypeError): randint(2, 9) * autp with pytest.raises(TypeError): autp * PLoop(randint(0, 0x1fff))
def solve_gcode_diag(l): """Solve cocode equation Here ``l`` is a list of tupeles ``(i0, i1, k)``. For an unknown Golay code word ``x``, each tuple means an equation ``<x, Cocode([i0,i1])> = k``, where ``<.,.>`` is the scalar product. If a solution ``x`` exists then the function returns ``x`` as an instance of class |PLoop|. """ a = np.zeros(len(l), dtype=np.uint64) for i, (i0, i1, k) in enumerate(l): a[i] = Cocode([i0, i1]).ord + ((int(k) & 1) << 12) v = bitmatrix64_solve_equation(a, len(l), 12) if v < 0: err = "Off-diagonal matrix equation has no solution" raise ValueError(err) result = PLoop(v) for i0, i1, k in l: c = Cocode([i0, i1]) check = hex(result.ord), hex(c.ord), k assert result & c == Parity(int(k)), check return result
def test_create_Parker_loop(): print("\nTesting creation of Parker loop elements") for sign, v, syn in ploop_creation_testvectors(): # Test power map, inveriosn, sign, theta, and conversion to GCode vlist = [i for i in range(24) if (v >> i) & 1] shuffle(vlist) p1 = (-1)**sign * PLoop(vlist) ploop = mat24.vect_to_gcode(v ^ syn) + (sign << 12) p2 = PLoop(ploop) assert p1 == p2 g1 = GCode(ploop) p3 = (-1)**sign * PLoop(g1) assert p1 == p3 assert p1 == PLoop(p3) assert abs(p1) == PLoop(GCode(vlist)) assert abs(p1) == PLoop(GcVector(vlist)) print("Parker Loop creation test passed")
def test_XYZ(n_cases): for p in characteristics(): sp = space(p) for _ in range(n_cases): d = randint(0, 0x1fff) i = randint(0, 23) c = Cocode([i]) v = GcVector([i]) pl = PLoop(d) gc = GCode(d & 0xfff) d0 = d & 0x7ff scalar = randint(1, p-1) + p * randint(-5, 5) for tag in "XYZ": s2 = s1 = d >> 12 if tag == "Y": s1 ^= d >> 11 ref = (-1)**s1 * sp(tag, d0, i) for sign, dd in ((s1, d0), (s2, gc), (0, pl)): for j in [i, c, v]: assert (-1)**sign * sp(tag, dd, j) == ref mmv = sp() mmv[tag, dd, j] = (-1)**sign * scalar assert mmv == scalar * ref signed_scalar = (-1)**sign * scalar % p assert mmv[tag, dd, j] == signed_scalar
def ploop_testvectors(n_cases=50): for i in range(n_cases): yield PLoop(randint(0, 0x1fff))
def xsp2xco1_xsp2(v): pl = PLoop(v >> 12) coc = Cocode(v) return Xsp2_Co1([("x", v >> 12), pl.theta(), coc])
if g == G(): yield from iter_Q_x0() return b = transversal_basis(g) if len(b) >= 16: for x in vector_space(b): yield g * G("q", x) for x in invariant_basis(b): yield g * G("q", x) yield g * G("q", x) * G("x", 0x1000) else: for x in vector_space(b): yield g * G("q", x) y8 = G("y", PLoop(range(8))) #y12 = G("y", PLoop([0,4,8,12,16,20,2,7,10,])) y12 = G("y", PLoop(std_hexads(0, 1))) neg = G('x', 0x1000) def check_y12_involution_conjugates_to_its_negative(): n_total = 0 n_involution_transversals = 0 n_good_tansversals = 0 for g in iter_transversal(y12): n_total += 1 #print(g.order()) if g.order() == 2: n_involution_transversals += 1
######################################################################## ######################################################################## # Generate a random element of Co_2 ######################################################################## FIXED_PAIR = [3, 2] FIXED_TUPLE = ("I", 3, 2) PI22 = set([0, 1] + list(range(4, 24))) PI7 = [2, 3, 0, 1, 4, 5, 8] StdCocodeVector = Cocode(FIXED_PAIR) EvenGCodeMask = None for i in range(12): if (PLoop(1 << i) & StdCocodeVector).ord: assert EvenGCodeMask == None EvenGCodeMask = 0xfff ^ (1 << i) assert EvenGCodeMask is not None def rand_pi_m22(): r = randint(0, 1) img = [2 + r, 3 - r] + sample(PI22, 3) syn = GcVector(img).syndrome_list() compl = set(range(24)) - set(img + syn) img += sample(syn, 1) + sample(compl, 1) result = AutPL('r', zip(PI7, img)) return result
def test_Parker_loop(): print("\nTesting operation on Parker loop") for i in range(200): # Test power map, inveriosn, sign, theta, and conversion to GCode n1 = randint(0, 0x1fff) p1 = PLoop(n1) assert p1.ord == n1 == p1.gcode + 0x1000 * (1 - p1.sign) / 2 if (i < 2): print("Testing Parker loop element", p1, ", GCode = ", GCode(p1)) assert len(p1) == mat24.gcode_weight(n1) << 2 assert p1.theta() == Cocode(mat24.ploop_theta(n1)) assert p1 / 4 == p1.theta(p1) == Parity(mat24.ploop_cocycle(n1, n1)) assert p1**2 == PLoopZ(p1 / 4) == (-PLoopZ())**(p1 / 4) assert p1**(-5) == PLoopZ(p1 / 4) * p1 == (-1)**(p1 / 4) * p1 == 1 / p1 assert (1 / p1).ord ^ n1 == (mat24.gcode_weight(n1) & 1) << 12 assert -1 / p1 == -p1**3 == -(1 / p1) assert p1 * (-1 / p1) == PLoopZ(1) == -PLoopZ() assert abs(p1).ord == GCode(p1).ord == p1.ord & 0xfff assert p1 != GCode(p1) assert +p1 == 1 * p1 == p1 * 1 == p1 assert p1 != -p1 and p1 != ~p1 assert (-p1).ord == p1.ord ^ 0x1000 assert (~p1).ord == p1.ord ^ 0x800 s, o, p1_pos = p1.split() assert s == (p1.ord >> 12) & 1 assert o == (p1.ord >> 11) & 1 assert p1.sign == (-1)**s assert p1_pos.ord == p1.ord & 0x7ff assert PLoopZ(s, o) * p1_pos == p1 assert PLoopZ(0, o) * p1_pos == abs(p1) assert PLoopZ(s + 1, o) * p1_pos == -p1 assert -p1 == -1 * p1 == p1 * -1 == p1 / -1 == -1 / p1**-5 assert PLoopZ(s, 1 + o) * p1_pos == ~p1 assert PLoopZ(1 + s, 1 + o) * p1_pos == ~-p1 == -~p1 == ~p1 / -1 assert 2 * p1 == GCode(0) == -2 * GCode(p1) assert -13 * p1 == GCode(p1) == 7 * GCode(p1) if len(p1) & 7 == 0: assert p1**Parity(1) == p1 assert p1**Parity(0) == PLoop(0) else: with pytest.raises(ValueError): p1**Parity(randint(0, 1)) assert p1.bit_list == mat24.gcode_to_bit_list(p1.value & 0xfff) assert p1.bit_list == GcVector(p1).bit_list assert p1.bit_list == GCode(p1).bit_list assert PLoop(GcVector(p1) + 0) == PLoop(GCode(p1) + 0) == abs(p1) assert p1 + 0 == 0 + p1 == GCode(p1) + 0 == 0 + GCode(p1) assert Cocode(GcVector(p1)) == Cocode(0) assert p1 / 2 == Parity(0) # Test Parker loop multiplication and commutator n2 = randint(0, 0x1fff) p2 = PLoop(n2) coc = Cocode(mat24.ploop_cap(n1, n2)) if (i < 1): print("Intersection with", p2, "is", coc) p2inv = p2**-1 assert p1 * p2 == PLoop(mat24.mul_ploop(p1.ord, p2.ord)) assert p1 / p2 == p1 * p2**(-1) assert p1 + p2 == p1 - p2 == GCode(p1 * p2) == GCode(n1 ^ n2) assert (p1 * p2) / (p2 * p1) == PLoopZ((p1 & p2) / 2) assert p1 & p2 == coc assert p1.theta() == Cocode(mat24.ploop_theta(p1.ord)) assert p1.theta(p2) == Parity(mat24.ploop_cocycle(p1.ord, p2.ord)) assert (p1 & p2) / 2 == p1.theta(p2) + p2.theta(p1) assert p1 & p2 == p1.theta() + p2.theta() + (p1 + p2).theta() assert int((p1 & p2) / 2) == mat24.ploop_comm(p1.ord, p2.ord) assert GcVector(p1 & p2) == GcVector(p1) & GcVector(p2) assert ~GcVector(p1 & p2) == ~GcVector(p1) | ~GcVector(p2) assert Cocode(GcVector(p1 & p2)) == p1 & p2 # Test associator n3 = randint(0, 0x1fff) p3 = PLoop(n3) assert p1 * p2 * p3 / (p1 * (p2 * p3)) == PLoopZ(p1 & p2 & p3) assert int(p1 & p2 & p3) == mat24.ploop_assoc(p1.ord, p2.ord, p3.ord) i = randint(-1000, 1000) par = Parity(i) s3 = ((p3 & p1) & p2) + par assert s3 == ((p3 & p1) & p2) + par assert s3 == i + (p1 & (p2 & p3)) assert s3 == par + (p1 & (p2 & p3)) # Test some operations leading to a TypeError with pytest.raises(TypeError): p1 & p2 & p3 & p1 with pytest.raises(TypeError): coc & coc with pytest.raises(TypeError): GCode(p1) * GCode(p2) with pytest.raises(TypeError): 1 / GCode(p2) with pytest.raises(ValueError): coc / 4 with pytest.raises(TypeError): p1 * coc with pytest.raises(TypeError): coc * p1 types = [GcVector, GCode, Cocode, PLoop] for type_ in types: with pytest.raises(TypeError): int(type_(0)) print("Parker Loop test passed")
from mmgroup.generators import gen_leech2_count_type2 from mmgroup.generators import gen_leech2_op_word from mmgroup.generators import gen_leech2_type_selftest from mmgroup.clifford12 import xsp2co1_leech2_count_type2 ##################################################################### # Test computation of subtype Leech lattice vector ##################################################################### # We test the computation of the subtype of a vetor in the # Leech lattice with function gen_leech2_subtype(). # Next we specify certain elements of the Parker loop # Neutral element of the Parker loop ZERO = PLoop([]) # The element \Omega of the Parker loop OMEGA = ~ZERO # A (preimage of a) dodecad DODECAD = PLoop([0, 4, 8, 13, 14, 15, 17, 18, 19, 21, 22, 23]) # A (preimage of an) octad OCTAD = PLoop([0, 1, 2, 3, 4, 5, 6, 7]) # A (preimage of a) complmented octad HEXADECAD = ~OCTAD # The following list contains a sample of a vector of each subtype # in the Leech lattice mod 2. Such a sample is given as a triple # (ploop, cocode, subytpe) # such that Xsp2_Co1([("x", ploop), ("d", Cocode(cocode))]) # correspond to a vector of subtype ``subtype`` in the Leech # lattice modulo 2.