Skip to main content
IBM Quantum Platform
This page is from an old version of Qiskit SDK and does not exist in the latest version. We recommend you migrate to the latest version. See the release notes for more information.

AerDensityMatrix

class AerDensityMatrix(data, dims=None, **configs)

GitHub

Bases: qiskit.quantum_info.states.densitymatrix.DensityMatrix

AerDensityMatrix class This class inherits DensityMatrix.

Parameters

  • or (data (np.array or list or Statevector orAerStatevector orDensityMatrix) – AerDensityMatrix or QuantumCircuit or qiskit.circuit.Instruction): Data from which the densitymatrix can be constructed. This can be either a complex vector, another densitymatrix or statevector or a QuantumCircuit or Instruction (Operator is not supportted in the current implementation). If the data is a circuit or instruction, the densitymatrix is constructed by assuming that all qubits are initialized to the zero state.
  • dims (int or tuple or list) – Optional. The subsystem dimension of the state (See additional information).
  • configs (kwargs) – configurations of AerDensityMatrix. _aer_state and method are valid.

Raises

AerError – if input data is not valid.

Additional Information:

The dims kwarg is used to AerDensityMatrix constructor.


Methods

conjugate

AerDensityMatrix.conjugate()

Return the conjugate of the density matrix.

copy

AerDensityMatrix.copy()

Make a copy of current operator.

dims

AerDensityMatrix.dims(qargs=None)

Return tuple of input dimension for specified subsystems.

draw

AerDensityMatrix.draw(output=None, **drawer_args)

Return a visualization of the Statevector.

repr: ASCII TextMatrix of the state’s __repr__.

text: ASCII TextMatrix that can be printed in the console.

latex: An IPython Latex object for displaying in Jupyter Notebooks.

latex_source: Raw, uncompiled ASCII source to generate array using LaTeX.

qsphere: Matplotlib figure, rendering of density matrix using plot_state_qsphere().

hinton: Matplotlib figure, rendering of density matrix using plot_state_hinton().

bloch: Matplotlib figure, rendering of density matrix using plot_bloch_multivector().

Parameters

  • output (str) – Select the output method to use for drawing the state. Valid choices are repr, text, latex, latex_source, qsphere, hinton, or bloch. Default is repr. Default can be changed by adding the line state_drawer = <default> to ~/.qiskit/settings.conf under [default].
  • drawer_args – Arguments to be passed directly to the relevant drawing function or constructor (TextMatrix(), array_to_latex(), plot_state_qsphere(), plot_state_hinton() or plot_bloch_multivector()). See the relevant function under qiskit.visualization for that function’s documentation.

Returns

matplotlib.Figure or str or TextMatrix or IPython.display.Latex: Drawing of the Statevector.

Raises

ValueError – when an invalid output method is selected.

evolve

AerDensityMatrix.evolve(other, qargs=None)

Evolve a quantum state by an operator.

Parameters

  • QuantumChannel (other (Operator or) – or Instruction or Circuit): The operator to evolve by.
  • qargs (list) – a list of QuantumState subsystem positions to apply the operator on.

Returns

the output quantum state.

Return type

QuantumState

Raises

QiskitError – if the operator dimension does not match the specified QuantumState subsystem dimensions.

expand

AerDensityMatrix.expand(other)

Return the tensor product state other ⊗ self. :param other: a quantum state object. :type other: AerDensityMatrix

Returns

the tensor product state other ⊗ self.

Return type

AerDensityMatrix

Raises

QiskitError – if other is not a quantum state.

expectation_value

AerDensityMatrix.expectation_value(oper, qargs=None)

Compute the expectation value of an operator.

Parameters

  • oper (Operator) – an operator to evaluate expval.
  • qargs (None or list) – subsystems to apply the operator on.

Returns

the expectation value.

Return type

complex

from_instruction

classmethod AerDensityMatrix.from_instruction(instruction)

Return the output density matrix of an instruction.

The statevector is initialized in the state 0,,0|{0,\ldots,0}\rangle of the same number of qubits as the input instruction or circuit, evolved by the input instruction, and the output statevector returned.

Parameters

instruction (qiskit.circuit.Instruction orQuantumCircuit) – instruction or circuit

Returns

the final density matrix.

Return type

DensityMatrix

Raises

QiskitError – if the instruction contains invalid instructions for density matrix simulation.

from_int

static AerDensityMatrix.from_int(i, dims)

Return a computational basis state density matrix.

Parameters

  • i (int) – the basis state element.
  • dims (int or tuple or list) – The subsystem dimensions of the statevector (See additional information).

Returns

The computational basis state i ⁣i|i\rangle\!\langle i|.

Return type

DensityMatrix

Additional Information:

The dims kwarg can be an integer or an iterable of integers.

  • Iterable – the subsystem dimensions are the values in the list with the total number of subsystems given by the length of the list.
  • Int – the integer specifies the total dimension of the state. If it is a power of two the state will be initialized as an N-qubit state. If it is not a power of two the state will have a single d-dimensional subsystem.

from_label

