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

Calling AerState amplitude (also probability) is slow #2073

Open
aromanro opened this issue Feb 26, 2024 · 3 comments
Open

Calling AerState amplitude (also probability) is slow #2073

aromanro opened this issue Feb 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@aromanro
Copy link
Contributor

Informations

  • Qiskit Aer version: Latest dev version, main branch.
  • Python version: Irrelevant, as I'm compiling it into a c++ project.
  • Operating system: Windows 11.

What is the current behavior?

The problem appears for the default 'statevector' simulation.

I used the qiskit aer simulator to up to 27 qubits lately, also my own simulator (qcsim: https://github.com/aromanro/QCSim) in a c++ project. To ensure that the results are ok, I have some tests that at the end of the simulations they check the outcome by comparing the amplitudes from both simulators.

I noticed that getting the amplitudes like that is really slow for qiskit aer (compared with qcsim).

I know that flush_ops() needs to be called to have the ops actually applied, I ensured that it is actually called at the end of the simulation, so that's not the reason of the slow behavior for the amplitude call.

Steps to reproduce the problem

Just use a > 20 qubits simulator and try getting the amplitudes for all states.

What is the expected behavior?

I expected it to be much faster... not as fast as qcsim, as there is more involved (qcsim is very simple and it simply does this: https://github.com/aromanro/QCSim/blob/75e009f76828ee444566502242f56d359b310573/QCSim/QubitRegister.h#L33) but aer is much slower. Unfortunately this might prevent me to use it for some things in that project.

Suggested solutions

One workaround I found for what I do (unfortunately I cannot give details because I signed an NDA, I must ask for approval) is to use move_to_vector, but unfortunately this is destructive for the internal state... maybe the amplitude and probability calls could be improved somehow to be faster?

@aromanro aromanro added the bug Something isn't working label Feb 26, 2024
@doichanj
Copy link
Collaborator

I think AerState is designed to simulate each operation interactively from Python.
And implementations of amplitude and probability is not well optimized and they get only one value per call and they convert binary -> string -> binary inside.
I think we can have some shortcut function to access statevector directly, but I have no idea if it is right way or not and it will be useful. @hhorii how do you think?

@hhorii
Copy link
Collaborator

hhorii commented Feb 29, 2024

As @doichanj mentioned, AerState is not a public API. But if you want to it to get amplitudes of statevector somehow, I recommend to use move_to_vector. With moved ndarray, a new AerState can be constructed with initialize() method.

aer_state = AerState()
options = AerSimulator._default_options()
for config_key, config_value in configs.items():
if options.get(config_key):
aer_state.configure(config_key, config_value)
if len(init_data) == 0:
raise AerError("initial data must be larger than 0")
num_qubits = int(np.log2(len(init_data)))
aer_state.allocate_qubits(num_qubits)
aer_state.initialize(data=init_data, copy=do_copy)

Again, AerState is an internal class and use it at your own risk.

@aromanro
Copy link
Contributor Author

I'm already using move_to_vector as a workaround.

AerState is actually exposed in contrib/runtime (indeed, casted to a void*) and both aer_probability and aer_amplitude call the corresponding functions in AerState.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants