NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
554 stars 189 forks source link

New launch kernel is not invoked for python #2299

Open annagrin opened 1 month ago

annagrin commented 1 month ago

Required prerequisites

Describe the bug

Python code using states as arguments fails to synthesize on quantum targets and remote sim due to the new launch kernel not being invoked (old synthesis message is displayed)

Steps to reproduce the bug

Code

# RUN: PYTHONPATH=../../.. python3 %s --target  --remote-mqpu
# RUN: PYTHONPATH=../../.. python3 %s --target  --quantinuum --emulate

import cudaq
import numpy as np
def test_init_from_data_state():
    c = np.array([1. / np.sqrt(2.) + 0j, 1. / np.sqrt(2.), 0., 0.],
                 dtype=np.complex128)
    state = cudaq.State.from_data(c)
    @cudaq.kernel
    def kernel(vec: cudaq.State):
        q = cudaq.qvector(vec)
    counts = cudaq.sample(kernel, state)
    assert '10' in counts
    assert '00' in counts
test_init_from_data_state()

def test_init_from_state():
    @cudaq.kernel
    def init(n: int):
        q = cudaq.qvector(n)
        ry(np.pi/2.0, q[0])
    @cudaq.kernel
    def kernel(vec: cudaq.State):
        q = cudaq.qvector(vec)
    state = cudaq.get_state(init, 2)
    counts = cudaq.sample(kernel, state)
    assert '10' in counts
    assert '00' in counts
test_init_from_state()

Output

root@f4bab51c0fb2:/workspaces/cuda-quantum# python3 python/tests/mlir/target/tmp.py --target quantinuum
error: 'func.func' op synthesis: unsupported argument type for remote devices and simulators: state*
error: 'func.func' op We cannot synthesize argument(s) of this type.
RuntimeError: Could not successfully apply quake-synth.

Expected behavior

For remote sim, the code should run successfully. For quantinuum, the code should run successfully after https://github.com/NVIDIA/cuda-quantum/pull/2291

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

Suggestions

No response

schweitzpgi commented 1 week ago

This will require refactoring the platofrm/qpu interface along with the caching of Python kernels done in these layers. Specifically, this is caching a lower variant of the kernel with the expectation that the arguments will be provided late. The new argument synthesis takes the opposite approach: arguments are synthesized earlier. #2338 will help with many of the problems, but it will never support cudaq::state since that cannot be synthesized in the compiler.