-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperacao.py
584 lines (493 loc) · 18.1 KB
/
operacao.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
from sympy import I, Matrix, symbols,eye
from sympy.physics.quantum import TensorProduct
from vetor import *
# armazena operadores (gates).
class Operacao:
def __init__(self,circuito):
self.circ = circuito
self.n = circuito.n
self.N = circuito.N
self.Estado = circuito.Estado
self.flag_M = False # Indica se os operadores aplicados até o momento são vetores ou matrizes
self.isPerm = False # indica se a operação atual é uma permutação
self.V_op = []
self.M_op = []
self.test = False
# indica se deve executar a porta, openas verificar se é uma permutação ou matriz
def teste(self,t=False):
if t == 0 :
self.test = t
#
#### Implementação dos operadores (gates)
#
#
#
# k = bit inicial, t = repetições e theta,phi,lam = ângulos
''' Os gates e suas definições podem ser encontrados no site:
https://quantum-computing.ibm.com/composer/docs/iqx/operations_glossary
'''
# define uma permutação
## Implementa um operador genérico através da sua permutação
# P = permutação.
def perm(self,k,t,P):
self.isPerm = True
if self.test :
return self.isPerm
nova_perm = []
for i in range(2**self.n):
n_num = ""
num = bin(i + 2**self.n)[3:]
n_num += num[:self.n-(k+t)]
n_num += bin(P[int(num[self.n-(k + t):self.n-k],base = 2)] + 2**t)[-t:]
n_num += num[self.n- k:]
n_num = int(n_num,base = 2)
nova_perm.append(n_num)
self.V_op = vetor(self.n,nova_perm)
# x - NOT
## inverte o estado do bit
def x(self,k,t=1):
self.isPerm = True
if self.test :
return self.isPerm
V = vetor(self.n)
for j in range(0,t):
cont = 0
trava = 0
permut = []
for i in range(int(2**(self.n))):
if cont == 2**(k+j):
if trava < 2**(k+j)-1:
trava += 1
else:
trava = 0
cont = 0
else:
V.P[i],V.P[i+2**(k+j)] = V.P[i+2**(k+j)],V.P[i]
cont += 1
self.V_op = V
# h - hadamard gate
def h(self,k,t=1):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
had = Matrix([[1,1],[1,-1]])
had = had * 2**(-0.5)
op = TensorProduct(op,had)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = self.flag_M_atual = True
self.M_op = op
# z - Pauli Z gate
## Esta porta pode ser associada a um vetor ao em vez de matriz
def z(self,k,t=1):
self.isPerm = False
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pz = Matrix([[1,0],[0,-1]])
op = TensorProduct(op,pz)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# s - S gate
def s(self,k,t=1):
self.isPerm = False
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
ps = Matrix([[1,0],[0,I]])
op = TensorProduct(op,ps)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# sdg - gate usado no algoritmo de Shor
def sdg(self,k,t=1):
self.isPerm = False
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
ps = Matrix([[1,0],[0,-I]])
op = TensorProduct(op,ps)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# y - Pauli Y gate
def y(self,k,t=1,test=False):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
py = Matrix([[0,-I],[I,0]])
op = TensorProduct(op,py)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# t - T gate
def t(self,k,t=1,test=False):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[1,0],[0,e**(I*pi/4)]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# tdg - T-dagger gate
def tdg(self,k,t=1,test=False):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[1,0],[0,e**(-I*pi/4)]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# u3 - U gate
## Esse gate consegue gerar qualquer gate de 1 bit.
def u3(self,theta,phi,lam,k,t=1):
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[cos(theta/2),-e**(I*lam)*sin(theta/2)],[e**(I*phi)*sin(theta/2),e**(I*(phi+lam))*cos(theta/2)]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# u2 - U2 gate
def u2(self,phi,lam,k,t=1):
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[1,-e**(I*lam)],[e**(I*phi),e**(I*(phi+lam))]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# u1 - U1 gate
def u1(self,lam,k,t=1):
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[1,0],[0,e**(I*lam)]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# rx - RX gate
def rx(self,theta,k,t=1):
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[cos(theta/2),-I*sin(theta/2)],[-I*sin(theta/2),cos(theta/2)]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# ry - RY gate
def ry(self,theta,k,t=1):
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[cos(theta/2),-sin(theta/2)],[sin(theta/2),cos(theta/2)]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# rz - RZ gate
def rz(self,theta,k,t=1):
op = Matrix([1])
for i in range(self.n):
if i >= k and i <k+t:
pt = Matrix([[e**(-I*(theta/2)),0],[0,e**(I*(theta/2))]])
op = TensorProduct(op,pt)
else:
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
#
##gates com 2 bits.
#
# cx - CNOT gate
## veri = dígito verificador, target = bit alvo
def cx(self,veri,target):
self.isPerm = True
if self.test :
return self.isPerm
V = vetor(self.n)
for i in range(2**self.n):
i_bit = list(bin(2**self.n + i)[3:])
resp = ''
if i_bit[self.n-1-veri] == '1':
for j in range(self.n):
if j == (self.n-1-target):
resp += str((int(i_bit[j])+1)%2)
else:
resp += i_bit[j]
V.P[i] = int(resp, base =2)
else:
V.P[i] = i
self.V_op = V
# ch - Controlled Hadamard
def ch(self,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,2**(-0.5),0,2**(-0.5)],[0,0,1,0],[0,2**(-0.5),0,-2**(0.5)]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cy - Controlled Y gate
def cy(self,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,0,0,-I],[0,0,1,0],[0,I,0,0]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cz - Controlled Z gate
def cz(self,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,-1]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cs - Controlled S gate
def cs(self,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
cphase = Matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,I]])
op = TensorProduct(op,cphase)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# crx - Controlled RX gate
def crx(self,theta,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,cos(theta/2),0,-I*sin(theta/2)],[0,0,1,0],[0,-I*sin(theta/2),0,cos(theta/2)]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cry - Controlled RY gate
def cry(self,theta,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,cos(theta/2),0,-sin(theta/2)],[0,0,1,0],[0,sin(theta/2),0,cos(theta/2)]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# crz - Controlled RZ gate
def crz(self,theta,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,e**(-I*(theta/2)),0,0],[0,0,1,0],[0,0,0,e**(I*(theta/2))]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cu1 - Controlled U1 gate
def cu1(self,theta,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,e**(I*(theta))]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cu3 - Controlled U3 gate
def cu3(self,theta,phi,lam,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[1,0,0,0],[0,cos(theta/2),0,-e**(I*lam)*sin(theta/2)],[0,0,1,0],[0,e**(I*phi)*sin(theta),0,e**(I*(phi+lam)*cos(theta/2))]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# swap - Swap gate
## Troca os estados de 2 bits
## bit1 = primeiro bit, bit2 = segundo bit. (a ordem não importa)
def swap(self,bit1,bit2):
self.isPerm = True
if self.test :
return self.isPerm
V = vetor(self.n)
for i in range(2**self.n):
i_bit = list(bin(2**self.n + i)[3:])
resp = ''
for j in range(self.n):
if j == (self.n-1-bit1):
resp += i_bit[self.n-1-bit2]
elif j == (self.n-1-bit2):
resp += i_bit[self.n-1-bit1]
else:
resp += i_bit[j]
V.P[i] = int(resp, base =2)
self.V_op = V
# rxx - RXX gate
def rxx(self,theta,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[cos(theta/2),0,0,-I*sin(theta/2)],[0,cos(theta/2),-I*sin(theta/2),0],[0,-I*sin(theta/2),cos(theta/2),0],[-I*sin(theta/2),0,0,cos(theta/2)]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# czz - Controlled ZZ gate
def czz(self,theta,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_z = Matrix([[e**(-I*(theta/2)),0,0,0],[0,e**(I*(theta/2)),0,0],[0,0,e**(I*(theta/2)),0],[0,0,0,e**(-I*(theta/2))]])
op = TensorProduct(op,control_z)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# cfs - Condicional Phase Shift
def cfs(self,k,t):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
num = e**((pi*2*I/2**t))
phase_shift = Matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,num]])
op = TensorProduct(op,phase_shift)
elif i != (k + 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
#
## gates com 3 bits.
#
# ccx - Toffoli gate
## veri1 e veri2 = dígitos verificadores, target = bit alvo
def ccx(self,veri1,veri2,target):
self.isPerm = True
if self.test :
return self.isPerm
V = vetor(self.n)
for i in range(2**self.n):
i_bit = list(bin(2**self.n + i)[3:])
resp = ''
if i_bit[self.n-1-veri1] == '1' and i_bit[self.n-1-veri2] == '1':
for j in range(self.n):
if j == (self.n-1-target):
resp += str((int(i_bit[j])+1)%2)
else:
resp += i_bit[j]
V.P[i] = int(resp, base =2)
else:
V.P[i] = i
self.V_op = V
# cswap - Controlled Swap gate
## Atualmente está como matriz, porém pode ser transformado em permutação como foi feito em outros gates.
def cswap(self,k):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
control_swap = Matrix([[1,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0],[0,0,1,0,0,0,0,0],[0,0,0,1,0,0,0,0],[0,0,0,0,1,0,0,0],[0,0,0,0,0,0,1,0],[0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1]])
op = TensorProduct(op,control_swap)
elif i != (k + 1) and i != (k+2):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op
# gen_op - Operador genérico
## Implementa qualquer gate através da dua matriz
## q_bits = quantidade de bits do gate, matriz = matriz
def gen_op(self,k,q_bits,matriz):
self.isPerm = False
if self.test :
return self.isPerm
op = Matrix([1])
for i in range(self.n):
if i == k:
op = TensorProduct(op,Matrix(matriz))
elif i < k or i > (k + q_bits - 1):
op = TensorProduct(op,Matrix([[1,0],[0,1]]))
self.flag_M = True
self.M_op = op