Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ae9a6c6

Browse files
committedFeb 19, 2025··
* Added destructor for PyKernelDecorator which in turn calss the
destructor for CapturedDataStorage * Renamed duplicate kernels - need to investigate if this points to an underlying bug. Without this, when running the full test suite, we get error like - `RuntimeError: python alt_launch_kernel failed to get set cudaq state function for kernel: kernel` All tests run successfully standalone. Signed-off-by: Pradnya Khalate <[email protected]>
1 parent c12d2d3 commit ae9a6c6

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed
 

‎python/cudaq/kernel/kernel_decorator.py

+5
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ def __call__(self, *args):
524524
# self.capturedDataStorage = None
525525
return result
526526

527+
def __del__(self):
528+
if self.capturedDataStorage != None:
529+
self.capturedDataStorage.__del__()
530+
self.capturedDataStorage = None
531+
527532

528533
def kernel(function=None, **kwargs):
529534
"""

‎python/tests/builder/test_qalloc_init_state.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ def test_kernel_complex_capture_f64():
166166
def kernel():
167167
q = cudaq.qvector(state)
168168

169-
# kernel.compile()
170169
counts = cudaq.sample(kernel)
171170
print(counts)
172171
assert '11' in counts
@@ -215,11 +214,10 @@ def test_kernel_complex64_capture_f32():
215214
state = cudaq.State.from_data(c)
216215

217216
@cudaq.kernel
218-
def kernel():
217+
def kernel_foo():
219218
q = cudaq.qvector(state)
220219

221-
# kernel.compile()
222-
counts = cudaq.sample(kernel)
220+
counts = cudaq.sample(kernel_foo)
223221
print(counts)
224222
assert '11' in counts
225223
assert '00' in counts

‎python/tests/kernel/test_kernel_qvector_state_init.py

+44-44
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818
not (cudaq.num_available_gpus() > 0 and cudaq.has_target('nvidia')),
1919
reason='Could not find nvidia in installation')
2020

21+
22+
@pytest.fixture(autouse=True)
23+
def do_something():
24+
cudaq.reset_target()
25+
yield
26+
cudaq.reset_target()
27+
cudaq.__clearKernelRegistries()
28+
29+
2130
# synthesis
2231

2332

2433
def test_kernel_synthesis_complex():
25-
cudaq.reset_target()
2634

2735
c = np.array([1. / np.sqrt(2.) + 0j, 1. / np.sqrt(2.), 0., 0.],
2836
dtype=cudaq.complex())
@@ -50,7 +58,6 @@ def kernel(vec: cudaq.State):
5058
@skipIfNvidiaFP64NotInstalled
5159
def test_kernel_float_params_f64():
5260

53-
cudaq.reset_target()
5461
cudaq.set_target('nvidia', option='fp64')
5562

5663
f = np.array([1. / np.sqrt(2.), 0., 0., 1. / np.sqrt(2.)], dtype=float)
@@ -61,10 +68,9 @@ def test_kernel_float_params_f64():
6168
e)
6269

6370

64-
@skipIfNvidiaFP64NotInstalled
71+
@skipIfNvidiaNotInstalled
6572
def test_kernel_float_params_f32():
6673

67-
cudaq.reset_target()
6874
cudaq.set_target('nvidia')
6975

7076
f = np.array([1. / np.sqrt(2.), 0., 0., 1. / np.sqrt(2.)], dtype=np.float32)
@@ -80,7 +86,7 @@ def test_kernel_float_params_f32():
8086

8187
@skipIfNvidiaFP64NotInstalled
8288
def test_kernel_complex_params_f64():
83-
cudaq.reset_target()
89+
8490
cudaq.set_target('nvidia', option='fp64')
8591

8692
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -99,7 +105,7 @@ def kernel(vec: cudaq.State):
99105

100106
@skipIfNvidiaFP64NotInstalled
101107
def test_kernel_complex128_params_f64():
102-
cudaq.reset_target()
108+
103109
cudaq.set_target('nvidia', option='fp64')
104110

105111
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -118,7 +124,7 @@ def kernel(vec: cudaq.State):
118124

119125
@skipIfNvidiaFP64NotInstalled
120126
def test_kernel_complex64_params_f64():
121-
cudaq.reset_target()
127+
122128
cudaq.set_target('nvidia', option='fp64')
123129

124130
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -131,7 +137,7 @@ def test_kernel_complex64_params_f64():
131137

132138
@skipIfNvidiaNotInstalled
133139
def test_kernel_complex64_params_f32():
134-
cudaq.reset_target()
140+
135141
cudaq.set_target('nvidia')
136142

137143
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -150,7 +156,7 @@ def kernel(vec: cudaq.State):
150156

151157
@skipIfNvidiaNotInstalled
152158
def test_kernel_complex128_params_f32():
153-
cudaq.reset_target()
159+
154160
cudaq.set_target('nvidia')
155161

156162
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -163,7 +169,7 @@ def test_kernel_complex128_params_f32():
163169

164170
@skipIfNvidiaNotInstalled
165171
def test_kernel_complex_params_f32():
166-
cudaq.reset_target()
172+
167173
cudaq.set_target('nvidia')
168174

169175
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -176,7 +182,7 @@ def test_kernel_complex_params_f32():
176182

177183
@skipIfNvidiaFP64NotInstalled
178184
def test_kernel_complex_capture_f64():
179-
cudaq.reset_target()
185+
180186
cudaq.set_target('nvidia', option='fp64')
181187

182188
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -187,7 +193,6 @@ def test_kernel_complex_capture_f64():
187193
def kernel():
188194
q = cudaq.qvector(state)
189195

190-
# kernel.compile()
191196
counts = cudaq.sample(kernel)
192197
print(counts)
193198
assert '11' in counts
@@ -196,7 +201,7 @@ def kernel():
196201

197202
@skipIfNvidiaFP64NotInstalled
198203
def test_kernel_complex128_capture_f64():
199-
cudaq.reset_target()
204+
200205
cudaq.set_target('nvidia', option='fp64')
201206

202207
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -215,7 +220,7 @@ def kernel():
215220

216221
@skipIfNvidiaFP64NotInstalled
217222
def test_kernel_complex128_capture_f64():
218-
cudaq.reset_target()
223+
219224
cudaq.set_target('nvidia', option='fp64')
220225

221226
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -228,27 +233,26 @@ def test_kernel_complex128_capture_f64():
228233

229234
@skipIfNvidiaNotInstalled
230235
def test_kernel_complex64_capture_f32():
231-
cudaq.reset_target()
236+
232237
cudaq.set_target('nvidia')
233238

234239
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
235240
dtype=np.complex64)
236241
state = cudaq.State.from_data(c)
237242

238243
@cudaq.kernel
239-
def kernel():
244+
def kernel_foo():
240245
q = cudaq.qvector(state)
241246

242-
# kernel.compile()
243-
counts = cudaq.sample(kernel)
247+
counts = cudaq.sample(kernel_foo)
244248
print(counts)
245249
assert '11' in counts
246250
assert '00' in counts
247251

248252

249253
@skipIfNvidiaNotInstalled
250254
def test_kernel_complex128_capture_f32():
251-
cudaq.reset_target()
255+
252256
cudaq.set_target('nvidia')
253257

254258
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -261,7 +265,7 @@ def test_kernel_complex128_capture_f32():
261265

262266
@skipIfNvidiaNotInstalled
263267
def test_kernel_complex_capture_f32():
264-
cudaq.reset_target()
268+
265269
cudaq.set_target('nvidia')
266270

267271
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
@@ -277,77 +281,75 @@ def test_kernel_complex_capture_f32():
277281

278282
@skipIfNvidiaFP64NotInstalled
279283
def test_kernel_simulation_dtype_complex_params_f64():
280-
cudaq.reset_target()
284+
281285
cudaq.set_target('nvidia', option='fp64')
282286

283287
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
284288
dtype=cudaq.complex())
285289
state = cudaq.State.from_data(c)
286290

287291
@cudaq.kernel
288-
def kernel(vec: cudaq.State):
292+
def kernel_bar(vec: cudaq.State):
289293
q = cudaq.qvector(vec)
290294

291-
counts = cudaq.sample(kernel, state)
295+
counts = cudaq.sample(kernel_bar, state)
292296
print(counts)
293297
assert '11' in counts
294298
assert '00' in counts
295299

296300

297301
@skipIfNvidiaNotInstalled
298302
def test_kernel_simulation_dtype_complex_params_f32():
299-
cudaq.reset_target()
303+
300304
cudaq.set_target('nvidia')
301305

302306
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
303307
dtype=cudaq.complex())
304308
state = cudaq.State.from_data(c)
305309

306310
@cudaq.kernel
307-
def kernel(vec: cudaq.State):
311+
def kernel_baz(vec: cudaq.State):
308312
q = cudaq.qvector(vec)
309313

310-
counts = cudaq.sample(kernel, state)
314+
counts = cudaq.sample(kernel_baz, state)
311315
print(counts)
312316
assert '11' in counts
313317
assert '00' in counts
314318

315319

316320
@skipIfNvidiaFP64NotInstalled
317321
def test_kernel_simulation_dtype_capture_f64():
318-
cudaq.reset_target()
322+
319323
cudaq.set_target('nvidia', option='fp64')
320324

321325
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
322326
dtype=cudaq.complex())
323327
state = cudaq.State.from_data(c)
324328

325329
@cudaq.kernel
326-
def kernel():
330+
def kernel_qux():
327331
q = cudaq.qvector(state)
328332

329-
# kernel.compile()
330-
counts = cudaq.sample(kernel)
333+
counts = cudaq.sample(kernel_qux)
331334
print(counts)
332335
assert '11' in counts
333336
assert '00' in counts
334337

335338

336339
@skipIfNvidiaNotInstalled
337340
def test_kernel_simulation_dtype_capture_f32():
338-
cudaq.reset_target()
341+
339342
cudaq.set_target('nvidia')
340343

341344
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
342345
dtype=cudaq.complex())
343346
state = cudaq.State.from_data(c)
344347

345348
@cudaq.kernel
346-
def kernel():
349+
def kernel_corge():
347350
q = cudaq.qvector(state)
348351

349-
# kernel.compile()
350-
counts = cudaq.sample(kernel)
352+
counts = cudaq.sample(kernel_corge)
351353
print(counts)
352354
assert '11' in counts
353355
assert '00' in counts
@@ -358,7 +360,7 @@ def kernel():
358360

359361
@skipIfNvidiaFP64NotInstalled
360362
def test_init_from_other_kernel_state_f64():
361-
cudaq.reset_target()
363+
362364
cudaq.set_target('nvidia', option='fp64')
363365

364366
@cudaq.kernel
@@ -371,13 +373,13 @@ def bell():
371373
state.dump()
372374

373375
@cudaq.kernel
374-
def kernel(initialState: cudaq.State):
376+
def kernel_waldo(initialState: cudaq.State):
375377
qubits = cudaq.qvector(initialState)
376378

377-
state2 = cudaq.get_state(kernel, state)
379+
state2 = cudaq.get_state(kernel_waldo, state)
378380
state2.dump()
379381

380-
counts = cudaq.sample(kernel, state)
382+
counts = cudaq.sample(kernel_waldo, state)
381383
print(counts)
382384
assert '11' in counts
383385
assert '00' in counts
@@ -387,7 +389,7 @@ def kernel(initialState: cudaq.State):
387389

388390
@skipIfNvidiaFP64NotInstalled
389391
def test_init_from_other_kernel_state_f32():
390-
cudaq.reset_target()
392+
391393
cudaq.set_target('nvidia')
392394

393395
@cudaq.kernel
@@ -400,13 +402,13 @@ def bell():
400402
state.dump()
401403

402404
@cudaq.kernel
403-
def kernel(initialState: cudaq.State):
405+
def kernel_thud(initialState: cudaq.State):
404406
qubits = cudaq.qvector(initialState)
405407

406-
state2 = cudaq.get_state(kernel, state)
408+
state2 = cudaq.get_state(kernel_thud, state)
407409
state2.dump()
408410

409-
counts = cudaq.sample(kernel, state)
411+
counts = cudaq.sample(kernel_thud, state)
410412
print(counts)
411413
assert '11' in counts
412414
assert '00' in counts
@@ -434,7 +436,6 @@ def kernel2():
434436

435437
kernel2()
436438

437-
# kernel0.compile()
438439
counts = cudaq.sample(kernel0)
439440
print(counts)
440441
assert '1111' in counts
@@ -444,7 +445,6 @@ def kernel2():
444445

445446

446447
def test_invalid_arg_error_msg():
447-
cudaq.reset_target()
448448

449449
c = np.array([1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)],
450450
dtype=complex)

0 commit comments

Comments
 (0)
Please sign in to comment.