-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGates.py
executable file
·189 lines (116 loc) · 4.8 KB
/
Gates.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 8 17:44:07 2020
@author: ph30n1x
"""
'''
Code defining all the required quantum gates as tensors
'''
import numpy as np
import math
import cmath
class Gates():
def get_Proj0():
proj0 = np.array([[1.0,0.0],
[0.0,0.0]], dtype = np.complex128)
return proj0
def get_Proj1():
proj1 = np.array([[0.0,0.0],
[0.0,1.0]], dtype = np.complex128)
return proj1
def get_X():
x = np.array([[0.0,1.0],
[1.0,0.0]], dtype = np.complex128)
return x
def get_Rx(theta=math.pi):
if (theta == math.pi):
Rx = np.array([[0.0,1.0],
[1.0,0.0]], dtype = np.complex128)
else:
Rx = np.array([[math.cos(theta*0.5),-1.0j*math.sin(theta*0.5)],
[-1.0j*math.sin(theta*0.5),math.cos(theta*0.5)]],
dtype = np.complex128)
return Rx
def get_Y():
y = np.array([[0.0,-1.0j],
[1.0j,0.0]], dtype = np.complex128)
return y
def get_Ry(theta=math.pi):
if (theta == math.pi):
Ry = np.array([[0.0,-1.0j],
[1.0j,0.0]], dtype = np.complex128)
else:
Ry = np.array([[math.cos(theta*0.5),-1.0*math.sin(theta*0.5)],
[1.0*math.sin(theta*0.5),math.cos(theta*0.5)]],
dtype = np.complex128)
return Ry
def get_Z():
z = np.array([[1.0,0.0],
[0.0,-1.0]], dtype = np.complex128)
return z
def get_Rz(theta=math.pi):
if (theta == math.pi):
Rz = np.array([[1.0,0.0],
[0.0,-1.0]], dtype = np.complex128)
else:
Rz = np.array([[cmath.exp(-1.0j*theta*0.5),0.0],
[0.0,cmath.exp(1.0j*theta*0.5)]],
dtype = np.complex128)
return Rz
def get_I():
i = np.eye(2, dtype = np.complex128)
return i
def get_H():
h = np.array([[1.0,1.0],
[1.0,-1.0]], dtype = np.complex128)/np.sqrt(2)
return h
def get_CNOT():
cnot = np.array([[1.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0],
[0.0,0.0,0.0,1.0],
[0.0,0.0,1.0,0.0]], dtype = np.complex128)
cnot = cnot.reshape((2,2,2,2))
return cnot
def get_CZ():
cz = np.array([[1.0,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.0,0.0,-1.0]], dtype = np.complex128)
cz = cz.reshape((2,2,2,2))
return cz
def get_CY():
cy = np.array([[1.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0],
[0.0,0.0,0.0,-1.0j],
[0.0,0.0,1.0j,0.0]], dtype = np.complex128)
cy = cy.reshape((2,2,2,2))
return cy
def get_SWAP():
swap = np.array([[1., 0., 0., 0.],
[0., 0., 1., 0.],
[0., 1., 0., 0.],
[0., 0., 0., 1.]], dtype = np.complex128)
swap = swap.reshape((2,2,2,2))
return swap
def get_Jij(gamma,jij):
Jij = np.array([[np.exp(-1j*gamma*jij), 0., 0., 0.],
[0., np.exp(+1j*gamma*jij), 0., 0.],
[0., 0., np.exp(+1j*gamma*jij), 0.],
[0., 0., 0., np.exp(-1j*gamma*jij)]], dtype = np.complex128)
Jij = Jij.reshape((2,2,2,2))
return Jij
def get_Cij(gamma,cij):
Cij = np.array([[1.0, 0.0, 0.0, 0.0],
[0.0, np.exp(1j*gamma*cij), 0.0, 0.0],
[0.0, 0.0, np.exp(1j*gamma*cij), 0.0],
[0.0, 0.0, 0.0, 1.0]], dtype = np.complex128)
Cij = Cij.reshape((2,2,2,2))
return Cij
def get_fSim(theta,phi):
fSim = np.array([[1.0, 0.0, 0.0, 0.0],
[0.0, np.cos(theta), -1j*np.sin(theta), 0.0],
[0.0, -1j*np.sin(theta), np.cos(theta), 0.0],
[0.0, 0.0, 0.0, np.exp(-1j*phi)]], dtype = np.complex128)
fSim = fSim.reshape((2,2,2,2))
return fSim