forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kernel_features.py
More file actions
2069 lines (1524 loc) · 46.7 KB
/
test_kernel_features.py
File metadata and controls
2069 lines (1524 loc) · 46.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. #
# ============================================================================ #
import os
import pytest
import numpy as np
from typing import Callable, List
import sys
import cudaq
from cudaq import spin
@pytest.fixture(autouse=True)
def do_something():
yield
cudaq.__clearKernelRegistries()
def test_adjoint():
"""Test that adjoint can be called on kernels and operations."""
@cudaq.kernel
def single_adjoint_test():
q = cudaq.qubit()
t(q)
t.adj(q)
counts = cudaq.sample(single_adjoint_test)
assert '0' in counts
assert len(counts) == 1
@cudaq.kernel
def qvector_adjoint_test():
q = cudaq.qvector(2)
t(q)
t.adj(q)
counts = cudaq.sample(qvector_adjoint_test)
assert '00' in counts
assert len(counts) == 1
@cudaq.kernel
def rotation_adjoint_test():
q = cudaq.qubit()
rx(1.1, q)
rx.adj(1.1, q)
ry(1.1, q)
ry.adj(1.1, q)
counts = cudaq.sample(rotation_adjoint_test)
assert '0' in counts
assert len(counts) == 1
@cudaq.kernel
def test_kernel_adjoint(q: cudaq.qview):
h(q[0])
t(q[1])
s(q[2])
@cudaq.kernel
def test_caller():
q = cudaq.qvector(3)
x(q[0])
x(q[2])
test_kernel_adjoint(q)
cudaq.adjoint(test_kernel_adjoint, q)
counts = cudaq.sample(test_caller)
assert len(counts) == 1
assert '101' in counts
def test_control():
"""Test that we can control on kernel functions."""
@cudaq.kernel
def fancyCnot(a: cudaq.qubit, b: cudaq.qubit):
x.ctrl(a, b)
@cudaq.kernel
def toffoli():
q = cudaq.qvector(3)
ctrl = q.front()
# without a control, apply x to all
x(ctrl, q[2])
cudaq.control(fancyCnot, [ctrl], q[1], q[2])
counts = cudaq.sample(toffoli)
assert len(counts) == 1
assert '101' in counts
@cudaq.kernel
def test():
q, r, s = cudaq.qubit(), cudaq.qubit(), cudaq.qubit()
x(q, s)
swap.ctrl(q, r, s)
counts = cudaq.sample(test)
assert len(counts) == 1
assert '110' in counts
def test_grover():
"""Test that compute_action works in tandem with kernel composability."""
@cudaq.kernel
def reflect(qubits: cudaq.qview):
ctrls = qubits.front(qubits.size() - 1)
last = qubits.back()
cudaq.compute_action(lambda: (h(qubits), x(qubits)),
lambda: z.ctrl(ctrls, last))
@cudaq.kernel
def oracle(q: cudaq.qview):
z.ctrl(q[0], q[2])
z.ctrl(q[1], q[2])
print(reflect)
@cudaq.kernel
def grover(N: int, M: int, oracle: Callable[[cudaq.qview], None]):
q = cudaq.qvector(N)
h(q)
for i in range(M):
oracle(q)
reflect(q)
mz(q)
print(grover)
print(oracle)
counts = cudaq.sample(grover, 3, 1, oracle)
assert len(counts) == 2
assert '101' in counts
assert '011' in counts
def test_2grover_compute_action():
"""Test that compute_action works in tandem with kernel composability."""
@cudaq.kernel
def reflect2(qubits: cudaq.qview):
ctrls = qubits.front(qubits.size() - 1)
last = qubits.back()
def compute():
h(qubits)
x(qubits)
# can also use
# compute = lambda : (h(qubits), x(qubits))
cudaq.compute_action(compute, lambda: z.ctrl(ctrls, last))
print(reflect2)
# Order matters, kernels must be "in-scope"
@cudaq.kernel
def oracle2(q: cudaq.qview):
z.ctrl(q[0], q[2])
z.ctrl(q[1], q[2])
@cudaq.kernel
def grover(N: int, M: int, oracle: Callable[[cudaq.qview], None]):
q = cudaq.qvector(N)
h(q)
for i in range(M):
oracle(q)
reflect2(q)
mz(q)
# print(grover)
counts = cudaq.sample(grover, 3, 1, oracle2)
assert len(counts) == 2
assert '101' in counts
assert '011' in counts
def test_observe():
@cudaq.kernel
def ansatz():
q = cudaq.qvector(1)
molecule = 5.0 - 1.0 * spin.x(0)
res = cudaq.observe(ansatz, molecule, shots_count=10000)
assert np.isclose(res.expectation(), 5.0, atol=1e-1)
def test_pauli_word_input():
h2_data = [
3, 1, 1, 3, 0.0454063, 0, 2, 0, 0, 0, 0.17028, 0, 0, 0, 2, 0, -0.220041,
-0, 1, 3, 3, 1, 0.0454063, 0, 0, 0, 0, 0, -0.106477, 0, 0, 2, 0, 0,
0.17028, 0, 0, 0, 0, 2, -0.220041, -0, 3, 3, 1, 1, -0.0454063, -0, 2, 2,
0, 0, 0.168336, 0, 2, 0, 2, 0, 0.1202, 0, 0, 2, 0, 2, 0.1202, 0, 2, 0,
0, 2, 0.165607, 0, 0, 2, 2, 0, 0.165607, 0, 0, 0, 2, 2, 0.174073, 0, 1,
1, 3, 3, -0.0454063, -0, 15
]
h = cudaq.SpinOperator(h2_data, 4)
@cudaq.kernel
def kernel(theta: float, var: cudaq.pauli_word):
q = cudaq.qvector(4)
x(q[0])
x(q[1])
exp_pauli(theta, q, var)
print(kernel)
kernel(.11, 'XXXY')
want_exp = cudaq.observe(kernel, h, .11, 'XXXY').expectation()
assert np.isclose(want_exp, -1.13, atol=1e-2)
want_exp = cudaq.observe(kernel, h, .11,
cudaq.pauli_word('XXXY')).expectation()
assert np.isclose(want_exp, -1.13, atol=1e-2)
@cudaq.kernel
def test(theta: float, paulis: list[cudaq.pauli_word]):
q = cudaq.qvector(4)
x(q[0])
x(q[1])
for p in paulis:
exp_pauli(theta, q, p)
print(test)
want_exp = cudaq.observe(test, h, .11, ['XXXY']).expectation()
assert np.isclose(want_exp, -1.13, atol=1e-2)
words = [cudaq.pauli_word('XXXY')]
want_exp = cudaq.observe(test, h, .11, words).expectation()
assert np.isclose(want_exp, -1.13, atol=1e-2)
with pytest.raises(RuntimeError) as e:
kernel(.11, 'HELLOBADTERM')
def test_exp_pauli():
h2_data = [
3, 1, 1, 3, 0.0454063, 0, 2, 0, 0, 0, 0.17028, 0, 0, 0, 2, 0, -0.220041,
-0, 1, 3, 3, 1, 0.0454063, 0, 0, 0, 0, 0, -0.106477, 0, 0, 2, 0, 0,
0.17028, 0, 0, 0, 0, 2, -0.220041, -0, 3, 3, 1, 1, -0.0454063, -0, 2, 2,
0, 0, 0.168336, 0, 2, 0, 2, 0, 0.1202, 0, 0, 2, 0, 2, 0.1202, 0, 2, 0,
0, 2, 0.165607, 0, 0, 2, 2, 0, 0.165607, 0, 0, 0, 2, 2, 0.174073, 0, 1,
1, 3, 3, -0.0454063, -0, 15
]
h = cudaq.SpinOperator(h2_data, 4)
@cudaq.kernel
def kernel(theta: float):
q = cudaq.qvector(4)
x(q[0])
x(q[1])
exp_pauli(theta, q, 'XXXY')
print(kernel)
want_exp = cudaq.observe(kernel, h, .11).expectation()
assert np.isclose(want_exp, -1.13, atol=1e-2)
@pytest.mark.parametrize('target', ['default', 'stim'])
def test_dynamic_circuit(target):
"""Test that we correctly sample circuits with
mid-circuit measurements and conditionals."""
if target == 'stim':
save_target = cudaq.get_target()
cudaq.set_target('stim')
@cudaq.kernel
def simple():
q = cudaq.qvector(2)
h(q[0])
i = mz(q[0], register_name="c0")
if i:
x(q[1])
mz(q)
counts = cudaq.sample(simple, shots_count=100)
counts.dump()
c0 = counts.get_register_counts('c0')
assert '0' in c0 and '1' in c0
assert '00' in counts and '11' in counts
@cudaq.kernel
def simple():
q = cudaq.qvector(2)
h(q[0])
i = mz(q[0])
if i:
x(q[1])
mz(q)
counts = cudaq.sample(simple)
counts.dump()
c0 = counts.get_register_counts('i')
assert '0' in c0 and '1' in c0
assert '00' in counts and '11' in counts
if target == 'stim':
cudaq.set_target(save_target)
def test_teleport():
@cudaq.kernel
def teleport():
q = cudaq.qvector(3)
x(q[0])
h(q[1])
x.ctrl(q[1], q[2])
x.ctrl(q[0], q[1])
h(q[0])
b0 = mz(q[0])
b1 = mz(q[1])
if b1:
x(q[2])
if b0:
z(q[2])
mz(q[2])
counts = cudaq.sample(teleport, shots_count=100)
counts.dump()
# Note this is testing that we can provide
# the register name automatically
b0 = counts.get_register_counts('b0')
assert '0' in b0 and '1' in b0
def test_transitive_dependencies():
@cudaq.kernel()
def func0(q: cudaq.qubit):
x(q)
@cudaq.kernel()
def func1(q: cudaq.qubit):
func0(q)
@cudaq.kernel
def func2(q: cudaq.qubit):
func1(q)
@cudaq.kernel()
def callMe():
q = cudaq.qubit()
func2(q)
print(callMe)
counts = cudaq.sample(callMe)
assert len(counts) == 1 and '1' in counts
# This test is for a bug where by
# vqe_kernel thought kernel was a
# dependency because cudaq.kernel
# is a Call node in the AST.
@cudaq.kernel
def kernel():
qubit = cudaq.qvector(2)
h(qubit[0])
x.ctrl(qubit[0], qubit[1])
mz(qubit)
result = cudaq.sample(kernel)
print(result)
assert len(result) == 2 and '00' in result and '11' in result
@cudaq.kernel
def vqe_kernel(nn: int):
qubit = cudaq.qvector(nn)
h(qubit[0])
x.ctrl(qubit[0], qubit[1])
mz(qubit)
print(vqe_kernel)
result = cudaq.sample(vqe_kernel, 2)
print(result)
assert len(result) == 2 and '00' in result and '11' in result
def test_decrementing_range():
@cudaq.kernel
def test(q: int, p: int):
qubits = cudaq.qvector(5)
for k in range(q, p, -1):
cudaq.dbg.ast.print_i64(k)
x(qubits[k])
counts = cudaq.sample(test, 2, 0)
counts.dump()
assert '01100' in counts and len(counts) == 1
@cudaq.kernel
def test2(myList: List[int]):
q = cudaq.qvector(len(myList))
for i in range(0, len(myList), 2):
cudaq.dbg.ast.print_i64(i)
x(q[i])
counts = cudaq.sample(test2, [0, 1, 2, 3])
assert len(counts) == 1
assert '1010' in counts
def test_no_dynamic_Lists():
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel(params: List[float]):
params.append(1.0)
kernel([])
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel():
l = [i for i in range(10)]
l.append(11)
print(kernel)
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel():
l = [[i, i, i] for i in range(10)]
l.append([11, 12, 13])
print(kernel)
def test_no_dynamic_lists():
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel(params: list[float]):
params.append(1.0)
print(kernel)
def test_simple_return_types():
@cudaq.kernel
def kernel(a: int, b: int) -> int:
return a * b
ret = kernel(2, 4)
assert ret == 8
@cudaq.kernel
def qernel(a: float, b: float) -> float:
return a * b
ret = kernel(2, 4)
assert np.isclose(ret, 8., atol=1e-12)
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel(a: int, b: int): # No return type
return a * b
@cudaq.kernel
def boolKernel() -> bool:
return True
assert boolKernel()
def test_list_creation():
N = 10
@cudaq.kernel
def kernel(N: int, idx: int) -> int:
myList = [i + 1 for i in range(N - 1)]
return myList[idx]
for i in range(N - 1):
assert kernel(N, i) == i + 1
@cudaq.kernel
def kernel2(N: int, i: int, j: int) -> int:
myList = [[k, k] for k in range(N)]
l = myList[i]
return l[j]
print(kernel2(5, 0, 0))
for i in range(N):
for j in range(2):
print(i, j, kernel2(N, i, j))
assert kernel2(N, i, j) == i
@cudaq.kernel
def kernel3(N: int):
myList = list(range(N))
q = cudaq.qvector(N)
for i in myList:
x(q[i])
print(kernel3)
counts = cudaq.sample(kernel3, 5)
assert len(counts) == 1
assert '1' * 5 in counts
@cudaq.kernel
def kernel4(myList: List[int]):
q = cudaq.qvector(len(myList))
casted = list(myList)
for i in casted:
x(q[i])
print(kernel4)
counts = cudaq.sample(kernel4, list(range(5)))
assert len(counts) == 1
assert '1' * 5 in counts
def test_list_creation_with_cast():
@cudaq.kernel
def kernel(myList: list[int]):
q = cudaq.qvector(len(myList))
casted = list(myList)
for i in casted:
x(q[i])
print(kernel)
counts = cudaq.sample(kernel, list(range(5)))
assert len(counts) == 1
assert '1' * 5 in counts
def test_list_creation_with_cast():
@cudaq.kernel
def kernel(myList: list[int]):
q = cudaq.qvector(len(myList))
casted = list(myList)
for i in casted:
x(q[i])
print(kernel)
counts = cudaq.sample(kernel, list(range(5)))
assert len(counts) == 1
assert '1' * 5 in counts
def test_list_boundaries():
@cudaq.kernel
def kernel1():
qubits = cudaq.qvector(2)
r = range(0, 0)
for i in r:
x(qubits[i])
counts = cudaq.sample(kernel1)
assert len(counts) == 1
assert '00' in counts
@cudaq.kernel
def kernel2():
qubits = cudaq.qvector(2)
r = range(1, 0)
for i in r:
x(qubits[i])
counts = cudaq.sample(kernel2)
assert len(counts) == 1
assert '00' in counts
@cudaq.kernel
def kernel3():
qubits = cudaq.qvector(2)
for i in range(-1):
x(qubits[i])
counts = cudaq.sample(kernel3)
assert len(counts) == 1
assert '00' in counts
@cudaq.kernel
def kernel4():
qubits = cudaq.qvector(4)
r = [i * 2 + 1 for i in range(1)]
for i in r:
x(qubits[i])
counts = cudaq.sample(kernel4)
assert len(counts) == 1
assert '0100' in counts
@cudaq.kernel
def kernel5():
qubits = cudaq.qvector(4)
r = [i * 2 + 1 for i in range(0)]
for i in r:
x(qubits[i])
counts = cudaq.sample(kernel5)
assert len(counts) == 1
assert '0000' in counts
@cudaq.kernel
def kernel6():
qubits = cudaq.qvector(4)
r = [i * 2 + 1 for i in range(2)]
for i in r:
x(qubits[i])
counts = cudaq.sample(kernel6)
assert len(counts) == 1
assert '0101' in counts
def test_array_value_assignment():
@cudaq.kernel()
def foo():
a = [1, 1]
b = [0, 0]
b[0] = a[0]
b[1] = a[1]
q0 = cudaq.qubit()
q1 = cudaq.qubit()
if (b[0]):
x(q0)
if (b[1]):
x(q1)
counts = cudaq.sample(foo)
assert "11" in counts
def test_control_operations():
@cudaq.kernel
def test():
q = cudaq.qvector(4)
x.ctrl(q[0], q[1])
cx(q[0], q[1])
print(test)
counts = cudaq.sample(test)
def test_control_operations():
@cudaq.kernel
def test(angle: float):
q = cudaq.qvector(4)
x.ctrl(q[0], q[1])
cx(q[0], q[1])
rx.ctrl(angle, q[0], q[1])
crx(angle, q[0], q[1])
print(test)
counts = cudaq.sample(test, 0.785398)
def test_bool_op_short_circuit():
@cudaq.kernel
def kernel():
qubits = cudaq.qvector(2)
h(qubits[0])
if mz(qubits[0]) and mz(qubits[1]):
x(qubits[1])
mz(qubits[1])
print(kernel)
counts = cudaq.sample(kernel)
counts.dump()
assert len(counts) == 2 and '10' in counts and '00' in counts
def test_sample_async_issue_args_processed():
@cudaq.kernel
def kernel(params: np.ndarray):
q = cudaq.qvector(2)
x(q[0])
ry(params[0], q[1])
x.ctrl(q[1], q[0])
params = np.array([.59])
result = cudaq.sample_async(kernel, params, qpu_id=0)
counts = result.get()
assert len(counts) == 2 and '01' in counts and '10' in counts
def test_capture_vars():
n = 5
f = 0.0
m = 5
hello = str()
@cudaq.kernel
def kernel():
q = cudaq.qvector(n)
x(q)
cudaq.dbg.ast.print_f64(f)
for qb in q:
rx(f, qb)
counts = cudaq.sample(kernel)
counts.dump()
assert '1' * n in counts
f = np.pi
counts = cudaq.sample(kernel)
counts.dump()
assert '0' * n in counts
n = 7
f = 0.0
counts = cudaq.sample(kernel)
counts.dump()
assert '1' * n in counts
counts = cudaq.sample(kernel)
counts.dump()
assert '1' * n in counts
n = 3
counts = cudaq.sample(kernel)
counts.dump()
assert '1' * n in counts
@cudaq.kernel
def testCanOnlyCaptureIntAndFloat():
i = hello
with pytest.raises(RuntimeError) as e:
testCanOnlyCaptureIntAndFloat()
b = True
@cudaq.kernel
def canCaptureBool():
q = cudaq.qubit()
if b:
x(q)
counts = cudaq.sample(canCaptureBool)
counts.dump()
assert len(counts) == 1 and '1' in counts
b = False
counts = cudaq.sample(canCaptureBool)
counts.dump()
assert len(counts) == 1 and '0' in counts
l = [.59]
li = [0, 1]
@cudaq.kernel
def canCaptureList():
q = cudaq.qvector(2)
firstIdx = li[0]
secondIdx = li[1]
x(q[firstIdx])
ry(l[firstIdx], q[secondIdx])
x.ctrl(q[secondIdx], q[firstIdx])
hamiltonian = 5.907 - 2.1433 * spin.x(0) * spin.x(1) - 2.1433 * spin.y(
0) * spin.y(1) + .21829 * spin.z(0) - 6.125 * spin.z(1)
assert np.isclose(-1.748,
cudaq.observe(canCaptureList, hamiltonian).expectation(),
atol=1e-3)
def test_capture_disallow_change_variable():
n = 3
@cudaq.kernel
def kernel() -> int:
if True:
cudaq.dbg.ast.print_i64(n)
# Change n, emits an error
n = 4
return n
with pytest.raises(RuntimeError) as e:
kernel()
def test_inner_function_capture():
n = 3
m = 5
def innerClassical():
@cudaq.kernel()
def foo():
q = cudaq.qvector(n)
def innerInnerClassical():
@cudaq.kernel()
def bar():
q = cudaq.qvector(m)
x(q)
return cudaq.sample(bar)
return cudaq.sample(foo), innerInnerClassical()
fooCounts, barCounts = innerClassical()
assert len(fooCounts) == 1 and '0' * n in fooCounts
assert len(barCounts) == 1 and '1' * m in barCounts
def test_error_qubit_constructor():
@cudaq.kernel
def test():
q = cudaq.qubit(10)
h(q[0])
with pytest.raises(RuntimeError) as e:
test.compile()
def test_swallow_measure_value():
@cudaq.kernel
def test():
data = cudaq.qvector(2)
ancilla = cudaq.qvector(2)
mz(ancilla)
x(data[1])
# The test here is that this compiles.
test.compile()
print(test)
def test_compare_with_true():
@cudaq.kernel
def test():
data = cudaq.qvector(2)
ancilla = cudaq.qvector(2)
results = mz(ancilla)
if results[0] == True:
x(data[0])
# The test here is that this compiles.
test()
def test_with_docstring():
@cudaq.kernel
def oracle(register: cudaq.qvector, auxillary_qubit: cudaq.qubit,
hidden_bitstring: List[int]):
"""
The inner-product oracle for the Bernstein-Vazirani algorithm.
"""
for index, bit in enumerate(hidden_bitstring):
if bit == 0:
# Apply identity operation to the qubit if it's
# in the 0-state.
# In this case, we do nothing.
pass
else:
# Otherwise, apply a `cx` gate with the current qubit as
# the control and the auxillary qubit as the target.
cx(register[index], auxillary_qubit)
@cudaq.kernel
def bernstein_vazirani(qubit_count: int, hidden_bitstring: List[int]):
"""
Returns a kernel implementing the Bernstein-Vazirani algorithm
for a random, hidden bitstring.
"""
# Allocate the specified number of qubits - this
# corresponds to the length of the hidden bitstring.
qubits = cudaq.qvector(qubit_count)
# Allocate an extra auxillary qubit.
auxillary_qubit = cudaq.qubit()
# Prepare the auxillary qubit.
h(auxillary_qubit)
z(auxillary_qubit)
# Place the rest of the register in a superposition state.
h(qubits)
# Query the oracle.
oracle(qubits, auxillary_qubit, hidden_bitstring)
# Apply another set of Hadamards to the register.
h(qubits)
# Apply measurement gates to just the `qubits`
# (excludes the auxillary qubit).
mz(qubits)
# Test here is that it compiles
bernstein_vazirani.compile()
def test_disallow_list_no_element_type():
@cudaq.kernel
def test(listVar: List):
pass
with pytest.raises(RuntimeError) as e:
print(test)
def test_invalid_cudaq_type():
@cudaq.kernel
def test():
q = cudaq.qreg(5)
h(q)
with pytest.raises(RuntimeError) as e:
print(test)
def test_bool_list_elements():
@cudaq.kernel
def kernel(var: list[bool]):
q = cudaq.qubit()
x(q)
if var[0]:
x(q)
counts = cudaq.sample(kernel, [False], shots_count=100)
assert '1' in counts and len(counts) == 1
counts = cudaq.sample(kernel, [True], shots_count=100)
assert '0' in counts and len(counts) == 1
def test_list_float_pass_list_int():
@cudaq.kernel
def test(var: List[float]):
q = cudaq.qvector(2)
cudaq.dbg.ast.print_f64(var[0])
x(q[int(var[0])])
x(q[int(var[1])])
var = [0, 1]
counts = cudaq.sample(test, var)
assert len(counts) == 1 and '11' in counts
counts.dump()
def test_cmpi_error_ints_different_widths():
@cudaq.kernel
def test():
q = cudaq.qubit()
i = mz(q)
if i == 1:
x(q)
test()
counts = cudaq.sample(test)
assert '0' in counts and len(counts) == 1
def test_aug_assign_add():
@cudaq.kernel