0
$\begingroup$

OS: lubuntu 20.04 Mozilla Firefox browser jupyter-lab environment

Error message: AttributeError: 'QuantumCircuit' object has no attribute 'save_unitary'

Below is the code:

from qiskit import QuantumCircuit, Aer, assemble
from math import pi
import numpy as np
from qiskit.visualization import plot_bloch_multivector, plot_histogram, array_to_latex
import matplotlib.pyplot as plt


def show_figure(fig):

    # create a dummy figure and use its
    # manager to display "fig"

    
    dummy = plt.figure()
    #print("plt.figure is set")
    new_manager = dummy.canvas.manager
    #print("canvas.manager assigned")
    new_manager.canvas.figure = fig
    fig.set_canvas(new_manager.canvas)

qc = QuantumCircuit(2)
qc.x(0)
qc.x(1)
qc.h(0)
qc.h(1)
qc.cx(0,1)
qc.h(0)
qc.h(1)
display(qc.draw(initial_state=True, output='mpl'))
plt.show()
print(qc)


qc.save_unitary()

usim = Aer.get_backend("aer_simulator")
qobj = assemble(qc)
unitary = usim.run(qobj).result().get_unitary()
#array_to_latex(unitary, prefix="\\text{Circuit}\n")
```
$\endgroup$

1 Answer 1

0
$\begingroup$

You need to instantiate the simulator at first. That is, to move this line before calling save_unitary:

usim = Aer.get_backend("aer_simulator")

Another workaround is to import the method:

from qiskit.providers.aer.library import save_unitary

Although QuantumCircuit is defined in Qiskit Terra, save_unitary method is added to it in Qiskit Aer.

You can find more details here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.