classmethod AerDensityMatrix.from_label(label)

Return a tensor product of Pauli X,Y,Z eigenstates.

LabelStatevector

| "0" | (1000)\begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix} | | "1" | (0001)\begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix} | | "+" | 12(1111)\frac{1}{2}\begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix} | | "-" | 12(1111)\frac{1}{2}\begin{pmatrix} 1 & -1 \\ -1 & 1 \end{pmatrix} | | "r" | 12(1ii1)\frac{1}{2}\begin{pmatrix} 1 & -i \\ i & 1 \end{pmatrix} | | "l" | 12(1ii1)\frac{1}{2}\begin{pmatrix} 1 & i \\ -i & 1 \end{pmatrix} |

Parameters

label (string) – a eigenstate string ket label (see table for allowed values).

Returns

The N-qubit basis state density matrix.

Return type

Statevector

Raises

QiskitError – if the label contains invalid characters, or the length of the label is larger than an explicitly specified num_qubits.

is_valid

AerDensityMatrix.is_valid(atol=None, rtol=None)

Return True if trace 1 and positive semidefinite.

measure

AerDensityMatrix.measure(qargs=None)

Measure subsystems and return outcome and post-measure state.

Note that this function uses the QuantumStates internal random number generator for sampling the measurement outcome. The RNG seed can be set using the seed() method.

Parameters

qargs (list or None) – subsystems to sample measurements for, if None sample measurement of all subsystems (Default: None).

Returns

the pair (outcome, state) where outcome is the

measurement outcome string label, and state is the collapsed post-measurement state for the corresponding outcome.

Return type

tuple

metadata

AerDensityMatrix.metadata()

Return result metadata of an operation that executed lastly.

probabilities

AerDensityMatrix.probabilities(qargs=None, decimals=None)

Return the subsystem measurement probability vector.

Measurement probabilities are with respect to measurement in the computation (diagonal) basis.

Parameters

  • qargs (None or list) – subsystems to return probabilities for, if None return for all subsystems (Default: None).
  • decimals (None or int) – the number of decimal places to round values. If None no rounding is done (Default: None).

Returns

The Numpy vector array of probabilities.

Return type

np.array

Examples

Consider a 2-qubit product state ρ=ρ1ρ0\rho=\rho_1\otimes\rho_0 with ρ1=+ ⁣+\rho_1=|+\rangle\!\langle+|, ρ0=0 ⁣0\rho_0=|0\rangle\!\langle0|.

from qiskit.quantum_info import DensityMatrix
 
rho = DensityMatrix.from_label('+0')
 
# Probabilities for measuring both qubits
probs = rho.probabilities()
print('probs: {}'.format(probs))
 
# Probabilities for measuring only qubit-0
probs_qubit_0 = rho.probabilities([0])
print('Qubit-0 probs: {}'.format(probs_qubit_0))
 
# Probabilities for measuring only qubit-1
probs_qubit_1 = rho.probabilities([1])
print('Qubit-1 probs: {}'.format(probs_qubit_1))
probs: [0.5 0.  0.5 0. ]
Qubit-0 probs: [1. 0.]
Qubit-1 probs: [0.5 0.5]

We can also permute the order of qubits in the qargs list to change the qubit position in the probabilities output

from qiskit.quantum_info import DensityMatrix
 
rho = DensityMatrix.from_label('+0')
 
# Probabilities for measuring both qubits
probs = rho.probabilities([0, 1])
print('probs: {}'.format(probs))
 
# Probabilities for measuring both qubits
# but swapping qubits 0 and 1 in output
probs_swapped = rho.probabilities([1, 0])
print('Swapped probs: {}'.format(probs_swapped))
probs: [0.5 0.  0.5 0. ]
Swapped probs: [0.5 0.5 0.  0. ]

probabilities_dict

AerDensityMatrix.probabilities_dict(qargs=None, decimals=None)

Return the subsystem measurement probability dictionary.

Measurement probabilities are with respect to measurement in the computation (diagonal) basis.

This dictionary representation uses a Ket-like notation where the dictionary keys are qudit strings for the subsystem basis vectors. If any subsystem has a dimension greater than 10 comma delimiters are inserted between integers so that subsystems can be distinguished.

Parameters

  • qargs (None or list) – subsystems to return probabilities for, if None return for all subsystems (Default: None).
  • decimals (None or int) – the number of decimal places to round values. If None no rounding is done (Default: None).

Returns

The measurement probabilities in dict (ket) form.

Return type

dict

purity

AerDensityMatrix.purity()

Return the purity of the quantum state.

reset

AerDensityMatrix.reset(qargs=None)

Reset state or subsystems to the 0-state.

Parameters

qargs (list or None) – subsystems to reset, if None all subsystems will be reset to their 0-state (Default: None).

Returns

the reset state.

Return type

DensityMatrix

Additional Information:

If all subsystems are reset this will return the ground state on all subsystems. If only a some subsystems are reset this function will perform evolution by the reset SuperOp of the reset subsystems.

reverse_qargs

