NVIDIA/cuda-quantum

Consider adding a warning when a cudaq.State is used as a buffer, causing copying of the state to host

Opened this issue · 1 comments

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

Some python statements cause cudaq.State act to as a buffer, causing copying of the data from the device to the host. Should we add a warning in those cases?

Steps to reproduce the bug

import numpy as np
import cudaq

c = np.array([1, 2j, 3, 4j, 5, 6j, 7, 8j], dtype=np.complex64)
state = cudaq.State.from_data(c)
state = state/np.linalg.norm(state) # Copies the state to host twice

@cudaq.kernel
def kernel(vec: list[complex]):
    q = cudaq.qvector(vec)

counts = cudaq.sample(kernel, c)
print(counts)

Expected behavior

Output a warning when we copy data to the host (if possible, with the source information)

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

Not a regression

Environment

  • CUDA Quantum version:
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response

It seems like we maybe don't want a variable of type State to be used in general python expressions.

So after

c = np.array([1, 2j, 3, 4j, 5, 6j, 7, 8j], dtype=np.complex64)
state = cudaq.State.from_data(c)

the variable c is just a np.array to with one can do np.array sorts of things, but the variable state is an opaque box with no one really knows inside it.