forked from tandeitnik/Evolving_Quantum_Circuits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qecc_functions.py
523 lines (315 loc) · 15.6 KB
/
qecc_functions.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# -*- coding: utf-8 -*-
import numpy as np
import utility_functions as uf
import itertools
def syndrome_table(N,P,errors_literal,affected_qubits):
s_table = []
for i in range(len(errors_literal)):
#if (tuple(errors_literal[i]) in common_stabilizers_set) == False: #error is not a common stabilizer
if 1 == 1:
syndrome = ''
for j in range(len(P)):
if uf.pauli_commutation(errors_literal[i],P[j]) == -1:
syndrome += '1'
else:
syndrome += '0'
s_table.append([errors_literal[i],affected_qubits[i],syndrome])
return s_table
def common_stabilizers(N,stab_set):
#INPUT: a list of the codewords circuits
#OUTPUT: a list of common stabilizers of the codewords (the stabilizers must have the same signal to be considered common)
#computing the group of stabilizers for each codeword
common_stabilizers = stab_set[0].intersection(stab_set[1])
common_stabilizers = list(common_stabilizers)
common_stabilizers_list = [list(common_stabilizers[i][:N]) for i in range(len(common_stabilizers))]
return common_stabilizers_list
def syndrome_operators(N,stab_set):
common_stabilizers = stab_set[0].intersection(stab_set[1])
common_stabilizers = list(common_stabilizers)
common_stabilizers = [common_stabilizers[x] for x in range(len(common_stabilizers)) if common_stabilizers[x][-1] == '1']
#computing the generators of common_commuting_stabilizers
mat = []
for i in range(len(common_stabilizers)):
row = []
for j in range(N):
if common_stabilizers[i][j] == 'X':
row.append(1)
row.append(0)
elif common_stabilizers[i][j] == 'Z':
row.append(0)
row.append(1)
elif common_stabilizers[i][j] == 'Y':
row.append(1)
row.append(1)
elif common_stabilizers[i][j] == 'I':
row.append(0)
row.append(0)
mat.append(row)
I = uf.rank_mod2(np.array(mat))
generators = [mat[0]]
rank_generators = 1
for i in range(1,len(mat)):
generators.append(mat[i])
new_rank = uf.rank_mod2(np.array(generators))
if new_rank == rank_generators: #rank didn't raise
del(generators[-1])
else:
rank_generators = uf.rank_mod2(np.array(generators))
if rank_generators == I:
break
#translating back to literals and forming the output
P = []
for i in range(len(generators)):
P.append([])
for i in range(len(generators)):
syndrome_operator = []
for j in range(N):
syndrome_operator.append([])
for j in range(N):
if generators[i][2*j] == 0 and generators[i][2*j+1] == 0:
syndrome_operator[j] = 'I'
elif generators[i][2*j] == 0 and generators[i][2*j+1] == 1:
syndrome_operator[j] = 'Z'
elif generators[i][2*j] == 1 and generators[i][2*j+1] == 0:
syndrome_operator[j] = 'X'
elif generators[i][2*j] == 1 and generators[i][2*j+1] == 1:
syndrome_operator[j] = 'Y'
P[i] = syndrome_operator
P_bit = generators
return P, P_bit
def syndrome_operators_topo(N,stab_set):
#INPUT: a list of the codewords circuits
#OUTPUT: a list of syndrome operators which 1) stabilizes all codewords
#2) they commute with one another; 3) they are LI
#computing a set of common stabilizers between the groups
common_stabilizers = stab_set[0].intersection(stab_set[1])
common_stabilizers = list(common_stabilizers)
common_stabilizers = [common_stabilizers[x] for x in range(len(common_stabilizers)) if common_stabilizers[x][-1] == '1']
notas = np.zeros(len(common_stabilizers))
for i in range(len(common_stabilizers)):
len_x = len(uf.find_literal("X",np.array(common_stabilizers[i])))
len_y = len(uf.find_literal("Y",np.array(common_stabilizers[i])))
len_z = len(uf.find_literal("Z",np.array(common_stabilizers[i])))
if (len_y == 0 and len_z == 0) or (len_y == 0 and len_x == 0):
notas[i] = 1
elif len_y == max(len_x,len_y,len_z):
notas[i] = (N - len_y)/N
else:
notas[i] = (N-min(len_x,len_z))/N
css_group_degree = np.mean(notas)
stab_only_X = []
stab_only_Y = []
stab_only_Z = []
stab_XYZ = []
set_X = []
set_Y = []
set_Z = []
for i in range(len(common_stabilizers)):
#only X
if len(uf.find_literal("Y",np.array(common_stabilizers[i]))) == 0 and len(uf.find_literal("Z",np.array(common_stabilizers[i]))) == 0:
stab_only_X.append([common_stabilizers[i],len(uf.find_literal("X",np.array(common_stabilizers[i])))])
set_X.append(tuple(uf.find_literal("X",np.array(common_stabilizers[i]))))
#only Y
elif len(uf.find_literal("X",np.array(common_stabilizers[i]))) == 0 and len(uf.find_literal("Z",np.array(common_stabilizers[i]))) == 0:
stab_only_Y.append([common_stabilizers[i],len(uf.find_literal("Y",np.array(common_stabilizers[i])))])
set_Y.append(tuple(uf.find_literal("Y",np.array(common_stabilizers[i]))))
#only Z
elif len(uf.find_literal("Y",np.array(common_stabilizers[i]))) == 0 and len(uf.find_literal("X",np.array(common_stabilizers[i]))) == 0:
stab_only_Z.append([common_stabilizers[i],len(uf.find_literal("Z",np.array(common_stabilizers[i])))])
set_Z.append(tuple(uf.find_literal("Z",np.array(common_stabilizers[i]))))
else:
stab_XYZ.append([common_stabilizers[i],len(uf.find_literal("X",np.array(common_stabilizers[i])))+len(uf.find_literal("Y",np.array(common_stabilizers[i])))+len(uf.find_literal("Z",np.array(common_stabilizers[i])))])
stab_only_X = sorted(stab_only_X, key=lambda x: x[1])
stab_only_Y = sorted(stab_only_Y, key=lambda x: x[1])
stab_only_Z = sorted(stab_only_Z, key=lambda x: x[1])
stab_XYZ = sorted(stab_XYZ, key=lambda x: x[1])
stab_sorted = []
for i in range(len(stab_only_X)):
stab_sorted.append(stab_only_X[i][0])
for i in range(len(stab_only_Z)):
stab_sorted.append(stab_only_Z[i][0])
for i in range(len(stab_only_Y)):
stab_sorted.append(stab_only_Y[i][0])
for i in range(len(stab_XYZ)):
stab_sorted.append(stab_XYZ[i][0])
if len(set_X) == 0 or len(set_Z) == 0:
notas = 0
elif len(set_X) <= len(set_Z):
notas = np.zeros(len(set_X))
for i in range(len(set_X)):
notas_temp = np.zeros(len(set_Z))
x_temp = set(set_X[i])
for j in range(len(set_Z)):
z_temp = set(set_Z[j])
int_degree = len(x_temp.intersection(z_temp))
notas_temp[j] = int_degree
notas[i] = np.max(notas_temp)
else:
notas = np.zeros(len(set_Z))
for i in range(len(set_Z)):
notas_temp = np.zeros(len(set_X))
z_temp = set(set_Z[i])
for j in range(len(set_X)):
x_temp = set(set_X[j])
int_degree = len(z_temp.intersection(x_temp))
notas_temp[j] = int_degree
notas[i] = np.max(notas_temp)
topo_degree = 1+np.mean(notas)
#computing the generators of common_commuting_stabilizers
mat = []
for i in range(len(stab_sorted)):
row = []
for j in range(N):
if stab_sorted[i][j] == 'X':
row.append(1)
row.append(0)
elif stab_sorted[i][j] == 'Z':
row.append(0)
row.append(1)
elif stab_sorted[i][j] == 'Y':
row.append(1)
row.append(1)
elif stab_sorted[i][j] == 'I':
row.append(0)
row.append(0)
mat.append(row)
I = uf.rank_mod2(np.array(mat))
generators = [mat[0]]
rank_generators = 1
for i in range(1,len(mat)):
generators.append(mat[i])
new_rank = uf.rank_mod2(np.array(generators))
if new_rank == rank_generators: #rank didn't raise
del(generators[-1])
else:
rank_generators = uf.rank_mod2(np.array(generators))
if rank_generators == I:
break
#translating back to literals and forming the output
P = []
for i in range(len(generators)):
P.append([])
for i in range(len(generators)):
syndrome_operator = []
for j in range(N):
syndrome_operator.append([])
for j in range(N):
if generators[i][2*j] == 0 and generators[i][2*j+1] == 0:
syndrome_operator[j] = 'I'
elif generators[i][2*j] == 0 and generators[i][2*j+1] == 1:
syndrome_operator[j] = 'Z'
elif generators[i][2*j] == 1 and generators[i][2*j+1] == 0:
syndrome_operator[j] = 'X'
elif generators[i][2*j] == 1 and generators[i][2*j+1] == 1:
syndrome_operator[j] = 'Y'
P[i] = syndrome_operator
P_bit = generators
css_degre = css_degree(P,N)
return P, P_bit, topo_degree, css_group_degree, css_degre
# def css_degree(P):
# faulty_stabilizers = 0
# for i in range(len(P)):
# if len(uf.find_literal("X",np.array(P[i]))) > 0 and len(uf.find_literal("Z",np.array(P[i]))) > 0:
# faulty_stabilizers += 1
# elif len(uf.find_literal("Y",np.array(P[i]))) > 0:
# faulty_stabilizers += 1
# degree = (len(P)-faulty_stabilizers)/len(P)
# return degree
def css_degree(P,N):
notas = np.zeros(len(P))
for i in range(len(P)):
len_x = len(uf.find_literal("X",np.array(P[i])))
len_y = len(uf.find_literal("Y",np.array(P[i])))
len_z = len(uf.find_literal("Z",np.array(P[i])))
if (len_y == 0 and len_z == 0) or (len_y == 0 and len_x == 0):
notas[i] = 1
# elif len_y == max(len_x,len_y,len_z):
# notas[i] = (N - len_y)/N
# else:
# notas[i] = (N-min(len_x,len_z))/N
degree = np.mean(notas)
return degree
# def locality_degree(P,lattice_graph):
# local_stabilizers = 0
# for i in range(len(P)):
# nodes = list(uf.find_literal("X",np.array(P[i]))+1)+list(uf.find_literal("Y",np.array(P[i]))+1)+list(uf.find_literal("Z",np.array(P[i]))+1)
# nodes_permut = list(itertools.permutations(nodes))
# for j in range(len(nodes_permut)):
# path_test = nx.is_simple_path(lattice_graph, list(nodes_permut[j]))
# if path_test == True:
# local_stabilizers += 1
# break
# degree = local_stabilizers/len(P)
# return degree
def phase_distance(N,codewords,stab_set):
#evaluating phase distance
common_stabilizers = stab_set[0]
dif_stabilizers = stab_set[0]
for i in range(1,len(stab_set)):
common_stabilizers = common_stabilizers.intersection(stab_set[i])
dif_stabilizers = dif_stabilizers.union(stab_set[i])
dif_stabilizers = list(dif_stabilizers - common_stabilizers)
error_weight = [0]*len(dif_stabilizers)
for i in range(len(dif_stabilizers)):
for j in range(N):
if dif_stabilizers[i][j] != 'I':
error_weight[i] += 1
phase_distance = min(error_weight)
return phase_distance
def errors_list(N,t):
errors_literal = []
affected_qubits = []
for i in range(1,t+1):
combinations = list(itertools.combinations(range(N),i))
e_list = []
for j in range(len(combinations)*3**i):
e_list.append(['I']*N)
for j in range(len(combinations)):
for k in range(3**i):
affected_qubits.append(combinations[j])
for k in range(i):
for l in range(3**i):
if (l // 3**k) % 3 == 0:
e_list[j*3**i+l][combinations[j][k]] = 'X'
if (l // 3**k) % 3 == 1:
e_list[j*3**i + l][combinations[j][k]] = 'Y'
if (l // 3**k) % 3 == 2:
e_list[j*3**i + l][combinations[j][k]] = 'Z'
for k in range(len(e_list)):
errors_literal.append(e_list[k])
return errors_literal, affected_qubits
def correctability_degree(N,t,errors_literal,affected_qubits,stab_set):
#P, P_bit,topo_degree,css_group_degree, css_degre = syndrome_operators_topo(N,stab_set)
P, P_bit = syndrome_operators(N,stab_set)
common_stab = common_stabilizers(N,stab_set)
s_table = syndrome_table(N,P,errors_literal,affected_qubits)
P_set = set()
for i in range(len(P)):
P_set.add(tuple(P[i]))
code_set = set()
for i in range(len(s_table)):
code_set.add(s_table[i][2])
code_list = list(code_set)
undetectable_faults = 0
undecidable_faults = 0
#counting lines that make the error undetectable
for i in range(len(s_table)):
if s_table[i][2] == '0'*len(P):
undetectable_faults += 1
#counting the syndromes that make the correction undecidable
errors_per_code = []
for i in range(len(code_list)):
errors_per_code.append([])
for i in range(len(code_list)):
errors_per_code[i] = [code_list[i] , [s_table[x][0] for x in range(len(s_table)) if s_table[x][2] == code_list[i] ]]
for i in range(len(errors_per_code)):
if len(errors_per_code[i][1]) != 1 and errors_per_code[i][0] != '0'*len(P):
errors = list(itertools.combinations(list(range(len(errors_per_code[i][1]))), 2))
for j in range(len(errors)):
ans,signal = uf.pauli_multi_list(errors_per_code[i][1][errors[j][0]],errors_per_code[i][1][errors[j][1]])
if (ans in common_stab) == False:
undecidable_faults += 1
break
c_degree = float(len(s_table) + len(code_list) - undetectable_faults - undecidable_faults)/float(len(s_table) + len(code_list))
#return c_degree, topo_degree, css_group_degree, css_degre
return c_degree