Skip to content

Matrix ​

通用复值矩阵类,在量子态和信道表示内部使用。Matrix 封装了底层复矩阵并提供基本的线性代数运算。

签名 ​

python
Matrix(data: numpy.ndarray)

构造函数 ​

默认构造函数 ​

python
Matrix()

构造空的 Matrix 对象。

从数据构造 ​

python
Matrix(data: numpy.ndarray)

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

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

方法 ​

ndarray ​

python
ndarray() -> numpy.ndarray[numpy.complex128]

返回内部矩阵数据作为 numpy.ndarray。

row_total ​

python
row_total() -> int

返回矩阵的行数。

col_total ​

python
col_total() -> int

返回矩阵的列数。

is_hermitian ​

python
is_hermitian() -> bool

如果矩阵是厄米矩阵则返回 True,即 A=A†。

transpose ​

python
transpose() -> Matrix

返回矩阵的转置 AT。

T ​

python
T() -> Matrix

transpose() 的别名。返回矩阵的转置。

hermitian_conjugate ​

python
hermitian_conjugate() -> Matrix

返回矩阵的共轭转置(厄米伴随)A†。

adjoint ​

python
adjoint() -> Matrix

hermitian_conjugate() 的别名。返回矩阵的共轭转置。

L2 ​

python
L2() -> complex

返回谱范数(通过 SVD 计算最大奇异值)。对于向量则返回欧几里得范数。

at ​

python
at(rowIdx: int, colIdx: int) -> complex

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

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

运算符 ​

相等 ​

python
matrix_a == matrix_b -> bool

如果两个矩阵具有相同的内部数据则返回 True。

示例 ​

python
import numpy as np
from pyqpanda3.quantum_info import Matrix

# 从 numpy 数组构造
data = np.array([[1+0j, 0+1j],
                 [0-1j, 1+0j]])
m = Matrix(data)

print(m.row_total())      # 2
print(m.col_total())      # 2
print(m.is_hermitian())   # True

# 转置和伴随
mt = m.transpose()
ma = m.adjoint()

# 访问单个元素
val = m.at(0, 1)  # 0+1j
print(val)

# 获取底层 numpy 数组
arr = m.ndarray()
print(arr)

运行结果:

2
2
True
1j
[[1.+0.j 0.+1.j]
 [0.-1.j 1.+0.j]]

另见 ​

Released under the MIT License.