2
$\begingroup$

So the documentation is all mixed up and I can't find how to get unitaries, statevectors, etc. in the AerSimulator module. For an example, see e.g. this docs. In the old days, one would get this information using only the Aer module (as described in the previous link). But now, fresh qiskit installations do not recognize the Aer module and instead one has to use the "AerSimulator" module. If my circuit is

qreg_q = QuantumRegister(1, 'q')
creg_c = ClassicalRegister(1, 'c')
circuit = QuantumCircuit(qreg_q, creg_c)
circuit.h(0)

I call the "backend" as (not sure if correctly)

backend = AerSimulator(method='unitary')

and run

job = backend.run(circuit).result()

The job object has the attribute (?) job.get_unitary(), but I can't get the unitary matrix from it. Any idea how to extract this information?

$\endgroup$

2 Answers 2

2
$\begingroup$

You will need to add one of the save instructions to your circuit. For example, to get the unitary, add SaveUnitary instruction as follows:

circuit.save_unitary()

Then, run the simulator:

backend = AerSimulator(method='unitary')
job = backend.run(circuit)

Now, you can get the unitary:

unitary = job.result().get_unitary()

Similarly, you can get the statevector by calling circuit.save_statevector and density matrix by calling circuit.save_density_matrix, ...etc.

You can also use SaveState instruction which will automatically select the format based on the simulation method.

$\endgroup$
1
$\begingroup$

According to the Qiskit 1.0 release notes https://docs.quantum.ibm.com/api/qiskit/release-notes/1.0#providers-upgrade-notes the preferred method of getting your desired information is to use qiskit.quantum_info. For example:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator, Statevector, DensityMatrix

qc = QuantumCircuit(1)
qc.h(0)

print(Operator(qc)) # unitary
print(Statevector(qc))
print(DensityMatrix(qc))
Operator([[ 0.70710678+0.j,  0.70710678+0.j],
          [ 0.70710678+0.j, -0.70710678+0.j]],
         input_dims=(2,), output_dims=(2,))
Statevector([0.70710678+0.j, 0.70710678+0.j],
            dims=(2,))
DensityMatrix([[0.5+0.j, 0.5+0.j],
               [0.5+0.j, 0.5+0.j]],
              dims=(2,))

It is also worth noting that you can still use Aer as in the old days with from qiskit_aer import Aer.

$\endgroup$
1
  • $\begingroup$ The release notes are about migration from BasicAer to quantum_info. Both are written in Python. Aer is written in C++ and is therefore much faster. The question was about Aer, so it remains unanswered. $\endgroup$ Mar 14 at 5:10

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.