Skip to content

two_qubit_gate

TwoQubitGate

Bases: Gate

Source code in opensquirrel/ir/two_qubit_gate.py
class TwoQubitGate(Gate):
    def __init__(
        self, qubit0: QubitLike, qubit1: QubitLike, gate_semantic: GateSemantic, name: str = "TwoQubitGate"
    ) -> None:
        Gate.__init__(self, name)
        self.qubit0 = Qubit(qubit0)
        self.qubit1 = Qubit(qubit1)

        self._controlled = gate_semantic if isinstance(gate_semantic, ControlledGateSemantic) else None
        self._matrix = gate_semantic if isinstance(gate_semantic, MatrixGateSemantic) else None
        self._canonical = gate_semantic if isinstance(gate_semantic, CanonicalGateSemantic) else None
        self.gate_semantic = gate_semantic

        if self._check_repeated_qubit_operands(self.qubit_operands):
            msg = "qubit operands cannot be the same qubit"
            raise ValueError(msg)

    @cached_property
    def matrix(self) -> MatrixGateSemantic:
        if self._matrix:
            return self._matrix

        if self.controlled:
            self._matrix = MatrixGateSemantic(get_matrix(self, 2))
            return self._matrix

        if self.canonical:
            from opensquirrel.utils.matrix_expander import can2

            return MatrixGateSemantic(can2(self.canonical.axis))
        return MatrixGateSemantic(np.eye(4))

    @cached_property
    def canonical(self) -> CanonicalGateSemantic | None:
        return self._canonical

    @cached_property
    def controlled(self) -> ControlledGateSemantic | None:
        return self._controlled

    @property
    def qubit_operands(self) -> tuple[Qubit, ...]:
        return (self.qubit0, self.qubit1)

    def accept(self, visitor: IRVisitor) -> Any:
        """Accepts visitor and processes this IR node."""
        visit_parent = super().accept(visitor)
        return visit_parent if visit_parent is not None else visitor.visit_two_qubit_gate(self)

    def is_identity(self) -> bool:
        """Checks if the two-qubit gate is an identity gate.

        Returns:
            True if the two-qubit gate is an identity gate, False otherwise.

        """
        if self.controlled:
            return self.controlled.is_identity()
        if self.matrix:
            return self.matrix.is_identity()
        if self.canonical:
            return self.canonical.is_identity()
        return False

    def __repr__(self) -> str:
        return f"TwoQubitGate(qubits=[{self.qubit0, self.qubit1}], gate_semantic={self.gate_semantic})"

accept

accept(visitor: IRVisitor) -> Any

Accepts visitor and processes this IR node.

Source code in opensquirrel/ir/two_qubit_gate.py
def accept(self, visitor: IRVisitor) -> Any:
    """Accepts visitor and processes this IR node."""
    visit_parent = super().accept(visitor)
    return visit_parent if visit_parent is not None else visitor.visit_two_qubit_gate(self)

is_identity

is_identity() -> bool

Checks if the two-qubit gate is an identity gate.

Returns:

Type Description
bool

True if the two-qubit gate is an identity gate, False otherwise.

Source code in opensquirrel/ir/two_qubit_gate.py
def is_identity(self) -> bool:
    """Checks if the two-qubit gate is an identity gate.

    Returns:
        True if the two-qubit gate is an identity gate, False otherwise.

    """
    if self.controlled:
        return self.controlled.is_identity()
    if self.matrix:
        return self.matrix.is_identity()
    if self.canonical:
        return self.canonical.is_identity()
    return False