-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbackend_test.py
1591 lines (1396 loc) · 49.7 KB
/
backend_test.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 Quantinuum
#
# 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.
import datetime
import gc
import json
import os
import time
from base64 import b64encode
from collections import Counter
from pathlib import Path
from typing import Any, Callable, cast # pylint: disable=unused-import
import hypothesis.strategies as st
import numpy as np
import pytest
from hypothesis import HealthCheck, given, settings
from hypothesis.strategies._internal import SearchStrategy
from llvmlite.binding import create_context, parse_assembly # type: ignore
from pytket.backends import CircuitNotValidError
from pytket.backends.status import StatusEnum
from pytket.circuit import (
Bit,
Circuit,
Conditional,
Node,
OpType,
Qubit,
if_not_bit,
reg_eq,
reg_geq,
reg_gt,
reg_leq,
reg_lt,
reg_neq,
)
from pytket.extensions.quantinuum import (
Language,
QuantinuumBackend,
QuantinuumBackendCompilationConfig,
prune_shots_detected_as_leaky,
)
from pytket.extensions.quantinuum.backends.api_wrappers import (
QuantinuumAPI,
QuantinuumAPIError,
)
from pytket.extensions.quantinuum.backends.quantinuum import _ALL_GATES, GetResultFailed
from pytket.predicates import CompilationUnit
from pytket.wasm import WasmFileHandler
skip_remote_tests: bool = os.getenv("PYTKET_RUN_REMOTE_TESTS") is None
skip_remote_tests_prod: bool = os.getenv("PYTKET_RUN_REMOTE_TESTS_PROD") is None
skip_mpl_tests: bool = os.getenv("PYTKET_RUN_MPL_TESTS") is None
REASON = (
"PYTKET_RUN_REMOTE_TESTS not set (requires configuration of Quantinuum username)"
)
REASON_PROD = "PYTKET_RUN_REMOTE_TESTS_PROD not set \
(requires configuration of Quantinuum username)"
REASON_MPL = "PYTKET_RUN_MPL_TESTS not set \
(requires configuration of Quantinuum username)"
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize("authenticated_quum_backend_qa", [None], indirect=True)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_quantinuum(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
backend = authenticated_quum_backend_qa
c = Circuit(4, 4, "test 1")
c.H(0)
c.CX(0, 1)
c.Rz(0.3, 2)
c.CSWAP(0, 1, 2)
c.CRz(0.4, 2, 3)
c.CY(1, 3)
c.ZZPhase(0.1, 2, 0)
c.Tdg(3)
c.measure_all()
c = backend.get_compiled_circuit(c)
n_shots = 4
handle = backend.process_circuits([c], n_shots)[0]
correct_shots = np.zeros((4, 4))
correct_counts = {(0, 0, 0, 0): 4}
res = backend.get_result(handle, timeout=49)
shots = res.get_shots()
counts = res.get_counts()
assert backend.circuit_status(handle).status is StatusEnum.COMPLETED
assert np.all(shots == correct_shots)
assert counts == correct_counts
res = backend.run_circuit(c, n_shots=4, timeout=49, language=language)
newshots = res.get_shots()
assert np.all(newshots == correct_shots)
newcounts = res.get_counts()
assert newcounts == correct_counts
if skip_remote_tests:
assert backend.backend_info is None
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.timeout(120)
def test_max_classical_register(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
backend = authenticated_quum_backend_qa
info = backend.backend_info
assert info is not None
n_cl_reg = info.n_cl_reg
assert isinstance(n_cl_reg, int)
c = Circuit(4, 4, "test 1")
c.H(0)
c.CX(0, 1)
c.measure_all()
c = backend.get_compiled_circuit(c)
assert backend._check_all_circuits([c])
for i in range(n_cl_reg - 1):
c.add_c_register(f"creg-{i}", 32)
assert backend._check_all_circuits([c])
c.add_c_register("creg-extra", 32)
with pytest.raises(CircuitNotValidError):
backend._check_all_circuits([c])
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_bell(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
b = authenticated_quum_backend_qa
c = Circuit(2, 2, "test 2")
c.H(0)
c.CX(0, 1)
c.measure_all()
c = b.get_compiled_circuit(c)
n_shots = 10
shots = b.run_circuit(c, n_shots=n_shots, language=language).get_shots()
assert all(q[0] == q[1] for q in shots)
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": "H1-1SC", "label": "test 3"}],
indirect=True,
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_multireg(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
gc.disable()
b = authenticated_quum_backend_qa
c = Circuit()
q1 = Qubit("q1", 0)
q2 = Qubit("q2", 0)
c1 = Bit("c1", 0)
c2 = Bit("c2", 0)
for q in (q1, q2):
c.add_qubit(q)
for cb in (c1, c2):
c.add_bit(cb)
c.H(q1)
c.CX(q1, q2)
c.Measure(q1, c1)
c.Measure(q2, c2)
c = b.get_compiled_circuit(c)
n_shots = 10
shots = b.run_circuit(c, n_shots=n_shots, language=language).get_shots()
assert np.array_equal(shots, np.zeros((10, 2)))
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.timeout(120)
def test_default_pass(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
b = authenticated_quum_backend_qa
for ol in range(3):
comp_pass = b.default_compilation_pass(ol)
c = Circuit(3, 3)
q0 = Qubit("test0", 5)
q1 = Qubit("test1", 6)
c.add_qubit(q0)
c.H(q0)
c.H(0)
c.CX(0, 1)
c.CSWAP(1, 0, 2)
c.ZZPhase(0.84, 2, 0)
c.measure_all()
c.add_qubit(q1)
cu = CompilationUnit(c)
comp_pass.apply(cu)
# 5 qubits added to Circuit, one is removed when flattening registers
assert cu.circuit.qubits == [
Node("q", 0),
Node("q", 1),
Node("q", 2),
Node("q", 3),
]
assert cu.initial_map[Qubit(0)] == Node("q", 0)
assert cu.initial_map[Qubit(1)] == Node("q", 1)
assert cu.initial_map[Qubit(2)] == Node("q", 2)
assert cu.initial_map[q0] == Node("q", 3)
assert cu.initial_map[q1] == q1
for pred in b.required_predicates:
assert pred.verify(cu.circuit)
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[
{"device_name": name, "label": "test cancel"}
for name in pytest.ALL_SIMULATOR_NAMES # type: ignore
],
indirect=True,
)
@pytest.mark.timeout(120)
def test_cancel(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
b = authenticated_quum_backend_qa
c = Circuit(2, 2).H(0).CX(0, 1).measure_all()
c = b.get_compiled_circuit(c)
handle = b.process_circuit(c, 10)
try:
# will raise HTTP error if job is already completed
b.cancel(handle)
time.sleep(1.0)
assert b.circuit_status(handle).status in [StatusEnum.CANCELLED]
except QuantinuumAPIError as err:
check_completed = "job has completed already" in str(err)
assert check_completed
if not check_completed:
raise err
@st.composite
def circuits(
draw: Callable[[SearchStrategy[Any]], Any],
n_qubits: SearchStrategy[int] = st.integers(min_value=2, max_value=6),
depth: SearchStrategy[int] = st.integers(min_value=1, max_value=100),
) -> Circuit:
total_qubits = draw(n_qubits)
circuit = Circuit(total_qubits, total_qubits)
for _ in range(draw(depth)):
gate = draw(st.sampled_from(list(_ALL_GATES)))
control = draw(st.integers(min_value=0, max_value=total_qubits - 1))
if gate == OpType.ZZMax:
target = draw(
st.integers(min_value=0, max_value=total_qubits - 1).filter(
lambda x: x != control
)
)
circuit.add_gate(gate, [control, target])
elif gate == OpType.Measure:
circuit.add_gate(gate, [control, control])
circuit.add_gate(OpType.Reset, [control])
elif gate == OpType.Rz:
param = draw(st.floats(min_value=0, max_value=2))
circuit.add_gate(gate, [param], [control])
elif gate == OpType.PhasedX:
param1 = draw(st.floats(min_value=0, max_value=2))
param2 = draw(st.floats(min_value=0, max_value=2))
circuit.add_gate(gate, [param1, param2], [control])
circuit.measure_all()
return circuit
@pytest.mark.skipif(skip_remote_tests_prod, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_prod",
[
{"device_name": name}
for name in [
*pytest.ALL_QUANTUM_HARDWARE_NAMES, # type: ignore
*pytest.ALL_SYNTAX_CHECKER_NAMES, # type: ignore
]
],
indirect=True,
)
@given(
c=circuits(), # pylint: disable=no-value-for-parameter
n_shots=st.integers(min_value=1, max_value=10000),
)
@settings(
max_examples=5,
deadline=None,
suppress_health_check=[HealthCheck.function_scoped_fixture],
)
@pytest.mark.timeout(120)
def test_cost_estimate(
authenticated_quum_backend_prod: QuantinuumBackend,
c: Circuit,
n_shots: int,
) -> None:
b = authenticated_quum_backend_prod
c = b.get_compiled_circuit(c)
estimate = None
if b._device_name.endswith("SC"):
estimate = b.cost(c, n_shots)
assert estimate == 0.0
else:
# All other real hardware backends should have the
# "syntax_checker" misc property set, so there should be no
# need of providing it explicitly.
estimate = b.cost(c, n_shots)
if estimate is None:
pytest.skip("API is flaky, sometimes returns None unexpectedly.")
assert isinstance(estimate, float)
assert estimate > 0.0
@pytest.mark.skipif(skip_remote_tests_prod, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_prod",
[
{"device_name": name}
for name in [
*pytest.ALL_QUANTUM_HARDWARE_NAMES, # type: ignore
]
],
indirect=True,
)
@pytest.mark.timeout(120)
def test_cost_estimate_wrong_syntax_checker(
authenticated_quum_backend_prod: QuantinuumBackend,
) -> None:
b = authenticated_quum_backend_prod
c = Circuit(1).PhasedX(0.5, 0.5, 0).measure_all()
with pytest.raises(ValueError):
_ = b.cost(c, 10, syntax_checker="H6-2SC")
@pytest.mark.skipif(skip_remote_tests_prod, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_prod", [{"device_name": "H1-1E"}], indirect=True
)
@pytest.mark.timeout(120)
def test_cost_estimate_bad_syntax_checker(
authenticated_quum_backend_prod: QuantinuumBackend,
) -> None:
b = authenticated_quum_backend_prod
c = Circuit(1).PhasedX(0.5, 0.5, 0).measure_all()
with pytest.raises(ValueError):
_ = b.cost(c, 10, syntax_checker="H2-1E")
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.parametrize(
"language",
[
Language.QASM,
Language.QIR,
Language.PQIR,
],
)
@pytest.mark.timeout(120)
def test_classical(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
# circuit to cover capabilities covered in example notebook
c = Circuit(1, name="test_classical")
a = c.add_c_register("a", 8)
b = c.add_c_register("b", 10)
d = c.add_c_register("d", 10)
c.add_c_setbits([True], [a[0]])
c.add_c_setbits([False, True] + [False] * 6, a) # type: ignore
c.add_c_setbits([True, True] + [False] * 8, b) # type: ignore
c.add_c_setreg(23, a)
c.add_c_copyreg(a, b)
c.add_classicalexpbox_register(a + b, d.to_list())
c.add_classicalexpbox_register(a - b, d.to_list())
c.add_classicalexpbox_register(a << 1, a.to_list())
c.add_classicalexpbox_register(a >> 1, b.to_list())
c.X(0, condition=reg_eq(a ^ b, 1))
c.X(0, condition=(a[0] ^ b[0]))
c.X(0, condition=reg_eq(a & b, 1))
c.X(0, condition=reg_eq(a | b, 1))
c.X(0, condition=a[0])
c.X(0, condition=reg_neq(a, 1))
c.X(0, condition=if_not_bit(a[0]))
c.X(0, condition=reg_gt(a, 1))
c.X(0, condition=reg_lt(a, 1))
c.X(0, condition=reg_geq(a, 1))
c.X(0, condition=reg_leq(a, 1))
c.Phase(0, condition=a[0])
backend = authenticated_quum_backend_qa
c = backend.get_compiled_circuit(c)
assert backend.run_circuit(c, n_shots=10, language=language).get_counts()
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.parametrize(
"language",
[
Language.QIR,
Language.PQIR,
pytest.param(
Language.QASM,
marks=pytest.mark.xfail(reason="https://github.com/CQCL/tket/issues/1173"),
),
],
)
@pytest.mark.timeout(120)
def test_division(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
c = Circuit()
a = c.add_c_register("a", 8)
b = c.add_c_register("b", 10)
d = c.add_c_register("d", 10)
c.add_c_setbits([False, True] + [False] * 6, a) # type: ignore
c.add_c_setbits([True, True] + [False] * 8, b) # type: ignore
c.add_classicalexpbox_register(a * b // d, d.to_list())
backend = authenticated_quum_backend_qa
c = backend.get_compiled_circuit(c)
with pytest.raises(ValueError):
backend.run_circuit(c, n_shots=10, language=language).get_counts()
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_postprocess(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
b = authenticated_quum_backend_qa
assert b.supports_contextual_optimisation
c = Circuit(2, 2)
c.add_gate(OpType.PhasedX, [1, 1], [0])
c.add_gate(OpType.PhasedX, [1, 1], [1])
c.add_gate(OpType.ZZMax, [0, 1])
c.measure_all()
c = b.get_compiled_circuit(c)
h = b.process_circuit(c, n_shots=10, postprocess=True, language=language)
ppcirc = Circuit.from_dict(json.loads(cast(str, h[1])))
ppcmds = ppcirc.get_commands()
assert len(ppcmds) > 0
assert all(ppcmd.op.type == OpType.ClassicalTransform for ppcmd in ppcmds)
r = b.get_result(h)
shots = r.get_shots()
assert len(shots) == 10
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.timeout(120)
def test_leakage_detection(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
b = authenticated_quum_backend_qa
c = Circuit(2, 2).H(0).CZ(0, 1).Measure(0, 0).Measure(1, 1)
with pytest.raises(ValueError):
b.process_circuit(
c, n_shots=10, leakage_detection=True, n_leakage_detection_qubits=1000
)
h = b.process_circuit(c, n_shots=10, leakage_detection=True)
r = b.get_result(h)
assert len(r.c_bits) == 4
assert sum(r.get_counts().values()) == 10
r_discarded = prune_shots_detected_as_leaky(r)
assert len(r_discarded.c_bits) == 2
assert sum(r_discarded.get_counts().values()) == 10
@given(
n_shots=st.integers(min_value=1, max_value=10), # type: ignore
n_bits=st.integers(min_value=0, max_value=10),
)
@pytest.mark.timeout(120)
def test_shots_bits_edgecases(n_shots, n_bits) -> None:
quantinuum_backend = QuantinuumBackend("H1-1SC", machine_debug=True)
c = Circuit(n_bits, n_bits)
# TODO TKET-813 add more shot based backends and move to integration tests
h = quantinuum_backend.process_circuit(c, n_shots)
res = quantinuum_backend.get_result(h)
correct_shots = np.zeros((n_shots, n_bits), dtype=int)
correct_shape = (n_shots, n_bits)
correct_counts = Counter({(0,) * n_bits: n_shots})
# BackendResult
assert np.array_equal(res.get_shots(), correct_shots)
assert res.get_shots().shape == correct_shape
assert res.get_counts() == correct_counts
# Direct
res = quantinuum_backend.run_circuit(c, n_shots=n_shots)
assert np.array_equal(res.get_shots(), correct_shots)
assert res.get_shots().shape == correct_shape
assert res.get_counts() == correct_counts
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1E"}], indirect=True
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(200)
def test_simulator(
authenticated_quum_handler: QuantinuumAPI,
authenticated_quum_backend_qa: QuantinuumBackend,
language: Language,
) -> None:
circ = Circuit(2, name="sim_test").H(0).CX(0, 1).measure_all()
n_shots = 1000
state_backend = authenticated_quum_backend_qa
stabilizer_backend = QuantinuumBackend(
"H1-1E", simulator="stabilizer", api_handler=authenticated_quum_handler
)
circ = state_backend.get_compiled_circuit(circ)
noisy_handle = state_backend.process_circuit(circ, n_shots, language=language)
pure_handle = state_backend.process_circuit(
circ, n_shots, noisy_simulation=False, language=language
)
stab_handle = stabilizer_backend.process_circuit(
circ, n_shots, noisy_simulation=False, language=language
)
noisy_counts = state_backend.get_result(noisy_handle).get_counts()
assert sum(noisy_counts.values()) == n_shots
assert len(noisy_counts) > 2 # some noisy results likely
pure_counts = state_backend.get_result(pure_handle).get_counts()
assert sum(pure_counts.values()) == n_shots
assert len(pure_counts) == 2
stab_counts = stabilizer_backend.get_result(stab_handle).get_counts()
assert sum(stab_counts.values()) == n_shots
assert len(stab_counts) == 2
# test non-clifford circuit fails on stabilizer backend
# unfortunately the job is accepted, then fails, so have to check get_result
non_stab_circ = (
Circuit(2, name="non_stab_circ").H(0).Rx(0.1, 0).CX(0, 1).measure_all()
)
non_stab_circ = stabilizer_backend.get_compiled_circuit(non_stab_circ)
broken_handle = stabilizer_backend.process_circuit(
non_stab_circ, n_shots, language=language
)
with pytest.raises(GetResultFailed) as _:
_ = stabilizer_backend.get_result(broken_handle)
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.timeout(120)
def test_retrieve_available_devices(
authenticated_quum_backend_qa: QuantinuumBackend,
authenticated_quum_handler: QuantinuumAPI,
) -> None:
# authenticated_quum_backend_qa still needs a handler or it will
# attempt to use the DEFAULT_API_HANDLER.
backend_infos = authenticated_quum_backend_qa.available_devices(
api_handler=authenticated_quum_handler
)
assert len(backend_infos) > 0
assert all(
{OpType.TK2, OpType.ZZMax, OpType.ZZPhase} & backend_info.gate_set
for backend_info in backend_infos
)
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1E"}], indirect=True
)
@pytest.mark.timeout(120)
def test_batching(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
circ = Circuit(2, name="batching_test").H(0).CX(0, 1).measure_all()
state_backend = authenticated_quum_backend_qa
circ = state_backend.get_compiled_circuit(circ)
# test batch can be resumed
h1 = state_backend.start_batch(500, circ, 10)
h2 = state_backend.add_to_batch(h1, circ, 10)
h3 = state_backend.add_to_batch(h1, circ, 10, batch_end=True)
assert state_backend.get_results([h1, h2, h3])
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_submission_with_group(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
b = authenticated_quum_backend_qa
c = Circuit(2, 2, "test 2")
c.H(0)
c.CX(0, 1)
c.measure_all()
c = b.get_compiled_circuit(c)
n_shots = 10
shots = b.run_circuit(
c,
n_shots=n_shots,
group=os.getenv("PYTKET_REMOTE_QUANTINUUM_GROUP", default="DEFAULT"),
language=language,
).get_shots()
assert all(q[0] == q[1] for q in shots)
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_zzphase(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
backend = authenticated_quum_backend_qa
c = Circuit(2, 2, "test rzz")
c.H(0)
c.CX(0, 1)
c.Rz(0.3, 0)
c.CY(0, 1)
c.ZZPhase(0.1, 1, 0)
c.measure_all()
c0 = backend.get_compiled_circuit(c, 0)
assert c0.n_gates_of_type(backend.default_two_qubit_gate) > 0
n_shots = 4
handle = backend.process_circuits([c0], n_shots, language=language)[0]
correct_counts = {(0, 0): 4}
res = backend.get_result(handle, timeout=49)
counts = res.get_counts()
assert counts == correct_counts
c = Circuit(2, 2, "test_rzz_1")
c.H(0).H(1)
c.ZZPhase(1, 1, 0)
c.H(0).H(1)
c1 = backend.get_compiled_circuit(c, 1)
assert c1.n_gates_of_type(OpType.ZZPhase) == 0
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.timeout(120)
def test_zzphase_support_opti2(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
backend = authenticated_quum_backend_qa
c = Circuit(3, 3, "test rzz synthesis")
c.H(0)
c.CX(0, 2)
c.Rz(0.2, 2)
c.CX(0, 2)
c.measure_all()
c0 = backend.get_compiled_circuit(c, 2)
assert c0.n_gates_of_type(backend.default_two_qubit_gate) == 1
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.timeout(120)
def test_prefer_zzphase(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
# We should prefer small-angle ZZPhase to alternative ZZMax decompositions
backend = authenticated_quum_backend_qa
c = (
Circuit(2)
.H(0)
.H(1)
.ZZPhase(0.1, 0, 1)
.Rx(0.2, 0)
.Ry(0.3, 1)
.ZZPhase(0.1, 0, 1)
.H(0)
.H(1)
.measure_all()
)
c0 = backend.get_compiled_circuit(c)
if backend.default_two_qubit_gate == OpType.ZZPhase:
assert c0.n_gates_of_type(OpType.ZZPhase) == 2
elif backend.default_two_qubit_gate == OpType.ZZMax:
assert c0.n_gates_of_type(OpType.ZZMax) == 2
else:
assert backend.default_two_qubit_gate == OpType.TK2
assert c0.n_gates_of_type(OpType.TK2) == 1
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize("device_name", pytest.ALL_DEVICE_NAMES) # type: ignore
@pytest.mark.timeout(120)
def test_device_state(
device_name: str, authenticated_quum_handler: QuantinuumAPI
) -> None:
assert isinstance(
QuantinuumBackend.device_state(
device_name, api_handler=authenticated_quum_handler
),
str,
)
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.parametrize(
"language",
[
Language.QASM,
Language.QIR,
Language.PQIR,
],
)
@pytest.mark.timeout(120)
def test_wasm_qa(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
wasfile = WasmFileHandler(str(Path(__file__).parent.parent / "wasm" / "add1.wasm"))
c = Circuit(1)
c.name = "test_wasm"
a = c.add_c_register("a", 8)
c.add_wasm_to_reg("add_one", wasfile, [a], [a])
b = authenticated_quum_backend_qa
c = b.get_compiled_circuit(c)
h = b.process_circuits(
[c], n_shots=10, wasm_file_handler=wasfile, language=language
)[0]
r = b.get_result(h)
shots = r.get_shots()
assert len(shots) == 10
@pytest.mark.skipif(skip_remote_tests_prod, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_prod", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.parametrize(
"language",
[
Language.QASM,
Language.QIR,
Language.PQIR,
],
)
@pytest.mark.timeout(120)
def test_wasm(
authenticated_quum_backend_prod: QuantinuumBackend, language: Language
) -> None:
wasfile = WasmFileHandler(str(Path(__file__).parent.parent / "wasm" / "add1.wasm"))
c = Circuit(1)
c.name = "test_wasm"
a = c.add_c_register("a", 8)
c.add_wasm_to_reg("add_one", wasfile, [a], [a])
b = authenticated_quum_backend_prod
c = b.get_compiled_circuit(c)
h = b.process_circuits(
[c], n_shots=10, wasm_file_handler=wasfile, language=language
)[0]
r = b.get_result(h)
shots = r.get_shots()
assert len(shots) == 10
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1E"}], indirect=True
)
@pytest.mark.parametrize(
"language",
[
Language.QASM,
Language.QIR,
Language.PQIR,
],
)
@pytest.mark.timeout(120)
def test_wasm_costs(
authenticated_quum_backend_qa: QuantinuumBackend,
language: Language,
) -> None:
wasfile = WasmFileHandler(str(Path(__file__).parent.parent / "wasm" / "add1.wasm"))
c = Circuit(1)
c.name = "test_wasm"
a = c.add_c_register("a", 8)
c.add_wasm_to_reg("add_one", wasfile, [a], [a])
b = authenticated_quum_backend_qa
c = b.get_compiled_circuit(c)
costs = b.cost(
c,
n_shots=10,
syntax_checker="H1-1SC",
wasm_file_handler=wasfile,
language=language,
)
if costs is None:
pytest.skip("API is flaky, sometimes returns None unexpectedly.")
assert isinstance(costs, float)
assert costs > 0.0
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.timeout(120)
def test_submit_qasm(
authenticated_quum_backend_qa: QuantinuumBackend,
) -> None:
qasm = """
OPENQASM 2.0;
include "hqslib1.inc";
qreg q[2];
creg c[2];
U1q(0.5*pi,0.5*pi) q[0];
measure q[0] -> c[0];
if(c[0]==1) rz(1.5*pi) q[0];
if(c[0]==1) rz(0.0*pi) q[1];
if(c[0]==1) U1q(3.5*pi,0.5*pi) q[1];
if(c[0]==1) ZZ q[0],q[1];
if(c[0]==1) rz(3.5*pi) q[1];
if(c[0]==1) U1q(3.5*pi,1.5*pi) q[1];
"""
b = authenticated_quum_backend_qa
h = b.submit_program(Language.QASM, qasm, n_shots=10)
r = b.get_result(h)
shots = r.get_shots()
assert len(shots) == 10
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_options(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
# Unrecognized options are ignored
c0 = Circuit(1).H(0).measure_all()
b = authenticated_quum_backend_qa
c = b.get_compiled_circuit(c0, 0)
h = b.process_circuits([c], n_shots=1, options={"ignoreme": 0}, language=language)
r = b.get_results(h)[0]
shots = r.get_shots()
assert len(shots) == 1
assert len(shots[0]) == 1
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa",
[{"device_name": name} for name in pytest.ALL_SYNTAX_CHECKER_NAMES], # type: ignore
indirect=True,
)
@pytest.mark.parametrize("language", [Language.QASM, Language.QIR, Language.PQIR])
@pytest.mark.timeout(120)
def test_tk2(
authenticated_quum_backend_qa: QuantinuumBackend, language: Language
) -> None:
c0 = (
Circuit(2)
.XXPhase(0.1, 0, 1)
.YYPhase(0.2, 0, 1)
.ZZPhase(0.3, 0, 1)
.measure_all()
)
b = authenticated_quum_backend_qa
b.set_compilation_config_target_2qb_gate(OpType.TK2)
c = b.get_compiled_circuit(c0, 2)
h = b.process_circuit(c, n_shots=1, language=language)
r = b.get_result(h)
shots = r.get_shots()
assert len(shots) == 1
assert len(shots[0]) == 2
@pytest.mark.skipif(skip_remote_tests, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.timeout(120)
def test_qir_submission(authenticated_quum_backend_qa: QuantinuumBackend) -> None:
# disable Garbage Collector because of
# https://github.com/CQCL/pytket-quantinuum/issues/170
gc.disable()
b = authenticated_quum_backend_qa
with open("integration/qir/qat-link_2.ll") as f:
qir = f.read()
ctx = create_context()
module = parse_assembly(qir, context=ctx)
ir = module.as_bitcode()
h = b.submit_program(Language.QIR, b64encode(ir).decode("utf-8"), n_shots=10)
r = b.get_result(h)
assert set(r.get_bitlist()) == set([Bit("0_t0", 0), Bit("0_t1", 0)])
assert len(r.get_shots()) == 10
@pytest.mark.skipif(skip_remote_tests_prod, reason=REASON)
@pytest.mark.parametrize(
"authenticated_quum_backend_prod", [{"device_name": "H1-1SC"}], indirect=True
)
@pytest.mark.timeout(120)
def test_qir_entrypoints(authenticated_quum_backend_prod: QuantinuumBackend) -> None:
# disable Garbage Collector because of
# https://github.com/CQCL/pytket-quantinuum/issues/170
gc.disable()
b = authenticated_quum_backend_prod
with open("integration/qir/qat-link.ll") as f:
qir = f.read()