Skip to content

pauli

Single-qubit Pauli operators.

Pauli

Single-qubit Pauli operator: I, X, Y, or Z.

Source code in shadowsim/core/pauli.py
class Pauli:
    """Single-qubit Pauli operator: I, X, Y, or Z."""

    __slots__ = ("_label",)

    def __init__(self, label: str) -> None:
        """Initialize a Pauli from a label in ``{I, X, Y, Z}``."""
        label = label.upper()
        if label not in _PAULI_MATRICES:
            raise ValueError(f"invalid Pauli label {label!r}; expected one of I, X, Y, Z")
        self._label = label

    @property
    def label(self) -> str:
        """Return the Pauli label in ``{I, X, Y, Z}``."""
        return self._label

    def matrix(self) -> npt.NDArray[np.complexfloating]:
        """Return a copy of the Pauli matrix."""
        return _PAULI_MATRICES[self._label].copy()

    def __str__(self) -> str:
        """Return a string representation of the Pauli."""
        return self._label

    def __repr__(self) -> str:
        """Return a string representation of the Pauli."""
        return f"Pauli({self._label!r})"

    def __eq__(self, other: object) -> bool:
        """Return whether this Pauli equals another."""
        if not isinstance(other, Pauli):
            return NotImplemented
        return self._label == other._label

    def __hash__(self) -> int:
        """Return a hash based on the Pauli label."""
        return hash(self._label)

    def multiply(self, other: Self) -> tuple[complex, Self]:
        """Multiply in the Pauli group: ``self * other = phase * result``.

        ``phase`` is in ``{±1, ±i}``.
        """
        phase, label = _multiply_labels(self._label, other._label)
        return phase, type(self)(label)

    def commutator(self, other: Self) -> tuple[complex | None, Self | None]:
        """Return ``[self, other]`` as ``phase * P``, or ``(None, None)`` if zero.

        For single-qubit Paulis this is either ``0`` or ``2i`` times the third Pauli.
        """
        if not _single_qubit_anticommutes(self._label, other._label):
            return None, None
        phase, res = self.multiply(other)
        return 2 * phase, res

label property

label: str

Return the Pauli label in {I, X, Y, Z}.

__init__

__init__(label: str) -> None

Initialize a Pauli from a label in {I, X, Y, Z}.

Source code in shadowsim/core/pauli.py
def __init__(self, label: str) -> None:
    """Initialize a Pauli from a label in ``{I, X, Y, Z}``."""
    label = label.upper()
    if label not in _PAULI_MATRICES:
        raise ValueError(f"invalid Pauli label {label!r}; expected one of I, X, Y, Z")
    self._label = label

matrix

matrix() -> NDArray[complexfloating]

Return a copy of the Pauli matrix.

Source code in shadowsim/core/pauli.py
def matrix(self) -> npt.NDArray[np.complexfloating]:
    """Return a copy of the Pauli matrix."""
    return _PAULI_MATRICES[self._label].copy()

__str__

__str__() -> str

Return a string representation of the Pauli.

Source code in shadowsim/core/pauli.py
def __str__(self) -> str:
    """Return a string representation of the Pauli."""
    return self._label

__repr__

__repr__() -> str

Return a string representation of the Pauli.

Source code in shadowsim/core/pauli.py
def __repr__(self) -> str:
    """Return a string representation of the Pauli."""
    return f"Pauli({self._label!r})"

__eq__

__eq__(other: object) -> bool

Return whether this Pauli equals another.

Source code in shadowsim/core/pauli.py
def __eq__(self, other: object) -> bool:
    """Return whether this Pauli equals another."""
    if not isinstance(other, Pauli):
        return NotImplemented
    return self._label == other._label

__hash__

__hash__() -> int

Return a hash based on the Pauli label.

Source code in shadowsim/core/pauli.py
def __hash__(self) -> int:
    """Return a hash based on the Pauli label."""
    return hash(self._label)

multiply

multiply(other: Self) -> tuple[complex, Self]

Multiply in the Pauli group: self * other = phase * result.

phase is in {±1, ±i}.

Source code in shadowsim/core/pauli.py
def multiply(self, other: Self) -> tuple[complex, Self]:
    """Multiply in the Pauli group: ``self * other = phase * result``.

    ``phase`` is in ``{±1, ±i}``.
    """
    phase, label = _multiply_labels(self._label, other._label)
    return phase, type(self)(label)

commutator

commutator(
    other: Self,
) -> tuple[complex | None, Self | None]

Return [self, other] as phase * P, or (None, None) if zero.

For single-qubit Paulis this is either 0 or 2i times the third Pauli.

Source code in shadowsim/core/pauli.py
def commutator(self, other: Self) -> tuple[complex | None, Self | None]:
    """Return ``[self, other]`` as ``phase * P``, or ``(None, None)`` if zero.

    For single-qubit Paulis this is either ``0`` or ``2i`` times the third Pauli.
    """
    if not _single_qubit_anticommutes(self._label, other._label):
        return None, None
    phase, res = self.multiply(other)
    return 2 * phase, res