DensityMatrix
表示为半正定、迹为 1 的密度矩阵
有效的密度矩阵满足:
- 厄米性:
- 半正定性:所有特征值
- 迹为 1:
构造函数
默认构造函数
python
DensityMatrix()构造
拷贝构造函数
python
DensityMatrix(other: DensityMatrix)作为另一个 DensityMatrix 的拷贝构造 DensityMatrix。
| 参数 | 类型 | 说明 |
|---|---|---|
| other | DensityMatrix | 要拷贝的现有 DensityMatrix。 |
从二维复数列表构造
python
DensityMatrix(data: list[list[complex]])从复数的二维列表构造密度矩阵。
| 参数 | 类型 | 说明 |
|---|---|---|
| data | list[list[complex]] | 复数的方阵二维数组。 |
从 numpy 数组 / Eigen 矩阵构造
python
DensityMatrix(data: numpy.ndarray)从二维复数 numpy 数组(或 Eigen 矩阵)构造密度矩阵。
| 参数 | 类型 | 说明 |
|---|---|---|
| data | numpy.ndarray | 复数的方阵二维数组。 |
从量子比特数构造
python
DensityMatrix(qbit_total: int)构造 qbit_total 个量子比特的密度矩阵,初始化为全零态
| 参数 | 类型 | 说明 |
|---|---|---|
| qbit_total | int | 量子比特数。结果矩阵的维度为 |
从 StateVector 构造
python
DensityMatrix(other: StateVector)从现有 StateVector 构造密度矩阵为
| 参数 | 类型 | 说明 |
|---|---|---|
| other | StateVector | 纯量子态。 |
方法
ndarray
python
ndarray() -> numpy.ndarray返回内部密度矩阵数据作为二维 numpy.ndarray。
purity
python
purity() -> float返回密度矩阵的纯度
evolve
python
evolve(circuit: QCircuit) -> DensityMatrix在给定量子线路下演化密度矩阵。返回一个新的 DensityMatrix 包含结果;原始对象不被修改。
| 参数 | 类型 | 说明 |
|---|---|---|
| circuit | QCircuit | 要应用的量子线路。 |
update_by_evolve
python
update_by_evolve(circuit: QCircuit) -> DensityMatrix在给定量子线路下原地演化密度矩阵,更新内部数据。同时返回结果的 DensityMatrix。
| 参数 | 类型 | 说明 |
|---|---|---|
| circuit | QCircuit | 要应用的量子线路。 |
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_idx | int | 行索引(从 0 开始)。 |
| col_idx | int | 列索引(从 0 开始)。 |
dim
python
dim() -> int返回密度矩阵的维度
运算符
相等
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()