forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kernel_builder.py
More file actions
1450 lines (1157 loc) · 45.7 KB
/
test_kernel_builder.py
File metadata and controls
1450 lines (1157 loc) · 45.7 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
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 (c) 2022 - 2025 NVIDIA Corporation & Affiliates. #
# All rights reserved. #
# #
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
# This file is responsible for testing the accuracy of gates within
# the kernel builder.
import pytest
import random
import numpy as np
import os
from typing import List
import cudaq
from cudaq import spin
def test_sdg_0_state():
"""Tests the adjoint S-gate on a qubit starting in the 0-state."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in superposition state.
kernel.h(qubit)
# Rotate around Z by -pi/2, twice. Total rotation of -pi.
kernel.sdg(qubit)
kernel.sdg(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Since the qubit began in the 0-state, it should now be in the
# 1-state.
assert counts["1"] == 1000
def test_sdg_1_state():
"""Tests the adjoint S-gate on a qubit starting in the 1-state."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in 1-state.
kernel.x(qubit)
# Superposition.
kernel.h(qubit)
# Rotate around Z by -pi/2, twice. Total rotation of -pi.
kernel.sdg(qubit)
kernel.sdg(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Since the qubit began in the 1-state, it should now be in the
# 0-state.
assert counts["0"] == 1000
def test_sdg_0_state_negate():
"""Tests that the sdg and s gates cancel each other out."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in superposition state.
kernel.h(qubit)
# Rotate around Z by -pi/2, twice. Total rotation of -pi.
kernel.sdg(qubit)
kernel.sdg(qubit)
# Rotate back around by pi. Will use two gates here, but will
# also test with a plain Z-gate in the 1-state test.
kernel.s(qubit)
kernel.s(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Qubit should still be in 0 state.
assert counts["0"] == 1000
def test_sdg_1_state_negate():
"""Tests that the sdg and s gates cancel each other out."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in 1-state.
kernel.x(qubit)
# Superpositoin.
kernel.h(qubit)
# Rotate around Z by -pi/2, twice. Total rotation of -pi.
kernel.sdg(qubit)
kernel.sdg(qubit)
# Rotate back by pi.
kernel.z(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Qubit should still be in 1 state.
assert counts["1"] == 1000
def test_tdg_0_state():
"""Tests the adjoint T-gate on a qubit starting in the 0-state."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in superposition state.
kernel.h(qubit)
# Rotate around Z by -pi/4, four times. Total rotation of -pi.
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Since the qubit began in the 0-state, it should now be in the
# 1-state.
assert counts["1"] == 1000
def test_tdg_1_state():
"""Tests the adjoint T-gate on a qubit starting in the 1-state."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in 1-state.
kernel.x(qubit)
# Superposition.
kernel.h(qubit)
# Rotate around Z by -pi/4, four times. Total rotation of -pi.
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Since the qubit began in the 1-state, it should now be in the
# 0-state.
assert counts["0"] == 1000
def test_tdg_0_state_negate():
"""Tests that the adjoint T gate cancels with a T gate."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in superposition state.
kernel.h(qubit)
# Rotate around Z by -pi/4, four times. Total rotation of -pi.
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
# Rotate back by pi.
kernel.z(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Qubit should remain in 0-state.
assert counts["0"] == 1000
def test_tdg_1_state_negate():
"""Tests that the adjoint T gate cancels with a T gate."""
kernel = cudaq.make_kernel()
qubit = kernel.qalloc(1)
# Place qubit in 1-state.
kernel.x(qubit)
# Superposition.
kernel.h(qubit)
# Rotate around Z by -pi/4, four times. Total rotation of -pi.
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
kernel.tdg(qubit)
# Rotate back by pi.
kernel.t(qubit)
kernel.t(qubit)
kernel.t(qubit)
kernel.t(qubit)
# Apply another hadamard.
kernel.h(qubit)
kernel.mz(qubit)
counts = cudaq.sample(kernel)
print(counts)
# Qubit should remain in 1-state.
assert counts["1"] == 1000
def test_rotation_multi_target():
"""
Tests the accuracy of rotation gates when applied to
entire qregs.
"""
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(3)
# Start in the |1> state.
kernel.x(qubits)
# Rotate qubits back to the |0> state.
kernel.rx(np.pi, qubits)
# Phase rotation.
kernel.r1(-np.pi, qubits)
# Rotate back to |1> state.
kernel.ry(np.pi, qubits)
# Phase rotation.
kernel.rz(np.pi, qubits)
counts = cudaq.sample(kernel)
assert counts["111"] == 1000
def test_ctrl_x():
"""Tests the accuracy of the overloads for the controlled-X gate."""
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
kernel.cx(qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
kernel.x(qubits[2])
kernel.cx(qubits[2], qubits[3])
# Overload 2: list of control qubits, one target.
# The last qubit in `qubits` is still in 0-state, as is our
# `control` register. A controlled gate between them should
# have no impact.
kernel.cx([controls[0], controls[1]], qubits[4])
# Overload 3: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
kernel.cx(controls, qubits[4])
counts = cudaq.sample(kernel)
print(counts)
# State of system should now be `|qubits, controls> = |00111 11>`.
assert counts["0011111"] == 1000
def test_ctrl_y():
"""Tests the accuracy of the overloads for the controlled-Y gate."""
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
kernel.cy(qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
kernel.x(qubits[2])
kernel.cy(qubits[2], qubits[3])
# Overload 2: list of control qubits, one target.
# The last qubit in `qubits` is still in 0-state, as is our
# `control` register. A controlled gate between them should
# have no impact.
kernel.cy([controls[0], controls[1]], qubits[4])
# Overload 3: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
kernel.cy(controls, qubits[4])
counts = cudaq.sample(kernel)
print(counts)
# State of system should now be `|qubits, controls> = |00111 11>`.
assert counts["0011111"] == 1000
def test_ctrl_z():
"""Tests the accuracy of the overloads for the controlled-Z gate."""
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
kernel.cz(qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
kernel.x(qubits[2])
kernel.cz(qubits[2], qubits[3])
# Overload 2: list of control qubits, one target.
# The last qubit in `qubits` is still in 0-state, as is our
# `control` register. A controlled gate between them should
# have no impact.
kernel.cz([controls[0], controls[1]], qubits[4])
# Overload 3: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
kernel.cz(controls, qubits[4])
counts = cudaq.sample(kernel)
print(counts)
# The phase should not affect the final state of any target qubits,
# leaving us with the total state: `|qubits, controls> = |00100 11>`.
assert counts["0010011"] == 1000
def test_ctrl_h():
"""Tests the accuracy of the overloads for the controlled-H gate."""
cudaq.set_random_seed(4)
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
kernel.ch(qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
kernel.x(qubits[2])
kernel.ch(qubits[2], qubits[3])
# Overload 2: list of control qubits, one target.
# The last qubit in `qubits` is still in 0-state, as is our
# `control` register. A controlled gate between them should
# have no impact.
kernel.ch([controls[0], controls[1]], qubits[4])
# Overload 3: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
kernel.ch(controls, qubits[4])
counts1 = cudaq.sample(kernel)
print(counts1)
# Our first two qubits remain untouched, while `qubits[2]` is rotated
# to 1, and `qubits[3]` receives a Hadamard. This results in a nearly 50/50
# split of measurements on `qubits[3]` between 0 and 1.
# The controlled Hadamard on `qubits[4]` also results in a 50/50 split of its
# measurements between 0 and 1.
assert counts1["0011011"] >= 225 and counts1["0011011"] <= 275
assert counts1["0011111"] >= 225 and counts1["0011111"] <= 275
assert counts1["0010011"] >= 225 and counts1["0010011"] <= 275
assert counts1["0010111"] >= 225 and counts1["0010111"] <= 275
assert counts1["0011011"] + counts1["0011111"] + counts1[
"0010011"] + counts1["0010111"] == 1000
kernel.h(qubits[3])
kernel.h(qubits[4])
counts2 = cudaq.sample(kernel)
print(counts2)
assert counts2["0010011"] == 1000
def test_ctrl_s():
"""Tests the accuracy of the overloads for the controlled-S gate."""
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
kernel.cs(qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
kernel.x(qubits[2])
kernel.cs(qubits[2], qubits[3])
# Overload 2: list of control qubits, one target.
# The last qubit in `qubits` is still in 0-state, as is our
# `control` register. A controlled gate between them should
# have no impact.
kernel.cz([controls[0], controls[1]], qubits[4])
# Overload 3: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
kernel.cz(controls, qubits[4])
counts = cudaq.sample(kernel)
print(counts)
# The phase should not affect the final state of any target qubits,
# leaving us with the total state: `|qubits, controls> = |00100 11>`.
assert counts["0010011"] == 1000
def test_ctrl_t():
"""Tests the accuracy of the overloads for the controlled-T gate."""
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
kernel.ct(qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
kernel.x(qubits[2])
kernel.ct(qubits[2], qubits[3])
# Overload 2: list of control qubits, one target.
# The last qubit in `qubits` is still in 0-state, as is our
# `control` register. A controlled gate between them should
# have no impact.
kernel.cz([controls[0], controls[1]], qubits[4])
# Overload 3: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
kernel.cz(controls, qubits[4])
counts = cudaq.sample(kernel)
print(counts)
# The phase should not affect the final state of any target qubits,
# leaving us with the total state: `|qubits, controls> = |00100 11>`.
assert counts["0010011"] == 1000
def test_cr1_gate():
"""Tests the accuracy of the overloads for the controlled-r1 gate."""
kernel, angle = cudaq.make_kernel(float)
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
angle_value = np.pi
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
# Testing the `QuakeValue` parameter overload.
kernel.cr1(angle, qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
# Testing the `float` parameter overload.
kernel.x(qubits[2])
kernel.cr1(angle_value, qubits[2], qubits[3])
# Overload 2: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
# `QuakeValue` parameter.
kernel.cr1(angle, controls, qubits[4])
# `float` parameter that we set = 0.0 so it doesn't impact state.
kernel.cr1(0.0, controls, qubits[4])
counts = cudaq.sample(kernel, angle_value)
print(counts)
# The phase should not affect the final state of any target qubits,
# leaving us with the total state: `|qubits, controls> = |00100 11>`.
assert counts["0010011"] == 1000
def test_crx_gate():
"""Tests the accuracy of the overloads for the controlled-rx gate."""
kernel, angle = cudaq.make_kernel(float)
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
angle_value = np.pi
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
# Testing the `QuakeValue` parameter overload.
kernel.crx(angle, qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
# Testing the `float` parameter overload.
kernel.x(qubits[2])
kernel.crx(angle_value, qubits[2], qubits[3])
# Overload 2: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
# `QuakeValue` parameter.
kernel.crx(angle, controls, qubits[4])
# `float` parameter that we set = 0.0 so it doesn't impact state.
kernel.crx(0.0, controls, qubits[4])
counts = cudaq.sample(kernel, angle_value)
print(counts)
# State of system should now be `|qubits, controls> = |00111 11>`.
assert counts["0011111"] == 1000
def test_cry_gate():
"""Tests the accuracy of the overloads for the controlled-ry gate."""
kernel, angle = cudaq.make_kernel(float)
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
angle_value = np.pi
# Overload 1: one control qubit, one target.
# 2-qubit controlled operation with control in 0-state.
# Testing the `QuakeValue` parameter overload.
kernel.cry(angle, qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
# Testing the `float` parameter overload.
kernel.x(qubits[2])
kernel.cry(angle_value, qubits[2], qubits[3])
# Overload 2: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
# `QuakeValue` parameter.
kernel.cry(angle, controls, qubits[4])
# `float` parameter that we set = 0.0 so it doesn't impact state.
kernel.cry(0.0, controls, qubits[4])
counts = cudaq.sample(kernel, angle_value)
print(counts)
# State of system should now be `|qubits, controls> = |00111 11>`.
assert counts["0011111"] == 1000
def test_crz_gate():
"""Tests the accuracy of the overloads for the controlled-rz gate."""
kernel, angle = cudaq.make_kernel(float)
qubits = kernel.qalloc(5)
controls = kernel.qalloc(2)
angle_value = np.pi
# 2-qubit controlled operation with control in 0-state.
# Testing the `QuakeValue` parameter overload.
kernel.crz(angle, qubits[0], qubits[1])
# 2-qubit controlled operation with control in 1-state.
# Testing the `float` parameter overload.
kernel.x(qubits[2])
kernel.crz(angle_value, qubits[2], qubits[3])
# Overload 2: register of control qubits, one target.
# Now place `control` register in 1-state and perform another
# controlled operation on the final qubit.
kernel.x(controls)
# `QuakeValue` parameter.
kernel.crz(angle, controls, qubits[4])
# `float` parameter that we set = 0.0 so it doesn't impact state.
kernel.crz(0.0, controls, qubits[4])
counts = cudaq.sample(kernel, angle_value)
print(counts)
# The phase should not affect the final state of any target qubits,
# leaving us with the total state: `|qubits, controls> = |00100 11>`.
assert counts["0010011"] == 1000
@pytest.mark.parametrize("control_count", [1, 2, 3])
def test_cswap_gate_ctrl_list(control_count):
"""Tests the controlled-SWAP operation given a vector of control qubits."""
kernel = cudaq.make_kernel()
controls = [kernel.qalloc() for _ in range(control_count)]
first = kernel.qalloc()
second = kernel.qalloc()
kernel.x(first)
# All controls in the |0> state, no SWAP should occur.
kernel.cswap(controls, first, second)
# If we have multiple controls, place a random control qubit
# in the |1> state. This check ensures that our controlled
# SWAP's are performed if and only if all controls are in the
# |1> state.
if (len(controls) > 1):
random_index = random.randint(0, control_count - 1)
kernel.x(controls[random_index])
# Not all controls in the in |1>, no SWAP.
kernel.cswap(controls, first, second)
# Rotate that random control back to |0>.
kernel.x(controls[random_index])
# Now place all of the controls in |1>.
for control in controls:
kernel.x(control)
# Should now SWAP our `first` and `second` qubits.
kernel.cswap(controls, first, second)
counts = cudaq.sample(kernel)
print(counts)
controls_state = "1" * control_count
want_state = controls_state + "01"
assert counts[want_state] == 1000
def test_cswap_gate_mixed_ctrls():
"""
Tests the controlled-SWAP gate given a list of a mix of ctrl
qubits and registers.
"""
kernel = cudaq.make_kernel()
controls_vector = [kernel.qalloc() for _ in range(2)]
controls_register = kernel.qalloc(2)
first = kernel.qalloc()
second = kernel.qalloc()
# `first` in |1> state.
kernel.x(first)
# `controls_register` in |1> state.
kernel.x(controls_register)
# `controls_vector` in |0>, no SWAP.
kernel.cswap(controls_vector, first, second)
# `controls_register` in |1>, SWAP.
kernel.cswap(controls_register, first, second)
# Pass the vector and register as the controls. The vector is still in |0>, so
# no SWAP.
kernel.cswap([controls_vector[0], controls_vector[1], controls_register],
first, second)
# Place the vector in |1>, should now get a SWAP.
kernel.x(controls_vector[0])
kernel.x(controls_vector[1])
kernel.cswap([controls_vector[0], controls_vector[1], controls_register],
first, second)
counts = cudaq.sample(kernel)
print(counts)
controls_state = "1111"
# The SWAP's should make the targets end up back in |10>.
want_state = controls_state + "10"
assert counts[want_state] == 1000
def test_crx_control_list():
kernel, value = cudaq.make_kernel(float)
target = kernel.qalloc()
q1 = kernel.qalloc()
q2 = kernel.qalloc()
q3 = kernel.qalloc()
# Place a subset of controls in 1-state.
kernel.x(q1)
kernel.x(q2)
# Using different orientations of our control qubits
# to make kernel less trivial.
# Overload 1: `QuakeValue` parameter. All controls are in |1>,
# so this should rotate our `target`.
kernel.crx(value, [q1, q2], target)
# Overload 2: `float` parameter. `q3` is still in |0>, so this
# should not rotate our `target`.
kernel.crx(np.pi, [q3, q2, q1], target)
print(kernel)
result = cudaq.sample(kernel, np.pi)
print(result)
# Target is still in 1-state, while q1 = q2 = 1, and q3 = 0
assert result["1110"] == 1000
def test_cry_control_list():
kernel, value = cudaq.make_kernel(float)
target = kernel.qalloc()
q1 = kernel.qalloc()
q2 = kernel.qalloc()
q3 = kernel.qalloc()
# Place a subset of controls in 1-state.
kernel.x(q1)
kernel.x(q2)
# Using different orientations of our control qubits
# to make kernel less trivial.
# Overload 1: `QuakeValue` parameter. All controls are in |1>,
# so this should rotate our `target`.
kernel.cry(value, [q1, q2], target)
# Overload 2: `float` parameter. `q3` is still in |0>, so this
# should not rotate our `target`.
kernel.cry(np.pi, [q3, q2, q1], target)
result = cudaq.sample(kernel, np.pi)
print(result)
# Target is still in 1-state, while q1 = q2 = 1, and q3 = 0
assert result["1110"] == 1000
def test_crz_control_list():
kernel, value = cudaq.make_kernel(float)
target = kernel.qalloc()
q1 = kernel.qalloc()
q2 = kernel.qalloc()
q3 = kernel.qalloc()
# Place controls in 1-state.
kernel.x(q1)
kernel.x(q2)
kernel.x(q3)
# Hadamard our target.
kernel.h(target)
# Overload 1: `QuakeValue` parameter.
kernel.crz(value, [q1, q2, q3], target)
# Overload 2: `float` parameter.
kernel.crz(-np.pi / 2, [q3, q2, q1], target)
# Another hadamard to our target.
kernel.h(target)
result = cudaq.sample(kernel, -np.pi / 2)
print(result)
# The phase rotation on our target by -pi should mean
# we measure it in the 1-state.
assert result["1111"] == 1000
def test_cr1_control_list():
kernel, value = cudaq.make_kernel(float)
target = kernel.qalloc()
q1 = kernel.qalloc()
q2 = kernel.qalloc()
q3 = kernel.qalloc()
# Place controls in 1-state.
kernel.x(q1)
kernel.x(q2)
kernel.x(q3)
# Hadamard our target.
kernel.h(target)
# Overload 1: `QuakeValue` parameter.
kernel.cr1(value, [q1, q2, q3], target)
# Overload 2: `float` parameter.
kernel.cr1(-np.pi / 2, [q3, q2, q1], target)
# Another hadamard to our target.
kernel.h(target)
result = cudaq.sample(kernel, -np.pi / 2)
print(result)
# The phase rotation on our target by -pi should mean
# we measure it in the 1-state.
assert result["1111"] == 1000
def test_ctrl_rotation_integration():
"""
Tests more complex controlled rotation kernels, including
pieces that will only run in quantinuum emulation.
"""
cudaq.set_random_seed(4)
cudaq.set_target("quantinuum", emulate=True)
kernel = cudaq.make_kernel()
ctrls = kernel.qalloc(4)
ctrl = kernel.qalloc()
target = kernel.qalloc()
# Subset of `ctrls` in |1> state.
kernel.x(ctrls[0])
kernel.x(ctrls[1])
# Multi-controlled rotation with that qreg should have
# no impact on our target, since not all `ctrls` are |1>.
kernel.cry(1.0, ctrls, target)
# Flip the rest of our `ctrls` to |1>.
kernel.x(ctrls[2])
kernel.x(ctrls[3])
# Multi-controlled rotation should now flip our target.
kernel.crx(np.pi / 4., ctrls, target)
# Test (1) (only works in emulation): mixed list of veqs and qubits.
# Has no impact because `ctrl` = |0>
kernel.crx(1.0, [ctrls, ctrl], target)
# Test (2): Flip `ctrl` and try again.
kernel.x(ctrl)
kernel.crx(np.pi / 4., [ctrls, ctrl], target)
result = cudaq.sample(kernel)
print(result)
# The `target` should be in a 50/50 mix between |0> and |1>.
extra_mapping_qubits = "0000"
want_1_state = extra_mapping_qubits + "111111"
want_0_state = extra_mapping_qubits + "111110"
assert result[want_1_state] == 505
assert result[want_0_state] == 495
cudaq.reset_target()
def test_can_progressively_build():
"""Tests that a kernel can be build progressively."""
cudaq.reset_target()
kernel = cudaq.make_kernel()
q = kernel.qalloc(2)
kernel.h(q[0])
print(kernel)
state = cudaq.get_state(kernel)
assert np.isclose(1. / np.sqrt(2.), state[0].real)
assert np.isclose(0., state[1].real)
assert np.isclose(1. / np.sqrt(2.), state[2].real)
assert np.isclose(0., state[3].real)
counts = cudaq.sample(kernel)
print(counts)
assert '10' in counts
assert '00' in counts
# Continue building the kernel
kernel.cx(q[0], q[1])
print(kernel)
state = cudaq.get_state(kernel)
assert np.isclose(1. / np.sqrt(2.), state[0].real)
assert np.isclose(0., state[1].real)
assert np.isclose(0., state[2].real)
assert np.isclose(1. / np.sqrt(2.), state[3].real)
counts = cudaq.sample(kernel)
print(counts)
assert '11' in counts
assert '00' in counts
def test_recursive_calls():
kernel1, qubit1 = cudaq.make_kernel(cudaq.qubit)
# print(kernel1)
kernel2, qubit2 = cudaq.make_kernel(cudaq.qubit)
kernel2.apply_call(kernel1, qubit2)
# print(kernel2)
kernel3 = cudaq.make_kernel()
qreg3 = kernel3.qalloc(1)
qubit3 = qreg3[0]
kernel3.apply_call(kernel2, qubit3)
print(kernel3)
skipIfNvidiaFP64NotInstalled = pytest.mark.skipif(
not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia-fp64')),
reason='Could not find nvidia-fp64 in installation')
def test_state_capture():
state = np.array([.70710678, 0., 0., 0.70710678], dtype=complex)
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
assert '11' in counts
assert '00' in counts
t = state[1]
state[1] = state[3]
state[3] = t
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
assert '10' in counts
assert '00' in counts
@skipIfNvidiaFP64NotInstalled
def test_from_state0():
cudaq.set_target('nvidia-fp64')
kernel, initState = cudaq.make_kernel(list[complex])
qubits = kernel.qalloc(initState)
# Test float64 list, casts to complex
state = [.70710678, 0., 0., 0.70710678]
counts = cudaq.sample(kernel, state)
print(counts)
assert '11' in counts
assert '00' in counts
# Test complex list
state = [.70710678j, 0., 0., 0.70710678]
counts = cudaq.sample(kernel, state)
print(counts)
assert '11' in counts
assert '00' in counts
# Test Numpy array
state = np.asarray([.70710678, 0., 0., 0.70710678])
counts = cudaq.sample(kernel, state)
print(counts)
assert '11' in counts
assert '00' in counts
# Now test constant array data, not kernel input
state = np.array([.70710678, 0., 0., 0.70710678], dtype=complex)
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
print(counts)
assert '11' in counts
assert '00' in counts
state = [.70710678 + 0j, 0., 0., 0.70710678]
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
print(counts)
assert '11' in counts
assert '00' in counts
state = np.array([.70710678, 0., 0., 0.70710678])
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
print(counts)
assert '11' in counts
assert '00' in counts
state = np.array([.70710678, 0., 0., 0.70710678], dtype=np.complex64)
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
print(counts)
assert '11' in counts
assert '00' in counts
with pytest.raises(RuntimeError) as e:
qubits = kernel.qalloc(np.array([1., 0., 0.], dtype=complex))
assert 'invalid input state size for qalloc (not a power of 2)' in repr(e)
cudaq.reset_target()
skipIfNvidiaNotInstalled = pytest.mark.skipif(
not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia')),
reason='Could not find nvidia in installation')
@skipIfNvidiaNotInstalled
def test_from_state1():
cudaq.set_target('nvidia')
state = np.array([.70710678, 0., 0., 0.70710678], dtype=np.complex128)
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(state)
counts = cudaq.sample(kernel)
print(counts)
assert '11' in counts
assert '00' in counts
state = np.array([.70710678, 0., 0., 0.70710678], dtype=np.complex64)
kernel2 = cudaq.make_kernel()
qubits = kernel2.qalloc(state)
counts = cudaq.sample(kernel2)
print(counts)
assert '11' in counts
assert '00' in counts
cudaq.reset_target()
# Regardless of the target precision, use
# cudaq.complex() or cudaq.amplitudes()
state = np.array([.70710678, 0., 0., 0.70710678], dtype=cudaq.complex())
kernel2 = cudaq.make_kernel()
qubits = kernel2.qalloc(state)