AerDensityMatrix.reverse_qargs()

Return a DensityMatrix with reversed subsystem ordering.

For a tensor product state this is equivalent to reversing the order of tensor product subsystems. For a density matrix ρ=ρn1...ρ0\rho = \rho_{n-1} \otimes ... \otimes \rho_0 the returned state will be ρ0...ρn1\rho_0 \otimes ... \otimes \rho_{n-1}.

Returns

the state with reversed subsystem order.

Return type

DensityMatrix

sample_counts

AerDensityMatrix.sample_counts(shots, qargs=None)

Sample a dict of qubit measurement outcomes in the computational basis.

Parameters

  • shots (int) – number of samples to generate.
  • qargs (None or list) – subsystems to sample measurements for, if None sample measurement of all subsystems (Default: None).

Returns

sampled counts dictionary.

Return type

Counts

Additional Information:

This function samples measurement outcomes using the measure probabilities() for the current state and qargs. It does not actually implement the measurement so the current state is not modified.

The seed for random number generator used for sampling can be set to a fixed value by using the stats seed() method.

sample_memory

AerDensityMatrix.sample_memory(shots, qargs=None)

Sample a list of qubit measurement outcomes in the computational basis.

Parameters

  • shots (int) – number of samples to generate.
  • qargs (None or list) – subsystems to sample measurements for, if None sample measurement of all subsystems (Default: None).

Returns

list of sampled counts if the order sampled.

Return type

np.array

Additional Information:

This function samples measurement outcomes using the measure probabilities() for the current state and qargs. It does not actually implement the measurement so the current state is not modified.

The seed for random number generator used for sampling can be set to a fixed value by using the stats seed() method.

seed

AerDensityMatrix.seed(value=None)

Set the seed for the quantum state RNG.

tensor

AerDensityMatrix.tensor(other)

Return the tensor product state self ⊗ other. :param other: a quantum state object. :type other: AerDensityMatrix

Returns

the tensor product operator self ⊗ other.

Return type

AerDensityMatrix

Raises

QiskitError – if other is not a quantum state.

to_dict

AerDensityMatrix.to_dict(decimals=None)

Convert the density matrix to dictionary form.

This dictionary representation uses a Ket-like notation where the dictionary keys are qudit strings for the subsystem basis vectors. If any subsystem has a dimension greater than 10 comma delimiters are inserted between integers so that subsystems can be distinguished.

Parameters

decimals (None or int) – the number of decimal places to round values. If None no rounding is done (Default: None).

Returns

the dictionary form of the DensityMatrix.

Return type

dict

Examples

The ket-form of a 2-qubit density matrix rho= ⁣0 ⁣0rho = |-\rangle\!\langle -|\otimes |0\rangle\!\langle 0|

from qiskit.quantum_info import DensityMatrix
 
rho = DensityMatrix.from_label('-0')
print(rho.to_dict())
{
    '00|00': (0.4999999999999999+0j),
    '10|00': (-0.4999999999999999-0j),
    '00|10': (-0.4999999999999999+0j),
    '10|10': (0.4999999999999999+0j)
}

For non-qubit subsystems the integer range can go from 0 to 9. For example in a qutrit system

import numpy as np
from qiskit.quantum_info import DensityMatrix
 
mat = np.zeros((9, 9))
mat[0, 0] = 0.25
mat[3, 3] = 0.25
mat[6, 6] = 0.25
mat[-1, -1] = 0.25
rho = DensityMatrix(mat, dims=(3, 3))
print(rho.to_dict())
{'00|00': (0.25+0j), '10|10': (0.25+0j), '20|20': (0.25+0j), '22|22': (0.25+0j)}

For large subsystem dimensions delimiters are required. The following example is for a 20-dimensional system consisting of a qubit and 10-dimensional qudit.

import numpy as np
from qiskit.quantum_info import DensityMatrix
 
mat = np.zeros((2 * 10, 2 * 10))
mat[0, 0] = 0.5
mat[-1, -1] = 0.5
rho = DensityMatrix(mat, dims=(2, 10))
print(rho.to_dict())
{'00|00': (0.5+0j), '91|91': (0.5+0j)}

to_operator

AerDensityMatrix.to_operator()

Convert to Operator

to_statevector

AerDensityMatrix.to_statevector(atol=None, rtol=None)

Return a statevector from a pure density matrix. :param atol: Absolute tolerance for checking operation validity. :type atol: float :param rtol: Relative tolerance for checking operation validity. :type rtol: float

Returns

The pure density matrix’s corresponding statevector.

Corresponds to the eigenvector of the only non-zero eigenvalue.

Return type

AerStatevector

Raises

QiskitError – if the state is not pure.

trace

AerDensityMatrix.trace()

Return the trace of the density matrix.


Attributes

atol

Default value: 1e-08

data

Return data.

dim

Return total state dimension.

num_qubits

Return the number of qubits if a N-qubit state or None otherwise.

rtol

Default value: 1e-05

settings

Return settings.

Was this page helpful?
Report a bug or request content on GitHub.