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

save_unitary state error? It's not saving the unitary and providing an output.

Open WiFisunset opened this issue 4 years ago • 12 comments

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.

WiFisunset avatar Apr 23 '21 03:04 WiFisunset

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

Screenshot 2021-04-26 at 1 40 17 AM

Maybe an update in qiskit version might be helpful for you.

AshwinKul28 avatar Apr 25 '21 20:04 AshwinKul28

Hi @AshwinKul28, I updated qiskit via 'pip install update qiskit==0.25.2', and I still got the error. get-unitary-error

#Update: Here's the same circuit in the conda terminal get-unitary-error-2

WiFisunset avatar Apr 26 '21 06:04 WiFisunset

HI @WiFisunset I feel the behavior is somewhat different, so I tried some hack here

  1. First, comment the circ.save_unitary() line and run the program, it will give you the following error. Screenshot 2021-04-27 at 1 00 02 AM

  2. Then uncomment the circ.save_unitary() line and run again it should give you the expected output. Screenshot 2021-04-27 at 1 02 07 AM

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.

AshwinKul28 avatar Apr 26 '21 19:04 AshwinKul28

I was able to fix the error, but only by having 'circ.save_unitary()' called after 'circ = transpile(circ, simulator)'.

get-unitary-error-fix

WiFisunset avatar Apr 26 '21 23:04 WiFisunset

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 avatar Apr 27 '21 11:04 AshwinKul28

@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:

get-unitary-error-fix-2

WiFisunset avatar Apr 27 '21 18:04 WiFisunset

@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

Screenshot 2021-04-28 at 12 44 28 AM

AshwinKul28 avatar Apr 27 '21 19:04 AshwinKul28

@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 avatar Apr 27 '21 19:04 AshwinKul28

@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?

WiFisunset avatar Apr 28 '21 04:04 WiFisunset

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.

AshwinKul28 avatar Apr 29 '21 13:04 AshwinKul28

HI @WiFisunset I'm unable to add you in the above PR. but feel free to add your comment in the attached PR.

AshwinKul28 avatar Apr 29 '21 13:04 AshwinKul28

@AshwinKul28 I saw the previous Pull Request needed to be closed.

This Pull Request has been created as a resolve #1176.

WiFisunset avatar May 03 '21 01:05 WiFisunset

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!

Eric-Arellano avatar Aug 21 '23 15:08 Eric-Arellano