Optimize data copy from python to cpp #11
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Currenly
StateVector
is implemented usingstd::vector
which takes ownership of its data, so it will incur memory copy from pythonnumpy
array. If the input statevector is very large. This will have significant overhead.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$2**10=1024$ and the elements are all non-zero values. Then we cannot prune the last 992 state amplitudes.
QuantumCircuit(10)
and only used 5 qubits from qubit 0 to qubit 4, but we want to initialize the circuit with a statevector lengthI did following changes
std::complex<T>*
pointers instead of STL containers. In the future we can usestd::span
(since C++20) which does not require ownership of data.num_ > 0
, meaning that statevector has been initialized from a input state, then we skipset_num
.count_
toStateVector
, 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).