-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathtest_circuit.py
3394 lines (3036 loc) · 114 KB
/
test_circuit.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
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
from unittest.mock import Mock
import numpy as np
import pytest
import braket.ir.jaqcd as jaqcd
from braket.circuits import (
Circuit,
FreeParameter,
FreeParameterExpression,
Gate,
Instruction,
Moments,
Observable,
QubitSet,
ResultType,
UnicodeCircuitDiagram,
circuit,
compiler_directives,
gates,
noise,
observables,
)
from braket.circuits.gate_calibrations import GateCalibrations
from braket.circuits.parameterizable import Parameterizable
from braket.circuits.serialization import (
IRType,
OpenQASMSerializationProperties,
QubitReferenceType,
)
from braket.circuits.translations import braket_result_to_result_type
from braket.ir.openqasm import Program as OpenQasmProgram
from braket.pulse import DragGaussianWaveform, Frame, GaussianWaveform, Port, PulseSequence
@pytest.fixture
def cnot():
return Circuit().add_instruction(Instruction(Gate.CNot(), [0, 1]))
@pytest.fixture
def cnot_instr():
return Instruction(Gate.CNot(), [0, 1])
@pytest.fixture
def h():
return Circuit().add_instruction(Instruction(Gate.H(), 0))
@pytest.fixture
def h_instr():
return Instruction(Gate.H(), 0)
@pytest.fixture
def prob():
return ResultType.Probability([0, 1])
@pytest.fixture
def cnot_prob(cnot_instr, prob):
return Circuit().add_result_type(prob).add_instruction(cnot_instr)
@pytest.fixture
def bell_pair(prob):
return (
Circuit()
.add_instruction(Instruction(Gate.H(), 0))
.add_instruction(Instruction(Gate.CNot(), [0, 1]))
.add_result_type(prob)
)
@pytest.fixture
def port():
return Port(port_id="device_port_x0", dt=1e-9, properties={})
@pytest.fixture
def predefined_frame_1(port):
return Frame(
frame_id="predefined_frame_1", frequency=2e9, port=port, phase=0, is_predefined=True
)
@pytest.fixture
def user_defined_frame(port):
return Frame(
frame_id="user_defined_frame_0",
port=port,
frequency=1e7,
phase=3.14,
is_predefined=False,
properties={"associatedGate": "rz"},
)
@pytest.fixture
def pulse_sequence(predefined_frame_1):
return (
PulseSequence()
.set_frequency(
predefined_frame_1,
6e6,
)
.play(
predefined_frame_1,
DragGaussianWaveform(length=3e-3, sigma=0.4, beta=0.2, id="drag_gauss_wf"),
)
)
@pytest.fixture
def pulse_sequence_2(predefined_frame_1):
return (
PulseSequence()
.shift_phase(
predefined_frame_1,
FreeParameter("alpha"),
)
.set_phase(
predefined_frame_1,
FreeParameter("gamma"),
)
.shift_phase(
predefined_frame_1,
FreeParameter("beta"),
)
.play(
predefined_frame_1,
DragGaussianWaveform(length=3e-3, sigma=0.4, beta=0.2, id="drag_gauss_wf"),
)
)
@pytest.fixture
def gate_calibrations(pulse_sequence, pulse_sequence_2):
calibration_key = (Gate.Z(), QubitSet([0, 1]))
calibration_key_2 = (Gate.Rx(FreeParameter("theta")), QubitSet([0]))
calibration_key_3 = (
Gate.MS(FreeParameter("alpha"), FreeParameter("beta"), FreeParameter("gamma")),
QubitSet([0, 1]),
)
return GateCalibrations(
{
calibration_key: pulse_sequence,
calibration_key_2: pulse_sequence,
calibration_key_3: pulse_sequence_2,
}
)
def test_repr_instructions(h):
expected = f"Circuit('instructions': {h.instructions})"
assert repr(h) == expected
def test_repr_result_types(cnot_prob):
circuit = cnot_prob
expected = (
f"Circuit('instructions': {circuit.instructions}"
+ f", 'result_types': {circuit.result_types})"
)
assert repr(circuit) == expected
def test_str(h):
expected = UnicodeCircuitDiagram.build_diagram(h)
assert str(h) == expected
def test_equality():
circ_1 = Circuit().h(0).probability([0, 1])
circ_2 = Circuit().h(0).probability([0, 1])
other_circ = Circuit().h(1)
non_circ = "non circuit"
assert circ_1 == circ_2
assert circ_1 is not circ_2
assert circ_1 != other_circ
assert circ_1 != non_circ
def test_call():
alpha = FreeParameter("alpha")
theta = FreeParameter("theta")
circ = Circuit().h(0).rx(angle=theta, target=1).ry(angle=alpha, target=0)
new_circ = circ(theta=1, alpha=0)
expected = Circuit().h(0).rx(angle=1, target=1).ry(angle=0, target=0)
assert new_circ == expected and not new_circ.parameters
def test_call_with_result_type(prob):
alpha = FreeParameter("alpha")
theta = FreeParameter("theta")
circ = Circuit().h(0).rx(angle=theta, target=1).ry(angle=alpha, target=0).add_result_type(prob)
new_circ = circ(theta=1, alpha=0)
expected = Circuit().h(0).rx(angle=1, target=1).ry(angle=0, target=0).add_result_type(prob)
assert new_circ == expected and not new_circ.parameters
assert new_circ.observables_simultaneously_measurable
assert new_circ.result_types == [prob]
def test_call_one_param_not_bound():
alpha = FreeParameter("alpha")
theta = FreeParameter("theta")
circ = Circuit().h(0).rx(angle=theta, target=1).ry(angle=alpha, target=0)
new_circ = circ(theta=1)
expected_circ = Circuit().h(0).rx(angle=1, target=1).ry(angle=alpha, target=0)
expected_parameters = set()
expected_parameters.add(alpha)
assert new_circ == expected_circ and new_circ.parameters == expected_parameters
def test_call_with_default_parameter_val():
alpha = FreeParameter("alpha")
beta = FreeParameter("beta")
theta = FreeParameter("theta")
gamma = FreeParameter("gamma")
circ = (
Circuit()
.h(0)
.rx(angle=theta, target=1)
.ry(angle=alpha, target=0)
.ry(angle=beta, target=2)
.rx(angle=gamma, target=1)
)
new_circ = circ(np.pi, theta=1, alpha=0)
expected = (
Circuit()
.h(0)
.rx(angle=1, target=1)
.ry(angle=0, target=0)
.ry(angle=np.pi, target=2)
.rx(angle=np.pi, target=1)
)
assert new_circ == expected and not new_circ.parameters
def test_add_result_type_default(prob):
circ = Circuit().add_result_type(prob)
assert circ.observables_simultaneously_measurable
assert circ.result_types == [prob]
def test_add_result_type_with_mapping(prob):
expected = [ResultType.Probability([10, 11])]
circ = Circuit().add_result_type(prob, target_mapping={0: 10, 1: 11})
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_with_target(prob):
expected = [ResultType.Probability([10, 11])]
circ = Circuit().add_result_type(prob, target=[10, 11])
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_already_exists():
expected = [ResultType.StateVector()]
circ = Circuit(expected).add_result_type(expected[0])
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_observable_conflict_target():
circ = Circuit().add_result_type(ResultType.Probability([0, 1]))
circ.add_result_type(ResultType.Expectation(observable=Observable.Y(), target=0))
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_observable_conflict_all():
circ = Circuit().add_result_type(ResultType.Probability())
circ.add_result_type(ResultType.Expectation(observable=Observable.Y()))
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_observable_conflict_all_target_then_selected_target():
circ = Circuit().add_result_type(ResultType.Probability())
circ.add_result_type(ResultType.Expectation(observable=Observable.Y(), target=[0]))
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_observable_conflict_different_selected_targets_then_all_target():
circ = Circuit().add_result_type(ResultType.Expectation(observable=Observable.Z(), target=[0]))
circ.add_result_type(ResultType.Expectation(observable=Observable.Y(), target=[1]))
circ.add_result_type(ResultType.Expectation(observable=Observable.Y()))
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_observable_conflict_selected_target_then_all_target():
circ = Circuit().add_result_type(ResultType.Expectation(observable=Observable.Y(), target=[1]))
circ.add_result_type(ResultType.Probability())
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_observable_no_conflict_all_target():
expected = [
ResultType.Probability(),
ResultType.Expectation(observable=Observable.Z(), target=[0]),
]
circ = Circuit(expected)
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_observable_no_conflict_target_all():
expected = [
ResultType.Expectation(observable=Observable.Z(), target=[0]),
ResultType.Probability(),
]
circ = Circuit(expected)
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_observable_no_conflict_all():
expected = [
ResultType.Variance(observable=Observable.Y()),
ResultType.Expectation(observable=Observable.Y()),
]
circ = Circuit(expected)
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_observable_no_conflict_all_identity():
expected = [
ResultType.Variance(observable=Observable.Y()),
ResultType.Expectation(observable=Observable.I()),
ResultType.Expectation(observable=Observable.Y()),
]
circ = Circuit(expected)
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_observable_no_conflict_state_vector_obs_return_value():
expected = [
ResultType.StateVector(),
ResultType.Expectation(observable=Observable.Y()),
]
circ = Circuit(expected)
assert circ.observables_simultaneously_measurable
assert circ.result_types == expected
def test_add_result_type_same_observable_wrong_target_order_tensor_product():
circ = (
Circuit()
.add_result_type(
ResultType.Expectation(observable=Observable.Y() @ Observable.X(), target=[0, 1])
)
.add_result_type(
ResultType.Variance(observable=Observable.Y() @ Observable.X(), target=[1, 0])
)
)
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_same_observable_wrong_target_order_hermitian():
array = np.eye(4)
circ = (
Circuit()
.add_result_type(
ResultType.Expectation(observable=Observable.Hermitian(matrix=array), target=[0, 1])
)
.add_result_type(
ResultType.Variance(observable=Observable.Hermitian(matrix=array), target=[1, 0])
)
)
assert not circ.observables_simultaneously_measurable
assert not circ.basis_rotation_instructions
def test_add_result_type_with_target_and_mapping(prob):
with pytest.raises(TypeError):
Circuit().add_result_type(prob, target=[10], target_mapping={0: 10})
def test_add_instruction_default(cnot_instr):
circ = Circuit().add_instruction(cnot_instr)
assert circ.instructions == [cnot_instr]
def test_add_instruction_with_mapping(cnot_instr):
expected = [Instruction(Gate.CNot(), [10, 11])]
circ = Circuit().add_instruction(cnot_instr, target_mapping={0: 10, 1: 11})
assert circ.instructions == expected
def test_add_instruction_with_target(cnot_instr):
expected = [Instruction(Gate.CNot(), [10, 11])]
circ = Circuit().add_instruction(cnot_instr, target=[10, 11])
assert circ.instructions == expected
def test_add_multiple_single_qubit_instruction(h_instr):
circ = Circuit().add_instruction(h_instr, target=[0, 1, 2, 3])
expected = Circuit().h(0).h(1).h(2).h(3)
assert circ == expected
def test_add_instruction_with_target_and_mapping(h):
with pytest.raises(TypeError):
Circuit().add_instruction(h, target=[10], target_mapping={0: 10})
def test_add_circuit_default(bell_pair):
circ = Circuit().add_circuit(bell_pair)
assert circ == bell_pair
def test_add_circuit_with_mapping(bell_pair):
circ = Circuit().add_circuit(bell_pair, target_mapping={0: 10, 1: 11})
expected = (
Circuit()
.add_instruction(Instruction(Gate.H(), 10))
.add_instruction(Instruction(Gate.CNot(), [10, 11]))
.add_result_type(ResultType.Probability([10, 11]))
)
assert circ == expected
def test_add_circuit_with_target(bell_pair):
circ = Circuit().add_circuit(bell_pair, target=[10, 11])
expected = (
Circuit()
.add_instruction(Instruction(Gate.H(), 10))
.add_instruction(Instruction(Gate.CNot(), [10, 11]))
.add_result_type(ResultType.Probability([10, 11]))
)
assert circ == expected
def test_add_circuit_with_target_and_non_continuous_qubits():
widget = Circuit().h(5).h(50).h(100)
circ = Circuit().add_circuit(widget, target=[1, 3, 5])
expected = (
Circuit()
.add_instruction(Instruction(Gate.H(), 1))
.add_instruction(Instruction(Gate.H(), 3))
.add_instruction(Instruction(Gate.H(), 5))
)
assert circ == expected
def test_add_circuit_with_target_and_mapping(h):
with pytest.raises(TypeError):
Circuit().add_circuit(h, target=[10], target_mapping={0: 10})
def test_add_verbatim_box():
circ = Circuit().h(0).add_verbatim_box(Circuit().cnot(0, 1))
expected = (
Circuit()
.add_instruction(Instruction(Gate.H(), 0))
.add_instruction(Instruction(compiler_directives.StartVerbatimBox()))
.add_instruction(Instruction(Gate.CNot(), [0, 1]))
.add_instruction(Instruction(compiler_directives.EndVerbatimBox()))
)
assert circ == expected
def test_add_verbatim_box_different_qubits():
circ = Circuit().h(1).add_verbatim_box(Circuit().h(0)).cnot(3, 4)
expected = (
Circuit()
.add_instruction(Instruction(Gate.H(), 1))
.add_instruction(Instruction(compiler_directives.StartVerbatimBox()))
.add_instruction(Instruction(Gate.H(), 0))
.add_instruction(Instruction(compiler_directives.EndVerbatimBox()))
.add_instruction(Instruction(Gate.CNot(), [3, 4]))
)
assert circ == expected
def test_add_verbatim_box_no_preceding():
circ = Circuit().add_verbatim_box(Circuit().h(0)).cnot(2, 3)
expected = (
Circuit()
.add_instruction(Instruction(compiler_directives.StartVerbatimBox()))
.add_instruction(Instruction(Gate.H(), 0))
.add_instruction(Instruction(compiler_directives.EndVerbatimBox()))
.add_instruction(Instruction(Gate.CNot(), [2, 3]))
)
assert circ == expected
def test_add_verbatim_box_empty():
circuit = Circuit().add_verbatim_box(Circuit())
assert circuit == Circuit()
assert not circuit.qubits_frozen
def test_add_verbatim_box_with_mapping(cnot):
circ = Circuit().add_verbatim_box(cnot, target_mapping={0: 10, 1: 11})
expected = (
Circuit()
.add_instruction(Instruction(compiler_directives.StartVerbatimBox()))
.add_instruction(Instruction(Gate.CNot(), [10, 11]))
.add_instruction(Instruction(compiler_directives.EndVerbatimBox()))
)
assert circ == expected
def test_add_verbatim_box_with_target(cnot):
circ = Circuit().add_verbatim_box(cnot, target=[10, 11])
expected = (
Circuit()
.add_instruction(Instruction(compiler_directives.StartVerbatimBox()))
.add_instruction(Instruction(Gate.CNot(), [10, 11]))
.add_instruction(Instruction(compiler_directives.EndVerbatimBox()))
)
assert circ == expected
def test_add_verbatim_box_with_target_and_mapping(h):
with pytest.raises(TypeError):
Circuit().add_verbatim_box(h, target=[10], target_mapping={0: 10})
def test_add_verbatim_box_result_types():
with pytest.raises(ValueError):
Circuit().h(0).add_verbatim_box(
Circuit().cnot(0, 1).expectation(observable=Observable.X(), target=0)
)
def test_add_with_instruction_with_default(cnot_instr):
circ = Circuit().add(cnot_instr)
assert circ == Circuit().add_instruction(cnot_instr)
def test_add_with_instruction_with_mapping(cnot_instr):
target_mapping = {0: 10, 1: 11}
circ = Circuit().add(cnot_instr, target_mapping=target_mapping)
expected = Circuit().add_instruction(cnot_instr, target_mapping=target_mapping)
assert circ == expected
def test_add_with_instruction_with_target(cnot_instr):
target = [10, 11]
circ = Circuit().add(cnot_instr, target=target)
expected = Circuit().add_instruction(cnot_instr, target=target)
assert circ == expected
def test_add_with_circuit_with_default(bell_pair):
circ = Circuit().add(bell_pair)
assert circ == Circuit().add_circuit(bell_pair)
def test_add_with_circuit_with_mapping(bell_pair):
target_mapping = {0: 10, 1: 11}
circ = Circuit().add(bell_pair, target_mapping=target_mapping)
expected = Circuit().add_circuit(bell_pair, target_mapping=target_mapping)
assert circ == expected
def test_add_with_circuit_with_target(bell_pair):
target = [10, 11]
circ = Circuit().add(bell_pair, target=target)
expected = Circuit().add_circuit(bell_pair, target=target)
assert circ == expected
def test_adjoint():
circ = Circuit().s(0).add_verbatim_box(Circuit().rz(0, 0.123)).expectation(Observable.X(), 0)
expected = Circuit()
expected.add_verbatim_box(Circuit().rz(0, -0.123))
expected.si(0)
expected.expectation(Observable.X(), 0)
actual = circ.adjoint()
assert actual == expected
assert circ == expected.adjoint()
assert circ == actual.adjoint()
def test_adjoint_subcircuit_free_parameter():
circ = Circuit().h(0).add_circuit(Circuit().s(0).rz(0, FreeParameter("theta")).adjoint()).x(0)
expected = Circuit().h(0).rz(0, -FreeParameter("theta")).si(0).x(0)
assert circ == expected
def test_circuit_copy(h, bell_pair, cnot_instr):
original = Circuit().add(h).add(bell_pair).add(cnot_instr)
copy = original.copy()
assert copy is not original
assert copy == original
def test_circuit_copy_with_modification(h, bell_pair, cnot_instr):
original = Circuit().add(h).add(bell_pair)
copy = original.copy().add(cnot_instr)
assert copy != original
def test_iadd_operator(cnot_instr, h):
circ = Circuit()
circ += h
circ += cnot_instr
circ += [h, cnot_instr]
assert circ == Circuit().add(h).add(cnot_instr).add(h).add(cnot_instr)
def test_add_operator(h, bell_pair):
addition = h + bell_pair + h + h
expected = Circuit().add(h).add(bell_pair).add(h).add(h)
assert addition == expected
assert addition != (h + h + bell_pair + h)
def test_iadd_with_unknown_type(h):
with pytest.raises(TypeError):
h += 100
def test_subroutine_register():
# register a private method to avoid Sphinx docs picking this up
@circuit.subroutine(register=True)
def _foo(target):
"""this docstring will be added to the registered attribute"""
return Instruction(Gate.H(), target)
circ = Circuit()._foo(0)
assert circ == Circuit(Instruction(Gate.H(), 0))
assert Circuit._foo.__doc__ == _foo.__doc__
def test_subroutine_returns_circuit():
@circuit.subroutine()
def foo(target):
return Circuit().add(Instruction(Gate.H(), 0))
circ = Circuit().add(foo, 0)
assert circ == Circuit(Instruction(Gate.H(), 0))
def test_subroutine_returns_instruction():
@circuit.subroutine()
def foo(target):
return Instruction(Gate.H(), 0)
circ = Circuit().add(foo, 0)
assert circ == Circuit(Instruction(Gate.H(), 0))
def test_subroutine_returns_iterable():
@circuit.subroutine()
def foo(target):
for qubit in range(1):
yield Instruction(Gate.H(), qubit)
circ = Circuit().add(foo, 0)
assert circ == Circuit(Instruction(Gate.H(), 0))
def test_subroutine_nested():
@circuit.subroutine()
def h(target):
for qubit in target:
yield Instruction(Gate.H(), qubit)
@circuit.subroutine()
def h_nested(target):
for qubit in target:
yield h(target)
circ = Circuit().add(h_nested, [0, 1])
expected = Circuit([Instruction(Gate.H(), j) for i in range(2) for j in range(2)])
assert circ == expected
def test_ir_empty_instructions_result_types():
circ = Circuit()
assert circ.to_ir() == jaqcd.Program(
instructions=[], results=[], basis_rotation_instructions=[]
)
def test_ir_non_empty_instructions_result_types():
circ = Circuit().h(0).cnot(0, 1).probability([0, 1])
expected = jaqcd.Program(
instructions=[jaqcd.H(target=0), jaqcd.CNot(control=0, target=1)],
results=[jaqcd.Probability(targets=[0, 1])],
basis_rotation_instructions=[],
)
assert circ.to_ir() == expected
def test_ir_non_empty_instructions_result_types_basis_rotation_instructions():
circ = Circuit().h(0).cnot(0, 1).sample(observable=Observable.X(), target=[0])
expected = jaqcd.Program(
instructions=[jaqcd.H(target=0), jaqcd.CNot(control=0, target=1)],
results=[jaqcd.Sample(observable=["x"], targets=[0])],
basis_rotation_instructions=[jaqcd.H(target=0)],
)
assert circ.to_ir() == expected
@pytest.mark.parametrize(
"circuit, serialization_properties, expected_ir",
[
(
Circuit()
.rx(0, 0.15)
.ry(1, FreeParameterExpression("0.3"))
.rx(2, 3 * FreeParameterExpression(1)),
OpenQASMSerializationProperties(QubitReferenceType.VIRTUAL),
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[3] b;",
"qubit[3] q;",
"rx(0.15) q[0];",
"ry(0.3) q[1];",
"rx(3) q[2];",
"b[0] = measure q[0];",
"b[1] = measure q[1];",
"b[2] = measure q[2];",
]
),
inputs={},
),
),
],
)
def test_circuit_to_ir_openqasm(circuit, serialization_properties, expected_ir):
assert (
circuit.to_ir(
ir_type=IRType.OPENQASM,
serialization_properties=serialization_properties,
)
== expected_ir
)
@pytest.mark.parametrize(
"circuit, serialization_properties, expected_ir",
[
(
Circuit().rx(0, 0.15).rx(1, 0.3),
OpenQASMSerializationProperties(QubitReferenceType.VIRTUAL),
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[2] b;",
"qubit[2] q;",
"cal {",
" waveform drag_gauss_wf = drag_gaussian"
+ "(3.0ms, 400.0ms, 0.2, 1, false);",
"}",
"defcal z $0, $1 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal rx(float theta) $0 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal ms(float alpha, float beta, float gamma) $0, $1 {",
" shift_phase(predefined_frame_1, alpha);",
" set_phase(predefined_frame_1, gamma);",
" shift_phase(predefined_frame_1, beta);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"rx(0.15) q[0];",
"rx(0.3) q[1];",
"b[0] = measure q[0];",
"b[1] = measure q[1];",
]
),
inputs={},
),
),
(
Circuit().rx(0, 0.15).rx(4, 0.3),
OpenQASMSerializationProperties(QubitReferenceType.PHYSICAL),
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[2] b;",
"cal {",
" waveform drag_gauss_wf = drag_gaussian"
+ "(3.0ms, 400.0ms, 0.2, 1, false);",
"}",
"defcal z $0, $1 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal rx(float theta) $0 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal ms(float alpha, float beta, float gamma) $0, $1 {",
" shift_phase(predefined_frame_1, alpha);",
" set_phase(predefined_frame_1, gamma);",
" shift_phase(predefined_frame_1, beta);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"rx(0.15) $0;",
"rx(0.3) $4;",
"b[0] = measure $0;",
"b[1] = measure $4;",
]
),
inputs={},
),
),
(
Circuit()
.rx(0, 0.15)
.add_verbatim_box(Circuit().rx(4, 0.3))
.expectation(observable=Observable.I()),
OpenQASMSerializationProperties(QubitReferenceType.PHYSICAL),
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"cal {",
" waveform drag_gauss_wf = drag_gaussian"
+ "(3.0ms, 400.0ms, 0.2, 1, false);",
"}",
"defcal z $0, $1 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal rx(float theta) $0 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal ms(float alpha, float beta, float gamma) $0, $1 {",
" shift_phase(predefined_frame_1, alpha);",
" set_phase(predefined_frame_1, gamma);",
" shift_phase(predefined_frame_1, beta);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"rx(0.15) $0;",
"#pragma braket verbatim",
"box{",
"rx(0.3) $4;",
"}",
"#pragma braket result expectation i all",
]
),
inputs={},
),
),
(
Circuit()
.rx(0, 0.15)
.rx(4, 0.3)
.bit_flip(3, probability=0.2)
.expectation(observable=Observable.I(), target=0),
None,
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"qubit[5] q;",
"cal {",
" waveform drag_gauss_wf = drag_gaussian"
+ "(3.0ms, 400.0ms, 0.2, 1, false);",
"}",
"defcal z $0, $1 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal rx(float theta) $0 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal ms(float alpha, float beta, float gamma) $0, $1 {",
" shift_phase(predefined_frame_1, alpha);",
" set_phase(predefined_frame_1, gamma);",
" shift_phase(predefined_frame_1, beta);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"rx(0.15) q[0];",
"rx(0.3) q[4];",
"#pragma braket noise bit_flip(0.2) q[3]",
"#pragma braket result expectation i(q[0])",
]
),
inputs={},
),
),
(
Circuit().rx(0, 0.15).rx(1, FreeParameter("theta")),
OpenQASMSerializationProperties(QubitReferenceType.VIRTUAL),
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"input float theta;",
"bit[2] b;",
"qubit[2] q;",
"cal {",
" waveform drag_gauss_wf = drag_gaussian"
+ "(3.0ms, 400.0ms, 0.2, 1, false);",
"}",
"defcal z $0, $1 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal rx(float theta) $0 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal ms(float alpha, float beta, float gamma) $0, $1 {",
" shift_phase(predefined_frame_1, alpha);",
" set_phase(predefined_frame_1, gamma);",
" shift_phase(predefined_frame_1, beta);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"rx(0.15) q[0];",
"rx(theta) q[1];",
"b[0] = measure q[0];",
"b[1] = measure q[1];",
]
),
inputs={},
),
),
(
Circuit()
.rx(0, 0.15, control=2, control_state=0)
.rx(1, 0.3, control=[2, 3])
.cnot(target=0, control=[2, 3, 4]),
OpenQASMSerializationProperties(QubitReferenceType.VIRTUAL),
OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[5] b;",
"qubit[5] q;",
"cal {",
" waveform drag_gauss_wf = drag_gaussian"
+ "(3.0ms, 400.0ms, 0.2, 1, false);",
"}",
"defcal z $0, $1 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal rx(float theta) $0 {",
" set_frequency(predefined_frame_1, 6000000.0);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"defcal ms(float alpha, float beta, float gamma) $0, $1 {",
" shift_phase(predefined_frame_1, alpha);",
" set_phase(predefined_frame_1, gamma);",
" shift_phase(predefined_frame_1, beta);",
" play(predefined_frame_1, drag_gauss_wf);",
"}",
"negctrl @ rx(0.15) q[2], q[0];",
"ctrl(2) @ rx(0.3) q[2], q[3], q[1];",
"ctrl(2) @ cnot q[2], q[3], q[4], q[0];",
"b[0] = measure q[0];",
"b[1] = measure q[1];",
"b[2] = measure q[2];",
"b[3] = measure q[3];",
"b[4] = measure q[4];",
]
),
inputs={},
),
),
(