2
$\begingroup$

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
$\endgroup$

2 Answers 2

4
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks for the quick reply, it worked!!! $\endgroup$
    – Antonio
    Commented Mar 25 at 11:21
1
$\begingroup$

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")

The above circuit as displayed by Qiskit using matplotlib

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}

$\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.