-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGates.py
406 lines (360 loc) · 11.7 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
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
"""
Module containing:
- Distance metrics
- Gate class and subclasses
- Common gate definitions
- GADGETS dictionary
"""
import qiskit as qt
import numpy as np
from sympy import flatten
# Absolute distance metric
dist_abs = lambda matrix_a, matrix_b: np.sum(np.abs(matrix_a - matrix_b))
# Frobenius norm - sum of squares of absolute values
dist_square = lambda matrix_a, matrix_b: np.sqrt(np.sum(np.square(np.abs(matrix_a - matrix_b))))
dist_frob = lambda matrix_a, matrix_b: np.sum(np.square(np.abs(matrix_a - matrix_b)))
# Square-root absolute distance metric
dist_sqrt = lambda matrix_a, matrix_b: np.square(np.sum(np.sqrt(np.abs(matrix_a - matrix_b))))
class Gate:
"""Gate class used for representing and applying unitary gates to a matrix or qiskit circuit.
Attributes
----------
matrix:
Numpy matrix representation of the unitary operation
qiskit:
Function that accepts two arguments: a circuit object, and qubit index. Applies the gate to the
circuit at the given index
num_qubits:
The number of qubits on which this gate acts. Currently only one and two-qubit gates are supported.
Methods
-------
reverse_matrix():
Reverses the order of the qubits for the matrix and returns it.
"""
def __init__(self, matrix, qiskit, num_qubits):
"""
Arguments
---------
matrix:
Numpy matrix representation of the unitary operation
qiskit:
Function that accepts two arguments: a circuit object, and qubit index. Applies the gate to the
circuit at the given index
num_qubits:
The number of qubits on which this gate acts. Currently only one and two-qubit gates are supported.
"""
self.matrix = matrix
self.qiskit = qiskit
if num_qubits != 1 and num_qubits != 2: raise Exception("Only supports 1 and 2 qubit gates.")
self.num_qubits = num_qubits
def __repr__(self):
return repr(self.matrix)
def reverse_matrix(self):
"""Reverses the order of the qubits for the matrix and returns it."""
return SWAP.matrix @ self.matrix @ SWAP.matrix
class ControlGate(Gate):
"""ControlGate class used to create a controlled-version of a single-qubit gate. A subclass of the Gate class.
Arguments
---------
gate: Gate
The single qubit unitary gate from which the controlled gate is constructed.
decompose: optional
If None, use the actual rotation gates. If set to a function, use that function to compute approximation
to each gate, using Hadamard and T gates (default None).
*args, **kwargs:
Optional arguments to pass to the DecomposedGate() function.
Attributes
----------
matrix
The 4x4 numpy matrix representation of the controlled unitary operation
qiskit
Function which accepts two arguments: a circuit object, and qubit indices. Applies the controlled-
gate to the circuit at the given indices.
num_qubits: int
The number of qubits on which this gate acts (2)
Methods
-------
__control_unitary(unitary):
Calculate and return the coefficients and matrices used to construct the controlled gate operation.
Private class, used in the initialisation of the object.
"""
def __init__(self, gate: Gate, decompose = None, *args, **kwargs):
"""
Arguments
---------
gate: Gate
The single qubit unitary gate from which the controlled gate is constructed.
"""
[alpha, beta, gamma, delta], mat_A, mat_B, mat_C = self.__control_unitary(gate.matrix)
self.matrix = np.identity(4, dtype = "complex")
self.matrix[2:4, 2:4] = gate.matrix
def qiskit_func(circuit, qubit_control, qubit_target):
"""Function used to apply the control-gate to a qiskit circuit."""
def apply_step(step, circuit, qubit):
if decompose is not None:
step = DecomposedGate(step, decompose, *args, **kwargs)
step.qiskit(circuit, qubit)
if (delta - beta)/2 != 0:
step = ROTZ((delta - beta)/2)
apply_step(step, circuit, qubit_target)
circuit.cx(qubit_control, qubit_target)
if (delta + beta)/2 != 0:
step = ROTZ(-(delta + beta)/2)
apply_step(step, circuit, qubit_target)
if gamma != 0:
step = ROTY(-gamma/2)
apply_step(step, circuit, qubit_target)
circuit.cx(qubit_control, qubit_target)
if gamma != 0:
step = ROTY(gamma/2)
apply_step(step, circuit, qubit_target)
if beta != 0:
step = ROTZ(beta)
apply_step(step, circuit, qubit_target)
if alpha != 0:
step = P(alpha)
apply_step(step, circuit, qubit_control)
self.qiskit = qiskit_func
self.num_qubits = 2
def __control_unitary(self, unitary):
"""Constructs the decomposition that allows for an arbitrary controlled-unitary gate to be
expressed using CNOTs and single-qubit gates.
Arguments
---------
unitary:
The 2x2 unitary matrix corresponding to the single qubit gate
Returns
-------
coefs:
The four terms: alpha, beta, gamma, delta
mat_A, mat_B, mat_C:
The four matrices, used as per the equations above
"""
# Unpack the matrix elementwise, corresponding to the matrix
# [ a, b ]
# [ c, d ]
[a, b], [c, d] = unitary
# Solve for alpha, beta, gamma, delta as per the above equations
alpha = np.log(a*d - b*c) / 2j
gamma = np.arccos((a*d - b*c) / (a*d + b*c))
# If there are zeroes then we have multiple solutions.
# In that case, set beta to 0 then solve for delta
if c*d == 0 and a*b*np.sin(gamma) == 0:
beta = 0
delta = np.log(d / (a * np.cos(gamma/2)**2)) / 1j
else:
beta = np.log(-4*c*d / (a*b * (np.sin(gamma)**2))) / 2j
delta = np.log(-b*d/(a*c*(np.tan(gamma/2)**2))) / 2j
# Construct the matrices as per above equations
mat_A = ROTZ(beta).matrix @ ROTY(gamma/2).matrix
mat_B = ROTY(-gamma/2).matrix @ ROTZ(-(delta + beta)/2).matrix
mat_C = ROTZ((delta - beta)/2).matrix
mat_0 = np.array(
[
[1, 0],
[0, np.exp(1j * alpha)]
]
)
return np.real([alpha, beta, gamma, delta]), mat_A, mat_B, mat_C
class DecomposedGate(Gate):
"""Class which constructs a decomposed one-qubit gate in terms of H and T gates, using a chosen algorithm.
Arguments
---------
gate: Gate
The Gate which is being approximated
algorithm:
The function which computes the approximation and returns approximation, distance, sequence
delta: float, optional
The delta argument for the algorithm (default 1e-1)
dist_fun: optional
Function which accepts two matrices and computes the distance between them (default dist_square)
*args, **kwargs:
Additional arguments which are passed to the algorithm function
Attributes
----------
matrix
The 2x2 numpy matrix representation of the controlled unitary operation
qiskit
Function which accepts two arguments: a circuit object, and qubit indices. Applies the controlled-
gate to the circuit at the given indices.
num_qubits: int
The number of qubits on which this gate acts (1)
sequence:
A list of 'H' and 'T' indicating the approximation used for the gate
distance:
The distance between the approximation and the original gate, as per the chosen dist_fun metric
"""
def __init__(self, gate: Gate, algorithm, delta = 1e-1, dist_fun = dist_square, *args, **kwargs):
self.num_qubits = gate.num_qubits
approximation, distance, sequence = algorithm(gate.matrix, delta = delta, dist_fun = dist_fun, *args, **kwargs)
self.matrix = approximation
self.sequence = flatten(sequence)
self.distance = distance
def qiskit_func(circuit, qubit):
# Apply Hadamard and T gates as per the approximation sequence
for gate in list("".join(flatten(sequence))):
if gate == "H":
func = circuit.h
elif gate == "T":
func = circuit.t
elif gate == "S":
func = circuit.s
elif gate == "X":
func = circuit.x
elif gate == "Y":
func = circuit.y
elif gate == "Z":
func = circuit.z
elif gate == "t":
func = circuit.tdg
elif gate == "s":
func = circuit.sdg
else:
raise Exception(f"Unrecognised gate: {gate}")
func(qubit)
self.qiskit = qiskit_func
# Hadamard gate
H = Gate(
np.array(
[
[1, 1],
[1, -1 + 0j]
]
) / np.sqrt(2),
qt.QuantumCircuit.h,
1
)
# 2x2 Identity
I = np.identity(2, dtype = "complex")
X = Gate(
np.array(
[
[0, 1],
[1, 0j]
]
),
qt.QuantumCircuit.x,
1
)
Y = Gate(
np.array(
[
[0, -1j],
[1j, 0]
]
),
qt.QuantumCircuit.y,
1
)
Z = Gate(
np.array(
[
[1, 0j],
[0, -1]
]
),
qt.QuantumCircuit.z,
1
)
S = Gate(
np.array(
[
[1, 0],
[0, np.exp(1j * np.pi / 2)]
]
),
qt.QuantumCircuit.s,
1
)
T = Gate(
np.array(
[
[1, 0],
[0, np.exp(1j * np.pi / 4)]
]
),
qt.QuantumCircuit.t,
1
)
P = lambda phi: Gate(
np.array(
[
[1, 0],
[0, np.exp(1j * phi)]
]
),
lambda circuit, qubit: circuit.p(phi, qubit),
1
)
# Controlled not gate, with second qubit the target
CTRL_X = Gate(
np.array(
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0j]
]
),
qt.QuantumCircuit.cx,
2
)
SWAP = Gate(
np.array(
[
[1, 0, 0, 0j],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]
]
),
qt.QuantumCircuit.swap,
2
)
ROTZ = lambda phi: Gate(
np.array(
[
[np.exp(-1j * phi / 2), 0],
[0, np.exp(1j * phi / 2)]
]
),
lambda circuit, qubit: circuit.rz(phi, qubit),
1
)
ROTY = lambda theta: Gate(
np.array(
[
[np.cos(theta/2), -np.sin(theta/2)],
[np.sin(theta/2), np.cos(theta/2)]
]
),
lambda circuit, qubit: circuit.ry(theta, qubit),
1
)
ROTX = lambda theta: Gate(
np.array(
[
[np.cos(theta/2), -1j*np.sin(theta/2)],
[-1j*np.sin(theta/2), np.cos(theta/2)]
]
),
lambda circuit, qubit: circuit.rx(theta, qubit),
1
)
R = lambda k: Gate(
mat := np.array(
[
[1, 0j],
[0, np.exp(2j*np.pi / (2 ** (k)))]
]
),
lambda circuit, qubit: circuit.unitary(mat, qubit, label = f"R({k})"),
1
)
# Gadgets - Matrices that we want to use to approximate other unitary matrices
GADGETS = {
'H': H.matrix,
'T': T.matrix,
'HTH': H.matrix @ T.matrix @ H.matrix,
'THT': T.matrix @ H.matrix @ T.matrix
}