Skip to content

DensityMatrix

A general (mixed or pure) quantum state represented as a positive semi-definite, trace-1 density matrix ρ of dimension 2n×2n, where n is the number of qubits.

A valid density matrix satisfies:

  • Hermiticity: ρ=ρ
  • Positive semi-definiteness: all eigenvalues 0
  • Unit trace: Tr(ρ)=1

Constructors

Default constructor

python
DensityMatrix()

Constructs a single-qubit density matrix for the |0 state: ρ=|00|=(1000).

Copy constructor

python
DensityMatrix(other: DensityMatrix)

Construct a DensityMatrix as a copy of another DensityMatrix.

ParameterTypeDescription
otherDensityMatrixAn existing DensityMatrix to copy.

From a 2-D complex list

python
DensityMatrix(data: list[list[complex]])

Construct a density matrix from a 2-D list of complex numbers.

ParameterTypeDescription
datalist[list[complex]]A square 2-D array of complex numbers.

From a numpy array / Eigen matrix

python
DensityMatrix(data: numpy.ndarray)

Construct a density matrix from a 2-D complex numpy array (or Eigen matrix).

ParameterTypeDescription
datanumpy.ndarrayA square 2-D array of complex numbers.

From qubit count

python
DensityMatrix(qbit_total: int)

Construct a density matrix for qbit_total qubits initialized to the all-zeros state |0n.

ParameterTypeDescription
qbit_totalintThe number of qubits. The resulting matrix has dimension 2qbit_total×2qbit_total.

From a StateVector

python
DensityMatrix(other: StateVector)

Construct a density matrix as ρ=|ψψ| from an existing StateVector.

ParameterTypeDescription
otherStateVectorA pure quantum state.

Methods

ndarray

python
ndarray() -> numpy.ndarray

Return the internal density-matrix data as a 2-D numpy.ndarray.

purity

python
purity() -> float

Return the purity Tr(ρ2) of the density matrix. A pure state has purity 1.0; a maximally mixed state has purity 1/d where d=2n.

evolve

python
evolve(circuit: QCircuit) -> DensityMatrix

Evolve the density matrix under the given quantum circuit. Returns a new DensityMatrix with the result; the original object is not modified.

ParameterTypeDescription
circuitQCircuitThe quantum circuit to apply.

update_by_evolve

python
update_by_evolve(circuit: QCircuit) -> DensityMatrix

Evolve the density matrix under the given quantum circuit in place, updating the internal data. Also returns the resulting DensityMatrix.

ParameterTypeDescription
circuitQCircuitThe quantum circuit to apply.

is_valid

python
is_valid() -> bool

Check whether the internal data constitutes a valid density matrix (Hermitian, positive semi-definite, unit trace).

to_statevector

python
to_statevector() -> StateVector

If the density matrix represents a pure state (purity = 1), return the corresponding StateVector. This is equivalent to extracting the eigenvector associated with the eigenvalue 1.

at

python
at(row_idx: int, col_idx: int) -> complex

Return the element at the specified row and column.

ParameterTypeDescription
row_idxintRow index (0-based).
col_idxintColumn index (0-based).

dim

python
dim() -> int

Return the dimension 2n of the density matrix (number of basis states).

Operators

Equality

python
dm_a == dm_b -> bool

Return True if the two DensityMatrix objects have identical internal data.

Examples

python
import numpy as np
from pyqpanda3.quantum_info import DensityMatrix, StateVector

# Construct from a StateVector (pure state)
sv = StateVector([1/np.sqrt(2), 1/np.sqrt(2)])
dm = DensityMatrix(sv)

print(dm.dim())       # 2
print(dm.is_valid())  # True
print(dm.purity())    # 1.0

# Construct from a 2-D array (maximally mixed state)
dm_mixed = DensityMatrix([[0.5+0j, 0+0j], [0+0j, 0.5+0j]])
print(dm_mixed.purity())  # 0.5

# Access an element
val = dm.at(0, 1)  # (0.5+0j)

# Convert back to a state vector (pure states only)
sv_back = dm.to_statevector()

# Get numpy array
arr = dm.ndarray()

See Also

Released under the MIT License.