-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.py
121 lines (100 loc) · 2.49 KB
/
css.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#import ldpc
import numpy as np
from ldpc.mod2 import *
#.inverse
def main():
# test inverse
mat=np.array([[1,1,0],[0,1,0],[0,0,1]])
i_mat=inverse(mat)
print(i_mat@mat%2)
# test nullspace
H=np.array([[0, 0, 0, 1, 1, 1, 1],[0, 1, 1, 0, 0, 1, 1],[1, 0, 1, 0, 1, 0, 1]])
print(nullspace(H))
G = np.concatenate((H,H,H,H),axis=1)
for j in range(100):
for i in range(10000):
a=nullspace(G)
#print(a)
#break
print(j)
# test rre
rre=reduced_row_echelon(H)[0]
print(rre)
#main()
def make_it_full_rank(A):
R,r,_,_ = row_echelon(A)
return R[:r], r
def mat_rand(row,col,p=0.5):
'''Return a radnom binary matrix of dimension (r,c)
'''
R = np.random.rand(row,col)
B = np.floor(R + p).astype(int)
if True:
# make it full rank
F,r = make_it_full_rank(B)
return F,r
class CSSCode:
def __init__(self):
pass
def min_distance(Hx,Lx):
# return dx
r,n = Hx.shape
k = Lx.shape[0]
alpha = np.zeros((2**k-1,k),int)
for i in range(1,2**k):
b=mod10_to_mod2(i,k)
alpha[i-1]=b
#print(f"{alpha = }")
alpha_Lx = alpha @ Lx
dx = n
for j in range(0, 2**r):
b=mod10_to_mod2(j,r)
beta = b @ Hx
codewords = np.add(alpha_Lx , beta) %2
#print(codewords)
s = codewords.sum(axis=1)
m = s.min()
#print(s,m)
if m < dx:
dx = m
return dx
def match(A,B):
# assert A and B are identical
assert not ( A - B ).any()
def css_test():
#print('get a random CSS code')
n=10
rx=3
rz=5
Hx,rx = mat_rand(rx,n)
#print(f"{Hx = }")
U = nullspace(Hx)
theta, rz = mat_rand(rz,n-rx)
Hz = theta@U %2
#print(f"{Hz = }")
# check commutation
Hx_Hz = [email protected](Hz) %2
assert not Hx_Hz.any()
Ux = nullspace(Hz)
Uz = U
a = Ux @ np.transpose(Uz) % 2
r,k,P,Q = reduced_row_echelon(a)
match(r, (P @ Ux ) @ (Q.T @ Uz).T %2 )
#print('# of logical qubits k=',k)
Lx = P[:k] @ Ux %2
Lz = Q.T[:k] @ Uz %2
#print(f"{Lx = }")
#print(f"{Lz = }")
#check commutation
Lx_Lz = Lx @ Lz.T %2
assert not ( Lx_Lz - np.identity(k) ).any()
assert not (Hx @ Lz.T %2).any()
assert not (Hz @ Lx.T %2).any()
#check distance
dx = min_distance(Hx,Lx)
dz = min_distance(Hz,Lz)
print(f"{n = }, {k = }, {dx = }, {dz = }")
css_test()
for i in range(1000000):
print(i)
css_test()