forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_custom_operations.py
More file actions
412 lines (298 loc) · 10.7 KB
/
test_custom_operations.py
File metadata and controls
412 lines (298 loc) · 10.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
# ============================================================================ #
# Copyright (c) 2022 - 2026 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 pytest
import numpy as np
import cudaq
swap_matrix = np.array([1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
dtype=complex)
@pytest.fixture(autouse=True)
def reset_and_run():
cudaq.reset_target()
yield
## Ref: https://github.com/NVIDIA/cuda-quantum/issues/1954
# cudaq.__clearKernelRegistries()
def check_bell(entity):
"""Helper function to encapsulate checks for Bell pair"""
counts = cudaq.sample(entity, shots_count=100)
counts.dump()
assert len(counts) == 2
assert '00' in counts and '11' in counts
def test_basic():
"""
Showcase user-level APIs of how to
(a) define a custom operation using unitary,
(b) how to use it in kernel,
(c) express controlled custom operation
"""
cudaq.register_operation("custom_h",
1. / np.sqrt(2.) * np.array([1, 1, 1, -1]))
cudaq.register_operation("custom_x", np.array([0, 1, 1, 0]))
@cudaq.kernel
def bell():
qubits = cudaq.qvector(2)
custom_h(qubits[0])
custom_x.ctrl(qubits[0], qubits[1])
check_bell(bell)
def test_cnot_gate():
"""Test CNOT gate"""
cudaq.register_operation(
"custom_cnot",
np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0]))
@cudaq.kernel
def bell_pair():
qubits = cudaq.qvector(2)
h(qubits[0])
custom_cnot(qubits[0], qubits[1])
check_bell(bell_pair)
def test_cz_gate():
"""Test 2-qubit custom operation replicating CZ gate."""
cudaq.register_operation(
"custom_cz", np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
-1]))
@cudaq.kernel
def ctrl_z_kernel():
qubits = cudaq.qvector(5)
controls = cudaq.qvector(2)
custom_cz(qubits[1], qubits[0])
x(qubits[2])
custom_cz(qubits[3], qubits[2])
x(controls)
counts = cudaq.sample(ctrl_z_kernel)
assert counts["0010011"] == 1000
def test_three_qubit_op():
"""Test three-qubit operation replicating Toffoli gate."""
cudaq.register_operation(
"toffoli",
np.array([
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0
]))
@cudaq.kernel
def test_toffoli():
q = cudaq.qvector(3)
x(q)
toffoli(q[0], q[1], q[2])
counts = cudaq.sample(test_toffoli)
print(counts)
assert counts["110"] == 1000
# NOTE: Ref - https://github.com/NVIDIA/cuda-quantum/issues/1925
@pytest.mark.parametrize("target", [
'density-matrix-cpu', 'nvidia', 'nvidia-fp64', 'nvidia-mqpu',
'nvidia-mqpu-fp64', 'qpp-cpu'
])
def test_simulators(target):
"""Test simulation of custom operation on all available simulation targets."""
def can_set_target(name):
target_installed = True
try:
cudaq.set_target(name)
except RuntimeError:
target_installed = False
return target_installed
if can_set_target(target):
test_basic()
test_cnot_gate()
test_three_qubit_op()
cudaq.reset_target()
else:
pytest.skip("target not available")
cudaq.reset_target()
def test_custom_adjoint():
"""Test that adjoint can be called on custom operations."""
cudaq.register_operation("custom_s", np.array([1, 0, 0, 1j]))
cudaq.register_operation("custom_s_adj", np.array([1, 0, 0, -1j]))
@cudaq.kernel
def kernel():
q = cudaq.qubit()
h(q)
custom_s.adj(q)
custom_s_adj(q)
h(q)
counts = cudaq.sample(kernel)
counts.dump()
assert counts["1"] == 1000
def test_incorrect_matrix():
"""Incorrectly sized matrix raises error."""
with pytest.raises(RuntimeError) as error:
cudaq.register_operation("foo", [])
assert "invalid matrix size" in repr(error)
with pytest.raises(RuntimeError) as error:
cudaq.register_operation("bar", [1, 0])
assert "invalid matrix size" in repr(error)
with pytest.raises(RuntimeError) as error:
cudaq.register_operation("baz", np.array([[1, 0, 0, 0], [1, 0, 0, 1]]))
assert "invalid matrix size" in repr(error)
with pytest.raises(RuntimeError) as error:
cudaq.register_operation("qux",
np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
assert "invalid matrix size" in repr(error)
def test_bad_attribute():
"""Test that unsupported attributes on custom operations raise error."""
cudaq.register_operation("custom_s", np.array([1, 0, 0, 1j]))
with pytest.raises(Exception) as error:
@cudaq.kernel
def kernel():
q = cudaq.qubit()
custom_s.foo(q)
mz(q)
cudaq.sample(kernel)
def test_builder_mode():
"""Builder-mode API"""
kernel = cudaq.make_kernel()
cudaq.register_operation("custom_h",
1. / np.sqrt(2.) * np.array([1, 1, 1, -1]))
qubits = kernel.qalloc(2)
kernel.custom_h(qubits[0])
kernel.cx(qubits[0], qubits[1])
check_bell(kernel)
def test_builder_mode_control():
"""Controlled operation in builder-mode"""
kernel = cudaq.make_kernel()
cudaq.register_operation("custom_x", np.array([0, 1, 1, 0]))
qubits = kernel.qalloc(2)
kernel.h(qubits[0])
kernel.custom_x(qubits[0], qubits[1])
check_bell(kernel)
def test_invalid_ctrl():
cudaq.register_operation("custom_x", np.array([0, 1, 1, 0]))
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def bell():
q = cudaq.qubit()
custom_x.ctrl(q)
bell.compile()
assert 'missing value' in repr(error)
def test_individual_qubit_refs():
"""custom_swap(q0, q1)"""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel():
qvec = cudaq.qvector(2)
x(qvec[0])
custom_swap(qvec[0], qvec[1])
counts = cudaq.sample(kernel)
assert counts.most_probable() == "01"
def test_qvector_direct():
"""custom_swap(qvec)"""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel():
qvec = cudaq.qvector(2)
x(qvec[0])
custom_swap(qvec)
counts = cudaq.sample(kernel)
assert counts.most_probable() == "01"
def test_starred_qvector():
"""custom_swap(*qvec)"""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel():
qvec = cudaq.qvector(2)
x(qvec[0])
custom_swap(*qvec)
counts = cudaq.sample(kernel)
assert counts.most_probable() == "01"
def test_mixed_starred_qvec_and_qref():
"""custom_swap(*qvec, qbit)"""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel():
qvec = cudaq.qvector(1)
qbit = cudaq.qubit()
x(qvec[0])
custom_swap(*qvec, qbit)
counts = cudaq.sample(kernel)
assert counts.most_probable() == "01"
def test_unstarred_qvec_and_qref():
"""custom_swap(qvec, qbit)"""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel():
qvec = cudaq.qvector(1)
qbit = cudaq.qubit()
x(qvec[0])
custom_swap(qvec, qbit)
counts = cudaq.sample(kernel)
assert counts.most_probable() == "01"
def test_too_few_qubits_raises_error():
"""custom_swap with only 1 qubit when 2 are required"""
cudaq.register_operation("custom_swap", swap_matrix)
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel():
qbit = cudaq.qubit()
custom_swap(qbit)
kernel.compile()
assert 'custom operation requires 2 qubit target(s), but 1 were provided' in repr(
error)
def test_too_many_qubits_raises_error():
"""custom_swap with 3 qubits when 2 are required"""
cudaq.register_operation("custom_swap", swap_matrix)
with pytest.raises(RuntimeError) as error:
@cudaq.kernel
def kernel():
q1 = cudaq.qubit()
q2 = cudaq.qubit()
q3 = cudaq.qubit()
custom_swap(q1, q2, q3)
kernel.compile()
assert 'custom operation requires 2 qubit target(s), but 3 were provided' in repr(
error)
def test_unknown_veq_size_correct_count():
"""custom_swap(*qvec), qvec size is a runtime parameter"""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel(n: int):
qvec = cudaq.qvector(n)
x(qvec[0])
custom_swap(*qvec)
counts = cudaq.sample(kernel, 2)
assert counts.most_probable() == "01"
@pytest.mark.skip_macos_arm64_jit
def test_unknown_veq_size_incorrect_count():
"""custom_swap(*qvec), qvec has more qubits than the operation requires."""
cudaq.register_operation("custom_swap", swap_matrix)
@cudaq.kernel
def kernel(n: int):
qvec = cudaq.qvector(n)
x(qvec[0])
custom_swap(*qvec)
with pytest.raises(RuntimeError) as error:
cudaq.sample(kernel, 3)
assert 'custom operation requires 2 qubit target(s), but 3 were provided' in repr(
error)
def test_nested_kernel_single_qubit():
"""Regression test for issue #2485: custom op in a nested kernel on a single qubit."""
cudaq.register_operation("custom_x_nested", np.array([0, 1, 1, 0]))
@cudaq.kernel
def inner(q: cudaq.qubit):
custom_x_nested(q)
@cudaq.kernel
def outer():
q = cudaq.qubit()
inner(q)
counts = cudaq.sample(outer, shots_count=100)
assert counts["1"] == 100
def test_nested_kernel_qview():
"""Regression test for issue #2485: custom op in a nested kernel on a qview."""
cudaq.register_operation("custom_x_qview", np.array([0, 1, 1, 0]))
@cudaq.kernel
def inner(qubits: cudaq.qview):
for i in range(len(qubits)):
custom_x_qview(qubits[i])
@cudaq.kernel
def outer():
qubits = cudaq.qvector(2)
inner(qubits)
counts = cudaq.sample(outer, shots_count=100)
assert counts["11"] == 100
# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
pytest.main([loc, "-rP"])