Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing lists of boolean values to kernels in python. #2349

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions python/tests/kernel/test_kernel_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ def test():
print(test)


def test_bool_list_elements():
def test_bool_list_element():

@cudaq.kernel
def kernel(var: list[bool]):
Expand All @@ -868,6 +868,33 @@ def kernel(var: list[bool]):
assert '0' in counts and len(counts) == 1


def test_list_bool_elements():
import cudaq

@cudaq.kernel
def kernel(n: int, bools: list[bool]):
q = cudaq.qvector(n)
for j in range(n):
b = bools[j]
if b == False:
x(q[j])

counts = cudaq.sample(kernel, 2, [True, True])
assert "00" in counts and len(counts) == 1

counts = cudaq.sample(kernel, 2, [False, True])
assert "10" in counts and len(counts) == 1

counts = cudaq.sample(kernel, 2, [True, False])
assert "01" in counts and len(counts) == 1

counts = cudaq.sample(kernel, 2, [False, False])
assert "11" in counts and len(counts) == 1

counts = cudaq.sample(kernel, 5, [True, True, False, True, True])
assert "00100" in counts and len(counts) == 1


def test_list_float_pass_list_int():

@cudaq.kernel
Expand Down Expand Up @@ -919,12 +946,14 @@ def test2() -> int:
def test_empty_lists():

@cudaq.kernel
def empty(var: list[cudaq.pauli_word], varvar: list[float],
varvarvar: list[bool]):
def empty(a: list[cudaq.pauli_word], b: list[bool], c: list[int],
d: list[float], e: list[complex]) -> int:

q = cudaq.qvector(2)
x(q[0])
return len(a) + len(b) + len(c) + len(d) + len(e)

empty([], [], [])
assert empty([], [], [], [], []) == 0


def test_no_valueerror_np_array():
Expand Down
4 changes: 2 additions & 2 deletions python/utils/OpaqueArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ inline void packArgs(OpaqueArguments &argData, py::args args,
.Case([&](IntegerType type) {
// Handle vec<bool> and vec<int>
if (type.getIntOrFloatBitWidth() == 1) {
genericVecAllocator.template operator()<bool>(
genericVecAllocator.template operator()<char>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required if we merge #2338 ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to this file can be dropped AFAICT.

It would be great to add the extended tests though.

[](py::handle element, int index, int elementIndex) {
checkListElementType<py::bool_>(element, index,
elementIndex);
return element.cast<bool>();
return static_cast<char>(element.cast<bool>());
});
return;
}
Expand Down
Loading