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

Support numpy arrays uses inside kernel functions #1523

Open
1 task done
annagrin opened this issue Apr 16, 2024 · 0 comments
Open
1 task done

Support numpy arrays uses inside kernel functions #1523

annagrin opened this issue Apr 16, 2024 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@annagrin
Copy link
Collaborator

annagrin commented Apr 16, 2024

Required prerequisites

  • Search the issue tracker to check if your feature has already been mentioned or rejected in other issues.

Describe the feature

Some of usage of numpy arrays is not supported yet inside kernel functions. Should we support all of those uses?

numpy arrays of float

Works:

    f = [0., 1., 1., 0.]
    arr = np.array(f)

    # Pass an array as a parameter
    @cudaq.kernel
    def test_float_array_param(vec : np.ndarray):
        f1 = vec

    counts = cudaq.sample(test_float_array_param, arr)
    assert len(counts) == 0

Does not work:

    # Capture an array
    @cudaq.kernel
    def test_float_array_capture():
        arr1 = arr

    # cudaq.kernel.ast_bridge.CompilerError: test_kernel_np_array.py:45: error: Invalid type for variable (arr) captured from parent scope (only int, bool, float, complex, and list[int|bool|float|complex] accepted, type was ndarray).
    # counts = cudaq.sample(test_float_array_capture)
    # assert len(counts) == 0

Works:

    # Define an array of float inside a kernel
    @cudaq.kernel
    def test_float_array_definition():
        f1 = np.array([1.0, 0., 0., 1.])

    counts = cudaq.sample(test_float_array_definition)
    assert len(counts) == 0

Numpy arrays of complex

Does not work:

    # Pass an array of complex as a parameter
    c = [.70710678 + 0j, 0., 0., 0.70710678]
    arr = np.array(c)

    @cudaq.kernel
    def test_complex_array_param(vec : np.ndarray):
        arr1 = vec

    # RuntimeError: error: Invalid runtime argument type. Argument of type list[complex] was provided, but list[float] was expected.
    # counts = cudaq.sample(test_complex_array_param, arr)
    # assert len(counts) == 0

Does not work:

    # Capture an array of complex
    @cudaq.kernel
    def test_complex_array_capture():
        arr1 = arr

    # cudaq.kernel.ast_bridge.CompilerError: test_kernel_np_array.py:80: error: Invalid type for variable (arr) captured from parent scope (only int, bool, float, complex, and list[int|bool|float|complex] accepted, type was ndarray).
    # counts = cudaq.sample(test_complex_array_capture)
    # assert len(counts) == 0

Works:

    # Define an array of complex inside a kernel
    @cudaq.kernel
    def test_complex_array_definition():
        arr = np.array([1.0 + 0j, 0., 0., 1.])

    counts = cudaq.sample(test_complex_array_definition)
    assert len(counts) == 0

Note

Support c = np.array([...], dtype=np.complex64) and demonstrate with the nvidia target. Also it would be good to test that we raise an exception for the use of complex with nvidia and np.complex64 with FP64 backends (all the other ones).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants