Skip to content

DensityMatrix

表示为半正定、迹为 1 的密度矩阵 ρ 的一般(混合或纯)量子态,维度为 2n×2n,其中 n 是量子比特数。

有效的密度矩阵满足:

  • 厄米性:ρ=ρ
  • 半正定性:所有特征值 0
  • 迹为 1:Tr(ρ)=1

构造函数

默认构造函数

python
DensityMatrix()

构造 |0 态的单量子比特密度矩阵:ρ=|00|=(1000)

拷贝构造函数

python
DensityMatrix(other: DensityMatrix)

作为另一个 DensityMatrix 的拷贝构造 DensityMatrix

参数类型说明
otherDensityMatrix要拷贝的现有 DensityMatrix

从二维复数列表构造

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

从复数的二维列表构造密度矩阵。

参数类型说明
datalist[list[complex]]复数的方阵二维数组。

从 numpy 数组 / Eigen 矩阵构造

python
DensityMatrix(data: numpy.ndarray)

从二维复数 numpy 数组(或 Eigen 矩阵)构造密度矩阵。

参数类型说明
datanumpy.ndarray复数的方阵二维数组。

从量子比特数构造

python
DensityMatrix(qbit_total: int)

构造 qbit_total 个量子比特的密度矩阵,初始化为全零态 |0n

参数类型说明
qbit_totalint量子比特数。结果矩阵的维度为 2qbit_total×2qbit_total

从 StateVector 构造

python
DensityMatrix(other: StateVector)

从现有 StateVector 构造密度矩阵为 ρ=|ψψ|

参数类型说明
otherStateVector纯量子态。

方法

ndarray

python
ndarray() -> numpy.ndarray

返回内部密度矩阵数据作为二维 numpy.ndarray

purity

python
purity() -> float

返回密度矩阵的纯度 Tr(ρ2)。纯态的纯度为 1.0;最大混合态的纯度为 1/d,其中 d=2n

evolve

python
evolve(circuit: QCircuit) -> DensityMatrix

在给定量子线路下演化密度矩阵。返回一个DensityMatrix 包含结果;原始对象不被修改。

参数类型说明
circuitQCircuit要应用的量子线路。

update_by_evolve

python
update_by_evolve(circuit: QCircuit) -> DensityMatrix

在给定量子线路下原地演化密度矩阵,更新内部数据。同时返回结果的 DensityMatrix

参数类型说明
circuitQCircuit要应用的量子线路。

is_valid

python
is_valid() -> bool

检查内部数据是否构成有效的密度矩阵(厄米、半正定、迹为 1)。

to_statevector

python
to_statevector() -> StateVector

如果密度矩阵表示纯态(纯度 = 1),返回对应的 StateVector。这等价于提取与特征值 1 关联的特征向量。

at

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

返回指定行和列处的元素。

参数类型说明
row_idxint行索引(从 0 开始)。
col_idxint列索引(从 0 开始)。

dim

python
dim() -> int

返回密度矩阵的维度 2n(基态数)。

运算符

相等

python
dm_a == dm_b -> bool

如果两个 DensityMatrix 对象具有相同的内部数据则返回 True

示例

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

# 从 StateVector 构造(纯态)
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

# 从二维数组构造(最大混合态)
dm_mixed = DensityMatrix([[0.5+0j, 0+0j], [0+0j, 0.5+0j]])
print(dm_mixed.purity())  # 0.5

# 访问元素
val = dm.at(0, 1)  # (0.5+0j)

# 转换回态矢量(仅纯态)
sv_back = dm.to_statevector()

# 获取 numpy 数组
arr = dm.ndarray()

另见

Released under the MIT License.