Skip to content

local_hamiltonian

Local Hamiltonian terms on chosen sites.

LocalHamiltonian

Bases: LocalOperator, Hamiltonian

Local Hamiltonian acting non-trivially on the provided sites.

sites are distinct integers in tensor-factor order; see LocalOperator. Gaps are allowed, and embedding puts identity on every skipped site.

Source code in shadowsim/core/local_hamiltonian.py
class LocalHamiltonian(LocalOperator, Hamiltonian):
    """Local Hamiltonian acting non-trivially on the provided sites.

    ``sites`` are distinct integers in tensor-factor order; see `LocalOperator`.
    Gaps are allowed, and embedding puts identity on every skipped site.
    """

    def __init__(
        self,
        matrix: _Matrix,
        sites: list[int],
        local_dim: int = 2,
    ) -> None:
        """Initialize a LocalHamiltonian on the given sites."""
        if isinstance(matrix, np.ndarray):
            if matrix.ndim != 2 or matrix.shape[0] != matrix.shape[1]:
                raise ValueError(f"matrix must be a square 2D array; got shape {matrix.shape}")
            if not hermitian(matrix):
                raise ValueError(f"matrix must be Hermitian; got shape {matrix.shape}")
        super().__init__(matrix, sites, local_dim)

    def to_full_hamiltonian(self, total_sites: int) -> Hamiltonian:
        """Return this local term embedded on ``total_sites`` sites.

        The embedding is `LocalOperator.to_full_operator`: ``sites[0]`` is the
        first tensor factor of the stored matrix.

        Parameter total_sites: The total number of sites in the full system.
        Precondition: total_sites is a positive integer.
        """
        return Hamiltonian(self.to_full_operator(total_sites).matrix)

    def get_sites(self) -> list[int]:
        """Return the sites on which the local Hamiltonian acts non-trivially."""
        return self.sites

    def get_local_dim(self) -> int:
        """Return the local dimension of the local Hamiltonian."""
        return self.local_dim

    def get_matrix(self) -> _Matrix:
        """Return the matrix representation of the local Hamiltonian."""
        return self.matrix

    def __str__(self) -> str:
        """Return a string representation of the LocalHamiltonian."""
        return f"LocalHamiltonian(sites={self.sites}, local_dim={self.local_dim}, shape={self.matrix.shape})"

    def __repr__(self) -> str:
        """Return a string representation of the LocalHamiltonian."""
        return f"LocalHamiltonian(matrix={self.matrix!r}, sites={self.sites!r}, local_dim={self.local_dim})"

__init__

__init__(
    matrix: _Matrix, sites: list[int], local_dim: int = 2
) -> None

Initialize a LocalHamiltonian on the given sites.

Source code in shadowsim/core/local_hamiltonian.py
def __init__(
    self,
    matrix: _Matrix,
    sites: list[int],
    local_dim: int = 2,
) -> None:
    """Initialize a LocalHamiltonian on the given sites."""
    if isinstance(matrix, np.ndarray):
        if matrix.ndim != 2 or matrix.shape[0] != matrix.shape[1]:
            raise ValueError(f"matrix must be a square 2D array; got shape {matrix.shape}")
        if not hermitian(matrix):
            raise ValueError(f"matrix must be Hermitian; got shape {matrix.shape}")
    super().__init__(matrix, sites, local_dim)

to_full_hamiltonian

to_full_hamiltonian(total_sites: int) -> Hamiltonian

Return this local term embedded on total_sites sites.

The embedding is LocalOperator.to_full_operator: sites[0] is the first tensor factor of the stored matrix.

Parameter total_sites: The total number of sites in the full system. Precondition: total_sites is a positive integer.

Source code in shadowsim/core/local_hamiltonian.py
def to_full_hamiltonian(self, total_sites: int) -> Hamiltonian:
    """Return this local term embedded on ``total_sites`` sites.

    The embedding is `LocalOperator.to_full_operator`: ``sites[0]`` is the
    first tensor factor of the stored matrix.

    Parameter total_sites: The total number of sites in the full system.
    Precondition: total_sites is a positive integer.
    """
    return Hamiltonian(self.to_full_operator(total_sites).matrix)

get_sites

get_sites() -> list[int]

Return the sites on which the local Hamiltonian acts non-trivially.

Source code in shadowsim/core/local_hamiltonian.py
def get_sites(self) -> list[int]:
    """Return the sites on which the local Hamiltonian acts non-trivially."""
    return self.sites

get_local_dim

get_local_dim() -> int

Return the local dimension of the local Hamiltonian.

Source code in shadowsim/core/local_hamiltonian.py
def get_local_dim(self) -> int:
    """Return the local dimension of the local Hamiltonian."""
    return self.local_dim

get_matrix

get_matrix() -> _Matrix

Return the matrix representation of the local Hamiltonian.

Source code in shadowsim/core/local_hamiltonian.py
def get_matrix(self) -> _Matrix:
    """Return the matrix representation of the local Hamiltonian."""
    return self.matrix

__str__

__str__() -> str

Return a string representation of the LocalHamiltonian.

Source code in shadowsim/core/local_hamiltonian.py
def __str__(self) -> str:
    """Return a string representation of the LocalHamiltonian."""
    return f"LocalHamiltonian(sites={self.sites}, local_dim={self.local_dim}, shape={self.matrix.shape})"

__repr__

__repr__() -> str

Return a string representation of the LocalHamiltonian.

Source code in shadowsim/core/local_hamiltonian.py
def __repr__(self) -> str:
    """Return a string representation of the LocalHamiltonian."""
    return f"LocalHamiltonian(matrix={self.matrix!r}, sites={self.sites!r}, local_dim={self.local_dim})"