NVIDIA/cuda-quantum

Support numpy arrays uses inside kernel functions

annagrin opened this issue · 0 comments

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).