Skip to content

matrix_gate

MatrixGateSemantic

Bases: GateSemantic

Source code in opensquirrel/ir/semantics/matrix_gate.py
class MatrixGateSemantic(GateSemantic):
    def __init__(self, matrix: ArrayLike | list[list[int | DTypeLike]]) -> None:
        self.matrix = np.asarray(matrix, dtype=np.complex128)

        number_of_rows, number_of_cols = self.matrix.shape
        if number_of_cols != number_of_rows:
            msg = (
                f"incorrect matrix shape {number_of_rows=} and {number_of_cols=}: number of rows should be equal to"
                " the number of columns"
            )
            raise ValueError(msg)

        if number_of_cols & (number_of_cols - 1) != 0:
            msg = (
                f"incorrect matrix shape  {number_of_rows=} and {number_of_cols=}: number of rows/columns should be"
                " a power of 2."
            )
            raise ValueError(msg)

    def accept(self, visitor: IRVisitor) -> Any:
        """Accepts visitor and processes this IR node."""
        return visitor.visit_matrix_gate_semantic(self)

    def is_identity(self) -> bool:
        """Checks if the matrix gate semantic represents an identity operation.

        Returns:
            True if the matrix gate semantic represents an identity operation, False otherwise.
        """
        return np.allclose(self.matrix, np.eye(self.matrix.shape[0]), atol=ATOL)

    def __array__(self, *args: Any, **kwargs: Any) -> NDArray[np.complex128]:
        return np.asarray(self.matrix, *args, **kwargs)

    def __repr__(self) -> str:
        return f"MatrixGateSemantic(matrix={repr_round(self.matrix)})"

accept

accept(visitor: IRVisitor) -> Any

Accepts visitor and processes this IR node.

Source code in opensquirrel/ir/semantics/matrix_gate.py
def accept(self, visitor: IRVisitor) -> Any:
    """Accepts visitor and processes this IR node."""
    return visitor.visit_matrix_gate_semantic(self)

is_identity

is_identity() -> bool

Checks if the matrix gate semantic represents an identity operation.

Returns:

Type Description
bool

True if the matrix gate semantic represents an identity operation, False otherwise.

Source code in opensquirrel/ir/semantics/matrix_gate.py
def is_identity(self) -> bool:
    """Checks if the matrix gate semantic represents an identity operation.

    Returns:
        True if the matrix gate semantic represents an identity operation, False otherwise.
    """
    return np.allclose(self.matrix, np.eye(self.matrix.shape[0]), atol=ATOL)