From 20b2e145e0da9778b8a3dfb745c5aa28ea94a7bb Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Sat, 13 Jul 2024 15:18:55 -0700 Subject: [PATCH] Recompile if array is captured --- python/cudaq/kernel/kernel_decorator.py | 6 +- python/tests/builder/test_kernel_builder.py | 20 +++ .../tests/kernel/test_kernel_qvector_init.py | 127 ++++++++++++++++-- 3 files changed, 144 insertions(+), 9 deletions(-) diff --git a/python/cudaq/kernel/kernel_decorator.py b/python/cudaq/kernel/kernel_decorator.py index 49c6f698f8..08482ff773 100644 --- a/python/cudaq/kernel/kernel_decorator.py +++ b/python/cudaq/kernel/kernel_decorator.py @@ -184,7 +184,11 @@ def compile(self): } if self.dependentCaptures != None: for k, v in self.dependentCaptures.items(): - if self.globalScopedVars[k] != v: + if (isinstance(v, (list, np.ndarray))): + # We can't detect a change in an array, always recompile. + self.module = None + break + elif self.globalScopedVars[k] != v: # Need to recompile self.module = None break diff --git a/python/tests/builder/test_kernel_builder.py b/python/tests/builder/test_kernel_builder.py index 68b27c1650..7cc347d4b9 100644 --- a/python/tests/builder/test_kernel_builder.py +++ b/python/tests/builder/test_kernel_builder.py @@ -883,6 +883,26 @@ def test_recursive_calls(): 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') diff --git a/python/tests/kernel/test_kernel_qvector_init.py b/python/tests/kernel/test_kernel_qvector_init.py index ddaeb6cc4d..4d25971a6e 100644 --- a/python/tests/kernel/test_kernel_qvector_init.py +++ b/python/tests/kernel/test_kernel_qvector_init.py @@ -19,6 +19,12 @@ reason='Could not find nvidia in installation') +def swap(arr, index1, index2): + t = arr[index2] + arr[index2] = arr[index1] + arr[index1] = t + + # float @skipIfNvidiaFP64NotInstalled def test_kernel_float_params_f64(): @@ -71,6 +77,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(f, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_float_capture_f32(): @@ -88,6 +101,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(f, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaFP64NotInstalled def test_kernel_float_np_array_from_capture_f64(): @@ -105,6 +125,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(f, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_float_np_array_from_capture_f32(): @@ -122,6 +149,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(f, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaFP64NotInstalled def test_kernel_float_definition_f64(): @@ -252,6 +286,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_complex_capture_f32(): @@ -269,6 +310,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaFP64NotInstalled def test_kernel_complex_np_array_from_capture_f64(): @@ -286,6 +334,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_complex_np_array_from_capture_f32(): @@ -303,6 +358,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaFP64NotInstalled def test_kernel_complex_definition_f64(): @@ -467,14 +529,21 @@ def test_kernel_amplitudes_complex_from_capture_f64(): c = [1. / np.sqrt(2.), 0., 0., 1. / np.sqrt(2.)] @cudaq.kernel - def kernel(vec: list[complex]): - q = cudaq.qvector(cudaq.amplitudes(vec)) + def kernel(): + q = cudaq.qvector(cudaq.amplitudes(c)) - counts = cudaq.sample(kernel, c) + counts = cudaq.sample(kernel) print(counts) assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_amplitudes_complex_from_capture_f32(): @@ -484,14 +553,21 @@ def test_kernel_amplitudes_complex_from_capture_f32(): c = [1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)] @cudaq.kernel - def kernel(vec: list[complex]): - q = cudaq.qvector(cudaq.amplitudes(vec)) + def kernel(): + q = cudaq.qvector(cudaq.amplitudes(c)) - counts = cudaq.sample(kernel, c) + counts = cudaq.sample(kernel) print(counts) assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaFP64NotInstalled def test_kernel_simulation_dtype_np_array_from_capture_f64(): @@ -509,6 +585,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_simulation_dtype_np_array_from_capture_f32(): @@ -526,6 +609,13 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaFP64NotInstalled def test_kernel_simulation_dtype_np_array_capture_f64(): @@ -533,7 +623,6 @@ def test_kernel_simulation_dtype_np_array_capture_f64(): cudaq.set_target('nvidia-fp64') c = [1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)] - state = np.array(c, dtype=cudaq.complex()) @cudaq.kernel @@ -545,6 +634,14 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + state = np.array(c, dtype=cudaq.complex()) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + @skipIfNvidiaNotInstalled def test_kernel_simulation_dtype_np_array_capture_f32(): @@ -552,7 +649,6 @@ def test_kernel_simulation_dtype_np_array_capture_f32(): cudaq.set_target('nvidia') c = [1. / np.sqrt(2.) + 0j, 0., 0., 1. / np.sqrt(2.)] - state = np.array(c, dtype=cudaq.complex()) @cudaq.kernel @@ -564,6 +660,14 @@ def kernel(): assert '11' in counts assert '00' in counts + swap(c, 1, 3) + state = np.array(c, dtype=cudaq.complex()) + + counts = cudaq.sample(kernel) + print(counts) + assert '10' in counts + assert '00' in counts + # test errors @@ -656,6 +760,13 @@ def kernel(): assert not '01' in counts assert '00' in counts + n = 1 + + counts = cudaq.sample(kernel) + print(counts) + assert not '1' in counts + assert '0' in counts + def test_kernel_qvector_init_from_int():