Skip to content

Commit

Permalink
Fix incorrect vector of boolean argument passing in python
Browse files Browse the repository at this point in the history
  • Loading branch information
annagrin committed Nov 1, 2024
1 parent a9e2324 commit 69ff3bd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
88 changes: 64 additions & 24 deletions python/tests/kernel/test_kernel_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ def grover(N: int, M: int, oracle: Callable[[cudaq.qview], None]):
def test_pauli_word_input():

h2_data = [
3, 1, 1, 3, 0.0454063, 0, 2, 0, 0, 0, 0.17028, 0, 0, 0, 2, 0,
-0.220041, -0, 1, 3, 3, 1, 0.0454063, 0, 0, 0, 0, 0, -0.106477, 0, 0,
2, 0, 0, 0.17028, 0, 0, 0, 0, 2, -0.220041, -0, 3, 3, 1, 1, -0.0454063,
-0, 2, 2, 0, 0, 0.168336, 0, 2, 0, 2, 0, 0.1202, 0, 0, 2, 0, 2, 0.1202,
0, 2, 0, 0, 2, 0.165607, 0, 0, 2, 2, 0, 0.165607, 0, 0, 0, 2, 2,
0.174073, 0, 1, 1, 3, 3, -0.0454063, -0, 15
3, 1, 1, 3, 0.0454063, 0, 2, 0, 0, 0, 0.17028, 0, 0, 0, 2, 0, -0.220041,
-0, 1, 3, 3, 1, 0.0454063, 0, 0, 0, 0, 0, -0.106477, 0, 0, 2, 0, 0,
0.17028, 0, 0, 0, 0, 2, -0.220041, -0, 3, 3, 1, 1, -0.0454063, -0, 2, 2,
0, 0, 0.168336, 0, 2, 0, 2, 0, 0.1202, 0, 0, 2, 0, 2, 0.1202, 0, 2, 0,
0, 2, 0.165607, 0, 0, 2, 2, 0, 0.165607, 0, 0, 0, 2, 2, 0.174073, 0, 1,
1, 3, 3, -0.0454063, -0, 15
]
h = cudaq.SpinOperator(h2_data, 4)

Expand Down Expand Up @@ -236,12 +236,12 @@ def test(theta: float, paulis: list[cudaq.pauli_word]):

def test_exp_pauli():
h2_data = [
3, 1, 1, 3, 0.0454063, 0, 2, 0, 0, 0, 0.17028, 0, 0, 0, 2, 0,
-0.220041, -0, 1, 3, 3, 1, 0.0454063, 0, 0, 0, 0, 0, -0.106477, 0, 0,
2, 0, 0, 0.17028, 0, 0, 0, 0, 2, -0.220041, -0, 3, 3, 1, 1, -0.0454063,
-0, 2, 2, 0, 0, 0.168336, 0, 2, 0, 2, 0, 0.1202, 0, 0, 2, 0, 2, 0.1202,
0, 2, 0, 0, 2, 0.165607, 0, 0, 2, 2, 0, 0.165607, 0, 0, 0, 2, 2,
0.174073, 0, 1, 1, 3, 3, -0.0454063, -0, 15
3, 1, 1, 3, 0.0454063, 0, 2, 0, 0, 0, 0.17028, 0, 0, 0, 2, 0, -0.220041,
-0, 1, 3, 3, 1, 0.0454063, 0, 0, 0, 0, 0, -0.106477, 0, 0, 2, 0, 0,
0.17028, 0, 0, 0, 0, 2, -0.220041, -0, 3, 3, 1, 1, -0.0454063, -0, 2, 2,
0, 0, 0.168336, 0, 2, 0, 2, 0, 0.1202, 0, 0, 2, 0, 2, 0.1202, 0, 2, 0,
0, 2, 0.165607, 0, 0, 2, 2, 0, 0.165607, 0, 0, 0, 2, 2, 0.174073, 0, 1,
1, 3, 3, -0.0454063, -0, 15
]
h = cudaq.SpinOperator(h2_data, 4)

Expand Down 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,38 @@ 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
assert len(counts) == 1

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

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

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

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


def test_list_float_pass_list_int():

@cudaq.kernel
Expand Down Expand Up @@ -919,12 +951,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 Expand Up @@ -1764,15 +1798,17 @@ def test() -> T:

test()


def test_disallow_recursive_quantum_struct():
from dataclasses import dataclass

@dataclass
class T:
q: cudaq.qview

@dataclass
class Holder:
t : T
t: T

with pytest.raises(RuntimeError) as e:

Expand All @@ -1783,37 +1819,41 @@ def test():
hh = Holder(t)

print(test)

with pytest.raises(RuntimeError) as e:

@cudaq.kernel
def test(hh : Holder):
def test(hh: Holder):
pass

print(test)


def test_disallow_struct_with_methods():
from dataclasses import dataclass

@dataclass
class T:
q: cudaq.qview

def doSomething(self):
pass
pass

with pytest.raises(RuntimeError) as e:

@cudaq.kernel
def test(t : T):
pass
def test(t: T):
pass

print(test)

with pytest.raises(RuntimeError) as e:

@cudaq.kernel
@cudaq.kernel
def test():
q = cudaq.qvector(2)
t = T(q)

print(test)


Expand Down
4 changes: 2 additions & 2 deletions python/utils/OpaqueArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,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>(
[](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

0 comments on commit 69ff3bd

Please sign in to comment.