# How do I get the unitary matrix of a circuit?

I'm studying on this textbook but the library has changed meanwhile and running those instructions on my IBM quantum learning notebook will result in errors. What's the current way to draw the unitary matrix resulting from a circuit using the current AerSimulator?

from qiskit_aer import AerSimulator


You can get back the matrix by using the save_unitary() method on the circuit after creating your circuit. Here is an example :

import numpy as np

# Import Qiskit
from qiskit import QuantumCircuit, transpile
from qiskit_aer import Aer, AerSimulator

# Construct quantum circuit without measure
circ = QuantumCircuit(2)
circ.h(0)
circ.cx(0, 1)
circ.save_unitary()

# Transpile for simulator
simulator = Aer.get_backend('aer_simulator')
# Another option to create the simulator
# simulator = AerSimulator(method = 'unitary')
circ = transpile(circ, simulator)

# Run and get unitary
result = simulator.run(circ).result()
unitary = result.get_unitary(circ)
print("Circuit unitary:\n", np.asarray(unitary).round(5))


This prints :

Circuit unitary:
[[ 0.70711+0.j  0.70711-0.j  0.     +0.j  0.     +0.j]
[ 0.     +0.j  0.     +0.j  0.70711+0.j -0.70711+0.j]
[ 0.     +0.j  0.     +0.j  0.70711+0.j  0.70711-0.j]
[ 0.70711+0.j -0.70711+0.j  0.     +0.j  0.     +0.j]]


It isn't available yet in the documentation, but an upgraded version of the tutorials will be available, you can check the tuto in GitHub if you're interested in learning more details about how to use qiskit_aer simulators.

• Thanks for the quick reply, it worked!!! Commented Mar 25 at 11:21

The Operator class has a constructor that can do this for you.

from qiskit import QuantumCircuit
circ = QuantumCircuit(2)
circ.h(0)
circ.cx(0, 1)
circ.draw("mpl")


from qiskit.quantum_info import Operator
circOp = Operator.from_circuit(circ)
circOp.draw("latex")


\begin{bmatrix} \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} & 0 & 0 \\ 0 & 0 & \frac{\sqrt{2}}{2} & - \frac{\sqrt{2}}{2} \\ 0 & 0 & \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} \\ \frac{\sqrt{2}}{2} & - \frac{\sqrt{2}}{2} & 0 & 0 \\ \end{bmatrix}