-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathpyqrack_simulator.py
More file actions
329 lines (259 loc) · 10.8 KB
/
pyqrack_simulator.py
File metadata and controls
329 lines (259 loc) · 10.8 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
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
#
# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by Stephanie Wehner, QuTech.
# 4. Neither the name of the QuTech organization nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
try:
from pyqrack import QrackSimulator, Pauli
except ImportError:
raise RuntimeError("If you want to use the pyqrack backend you need to install the python package 'pyqrack'")
import numpy as np
from simulaqron.virtual_node.basics import quantumEngine, quantumError, noQubitError
class pyqrackEngine(quantumEngine):
"""
Basic quantum engine which uses PyQrack.
Attributes:
maxQubits: maximum number of qubits this engine will support.
"""
def __init__(self, node, num, maxQubits=10):
"""
Initialize the simple engine. If no number is given for maxQubits, the assumption will be 10.
"""
super().__init__(node=node, num=num, maxQubits=maxQubits)
self.engine = QrackSimulator()
# We start with no active qubits
self.activeQubits = 0
self.nextQid = 0
self.qubitReg = []
def add_fresh_qubit(self):
"""
Add a new qubit initialized in the \|0\> state.
"""
# Check if we are still allowed to add qubits
if self.activeQubits >= self.maxQubits:
raise noQubitError("No more qubits available in register.")
# Prepare a clean qubit state in |0>
qid = self.nextQid
self.nextQid += 1
self.engine.allocate_qubit(qid)
self.activeQubits += 1
self.qubitReg.append(qid)
return qid
def add_qubit(self, newQubit):
"""
Add new qubit in the state described by the vector newQubit ([a, b])
"""
norm = np.dot(np.array(newQubit), np.array(newQubit).conj())
if not norm <= 1:
raise quantumError("State {} is not normalized.".format(newQubit))
# Create a fresh qubit
qid = self.add_fresh_qubit()
# Find an appropriate state preparation gate
prob = np.dot(complex(newQubit[1]), np.conj(complex(newQubit[1])))
sqrtProb = np.sqrt(prob)
sqrt1MinProb = np.sqrt(1 - prob)
phase0 = 0
if sqrt1MinProb > 0:
phase0 = complex(newQubit[0]) / sqrt1MinProb
phase1 = 0
if sqrtProb > 0:
phase1 = complex(newQubit[1]) / sqrt1MinProb
cMtrx = [sqrt1MinProb * phase0, sqrtProb * phase0, sqrtProb * phase1, -sqrt1MinProb * phase1]
# Transform the new qubit into the correct state
self.engine.mtrx(cMtrx, qid)
return qid
def validate_qid(self, qid):
if qid not in self.qubitReg:
raise quantumError("No such qubit to remove")
def remove_qubit(self, qubitNum):
"""
Removes the qubit with the desired number qubitNum
"""
self.validate_qid(qubitNum)
self.engine.release(qubitNum)
self.qubitReg.remove(qubitNum)
self.activeQubits -= 1
def get_register_RI(self):
"""
Retrieves the entire register in real and imaginary parts and returns the result as a
list. Twisted only likes to send real valued lists, not complex ones.
"""
raise NotImplementedError("get_register_RI() not implemented for this backend!")
def apply_H(self, qubitNum):
"""
Applies a Hadamard gate to the qubits with number qubitNum.
"""
self.validate_qid(qubitNum)
self.engine.h(qubitNum)
def apply_K(self, qubitNum):
"""
Applies a K gate to the qubits with number qubitNum. Maps computational basis to Y eigenbasis.
"""
self.validate_qid(qubitNum)
self.engine.h(qubitNum)
self.engine.s(qubitNum)
self.engine.h(qubitNum)
self.engine.z(qubitNum)
def apply_X(self, qubitNum):
"""
Applies a X gate to the qubits with number qubitNum.
"""
self.validate_qid(qubitNum)
self.engine.x(qubitNum)
def apply_Z(self, qubitNum):
"""
Applies a Z gate to the qubits with number qubitNum.
"""
self.validate_qid(qubitNum)
self.engine.z(qubitNum)
def apply_Y(self, qubitNum):
"""
Applies a Y gate to the qubits with number qubitNum.
"""
self.validate_qid(qubitNum)
self.engine.y(qubitNum)
def apply_T(self, qubitNum):
"""
Applies a T gate to the qubits with number qubitNum.
"""
self.validate_qid(qubitNum)
self.engine.t(qubitNum)
def apply_rotation(self, qubitNum, n, a):
"""
Applies a rotation around the axis n with the angle a to qubit with number qubitNum. If n is zero a ValueError
is raised.
:param qubitNum: int
Qubit number
:param n: tuple of floats
A tuple of three numbers specifying the rotation axis, e.g n=(1,0,0)
:param a: float
The rotation angle in radians.
"""
self.validate_qid(qubitNum)
n = tuple(n)
if n == (1, 0, 0):
self.engine.r(Pauli.PauliX, a, qubitNum)
elif n == (0, 1, 0):
self.engine.r(Pauli.PauliY, a, qubitNum)
elif n == (0, 0, 1):
self.engine.r(Pauli.PauliZ, a, qubitNum)
else:
raise NotImplementedError("Can only do rotations around X, Y, or Z axis right now")
def validate_control_qids(self, qid1, qid2):
if qid1 not in self.qubitReg:
raise quantumError("No such qubit to act as a control qubit")
if qid2 not in self.qubitReg:
raise quantumError("No such qubit to act as a target qubit")
if qid1 == qid2:
raise quantumError("Control and target are equal")
def apply_CNOT(self, qubitNum1, qubitNum2):
"""
Applies the CNOT to the qubit with the numbers qubitNum1 and qubitNum2.
"""
self.validate_control_qids(qubitNum1, qubitNum2)
self.engine.mcx([qubitNum1], qubitNum2)
def apply_CPHASE(self, qubitNum1, qubitNum2):
"""
Applies the CPHASE to the qubit with the numbers qubitNum1 and qubitNum2.
"""
self.validate_control_qids(qubitNum1, qubitNum2)
self.engine.mcz([qubitNum1], qubitNum2)
def apply_onequbit_gate(self, gate, qubitNum):
"""
Applies a unitary gate to the specified qubit.
Arguments:
gate The pyqrack gate to be applied
qubitNum the number of the qubit this gate is applied to
"""
self.validate_qid(qubitNum)
self.engine.mtrx(gate, qubitNum)
def apply_twoqubit_gate(self, gate, qubit1, qubit2):
"""
Applies a unitary gate to the two specified qubits.
Arguments:
gate The pyqrack gate to be applied
qubit1 the first qubit
qubit2 the second qubit
"""
raise NotImplementedError("apply_twoqubit_gate() not implemented for this backend!")
def measure_qubit_inplace(self, qubitNum):
"""
Measures the desired qubit in the standard basis. This returns the classical outcome. The quantum register
is in the post-measurment state corresponding to the obtained outcome.
Arguments:
qubitNum qubit to be measured
"""
# Check we have such a qubit...
self.validate_qid(qubitNum)
outcome = self.engine.m(qubitNum)
# return measurement outcome
return outcome
def measure_qubit(self, qubitNum):
"""
Measures the desired qubit in the standard basis. This returns the classical outcome and deletes the qubit.
Arguments:
qubitNum qubit to be measured
"""
outcome = self.measure_qubit_inplace(qubitNum)
self.remove_qubit(qubitNum)
return outcome
def replace_qubit(self, qubitNum, state):
"""
Replaces the qubit at position qubitNum with the one given by state.
"""
raise NotImplementedError("Currently you cannot replace a qubit using pyqrack as backend")
def absorb(self, other):
"""
Absorb the qubits from the other engine into this one. This is done by tensoring the state at the end.
"""
# Check whether there is space
newNum = self.activeQubits + other.activeQubits
if newNum > self.maxQubits:
raise quantumError("Cannot merge: qubits exceed the maximum available.\n")
# Check whether there are in fact qubits to tensor up....
if self.activeQubits == 0:
self.engine = other.engine
self.qubitReg = list(other.qubitReg)
self.nextQid = other.nextQid
elif other.activeQubits > 0:
# PyQrack can internally "compose" the two engines together.
nQubits = []
for q in range(other.activeQubits):
nQubits.append(self.nextQid)
self.nextQid += 1
self.engine.compose(other.engine, nQubits)
# Add the qubits to the list of qubits
self.qubitReg += nQubits
self.activeQubits = newNum
def absorb_parts(self, R, I, activeQ):
"""
Absorb the qubits, given in pieces
Arguments:
R real part of the qubit state as a list
I imaginary part as a list
activeQ active number of qubits
"""
raise NotImplementedError("Currently you cannot absorb_parts() using pyqrack as backend")