Skip to content

Matrix

A general-purpose complex-valued matrix class used internally by quantum state and channel representations. Matrix wraps an underlying complex matrix and provides basic linear-algebra operations.

Signature

python
Matrix(data: numpy.ndarray)

Constructors

Default constructor

python
Matrix()

Constructs an empty Matrix object.

From data

python
Matrix(data: numpy.ndarray)

Constructs a Matrix from a 2-D complex numpy array (or an Eigen matrix).

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

Methods

ndarray

python
ndarray() -> numpy.ndarray

Return the internal matrix data as a numpy.ndarray.

row_total

python
row_total() -> int

Return the number of rows in the matrix.

col_total

python
col_total() -> int

Return the number of columns in the matrix.

is_hermitian

python
is_hermitian() -> bool

Return True if the matrix is Hermitian, i.e., A=A.

transpose

python
transpose() -> Matrix

Return the transpose of the matrix, AT.

T

python
T() -> Matrix

Alias for transpose(). Return the transpose of the matrix.

hermitian_conjugate

python
hermitian_conjugate() -> Matrix

Return the conjugate transpose (Hermitian adjoint) of the matrix, A.

adjoint

python
adjoint() -> Matrix

Alias for hermitian_conjugate(). Return the conjugate transpose of the matrix.

L2

python
L2() -> float

Return the L2 (Frobenius) norm of the matrix:

|A|F=i,j|aij|2

at

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

Return the element at the specified row and column index.

ParameterTypeDescription
rowIdxintRow index (0-based).
colIdxintColumn index (0-based).

Operators

Equality

python
matrix_a == matrix_b -> bool

Return True if the two matrices have identical internal data.

Examples

python
import numpy as np
from pyqpanda3.quantum_info import Matrix

# Construct from a numpy array
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

# Transpose and adjoint
mt = m.transpose()
ma = m.adjoint()

# Access a single element
val = m.at(0, 1)  # 0+1j

# Get the underlying numpy array
arr = m.ndarray()

See Also

Released under the MIT License.