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

How can I use QuantumStateGPU.get_vector() to get cupy.array? #290

Open
yuguoshao opened this issue Aug 17, 2021 · 5 comments
Open

How can I use QuantumStateGPU.get_vector() to get cupy.array? #290

yuguoshao opened this issue Aug 17, 2021 · 5 comments
Labels
wontfix This will not be worked on

Comments

@yuguoshao
Copy link

yuguoshao commented Aug 17, 2021

In my experiments , I want to use state vector to do some numeral calculations. So I wish that I can get state vector on GPU directly. Compared to Numpy, Cupy or PyTorch may be better choices. But I can't find out how get_vector() runs. I can only find:

def get_vector(self): # real signature unknown; restored from __doc__
        """
        get_vector(self: qulacs.QuantumState) -> numpy.ndarray[complex128[m, 1]]
        
        Get state vector
        """
        pass

Is there anyway to help me?

@corryvrequan
Copy link
Contributor

Unfortunately, qulacs does not have an interface to get a pointer to state vectors on GPU or directly pass it to other GPU numerical libraries.
If we allow overheads of two copies from GPU->main ram and main ram -> GPU, we can copy the data managed in qulacs to other libraries. For example:

import qulacs
num_qubit = 10
statevec_on_qulacs = qulacs.QuantumStateGpu(10) # allocate 2^10 complex vector on GPU
statevec_on_cpu = statevec_on_qulacs.get_vector() # copy from GPU -> main ram
statevec_on_cupy = cupy.array(statevec_on_cpu) # copy from main ram -> GPU

Qulacs is currently written in C++/CUDA and its functions are exported to python with pybind. So, I think it is hard to provide an interface to directly convert internal state vectors on GPU to Cupy or relevant libraries unless pybind supports it...

@yuguoshao
Copy link
Author

Okay , thank you very much ! I hope one day quantum state can convert to vector directly on Gpu.😭😭😭

@leofang
Copy link

leofang commented Aug 24, 2021

Hi, a CuPy dev here. May I ask is the difficulty in that the statevector is not stored contiguously in memory? Otherwise there must be a way to get the pointer address and memory size, and CuPy can take from there.

@corryvrequan
Copy link
Contributor

Hi @leofang .
Thanks for your comments, and I'm very sorry for the late reply.

May I ask is the difficulty in that the state vector is not stored contiguously in memory?
No. The state vector is stored contiguously in memory in Qulacs.

Therefore, I agree that there is no technical problem in passing the pointer address of memory, and I didn't know CuPy supports wrapping foreign memory address as a CuPy array object.
cupy/cupy#4644

While I couldn't find a simple way to export a function to return CuPy object with pybind11, if it is useful for users, it is easy to implement a function to return the pointer address of state vector on GPU.

@leofang
Copy link

leofang commented Nov 12, 2021

@corryvrequan Yes, I think it'd be useful. At the pybind11 level the pointer address to the start of the state vector (assuming there's no extra requirement in padding / alignment / offset) can be returned as either uintptr_t or a Python int. Then, at the Python level you can use cupy.cuda.UnownedMemory to wrap it as you found out in the linked issue. If you decide to give it a go I'm happy to help review (it should need only few lines of code) 🙂

@KowerKoint KowerKoint added the wontfix This will not be worked on label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants