Skip to content

single_qubit_gate

SingleQubitGate

Bases: Gate

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

        self._bsr = gate_semantic if isinstance(gate_semantic, BlochSphereRotation) else None
        self._matrix = gate_semantic if isinstance(gate_semantic, MatrixGateSemantic) else None

    @cached_property
    def bsr(self) -> BlochSphereRotation:
        if self._bsr is None:
            from opensquirrel.ir.semantics.bsr import bsr_from_matrix

            self._bsr = bsr_from_matrix(self.matrix)
        return self._bsr

    @cached_property
    def matrix(self) -> MatrixGateSemantic:
        if self._matrix is None:
            from opensquirrel.utils import can1

            self._matrix = MatrixGateSemantic(can1(self.bsr.axis, self.bsr.angle, self.bsr.phase))
        return self._matrix

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

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

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

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

        """
        if self.bsr is not None:
            return self.bsr.is_identity()
        return self.matrix.is_identity() if self.matrix else False

    def __eq__(self, other: Any) -> bool:
        if not isinstance(other, SingleQubitGate):
            return False

        if self.qubit != other.qubit:
            return False

        return self.bsr == other.bsr

    def __mul__(self, other: SingleQubitGate) -> SingleQubitGate:
        if self.qubit != other.qubit:
            msg = "cannot merge two single-qubit gates on different qubits"
            raise ValueError(msg)
        return SingleQubitGate(self.qubit, self.bsr * other.bsr)

accept

accept(visitor: IRVisitor) -> Any

Accepts visitor and processes this IR node.

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

is_identity

is_identity() -> bool

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

Returns:

Type Description
bool

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

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

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

    """
    if self.bsr is not None:
        return self.bsr.is_identity()
    return self.matrix.is_identity() if self.matrix else False

try_match_replace_with_default_gate

try_match_replace_with_default_gate(
    gate: SingleQubitGate,
) -> SingleQubitGate

Tries to match a given single-qubit gate with a default single-qubit gate. It does that by checking if the parameters of the Bloch sphere rotation (BSR) semantic of the input single-qubit gate are close to those of any of the default single-qubit gates.

Note
  • The default (single-qubit) gates are defined in the cQASM standard gate set.
  • If no specific match is found, the general Rn gate is returned with the same parameters values as those of the input gate.

Parameters:

Name Type Description Default
gate SingleQubitGate

The single-qubit gate to be matched.

required

Returns:

Type Description
SingleQubitGate

A default single-qubit gate if this single-qubit gate matches it, the Rn gate otherwise.

Source code in opensquirrel/ir/single_qubit_gate.py
def try_match_replace_with_default_gate(gate: SingleQubitGate) -> SingleQubitGate:
    """Tries to match a given single-qubit gate with a _default_ single-qubit gate.
    It does that by checking if the parameters of the Bloch sphere rotation (BSR) semantic of the
    input single-qubit gate are close to those of any of the default single-qubit gates.

    Note:
        - The default (single-qubit) gates are defined in the
        [cQASM standard gate set](https://qutech-delft.github.io/cQASM-spec/latest/standard_gate_set/index.html).
        - If no specific match is found, the general Rn gate is returned with the same parameters
        values as those of the input gate.

    Args:
        gate: The single-qubit gate to be matched.

    Returns:
        A default single-qubit gate if this single-qubit gate matches it, the Rn gate otherwise.

    """
    from opensquirrel.default_instructions import (
        default_bsr_with_param_set,
        default_single_qubit_gate_set,
    )
    from opensquirrel.ir.default_gates.single_qubit_gates import Rn

    for gate_name in default_single_qubit_gate_set:
        if gate_name in ("Rn", "U"):
            continue

        arguments: tuple[Any, ...] = (gate.qubit,)
        if gate_name in default_bsr_with_param_set:
            arguments += (Float(gate.bsr.angle),)

        possible_gate = default_single_qubit_gate_set[gate_name](*arguments)  # ty: ignore[invalid-argument-type]
        if possible_gate == gate:
            return possible_gate

    nx, ny, nz = gate.bsr.axis.value
    return Rn(gate.qubit, nx=nx, ny=ny, nz=nz, theta=gate.bsr.angle, phi=gate.bsr.phase)