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 Python handling of kernel list parameters #1410

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ jobs:
strategy:
matrix:
platform: [amd64, arm64]
python_version: ['3.9', '3.11']
python_version: ['3.8', '3.11']
fail-fast: false
uses: ./.github/workflows/python_wheels.yml
secrets:
Expand Down
20 changes: 16 additions & 4 deletions python/cudaq/kernel/kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import random
import re
import string
import typing
import sys
from typing import get_origin, List
from .quake_value import QuakeValue
from .kernel_decorator import PyKernelDecorator
from .utils import mlirTypeFromPyType, nvqppPrefix, emitFatalError, mlirTypeToPyType
Expand Down Expand Up @@ -242,7 +243,7 @@ def __processArgType(self, ty):
"""
if ty in [cudaq_runtime.qvector, cudaq_runtime.qubit]:
return ty, None
if typing.get_origin(ty) == list or isinstance(ty(), list):
if get_origin(ty) == list or isinstance(ty(), list):
if '[' in str(ty) and ']' in str(ty):
allowedTypeMap = {'int': int, 'bool': bool, 'float': float}
# Infer the slice type
Expand Down Expand Up @@ -1084,11 +1085,22 @@ def __call__(self, *args):
f"Invalid number of arguments passed to kernel `{self.funcName}` ({len(args)} provided, {len(self.mlirArgTypes)} required"
)

def getListType(eleType: type):
if sys.version_info < (3, 9):
return List[eleType]
else:
return list[eleType]

# validate the argument types
processedArgs = []
for i, arg in enumerate(args):
mlirType = mlirTypeFromPyType(type(arg), self.ctx)
if mlirType != self.mlirArgTypes[i]:
argType = type(arg)
listType = None
if argType == list:
listType = getListType(type(arg[0]))
mlirType = mlirTypeFromPyType(argType, self.ctx)
if mlirType != self.mlirArgTypes[
i] and listType != mlirTypeToPyType(self.mlirArgTypes[i]):
emitFatalError(
f"Invalid runtime argument type ({type(arg)} provided, {mlirTypeToPyType(self.mlirArgTypes[i])} required)"
)
Expand Down
15 changes: 15 additions & 0 deletions python/tests/builder/test_kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,13 @@ def kernelThatTakesIntAndListListFloat(qubits: cudaq.qview, qbit: int,
cudaq.observe(ansatz, hamiltonian).expectation(),
atol=1e-2)

kernelAndArgs = cudaq.make_kernel(List[bool])
cudaq.sample(kernelAndArgs[0], [False, True, False])
kernelAndArgs = cudaq.make_kernel(List[int])
cudaq.sample(kernelAndArgs[0], [1, 2, 3, 4])
kernelAndArgs = cudaq.make_kernel(List[float])
cudaq.sample(kernelAndArgs[0], [5.5, 6.5, 7.5])


@skipIfPythonLessThan39
def test_call_kernel_expressions_list():
Expand Down Expand Up @@ -1113,6 +1120,13 @@ def kernelThatTakesIntAndListListFloat(qubits: cudaq.qview, qbit: int,
cudaq.observe(ansatz, hamiltonian).expectation(),
atol=1e-2)

kernelAndArgs = cudaq.make_kernel(list[bool])
cudaq.sample(kernelAndArgs[0], [False, True, False])
kernelAndArgs = cudaq.make_kernel(list[int])
cudaq.sample(kernelAndArgs[0], [1, 2, 3, 4])
kernelAndArgs = cudaq.make_kernel(list[float])
cudaq.sample(kernelAndArgs[0], [5.5, 6.5, 7.5])


def test_adequate_number_params():

Expand Down Expand Up @@ -1200,6 +1214,7 @@ def test_list_subscript():
kernelAndArgs = cudaq.make_kernel(bool, list[bool], List[int], list[float])
print(kernelAndArgs[0])
assert len(kernelAndArgs) == 5 and len(kernelAndArgs[0].arguments) == 4
cudaq.sample(kernelAndArgs[0], False, [False], [3], [3.5])


# leave for gdb debugging
Expand Down
Loading