Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass device to Hamiltonian, pass to pauli_string_to_matrix #292

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions torchquantum/algorithm/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import torchquantum.functional as tqf
import numpy as np

from typing import Any, Iterable, List
from typing import Any, Iterable, List, Union
from torchquantum.util import pauli_string_to_matrix

__all__ = ["Hamiltonian"]
Expand Down Expand Up @@ -96,6 +96,7 @@ def __init__(self,
coeffs: List[float],
paulis: List[str],
endianness: str = "big",
device: Union[torch.device, str, None] = None
) -> None:
"""Initialize the Hamiltonian.
Args:
Expand Down Expand Up @@ -125,6 +126,8 @@ def __init__(self,
self.coeffs = coeffs
self.paulis = paulis
self.endianness = endianness
self.dev = (torch.device(device) if isinstance(device, str)
else device)
if self.endianness == "little":
self.paulis = [pauli[::-1] for pauli in self.paulis]

Expand All @@ -135,9 +138,11 @@ def matrix(self) -> torch.Tensor:

def get_matrix(self) -> torch.Tensor:
"""Return the matrix of the Hamiltonian."""
matrix = self.coeffs[0] * pauli_string_to_matrix(self.paulis[0])
matrix = self.coeffs[0] * pauli_string_to_matrix(self.paulis[0],
device=self.dev)
for coeff, pauli in zip(self.coeffs[1:], self.paulis[1:]):
matrix += coeff * pauli_string_to_matrix(pauli)
matrix += coeff * pauli_string_to_matrix(pauli,
device=self.dev)

return matrix

Expand Down
Loading