-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGateDecomposition.py
More file actions
248 lines (192 loc) · 6.48 KB
/
GateDecomposition.py
File metadata and controls
248 lines (192 loc) · 6.48 KB
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
import numpy as np
import scipy.linalg as la
import math
"""Common Two-qubit gate"""
CNOT = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]])
SWAP = np.array([[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]])
"""Common Single-qubit gate"""
H = 1/math.sqrt(2) * np.array([[1, 1],
[1, -1]])
X = np.array([[0, 1],
[1, 0]])
Y = np.array([[0, -1j],
[1j, 0]], dtype = complex)
Z = np.array([[1, 0],
[0, -1]])
"""Single-qubit rotation gate"""
def Rx(angle):
return math.cos(angle/2) * np.eye(2) - 1j * math.sin(angle/2) * X
def Ry(angle):
return math.cos(angle/2) * np.eye(2) - 1j * math.sin(angle/2) * Y
def Rz(angle):
return math.cos(angle/2) * np.eye(2) - 1j * math.sin(angle/2) * Z
def Print_gate(uni_mat, qubit):
"""Print common single-qubit gate or print the matrix.
Common single-qubit gate includes H, X, Y, Z.
"""
if np.allclose(np.eye(2), uni_mat):
pass
elif np.allclose(H, uni_mat):
print("H on ", qubit)
elif np.allclose(X, uni_mat):
print("X on ", qubit)
elif np.allclose(Y, uni_mat):
print("Y on ", qubit)
elif np.allclose(Z, uni_mat):
print("Z on ", qubit)
else:
print(uni_mat)
print("on ", qubit)
def euler_angles_1q(unitary_matrix):
"""
Reference, modified from qiskit package
"""
if unitary_matrix.shape != (2, 2):
raise QiskitError("euler_angles_1q: expected 2x2 matrix")
phase = la.det(unitary_matrix)**(-1.0/2.0)
U = phase * unitary_matrix # U in SU(2)
theta = 2 * math.atan2(abs(U[1, 0]), abs(U[0, 0]))
# Find phi and lambda
phiplambda = 2 * np.angle(U[1, 1])
phimlambda = 2 * np.angle(U[1, 0])
phi = (phiplambda + phimlambda) / 2.0
lamb = (phiplambda - phimlambda) / 2.0
return phase, theta, phi, lamb
def is_unitary(mat):
"""Check if the matrix is unitary
UU* = I
"""
return np.allclose(np.eye(len(mat)), mat.dot(mat.T.conj()))
def P1(uni_mat):
"""Decompose the two-qubit gate without CNOT gate if possible
Decomposition without CNOT gate applies on the gate which can
be implemented with two parallel single qubit gate(without
entanglement). Then,
U = U1(2)⊗U2(2)
U1 = [[a, b],
[c, d]]
U2 = [[e, f],
[g, h]]
U = uij = [[ae, af, be, bf],
[ag, ah, bg, bh],
[ce, cf, de, df],
[cg, ch, dg, dh]]
Since |det U2| = 1 and |eh - fg| = 1,
|a| = sqrt(u00 * u11 - u01 * u10)
a = (u00 * u11 - u01 * u10)/sqrt() or (u00 * u11 - u01 * u10)/sqrt() * i
Same method for b, c, d.
Then apply this factorial method on U2 but with different positions in U.
|e| = sqrt(u00 * u22 - u02 * u20)
"""
print("U1(2)⊗U2(2) checking...")
U1 = uni_mat[:2, :2].copy()
det1 = abs(U1[0, 0] * U1[1, 1] - U1[0, 1] * U1[1, 0])
U2 = uni_mat[::2, ::2].copy()
det2 = abs(U2[0, 0] * U2[1, 1] - U2[0, 1] * U2[1, 0])
if det1 == 0 or det2 == 0:
U1 = uni_mat[2:, :2].copy()
det1 = abs(U1[0, 0] * U1[1, 1] - U1[0, 1] * U1[1, 0])
U2 = uni_mat[::2, ::2].copy()
det2 = abs(U2[0, 0] * U2[1, 1] - U2[0, 1] * U2[1, 0])
if det2 == 0:
U2 = uni_mat[1::2, ::2].copy()
det2 = abs(U2[0, 0] * U2[1, 1] - U2[0, 1] * U2[1, 0])
print("Tensor product decomposing...")
U1 = U1 / np.sqrt(det1)
U2 = U2 / np.sqrt(det2)
if np.allclose(np.kron(U2, U1), uni_mat) and is_unitary(U1) and is_unitary(U2):
print("Tensor product decomposing...")
Print_gate(U1, "q1")
Print_gate(U2, "q2")
return True
else:
if np.allclose(np.kron(U2/1j, U1), uni_mat) and is_unitary(U2/1j):
print("Tensor product decomposing...")
U2 = U2/1j
Print_gate(U1, "q1")
Print_gate(U2, "q2")
return True
elif np.allclose(np.kron(U2, U1/1j), uni_mat) and is_unitary(U1/1j):
print("Tensor product decomposing...")
U1 = U1/1j
Print_gate(U1, "q1")
Print_gate(U2, "q2")
return True
else:
print("Tensor product decomposing failed.")
return False
def P2(uni_mat):
"""Decompose SWAP gate to 3 CNOT gates
"""
if np.array_equal(uni_mat, SWAP):
print("SWAP gate found.")
print(CNOT)
print("on q1, q2.")
print(CNOT)
print("on q1, q2.")
print(CNOT)
print("on q1, q2.")
return True
else:
print("SWAP gate not found.")
return False
def P3(uni_mat):
"""Decompose Control U gate,
Control U = [[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, u1, u2],
[0, 0, u3, u4]]
U1 = [[u1, u2],
[u3, u4]]
= phase * Rz(beta) * Ry(gama) * Rz(delta)
U = EAXBXC
E = diag(1, phase)
A = Rz(beta) * Ry(gama/2)
B = Ry(-(gama)/2) * Rz(-(delta + beta)/2)
C = Rz((delta - beta)/2)
"""
print("Ctrl-U checking...")
I_check = uni_mat[:2, :2]
U = uni_mat[2:, 2:].copy()
if np.allclose(np.eye(len(uni_mat)-2), I_check) and is_unitary(U):
print("Ctrl-U found...")
print("U(2) decomposing...")
phase, beta, gama, delta = euler_angles_1q(U)
print("U = ", phase, "* Rz(", beta, ") * Ry(", gama, ") * Rz(", delta, ")")
print("Find A, B, C, which ABC = I and U = phase * AXBXC...")
A_mat = np.dot(Rz(beta), Ry(gama/2))
B_mat = np.dot(Ry(-(gama)/2), Rz(-(delta + beta)/2))
C_mat = Rz((delta - beta)/2)
E_mat = np.array([[1, 0],
[0, phase]], dtype = complex)
Print_gate(C_mat, "q2")
print("CNOT on q1, q2.")
Print_gate(B_mat, "q2")
print("CNOT on q1, q2.")
Print_gate(A_mat, "q2")
Print_gate(E_mat, "q1")
return True
else:
return False
#def P4
def two_qubit_decompose(uni_mat):
print("Decompose 2-qubit gate: ")
print(uni_mat)
print("Unitary matrix checking...")
if not is_unitary(uni_mat):
print("Unitary matrix not found.")
else:
if not P2(uni_mat):
if not P1(uni_mat):
P3(uni_mat)
if __name__ == '__main__':
## Create an unitary_matrix SU(4)
unitary_matrix = np.kron(H, H)
## Decompose it to single-qubit gate with CNOT.
two_qubit_decompose(unitary_matrix)