From 69ff3bd9358fb7bb9b62e460efd2be9cd7ed7cdf Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Fri, 1 Nov 2024 15:23:24 -0700 Subject: [PATCH 1/2] Fix incorrect vector of boolean argument passing in python --- python/tests/kernel/test_kernel_features.py | 88 +++++++++++++++------ python/utils/OpaqueArguments.h | 4 +- 2 files changed, 66 insertions(+), 26 deletions(-) diff --git a/python/tests/kernel/test_kernel_features.py b/python/tests/kernel/test_kernel_features.py index 3bd822c200..cbef332301 100644 --- a/python/tests/kernel/test_kernel_features.py +++ b/python/tests/kernel/test_kernel_features.py @@ -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) @@ -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) @@ -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]): @@ -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 @@ -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(): @@ -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: @@ -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) diff --git a/python/utils/OpaqueArguments.h b/python/utils/OpaqueArguments.h index 3e410a07b6..c786859be9 100644 --- a/python/utils/OpaqueArguments.h +++ b/python/utils/OpaqueArguments.h @@ -393,11 +393,11 @@ inline void packArgs(OpaqueArguments &argData, py::args args, .Case([&](IntegerType type) { // Handle vec and vec if (type.getIntOrFloatBitWidth() == 1) { - genericVecAllocator.template operator()( + genericVecAllocator.template operator()( [](py::handle element, int index, int elementIndex) { checkListElementType(element, index, elementIndex); - return element.cast(); + return static_cast(element.cast()); }); return; } From ea3ced2d6075c7af038d62402fb3f19897576343 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Fri, 1 Nov 2024 15:32:19 -0700 Subject: [PATCH 2/2] DCO Remediation Commit for Anna Gringauze I, Anna Gringauze , hereby add my Signed-off-by to this commit: 69ff3bd9358fb7bb9b62e460efd2be9cd7ed7cdf Signed-off-by: Anna Gringauze --- python/tests/kernel/test_kernel_features.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/python/tests/kernel/test_kernel_features.py b/python/tests/kernel/test_kernel_features.py index cbef332301..b01532f76d 100644 --- a/python/tests/kernel/test_kernel_features.py +++ b/python/tests/kernel/test_kernel_features.py @@ -880,24 +880,19 @@ def kernel(n: int, bools: list[bool]): x(q[j]) counts = cudaq.sample(kernel, 2, [True, True]) - assert "00" in counts - assert len(counts) == 1 + assert "00" in counts and len(counts) == 1 counts = cudaq.sample(kernel, 2, [False, True]) - assert "10" in counts - assert len(counts) == 1 + assert "10" in counts and len(counts) == 1 counts = cudaq.sample(kernel, 2, [True, False]) - assert "01" in counts - assert len(counts) == 1 + assert "01" in counts and len(counts) == 1 counts = cudaq.sample(kernel, 2, [False, False]) - assert "11" in counts - assert len(counts) == 1 + assert "11" in counts and len(counts) == 1 counts = cudaq.sample(kernel, 5, [True, True, False, True, True]) - assert "00100" in counts - assert len(counts) == 1 + assert "00100" in counts and len(counts) == 1 def test_list_float_pass_list_int():