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

Optimize data copy from python to cpp #11

Merged
merged 4 commits into from
Apr 2, 2023
Merged

Conversation

Zhaoyilunnn
Copy link
Collaborator

Hi,

I am developing a project and wants to integerate with pyquafu.

During testing, I found some issues with current implementation to support users to set initial state vector to the quantum circuit.

  1. Currenly StateVector is implemented using std::vector which takes ownership of its data, so it will incur memory copy from python numpy array. If the input statevector is very large. This will have significant overhead.

  2. qfvm has a great feature which determines the used qubits automatically. However, when initialized from a statevector, it should not resize the statevector based on used qubits. For example, I have a QuantumCircuit(10) and only used 5 qubits from qubit 0 to qubit 4, but we want to initialize the circuit with a statevector length $2**10=1024$ and the elements are all non-zero values. Then we cannot prune the last 992 state amplitudes.

I did following changes

  1. Use raw std::complex<T>* pointers instead of STL containers. In the future we can use std::span (since C++20) which does not require ownership of data.
  2. Check if num_ > 0, meaning that statevector has been initialized from a input state, then we skip set_num.
  3. Added count_ to StateVector, if moved to python, it will not free the state's memory.

I compared the performance using a random quantum circuit with 28 qubits (require 4GB memory).

Version AVG Runtime (s) RES (GB)
base 55.52 8
head 34.63 4

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

Successfully merging this pull request may close these issues.

3 participants