qrisp.operators.qubit.QubitOperator.from_matrix#

classmethod QubitOperator.from_matrix(matrix)[source]#

Represents a matrix as an operator

\[O=\sum_i\alpha_i\bigotimes_{j=0}^{n-1}O_{ij}\]

where \(O_{ij}\in\{A,C,P_0,P_1\}\).

Parameters:
matrixnumpy.ndarray or scipy.sparse.csr_matrix

The matrix.

Returns:
QubitOperator

The operator represented by the matrix.

Examples

from scipy.sparse import csr_matrix
from qrisp.operators import QubitOperator

sparse_matrix = csr_matrix([[0, 5, 0, 1],
                            [5, 0, 0, 0],
                            [0, 0, 0, 2],
                            [1, 0, 2, 0]])

O = QubitOperator.from_matrix(sparse_matrix)
print(O)
# Yields: A_0*A_1 + C_0*C_1 + 5*P^0_0*A_1 + 5*P^0_0*C_1 + 2*P^1_0*A_1 + 2*P^1_0*C_1