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: ρ=|0⟩⟨0|=(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 |0⟩⊗n.

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[numpy.complex128]

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

purity ​

python
purity() -> complex

Return the purity Tr(ρ2) of the density matrix. A pure state has purity (1+0j) (conceptually 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.

NOTE

QCircuit and gate operations are available from pyqpanda3.core:

python
from pyqpanda3.core import QCircuit, H
circ = QCircuit() << H(0)
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() -> list[complex]

If the density matrix represents a pure state (purity = 1), return the corresponding state vector amplitudes as a list of complex numbers. This is equivalent to extracting the eigenvector associated with the eigenvalue 1.

NOTE

Calling to_statevector() on a mixed state returns an empty list [].

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.0000000000000004+0j)

# 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+0j)

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

# Convert back to a state vector (pure states only, returns list of complex amplitudes)
sv_back = dm.to_statevector()
# sv_back == [(0.7071067811865475+0j), (0.7071067811865475+0j)]

# Get numpy array
arr = dm.ndarray()
print(arr)

Running the example produces:

2
True
(1.0000000000000004+0j)
(0.5+0j)
(0.5000000000000001+0j)
[[0.5+0.j 0.5+0.j]
 [0.5+0.j 0.5+0.j]]

See Also ​

Released under the MIT License.