-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy path_simulator.py
More file actions
executable file
·464 lines (405 loc) · 19 KB
/
_simulator.py
File metadata and controls
executable file
·464 lines (405 loc) · 19 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
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
# Copyright 2017 ProjectQ-Framework (www.projectq.ch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Contains the projectq interface to a C++-based simulator, which has to be
built first. If the c++ simulator is not exported to python, a (slow) python
implementation is used as an alternative.
"""
import math
import cmath
import random
from projectq.types import WeakQubitRef
from projectq.cengines import BasicEngine
from projectq.meta import get_control_count, LogicalQubitIDTag
from projectq.ops import (All,
NOT,
H,
R,
Measure,
FlushGate,
Allocate,
Deallocate,
BasicMathGate,
TimeEvolution,
UniformlyControlledGate,
DiagonalGate)
try:
from ._cppsim import Simulator as SimulatorBackend
except ImportError:
from ._pysim import Simulator as SimulatorBackend
class Simulator(BasicEngine):
"""
Simulator is a compiler engine which simulates a quantum computer using
C++-based kernels.
OpenMP is enabled and the number of threads can be controlled using the
OMP_NUM_THREADS environment variable, i.e.
.. code-block:: bash
export OMP_NUM_THREADS=4 # use 4 threads
export OMP_PROC_BIND=spread # bind threads to processors by spreading
"""
def __init__(self, gate_fusion=False, rnd_seed=None):
"""
Construct the C++/Python-simulator object and initialize it with a
random seed.
Args:
gate_fusion (bool): If True, gates are cached and only executed
once a certain gate-size has been reached (only has an effect
for the c++ simulator).
rnd_seed (int): Random seed (uses random.randint(0, 4294967295) by
default).
Example of gate_fusion: Instead of applying a Hadamard gate to 5
qubits, the simulator calculates the kronecker product of the 1-qubit
gate matrices and then applies one 5-qubit gate. This increases
operational intensity and keeps the simulator from having to iterate
through the state vector multiple times. Depending on the system (and,
especially, number of threads), this may or may not be beneficial.
Note:
If the C++ Simulator extension was not built or cannot be found,
the Simulator defaults to a Python implementation of the kernels.
While this is much slower, it is still good enough to run basic
quantum algorithms.
If you need to run large simulations, check out the tutorial in
the docs which gives futher hints on how to build the C++
extension.
"""
if rnd_seed is None:
rnd_seed = random.randint(0, 4294967295)
BasicEngine.__init__(self)
self._simulator = SimulatorBackend(rnd_seed)
self._gate_fusion = gate_fusion
def is_available(self, cmd):
"""
Specialized implementation of is_available: The simulator can deal
with all arbitrarily-controlled gates which provide a
gate-matrix (via gate.matrix) and acts on 5 or less qubits (not
counting the control qubits).
Args:
cmd (Command): Command for which to check availability (single-
qubit gate, arbitrary controls)
Returns:
True if it can be simulated and False otherwise.
"""
if (cmd.gate == Measure or cmd.gate == Allocate or
cmd.gate == Deallocate or
isinstance(cmd.gate, BasicMathGate) or
isinstance(cmd.gate, TimeEvolution) or
isinstance(cmd.gate, UniformlyControlledGate) or
isinstance(cmd.gate, DiagonalGate)):
return True
try:
m = cmd.gate.matrix
# Allow up to 5-qubit gates
if len(m) > 2 ** 5:
return False
return True
except:
return False
def _convert_logical_to_mapped_qureg(self, qureg):
"""
Converts a qureg from logical to mapped qubits if there is a mapper.
Args:
qureg (list[Qubit],Qureg): Logical quantum bits
"""
mapper = self.main_engine.mapper
if mapper is not None:
mapped_qureg = []
for qubit in qureg:
if qubit.id not in mapper.current_mapping:
raise RuntimeError("Unknown qubit id. "
"Please make sure you have called "
"eng.flush().")
new_qubit = WeakQubitRef(qubit.engine,
mapper.current_mapping[qubit.id])
mapped_qureg.append(new_qubit)
return mapped_qureg
else:
return qureg
def get_expectation_value(self, qubit_operator, qureg):
"""
Get the expectation value of qubit_operator w.r.t. the current wave
function represented by the supplied quantum register.
Args:
qubit_operator (projectq.ops.QubitOperator): Operator to measure.
qureg (list[Qubit],Qureg): Quantum bits to measure.
Returns:
Expectation value
Note:
Make sure all previous commands (especially allocations) have
passed through the compilation chain (call main_engine.flush() to
make sure).
Note:
If there is a mapper present in the compiler, this function
automatically converts from logical qubits to mapped qubits for
the qureg argument.
Raises:
Exception: If `qubit_operator` acts on more qubits than present in
the `qureg` argument.
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
num_qubits = len(qureg)
for term, _ in qubit_operator.terms.items():
if not term == () and term[-1][0] >= num_qubits:
raise Exception("qubit_operator acts on more qubits than "
"contained in the qureg.")
operator = [(list(term), coeff) for (term, coeff)
in qubit_operator.terms.items()]
return self._simulator.get_expectation_value(operator,
[qb.id for qb in qureg])
def apply_qubit_operator(self, qubit_operator, qureg):
"""
Apply a (possibly non-unitary) qubit_operator to the current wave
function represented by the supplied quantum register.
Args:
qubit_operator (projectq.ops.QubitOperator): Operator to apply.
qureg (list[Qubit],Qureg): Quantum bits to which to apply the
operator.
Raises:
Exception: If `qubit_operator` acts on more qubits than present in
the `qureg` argument.
Warning:
This function allows applying non-unitary gates and it will not
re-normalize the wave function! It is for numerical experiments
only and should not be used for other purposes.
Note:
Make sure all previous commands (especially allocations) have
passed through the compilation chain (call main_engine.flush() to
make sure).
Note:
If there is a mapper present in the compiler, this function
automatically converts from logical qubits to mapped qubits for
the qureg argument.
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
num_qubits = len(qureg)
for term, _ in qubit_operator.terms.items():
if not term == () and term[-1][0] >= num_qubits:
raise Exception("qubit_operator acts on more qubits than "
"contained in the qureg.")
operator = [(list(term), coeff) for (term, coeff)
in qubit_operator.terms.items()]
return self._simulator.apply_qubit_operator(operator,
[qb.id for qb in qureg])
def get_probability(self, bit_string, qureg):
"""
Return the probability of the outcome `bit_string` when measuring
the quantum register `qureg`.
Args:
bit_string (list[bool|int]|string[0|1]): Measurement outcome.
qureg (Qureg|list[Qubit]): Quantum register.
Returns:
Probability of measuring the provided bit string.
Note:
Make sure all previous commands (especially allocations) have
passed through the compilation chain (call main_engine.flush() to
make sure).
Note:
If there is a mapper present in the compiler, this function
automatically converts from logical qubits to mapped qubits for
the qureg argument.
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
bit_string = [bool(int(b)) for b in bit_string]
return self._simulator.get_probability(bit_string,
[qb.id for qb in qureg])
def get_amplitude(self, bit_string, qureg):
"""
Return the probability amplitude of the supplied `bit_string`.
The ordering is given by the quantum register `qureg`, which must
contain all allocated qubits.
Args:
bit_string (list[bool|int]|string[0|1]): Computational basis state
qureg (Qureg|list[Qubit]): Quantum register determining the
ordering. Must contain all allocated qubits.
Returns:
Probability amplitude of the provided bit string.
Note:
Make sure all previous commands (especially allocations) have
passed through the compilation chain (call main_engine.flush() to
make sure).
Note:
If there is a mapper present in the compiler, this function
automatically converts from logical qubits to mapped qubits for
the qureg argument.
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
bit_string = [bool(int(b)) for b in bit_string]
return self._simulator.get_amplitude(bit_string,
[qb.id for qb in qureg])
def set_wavefunction(self, wavefunction, qureg):
"""
Set the wavefunction and the qubit ordering of the simulator.
The simulator will adopt the ordering of qureg (instead of reordering
the wavefunction).
Args:
wavefunction (list[complex]): Array of complex amplitudes
describing the wavefunction (must be normalized).
qureg (Qureg|list[Qubit]): Quantum register determining the
ordering. Must contain all allocated qubits.
Note:
Make sure all previous commands (especially allocations) have
passed through the compilation chain (call main_engine.flush() to
make sure).
Note:
If there is a mapper present in the compiler, this function
automatically converts from logical qubits to mapped qubits for
the qureg argument.
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
self._simulator.set_wavefunction(wavefunction,
[qb.id for qb in qureg])
def collapse_wavefunction(self, qureg, values):
"""
Collapse a quantum register onto a classical basis state.
Args:
qureg (Qureg|list[Qubit]): Qubits to collapse.
values (list[bool|int]|string[0|1]): Measurement outcome for each
of the qubits in `qureg`.
Raises:
RuntimeError: If an outcome has probability (approximately) 0 or
if unknown qubits are provided (see note).
Note:
Make sure all previous commands have passed through the
compilation chain (call main_engine.flush() to make sure).
Note:
If there is a mapper present in the compiler, this function
automatically converts from logical qubits to mapped qubits for
the qureg argument.
"""
qureg = self._convert_logical_to_mapped_qureg(qureg)
return self._simulator.collapse_wavefunction([qb.id for qb in qureg],
[bool(int(v)) for v in
values])
def cheat(self):
"""
Access the ordering of the qubits and the state vector directly.
This is a cheat function which enables, e.g., more efficient
evaluation of expectation values and debugging.
Returns:
A tuple where the first entry is a dictionary mapping qubit
indices to bit-locations and the second entry is the corresponding
state vector.
Note:
Make sure all previous commands have passed through the
compilation chain (call main_engine.flush() to make sure).
Note:
If there is a mapper present in the compiler, this function
DOES NOT automatically convert from logical qubits to mapped
qubits.
"""
return self._simulator.cheat()
def _handle(self, cmd):
"""
Handle all commands, i.e., call the member functions of the C++-
simulator object corresponding to measurement, allocation/
deallocation, and (controlled) single-qubit gate.
Args:
cmd (Command): Command to handle.
Raises:
Exception: If a non-single-qubit gate needs to be processed
(which should never happen due to is_available).
"""
if cmd.gate == Measure:
assert(get_control_count(cmd) == 0)
ids = [qb.id for qr in cmd.qubits for qb in qr]
out = self._simulator.measure_qubits(ids)
i = 0
for qr in cmd.qubits:
for qb in qr:
# Check if a mapper assigned a different logical id
logical_id_tag = None
for tag in cmd.tags:
if isinstance(tag, LogicalQubitIDTag):
logical_id_tag = tag
if logical_id_tag is not None:
qb = WeakQubitRef(qb.engine,
logical_id_tag.logical_qubit_id)
self.main_engine.set_measurement_result(qb, out[i])
i += 1
elif cmd.gate == Allocate:
ID = cmd.qubits[0][0].id
self._simulator.allocate_qubit(ID)
elif cmd.gate == Deallocate:
ID = cmd.qubits[0][0].id
self._simulator.deallocate_qubit(ID)
elif isinstance(cmd.gate, BasicMathGate):
qubitids = []
for qr in cmd.qubits:
qubitids.append([])
for qb in qr:
qubitids[-1].append(qb.id)
math_fun = cmd.gate.get_math_function(cmd.qubits)
self._simulator.emulate_math(math_fun, qubitids,
[qb.id for qb in cmd.control_qubits])
elif isinstance(cmd.gate, TimeEvolution):
op = [(list(term), coeff) for (term, coeff)
in cmd.gate.hamiltonian.terms.items()]
t = cmd.gate.time
qubitids = [qb.id for qb in cmd.qubits[0]]
ctrlids = [qb.id for qb in cmd.control_qubits]
self._simulator.emulate_time_evolution(op, t, qubitids, ctrlids)
elif isinstance(cmd.gate, UniformlyControlledGate):
choice_ids = [qb.id for qb in cmd.qubits[0]]
ctrl_ids = [qb.id for qb in cmd.control_qubits]
target_id = cmd.qubits[1][0].id
unitaries = [gate.matrix.tolist() for gate in cmd.gate.gates]
assert len(unitaries) == 2**len(choice_ids)
self._simulator.apply_uniformly_controlled_gate(unitaries,
target_id,
choice_ids,
ctrl_ids)
if cmd.gate.up_to_diagonal:
angles = [-cmath.phase(p) for p in cmd.gate.decomposition[1]]
ids = [target_id]+choice_ids
self._simulator.apply_diagonal_gate(angles, ids, [])
elif isinstance(cmd.gate, DiagonalGate):
ids = [q.id for qr in cmd.qubits for q in qr]
ctrlids = [qb.id for qb in cmd.control_qubits]
angles = cmd.gate.angles
assert len(angles) == 2**len(ids)
self._simulator.apply_diagonal_gate(angles, ids, ctrlids)
elif len(cmd.gate.matrix) <= 2 ** 5:
matrix = cmd.gate.matrix
ids = [qb.id for qr in cmd.qubits for qb in qr]
if not 2 ** len(ids) == len(cmd.gate.matrix):
raise Exception("Simulator: Error applying {} gate: "
"{}-qubit gate applied to {} qubits.".format(
str(cmd.gate),
int(math.log(len(cmd.gate.matrix), 2)),
len(ids)))
self._simulator.apply_controlled_gate(matrix.tolist(),
ids,
[qb.id for qb in
cmd.control_qubits])
if not self._gate_fusion:
self._simulator.run()
else:
raise Exception("This simulator only supports controlled k-qubit"
" gates with k < 6!\nPlease add an auto-replacer"
" engine to your list of compiler engines.")
def receive(self, command_list):
"""
Receive a list of commands from the previous engine and handle them
(simulate them classically) prior to sending them on to the next
engine.
Args:
command_list (list<Command>): List of commands to execute on the
simulator.
"""
for cmd in command_list:
if not cmd.gate == FlushGate():
self._handle(cmd)
else:
self._simulator.run() # flush gate --> run all saved gates
if not self.is_last_engine:
self.send([cmd])