qiskit-tutorials icon indicating copy to clipboard operation
qiskit-tutorials copied to clipboard

Save states need 'from qiskit.providers.aer import Aer' called first in order to function.

Open WiFisunset opened this issue 3 years ago • 10 comments

Summary

Save simulator states need to be called after the Simulator in order to function. Otherwise the tutorial will output a "AttributeError: 'QuantumCircuit' object has no attribute 'save_state'" error. This issue affects all save simulator states in the tutorial, so the save states were moved underneath the Simulator in each example.

This fixes issue #1172.

Details and comments

Using the current tutorial code, programming the following will lead to an (AttributeError: 'QuantumCircuit' object has no attribute 'save_state') error:

import numpy as np from qiskit import QuantumCircuit from qiskit import Aer, transpile from qiskit.tools.visualization import plot_histogram, plot_state_city import qiskit.quantum_info as qi

circ = QuantumCircuit(2) circ.h(0) circ.cx(0, 1) circ.save_statevector()

simulator = Aer.get_backend('aer_simulator') circ = transpile(circ, simulator)

result = simulator.run(circ).result() statevector = result.get_statevector(circ) plot_state_city(statevector, title='Bell state')

WiFisunset avatar May 03 '21 01:05 WiFisunset

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@SooluThomas @nonhermitian could you review this Pull Request? This Pull Request also fixes issue #1172.

WiFisunset avatar May 03 '21 01:05 WiFisunset

save_statevector is an instruction of QuantumCircuit and should not be affected by the order of anything. You've changed the order of the lines

circ.save_statevector()

and

simulator = Aer.get_backend('aer_simulator')

These are independent lines and the code is equivalent. The second line doesn't do anything except for assigning a value to the simulator variable, which is not used in the first line.

A couple of additional comments:

  1. With the usage of save_statevector, I think get_statevector needs to be replaced by something else.
  2. As discussed in #1172, it appears that you have some mismatch with versions.

yaelbh avatar May 03 '21 05:05 yaelbh

Hi, @yaelbh I have added more explanation about it here. https://github.com/Qiskit/qiskit-tutorials/pull/1174 (although I need to close the PR for some another reason) but the details are here. I will add them here as well.

AshwinKul28 avatar May 03 '21 05:05 AshwinKul28

Summary

The circ.save_unitary() call is used to save the simulator state as a unitary matrix of the run circuit. In the example numbered as [10] in the simulators/1_aer_simulator notebook, the circ_save_unitary() call has been made before simulator initialization. Hence it is failing to run when a user tries to run exact same program.

Details and comments

Since, the save_unitary() function call described as follows mentions that save_unitary() function saves the state of the simulator. (references: ref1, ref2)

Screenshot 2021-04-28 at 1 11 27 AM

But according to the program if we don't call the simulator before the circ.save_unitary call, we cannot save the state of the simulator and hence QuantumCircuit is giving an error as follows:

Screenshot 2021-04-28 at 1 03 30 AM

Hence I have given the fix in the document as I'm calling save_unitary() function after the simulator initialisation.

Screenshot 2021-04-28 at 1 04 03 AM

AshwinKul28 avatar May 03 '21 05:05 AshwinKul28

Ok, I can restore it in my environment. I don't think it's a desired behavior. Will check some more.

yaelbh avatar May 03 '21 13:05 yaelbh

https://github.com/Qiskit/qiskit-terra/issues/6346

yaelbh avatar May 04 '21 06:05 yaelbh

I'm going to be updating this commit to issue a resolve found in https://github.com/Qiskit/qiskit-terra/issues/6346.

And reverting the changes made previously where the lines were moved (to where the save states were under the simulator call).

In which calling 'from qiskit.providers.aer import Aer' is the solution to the save state problem. qiskit poviders aer fix

WiFisunset avatar May 06 '21 00:05 WiFisunset

@mtreinish could you review the changes I made regarding qiskit.providers.aer?

WiFisunset avatar May 06 '21 04:05 WiFisunset

@mtreinish @nonhermitian. Could I get a review on the changes made?

WiFisunset avatar Jul 10 '21 21:07 WiFisunset

Pardon that we didn't finish reviewing this. The Simulator tutorials now live in Aer and were removed from this repository in https://github.com/Qiskit/qiskit-tutorials/pull/1489

Eric-Arellano avatar Jul 17 '23 16:07 Eric-Arellano