-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
QuantumCircuit has no attribute 'save_statevector' #6346
Comments
Yes this is confusing as these are |
Just importing Aer does not load the instructions. You need to explicitly call |
Either way, it is confusing as they appear as native methods of |
@nonhermitian I agree. There are 2 separate solutions I can think of. The first would be to change the tutorial documentation to suggest calling the simulator first. The second, would be to change qc.save_unitary() to simulator.save_unitary() or simulator.save_unitary(qc). Would moving save_unitary() and other save states to the class containing the Simulators raise any issues is the question (I'll take a look at the code later when I can get to it). |
The solution is to change Terra code such that |
@yaelbh I took a look at the Aer instructions library (https://qiskit.org/documentation/apidoc/aer_library.html) and the documentation everywhere is saying that a simulator call needs to be made. So save_unitary() is completely dependent on a simulator being called, as is the same for save statevector and etc. Which makes me think any fix to where we don't have to call a simulator to use save_unitary, would involve adding a simulator call to the instructions library or one of the Classes containing the save states (ex: save_unitary(param1, param2, param3, "simulator-of-choice")). The only documentation that differs in not specifying that a simulator is needed first is the tutorial code. Updating the documentation might be a good temp fix at the very least. |
Could you please refer more specifically where in https://qiskit.org/documentation/apidoc/aer_library.html it says that a simulator call is required? I see that it says that Aer needs to be imported, but in practice we see that merely |
Sure thing. I took a screenshot of the save state description. Specifically the line "can be used to save the state of the Simulator".
|
@yaelbh also more specifically, if a simulator call didn't need to be made, then "pershot" wouldn't be a parameter for save_unitary() |
I don't see how the line "can be used to save the state of the Simulator" implies that the code crashes if |
Same for |
@yaelbh I see what you mean, but Python as a language is line-by-line execution. So if the line save_unitary(pershot=True) were called, then Python is expecting a simulator when executing that line. So when 'get_backend' is called before the save state, python knows what to execute pershot due to 'get_backend' being executed previously. That may be the issue (the way python executes the code).
|
I disagree with this, the semantics of the save state instructions are very specific to aer, and don't apply to simulators more broadly (as they all work differently). Instructions do not need to live in terra, unless we want them to be a common interface for everything. As for the import error I think the point of confusion is that:
does not actually import qiskit-aer the package which means the monkey patching that aer does on import is not run. As @nonhermitian mentioned it's an unfortunate consequence of the use of namespace packaging combined with how we build the combined documentation for the metapackage that the monkey patching that aer does to add the circuit methods happens before the docs are built, so they show up as if they're actually methods of the I believe the fix here is simply to modify the docstrings for the |
@mtreinish I agree that the fix for now is to document the need to import |
I ran the code as intended, but this time imported via (from qiskit.providers.aer import Aer), and the code works. I'll update my pull request to reflect that in fix Qiskit/qiskit-tutorials#1176. @yaelbh I've been thinking about the issue you're raising, and trying to find a solution for it. So to start I looked at the following to see how any change would affect get_backend (https://qiskit.org/documentation/stubs/qiskit.providers.aer.AerProvider.html#qiskit.providers.aer.AerProvider.get_backend and also, https://qiskit.org/documentation/stubs/qiskit.providers.Backend.html#qiskit.providers.Backend). And it seems get_backend accepts keyword or named arguments through **kwargs, and then returns the backend which is then initialized (based off of what named arguement for the backend was requested; ex: 'unitary_simulator'). So get backends job seems to be 2 things, the first is to take a name argument to determine what simulator to initialize, and then to return that initialized backend. I'm not sure if the two tasks can or should be separated though (I'm not sure what that would impact overall). |
…es function Reverted all changes in the original commit. Added 'from qiskit.providers.aer import Aer' fix from Qiskit/qiskit#6346 into the tutorial section stating what import calls are needed.
I second the second solution proposed by @WiFisunset
My reasoning is that this is an operation specific to the Aer backend, and so should be localized to it. You can't always rely on people to read the whole documentation, and so a targeted error message is more effective and user-friendly. |
This is another possible solution: rht@27d934f, i.e. inform user in the error message that they should initialize the Aer backend. If you think this is a good fix (for now), I can add test to it and make a PR. |
Based on the discussions above and #8359 (comment), it sounds like this is not something we should fix in Terra. Should we close this issue as "Won't fix"? @jakelishman |
Reading a bit more carefully, the suggested solution is to fix the docstrings #6346 (comment)
I think this should apply to |
We can leave this open to refer to until we're got the documentation fix in Aer, but yeah, this is something that wants fixing there, not in Terra. |
We had an offline discussion about this. This issue should be fixed after qiskit-aer move to its own namespace. |
Perspectives from a confused user: I am trying to use I view the |
We really consider I feel like this would be clearer if the method had never been attached to We will still support adding such instructions to (fyi: #8440 should be in a wheel in two weeks' time - the planned release date for Terra 0.22 is two weeks from today, which will include that PR.) |
Thanks. Could you point me to some docs that show me what this will look like? Currently I use something like import qiskit
qc = qiskit.QuantumCircuit(2)
qc.save_statevector(label=r"\psi_0")
qc.h(0)
qc.save_statevector(label=r"\psi_1")
qc.cx(0,1)
qc.save_statevector(label=r"\psi_2")
qc.draw('mpl') Would this become something like import qiskit, qiskit_aer
sim = qiskit_aer.StatevectorSimulator()
qc = qiskit.QuantumCircuit(2)
qc.append(sim.save_statevector(label=r"\psi_0")) # Does not exist... what goes here?
qc.h(0)
... |
We don't have any docs yet, sorry - the action item from this issue is still about just documenting the current behaviour, which we're behind on! For now, the right thing to do is just to We do need to update all this documentation fairly urgently - the community team are currently in the process of trying to bring some order to all our documentation, and the core Terra team are currently a bit overwhelmed with the upcoming 0.22 release, which is why things are moving particularly slowly right now (though this issue is so old, we don't really have an excuse...). I'm sorry it's confusing in the mean time. |
Thanks, that will fix my issues. However, I would really recommend against having methods like |
Yeah, I totally agree - I want to move to that model (explicitly require an appropriate simulator) as well. We're going in that direction, but development on Aer's interface is quite slow, and we need to have a good long deprecation period to ease the transition for everyone. It's just in the interim where "improve the documentation" is important, while we do the deprecations. |
With |
Information
What is the current behavior?
Unable to apply the
save_statevector
instruction to a newly created QuantumCircuit. Doing so throws an error:AttributeError: 'QuantumCircuit' object has no attribute 'save_statevector'
However, if I first simulate a circuit without the
save_statevector
instruction, then everything works as intended and I can go back and apply thesave_statevector
instruction.Steps to reproduce the problem
What is the expected behavior?
I should be able to apply the
save_statevector
instruction to my quantum circuit.The text was updated successfully, but these errors were encountered: