save_unitary state error? It's not saving the unitary and providing an output.
Information
- Qiskit IBMQ Provider version: 0.12.3
- Python version: 3.8.8
- Operating system: Windows 10
What is the current behavior?
save.unitary creates an "AttributeError: 'QuantumCircuit' object has no attribute 'config' "
The same happens for the following commands: result = simulator.run(circ).result() unitary = result.get_unitary(circ)
Steps to reproduce the problem
Following the tutorial code at (https://qiskit.org/documentation/tutorials/simulators/1_aer_provider.html):
import numpy as np
from qiskit import QuantumCircuit from qiskit import Aer, transpile from qiskit.tools.visualization import plot_histogram, plot_state_city
circ = QuantumCircuit(2) circ.h(0) circ.cx(0, 1) circ.save_unitary()
simulator = Aer.get_backend('aer_simulator') circ = transpile(circ, simulator)
result = simulator.run(circ).result() unitary = result.get_unitary(circ) print("Circuit unitary:\n", unitary.round(5))
What is the expected behavior?
The expected behavior is a printed out circuit unitary.
Suggested solutions
Call save_unitary() after the Simulator has been defined.
HI @WiFisunset The same code is working for me,
Qiskit version: 0.17.1 OS: macOS Big Sur: 11.2.1 Python Version: 3.8.3
Maybe an update in qiskit version might be helpful for you.
Hi @AshwinKul28, I updated qiskit via 'pip install update qiskit==0.25.2', and I still got the error.
#Update:
Here's the same circuit in the conda terminal
HI @WiFisunset I feel the behavior is somewhat different, so I tried some hack here
-
First, comment the
circ.save_unitary()line and run the program, it will give you the following error.
-
Then uncomment the
circ.save_unitary()line and run again it should give you the expected output.
I know this is incorrect behavior, will try to get into it. Let me know if you find anything out of it and the above hack works or not.
I was able to fix the error, but only by having 'circ.save_unitary()' called after 'circ = transpile(circ, simulator)'.
Okay which means basic need is to transpile the circuit first and then we can make a unitary. If this is the solution we need to make changes in the doc as well.
@AshwinKul28 I think the main issue is that it needs to be called after the simulator. Because 'save_unitary()' still works with the transpile commented out:
@WiFisunset , I might have got the reason, Since, in the save unitary definition, they are mentioning that it saves the state of the unitary simulator, hence it must be called after simulator. And I have also verified it works after the simulator.
https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.html#qiskit.circuit.QuantumCircuit.save_unitary
@WiFisunset but my only concern is we haven't even used the simulator just by only initialising simulator is enough for circ.save_unitary(). This is might be happening because circ.save_unitary() might be expecting a call the Aer simulatory. Just a call from the program is enough. Not sure about the reason though.
@AshwinKul28 I'm a little confused by your wording. But it seems like the fix works. So is the next step to create a pull request for the fix we found?
Yes, @WiFisunset , I will add you in the PR as well. Since I have already raised a PR so you can just modify or add the comment there if you want.
HI @WiFisunset I'm unable to add you in the above PR. but feel free to add your comment in the attached PR.
@AshwinKul28 I saw the previous Pull Request needed to be closed.
This Pull Request has been created as a resolve #1176.
Sounds like you found a workaround. I'm not sure if this is still an issue? I know you had https://github.com/Qiskit/qiskit-tutorials/pull/1176 to improve this. If it's still relevant, please reopen a PR at https://github.com/Qiskit/qiskit-aer since the tutorial now lives there.
I'm closing this out because we're (likely today) archiving this repository. Thank you for opening it and making Qiskit better!