# How to simulate the density matrix in Qiskit when using the qasm simulator?

When simulating a circuit using qasm simulator, if there is a depolarizing channel in the noise model, then the results could be mixed states. How can I calculate the density matrix of the mixed states?

The easiest way to do this would be to probably use the density matrix snapshot instruction: https://qiskit.org/documentation/stubs/qiskit.providers.aer.extensions.SnapshotDensityMatrix.html#qiskit.providers.aer.extensions.SnapshotDensityMatrix basically just add circuit.snapshot_density_matrix('density_matrix') to your circuit where you want to get the density matrix. That will store the density matrix in the output results. Here is an example script:

from qiskit.test.mock import FakeVigo
from qiskit import QuantumCircuit
from qiskit.providers.aer import extensions  # import aer snapshot instructions
from qiskit import execute

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.snapshot_density_matrix('density_matrix')
result = execute(qc, FakeVigo()).result()
# Extract density matrix snapshot from result object:
density_matrix = result.data()['snapshots']['density_matrix']['density_matrix'][0]['value']
print(density_matrix)


In that script FakeVigo is just running Aer under the covers with a noise model taken from a snapshot of the backend properties from the IBMQ vigo device.

• nice answer +1. I am sure the OP appreciate your input/answer. Can you elaborate a bit more on the built in noise model? Is it being changed from time to time or is it just a fixed model based off from the hardware calibration of some sort? Feb 7 at 4:51
• In that example script? The noise model there is built from a snapshot of the calibration properties returned via the IBMQ api. It just uses Aer's NoiseModel.from_backend method to build the noise model which includes 1 and 2 qubit gate depolarization and thermal relaxation errors, and readout errors: qiskit.org/documentation/stubs/… Feb 7 at 15:36
• If you're using a real backend with the qiskit-ibmq-provider then the noise model built with NoiseModel.from_backend with that backend will be using the latest calibration data (it's updated once a day iirc) returned by backend.properties() (unless you manually build it from a previous calibration). I only used the fake backend in the example because the fake backends are always available, even without credentials, and just contain a fixed snapshot that was committed to the qiskit repo. Feb 7 at 15:41
• Thanks! Got it. Yes, I was preferring to when using real backend with qiskit-ibmq-provider. I asked because I think the noise in these devices fluctuates and wondering how often you guys changes the noise model to keep up with it. Feb 7 at 17:02