Save states need 'from qiskit.providers.aer import Aer' called first in order to function.
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')
Check out this pull request on ![]()
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.
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:
- With the usage of
save_statevector, I thinkget_statevectorneeds to be replaced by something else. - As discussed in #1172, it appears that you have some mismatch with versions.
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.
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)
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:
Hence I have given the fix in the document as I'm calling save_unitary() function after the simulator initialisation.
Ok, I can restore it in my environment. I don't think it's a desired behavior. Will check some more.
https://github.com/Qiskit/qiskit-terra/issues/6346
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.
@mtreinish could you review the changes I made regarding qiskit.providers.aer?
@mtreinish @nonhermitian. Could I get a review on the changes made?
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