-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy path_simulator_test.py
More file actions
executable file
·773 lines (639 loc) · 23.8 KB
/
_simulator_test.py
File metadata and controls
executable file
·773 lines (639 loc) · 23.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# 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.
"""
Tests for projectq.backends._sim._simulator.py, using both the Python
and the C++ simulator as backends.
"""
import copy
import math
import cmath
import numpy
import pytest
import random
import scipy
import scipy.sparse
import scipy.sparse.linalg
from projectq import MainEngine
from projectq.cengines import (BasicEngine, BasicMapperEngine, DummyEngine,
LocalOptimizer, NotYetMeasuredError)
from projectq.ops import (All, Allocate, BasicGate, BasicMathGate, CNOT,
Command, H, MatrixGate, Measure, QubitOperator,
Rx, Ry, Rz, S, T, TimeEvolution, Toffoli, X, Y, Z,
DiagonalGate, UniformlyControlledGate)
from projectq.meta import Control, Dagger, LogicalQubitIDTag
from projectq.types import WeakQubitRef
from projectq.backends import Simulator
def test_is_cpp_simulator_present():
import projectq.backends._sim._cppsim
assert projectq.backends._sim._cppsim
def get_available_simulators():
result = ["py_simulator"]
try:
import projectq.backends._sim._cppsim as _
result.append("cpp_simulator")
except ImportError:
# The C++ simulator was either not installed or is misconfigured. Skip.
pass
return result
@pytest.fixture(params=get_available_simulators())
def sim(request):
if request.param == "cpp_simulator":
from projectq.backends._sim._cppsim import Simulator as CppSim
sim = Simulator(gate_fusion=True)
sim._simulator = CppSim(1)
return sim
if request.param == "py_simulator":
from projectq.backends._sim._pysim import Simulator as PySim
sim = Simulator()
sim._simulator = PySim(1)
return sim
@pytest.fixture(params=["mapper", "no_mapper"])
def mapper(request):
"""
Adds a mapper which changes qubit ids by adding 1
"""
if request.param == "mapper":
class TrivialMapper(BasicMapperEngine):
def __init__(self):
BasicEngine.__init__(self)
self.current_mapping = dict()
def receive(self, command_list):
for cmd in command_list:
for qureg in cmd.all_qubits:
for qubit in qureg:
if qubit.id == -1:
continue
elif qubit.id not in self.current_mapping:
previous_map = self.current_mapping
previous_map[qubit.id] = qubit.id + 1
self.current_mapping = previous_map
self._send_cmd_with_mapped_ids(cmd)
return TrivialMapper()
if request.param == "no_mapper":
return None
class Mock1QubitGate(MatrixGate):
def __init__(self):
MatrixGate.__init__(self)
self.cnt = 0
@property
def matrix(self):
self.cnt += 1
return [[0, 1], [1, 0]]
class Mock6QubitGate(MatrixGate):
def __init__(self):
MatrixGate.__init__(self)
self.cnt = 0
@property
def matrix(self):
self.cnt += 1
return numpy.eye(2 ** 6)
class MockNoMatrixGate(BasicGate):
def __init__(self):
BasicGate.__init__(self)
self.cnt = 0
def test_simulator_is_available(sim):
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend, [])
qubit = eng.allocate_qubit()
Measure | qubit
BasicMathGate(lambda x: x) | qubit
qubit[0].__del__()
assert len(backend.received_commands) == 4
# Test that allocate, measure, basic math, and deallocate are available.
for cmd in backend.received_commands:
assert sim.is_available(cmd)
new_cmd = backend.received_commands[-1]
new_cmd.gate = Mock1QubitGate()
assert sim.is_available(new_cmd)
assert new_cmd.gate.cnt == 1
new_cmd.gate = Mock6QubitGate()
assert not sim.is_available(new_cmd)
assert new_cmd.gate.cnt == 1
new_cmd.gate = MockNoMatrixGate()
assert not sim.is_available(new_cmd)
assert new_cmd.gate.cnt == 0
def test_simulator_cheat(sim):
# cheat function should return a tuple
assert isinstance(sim.cheat(), tuple)
# first entry is the qubit mapping.
# should be empty:
assert len(sim.cheat()[0]) == 0
# state vector should only have 1 entry:
assert len(sim.cheat()[1]) == 1
eng = MainEngine(sim, [])
qubit = eng.allocate_qubit()
# one qubit has been allocated
assert len(sim.cheat()[0]) == 1
assert sim.cheat()[0][0] == 0
assert len(sim.cheat()[1]) == 2
assert 1. == pytest.approx(abs(sim.cheat()[1][0]))
qubit[0].__del__()
# should be empty:
assert len(sim.cheat()[0]) == 0
# state vector should only have 1 entry:
assert len(sim.cheat()[1]) == 1
def test_simulator_functional_measurement(sim):
eng = MainEngine(sim, [])
qubits = eng.allocate_qureg(5)
# entangle all qubits:
H | qubits[0]
for qb in qubits[1:]:
CNOT | (qubits[0], qb)
All(Measure) | qubits
bit_value_sum = sum([int(qubit) for qubit in qubits])
assert bit_value_sum == 0 or bit_value_sum == 5
def test_simulator_measure_mapped_qubit(sim):
eng = MainEngine(sim, [])
qb1 = WeakQubitRef(engine=eng, idx=1)
qb2 = WeakQubitRef(engine=eng, idx=2)
cmd0 = Command(engine=eng, gate=Allocate, qubits=([qb1],))
cmd1 = Command(engine=eng, gate=X, qubits=([qb1],))
cmd2 = Command(engine=eng, gate=Measure, qubits=([qb1],), controls=[],
tags=[LogicalQubitIDTag(2)])
with pytest.raises(NotYetMeasuredError):
int(qb1)
with pytest.raises(NotYetMeasuredError):
int(qb2)
eng.send([cmd0, cmd1, cmd2])
eng.flush()
with pytest.raises(NotYetMeasuredError):
int(qb1)
assert int(qb2) == 1
class Plus2Gate(BasicMathGate):
def __init__(self):
BasicMathGate.__init__(self, lambda x: (x+2,))
def test_simulator_emulation(sim):
eng = MainEngine(sim, [])
qubit1 = eng.allocate_qubit()
qubit2 = eng.allocate_qubit()
qubit3 = eng.allocate_qubit()
with Control(eng, qubit3):
Plus2Gate() | (qubit1 + qubit2)
assert 1. == pytest.approx(sim.cheat()[1][0])
X | qubit3
with Control(eng, qubit3):
Plus2Gate() | (qubit1 + qubit2)
assert 1. == pytest.approx(sim.cheat()[1][6])
All(Measure) | (qubit1 + qubit2 + qubit3)
def test_simulator_kqubit_gate(sim):
m1 = Rx(0.3).matrix
m2 = Rx(0.8).matrix
m3 = Ry(0.1).matrix
m4 = Rz(0.9).matrix.dot(Ry(-0.1).matrix)
m = numpy.kron(m4, numpy.kron(m3, numpy.kron(m2, m1)))
class KQubitGate(BasicGate):
@property
def matrix(self):
return m
eng = MainEngine(sim, [])
qureg = eng.allocate_qureg(4)
qubit = eng.allocate_qubit()
Rx(-0.3) | qureg[0]
Rx(-0.8) | qureg[1]
Ry(-0.1) | qureg[2]
Rz(-0.9) | qureg[3]
Ry(0.1) | qureg[3]
X | qubit
with Control(eng, qubit):
KQubitGate() | qureg
X | qubit
with Control(eng, qubit):
with Dagger(eng):
KQubitGate() | qureg
assert sim.get_amplitude('0' * 5, qubit + qureg) == pytest.approx(1.)
class LargerGate(BasicGate):
@property
def matrix(self):
return numpy.eye(2 ** 6)
with pytest.raises(Exception):
LargerGate() | (qureg + qubit)
def test_simulator_kqubit_exception(sim):
m1 = Rx(0.3).matrix
m2 = Rx(0.8).matrix
m3 = Ry(0.1).matrix
m4 = Rz(0.9).matrix.dot(Ry(-0.1).matrix)
m = numpy.kron(m4, numpy.kron(m3, numpy.kron(m2, m1)))
class KQubitGate(BasicGate):
@property
def matrix(self):
return m
eng = MainEngine(sim, [])
qureg = eng.allocate_qureg(3)
with pytest.raises(Exception):
KQubitGate() | qureg
with pytest.raises(Exception):
H | qureg
def test_simulator_probability(sim, mapper):
engine_list = [LocalOptimizer()]
if mapper is not None:
engine_list.append(mapper)
eng = MainEngine(sim, engine_list=engine_list)
qubits = eng.allocate_qureg(6)
All(H) | qubits
eng.flush()
bits = [0, 0, 1, 0, 1, 0]
for i in range(6):
assert (eng.backend.get_probability(bits[:i], qubits[:i]) ==
pytest.approx(0.5**i))
extra_qubit = eng.allocate_qubit()
with pytest.raises(RuntimeError):
eng.backend.get_probability([0], extra_qubit)
del extra_qubit
All(H) | qubits
Ry(2 * math.acos(math.sqrt(0.3))) | qubits[0]
eng.flush()
assert eng.backend.get_probability([0], [qubits[0]]) == pytest.approx(0.3)
Ry(2 * math.acos(math.sqrt(0.4))) | qubits[2]
eng.flush()
assert eng.backend.get_probability([0], [qubits[2]]) == pytest.approx(0.4)
assert (eng.backend.get_probability([0, 0], qubits[:3:2]) ==
pytest.approx(0.12))
assert (eng.backend.get_probability([0, 1], qubits[:3:2]) ==
pytest.approx(0.18))
assert (eng.backend.get_probability([1, 0], qubits[:3:2]) ==
pytest.approx(0.28))
All(Measure) | qubits
def test_simulator_amplitude(sim, mapper):
engine_list = [LocalOptimizer()]
if mapper is not None:
engine_list.append(mapper)
eng = MainEngine(sim, engine_list=engine_list)
qubits = eng.allocate_qureg(6)
All(X) | qubits
All(H) | qubits
eng.flush()
bits = [0, 0, 1, 0, 1, 0]
assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(1. / 8.)
bits = [0, 0, 0, 0, 1, 0]
assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(-1. / 8.)
bits = [0, 1, 1, 0, 1, 0]
assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(-1. / 8.)
All(H) | qubits
All(X) | qubits
Ry(2 * math.acos(0.3)) | qubits[0]
eng.flush()
bits = [0] * 6
assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(0.3)
bits[0] = 1
assert (eng.backend.get_amplitude(bits, qubits) ==
pytest.approx(math.sqrt(0.91)))
All(Measure) | qubits
# raises if not all qubits are in the list:
with pytest.raises(RuntimeError):
eng.backend.get_amplitude(bits, qubits[:-1])
# doesn't just check for length:
with pytest.raises(RuntimeError):
eng.backend.get_amplitude(bits, qubits[:-1] + [qubits[0]])
extra_qubit = eng.allocate_qubit()
eng.flush()
# there is a new qubit now!
with pytest.raises(RuntimeError):
eng.backend.get_amplitude(bits, qubits)
def test_simulator_expectation(sim, mapper):
engine_list = []
if mapper is not None:
engine_list.append(mapper)
eng = MainEngine(sim, engine_list=engine_list)
qureg = eng.allocate_qureg(3)
op0 = QubitOperator('Z0')
expectation = sim.get_expectation_value(op0, qureg)
assert 1. == pytest.approx(expectation)
X | qureg[0]
expectation = sim.get_expectation_value(op0, qureg)
assert -1. == pytest.approx(expectation)
H | qureg[0]
op1 = QubitOperator('X0')
expectation = sim.get_expectation_value(op1, qureg)
assert -1. == pytest.approx(expectation)
Z | qureg[0]
expectation = sim.get_expectation_value(op1, qureg)
assert 1. == pytest.approx(expectation)
X | qureg[0]
S | qureg[0]
Z | qureg[0]
X | qureg[0]
op2 = QubitOperator('Y0')
expectation = sim.get_expectation_value(op2, qureg)
assert 1. == pytest.approx(expectation)
Z | qureg[0]
expectation = sim.get_expectation_value(op2, qureg)
assert -1. == pytest.approx(expectation)
op_sum = QubitOperator('Y0 X1 Z2') + QubitOperator('X1')
H | qureg[1]
X | qureg[2]
expectation = sim.get_expectation_value(op_sum, qureg)
assert 2. == pytest.approx(expectation)
op_sum = QubitOperator('Y0 X1 Z2') + QubitOperator('X1')
X | qureg[2]
expectation = sim.get_expectation_value(op_sum, qureg)
assert 0. == pytest.approx(expectation)
op_id = .4 * QubitOperator(())
expectation = sim.get_expectation_value(op_id, qureg)
assert .4 == pytest.approx(expectation)
def test_simulator_expectation_exception(sim):
eng = MainEngine(sim, [])
qureg = eng.allocate_qureg(3)
op = QubitOperator('Z2')
sim.get_expectation_value(op, qureg)
op2 = QubitOperator('Z3')
with pytest.raises(Exception):
sim.get_expectation_value(op2, qureg)
op3 = QubitOperator('Z1') + QubitOperator('X1 Y3')
with pytest.raises(Exception):
sim.get_expectation_value(op3, qureg)
def test_simulator_applyqubitoperator_exception(sim):
eng = MainEngine(sim, [])
qureg = eng.allocate_qureg(3)
op = QubitOperator('Z2')
sim.apply_qubit_operator(op, qureg)
op2 = QubitOperator('Z3')
with pytest.raises(Exception):
sim.apply_qubit_operator(op2, qureg)
op3 = QubitOperator('Z1') + QubitOperator('X1 Y3')
with pytest.raises(Exception):
sim.apply_qubit_operator(op3, qureg)
def test_simulator_applyqubitoperator(sim, mapper):
engine_list = []
if mapper is not None:
engine_list.append(mapper)
eng = MainEngine(sim, engine_list=engine_list)
qureg = eng.allocate_qureg(3)
op = QubitOperator('X0 Y1 Z2')
sim.apply_qubit_operator(op, qureg)
X | qureg[0]
Y | qureg[1]
Z | qureg[2]
assert sim.get_amplitude('000', qureg) == pytest.approx(1.)
H | qureg[0]
op_H = 1. / math.sqrt(2.) * (QubitOperator('X0') + QubitOperator('Z0'))
sim.apply_qubit_operator(op_H, [qureg[0]])
assert sim.get_amplitude('000', qureg) == pytest.approx(1.)
op_Proj0 = 0.5 * (QubitOperator('') + QubitOperator('Z0'))
op_Proj1 = 0.5 * (QubitOperator('') - QubitOperator('Z0'))
H | qureg[0]
sim.apply_qubit_operator(op_Proj0, [qureg[0]])
assert sim.get_amplitude('000', qureg) == pytest.approx(1. / math.sqrt(2.))
sim.apply_qubit_operator(op_Proj1, [qureg[0]])
assert sim.get_amplitude('000', qureg) == pytest.approx(0.)
def test_simulator_time_evolution(sim):
N = 8 # number of qubits
time_to_evolve = 1.1 # time to evolve for
eng = MainEngine(sim, [])
qureg = eng.allocate_qureg(N)
# initialize in random wavefunction by applying some gates:
for qb in qureg:
Rx(random.random()) | qb
Ry(random.random()) | qb
eng.flush()
# Use cheat to get initial start wavefunction:
qubit_to_bit_map, init_wavefunction = copy.deepcopy(eng.backend.cheat())
Qop = QubitOperator
op = 0.3 * Qop("X0 Y1 Z2 Y3 X4")
op += 1.1 * Qop(())
op += -1.4 * Qop("Y0 Z1 X3 Y5")
op += -1.1 * Qop("Y1 X2 X3 Y4")
ctrl_qubit = eng.allocate_qubit()
H | ctrl_qubit
with Control(eng, ctrl_qubit):
TimeEvolution(time_to_evolve, op) | qureg
eng.flush()
qbit_to_bit_map, final_wavefunction = copy.deepcopy(eng.backend.cheat())
All(Measure) | qureg + ctrl_qubit
# Check manually:
def build_matrix(list_single_matrices):
res = list_single_matrices[0]
for i in range(1, len(list_single_matrices)):
res = scipy.sparse.kron(res, list_single_matrices[i])
return res
id_sp = scipy.sparse.identity(2, format="csr", dtype=complex)
x_sp = scipy.sparse.csr_matrix([[0., 1.], [1., 0.]], dtype=complex)
y_sp = scipy.sparse.csr_matrix([[0., -1.j], [1.j, 0.]], dtype=complex)
z_sp = scipy.sparse.csr_matrix([[1., 0.], [0., -1.]], dtype=complex)
gates = [x_sp, y_sp, z_sp]
res_matrix = 0
for t, c in op.terms.items():
matrix = [id_sp] * N
for idx, gate in t:
matrix[qbit_to_bit_map[qureg[idx].id]] = gates[ord(gate) -
ord('X')]
matrix.reverse()
res_matrix += build_matrix(matrix) * c
res_matrix *= -1j * time_to_evolve
init_wavefunction = numpy.array(init_wavefunction, copy=False)
final_wavefunction = numpy.array(final_wavefunction, copy=False)
res = scipy.sparse.linalg.expm_multiply(res_matrix, init_wavefunction)
half = int(len(final_wavefunction) / 2)
hadamard_f = 1. / math.sqrt(2.)
# check evolution and control
assert numpy.allclose(hadamard_f * res, final_wavefunction[half:])
assert numpy.allclose(final_wavefunction[:half], hadamard_f *
init_wavefunction)
def test_simulator_apply_diagonal_gate(sim):
eng = MainEngine(sim)
qureg = eng.allocate_qureg(4)
eng.flush()
target_1 = qureg[0]
control = qureg[1]
target_0 = qureg[2]
empty = qureg[3]
wf = [1./4.]*(1 << 4)
eng.backend.set_wavefunction(wf, qureg)
D = DiagonalGate(angles=range(4))
with Control(eng, control):
D | (target_0, target_1)
eng.flush()
qbit_to_bit_map, final_wavefunction = copy.deepcopy(eng.backend.cheat())
All(Measure) | qureg
desired_state = [1./4.*cmath.exp(1j*i) for i in
[0, 0, 0, 2, 0, 0, 1, 3, 0, 0, 0, 2, 0, 0, 1, 3]]
assert numpy.allclose(final_wavefunction, desired_state)
def test_simulator_apply_uniformly_controlled_gate():
eng = MainEngine()
qureg = eng.allocate_qureg(3)
eng.flush()
wf = [math.sqrt(1./8.)]*(1 << 3)
eng.backend.set_wavefunction(wf, qureg)
A = Rx(numpy.pi/5)
B = H
C = Rz(numpy.pi/5)
D = Ry(numpy.pi/3)
U = UniformlyControlledGate([A, B, C, D])
with Dagger(eng):
U | ([qureg[0], qureg[2]], qureg[1])
eng.flush()
qbit_to_bit_map, final_wavefunction = copy.deepcopy(eng.backend.cheat())
vec = numpy.array([final_wavefunction]).T
vec[[1, 2]] = vec[[2, 1]] # reorder basis
vec[[5, 6]] = vec[[6, 5]]
reference = numpy.matrix(scipy.linalg.block_diag(A.matrix, B.matrix,
C.matrix, D.matrix))
assert numpy.allclose(reference*vec, wf)
def test_simulator_apply_uniformly_controlled_gate_with_control(sim):
eng = MainEngine(sim)
qureg = eng.allocate_qureg(5)
eng.flush()
control = qureg[0]
choice_1 = qureg[1]
target = qureg[2]
empty = qureg[3]
choice_0 = qureg[4]
gates = [X, H, S, T]
U = UniformlyControlledGate(gates)
id = Rz(0.0)
gates_equiv = [id, X, id, S, id, X, id, S,
id, H, id, T, id, H, id, T]
U_equiv = UniformlyControlledGate(gates_equiv)
All(H) | qureg
with Control(eng, control):
U | ([choice_0, choice_1], target)
with Dagger(eng):
All(H) | qureg
U_equiv | (qureg[0:2]+qureg[3:5], target)
eng.flush()
qbit_to_bit_map, final_wavefunction = copy.deepcopy(eng.backend.cheat())
All(Measure) | qureg
print(final_wavefunction)
desired_state = [1.0] + [0.0]*31
assert numpy.allclose(final_wavefunction, desired_state)
def test_simulator_set_wavefunction(sim, mapper):
engine_list = [LocalOptimizer()]
if mapper is not None:
engine_list.append(mapper)
eng = MainEngine(sim, engine_list=engine_list)
qubits = eng.allocate_qureg(2)
wf = [0., 0., math.sqrt(0.2), math.sqrt(0.8)]
with pytest.raises(RuntimeError):
eng.backend.set_wavefunction(wf, qubits)
eng.flush()
eng.backend.set_wavefunction(wf, qubits)
assert pytest.approx(eng.backend.get_probability('1', [qubits[0]])) == .8
assert pytest.approx(eng.backend.get_probability('01', qubits)) == .2
assert pytest.approx(eng.backend.get_probability('1', [qubits[1]])) == 1.
All(Measure) | qubits
def test_simulator_set_wavefunction_always_complex(sim):
""" Checks that wavefunction is always complex """
eng = MainEngine(sim)
qubit = eng.allocate_qubit()
eng.flush()
wf = [1., 0]
eng.backend.set_wavefunction(wf, qubit)
Y | qubit
eng.flush()
assert eng.backend.get_amplitude('1', qubit) == pytest.approx(1j)
def test_simulator_collapse_wavefunction(sim, mapper):
engine_list = [LocalOptimizer()]
if mapper is not None:
engine_list.append(mapper)
eng = MainEngine(sim, engine_list=engine_list)
qubits = eng.allocate_qureg(4)
# unknown qubits: raises
with pytest.raises(RuntimeError):
eng.backend.collapse_wavefunction(qubits, [0] * 4)
eng.flush()
eng.backend.collapse_wavefunction(qubits, [0] * 4)
assert pytest.approx(eng.backend.get_probability([0] * 4, qubits)) == 1.
All(H) | qubits[1:]
eng.flush()
assert pytest.approx(eng.backend.get_probability([0] * 4, qubits)) == .125
# impossible outcome: raises
with pytest.raises(RuntimeError):
eng.backend.collapse_wavefunction(qubits, [1] + [0] * 3)
eng.backend.collapse_wavefunction(qubits[:-1], [0, 1, 0])
probability = eng.backend.get_probability([0, 1, 0, 1], qubits)
assert probability == pytest.approx(.5)
eng.backend.set_wavefunction([1.] + [0.] * 15, qubits)
H | qubits[0]
CNOT | (qubits[0], qubits[1])
eng.flush()
eng.backend.collapse_wavefunction([qubits[0]], [1])
probability = eng.backend.get_probability([1, 1], qubits[0:2])
assert probability == pytest.approx(1.)
def test_simulator_no_uncompute_exception(sim):
eng = MainEngine(sim, [])
qubit = eng.allocate_qubit()
H | qubit
with pytest.raises(RuntimeError):
qubit[0].__del__()
# If you wanted to keep using the qubit, you shouldn't have deleted it.
assert qubit[0].id == -1
class MockSimulatorBackend(object):
def __init__(self):
self.run_cnt = 0
def run(self):
self.run_cnt += 1
def test_simulator_flush():
sim = Simulator()
sim._simulator = MockSimulatorBackend()
eng = MainEngine(sim)
eng.flush()
assert sim._simulator.run_cnt == 1
def test_simulator_send():
sim = Simulator()
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend, [sim])
qubit = eng.allocate_qubit()
H | qubit
Measure | qubit
del qubit
eng.flush()
assert len(backend.received_commands) == 5
def test_simulator_functional_entangle(sim):
eng = MainEngine(sim, [])
qubits = eng.allocate_qureg(5)
# entangle all qubits:
H | qubits[0]
for qb in qubits[1:]:
CNOT | (qubits[0], qb)
# check the state vector:
assert .5 == pytest.approx(abs(sim.cheat()[1][0])**2)
assert .5 == pytest.approx(abs(sim.cheat()[1][31])**2)
for i in range(1, 31):
assert 0. == pytest.approx(abs(sim.cheat()[1][i]))
# unentangle all except the first 2
for qb in qubits[2:]:
CNOT | (qubits[0], qb)
# entangle using Toffolis
for qb in qubits[2:]:
Toffoli | (qubits[0], qubits[1], qb)
# check the state vector:
assert .5 == pytest.approx(abs(sim.cheat()[1][0])**2)
assert .5 == pytest.approx(abs(sim.cheat()[1][31])**2)
for i in range(1, 31):
assert 0. == pytest.approx(abs(sim.cheat()[1][i]))
# uncompute using multi-controlled NOTs
with Control(eng, qubits[0:-1]):
X | qubits[-1]
with Control(eng, qubits[0:-2]):
X | qubits[-2]
with Control(eng, qubits[0:-3]):
X | qubits[-3]
CNOT | (qubits[0], qubits[1])
H | qubits[0]
# check the state vector:
assert 1. == pytest.approx(abs(sim.cheat()[1][0])**2)
for i in range(1, 32):
assert 0. == pytest.approx(abs(sim.cheat()[1][i]))
All(Measure) | qubits
def test_simulator_convert_logical_to_mapped_qubits(sim):
mapper = BasicMapperEngine()
def receive(command_list):
pass
mapper.receive = receive
eng = MainEngine(sim, [mapper])
qubit0 = eng.allocate_qubit()
qubit1 = eng.allocate_qubit()
mapper.current_mapping = {qubit0[0].id: qubit1[0].id,
qubit1[0].id: qubit0[0].id}
assert (sim._convert_logical_to_mapped_qureg(qubit0 + qubit1) ==
qubit1 + qubit0)