Skip to content

Commit

Permalink
Recompile if array is captured
Browse files Browse the repository at this point in the history
  • Loading branch information
annagrin committed Jul 13, 2024
1 parent 0f7a56b commit 20b2e14
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
6 changes: 5 additions & 1 deletion python/cudaq/kernel/kernel_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions python/tests/builder/test_kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
127 changes: 119 additions & 8 deletions python/tests/kernel/test_kernel_qvector_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -526,14 +609,20 @@ 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():
cudaq.reset_target()
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
Expand All @@ -545,14 +634,21 @@ 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():
cudaq.reset_target()
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
Expand All @@ -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

Expand Down Expand Up @@ -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():

Expand Down

0 comments on commit 20b2e14

Please sign in to comment.