Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions prek.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ hooks = [
repo = "https://github.com/astral-sh/uv-pre-commit"
rev = "0.8.14"
hooks = [{ id = "uv-lock" }]


[[repos]]
# Keep in sync with the ty pin in pyproject.toml.
repo = "https://github.com/astral-sh/ty-pre-commit"
rev = "v0.0.82"
hooks = [{ id = "ty" }]
26 changes: 5 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dev = [
"prek>=0.2.6",
"ruff==0.16.5",
"cyclopts>=3.20",
"ty==0.0.82",
{ include-group = "test" },
]
interactive = [
Expand Down Expand Up @@ -269,25 +270,8 @@ convention = "google"
required-imports = ["from __future__ import annotations"]


[tool.pyright]
pythonVersion = "3.11"
[tool.ty.environment]
python-version = "3.11"

include = ["src/src_method/**/*.py"]

exclude = ["test_*.py", "docs/**", "**/__pycache__"]

typeCheckingMode = "basic"
useLibraryCodeForTypes = true

reportGeneralTypeIssues = "information"
reportMissingImports = true
reportMissingTypeStubs = "warning"
reportImportCycles = "information"
reportPrivateUsage = "information"
reportPrivateImportUsage = "information"
reportUnnecessaryIsInstance = "information"
reportUnnecessaryCast = "information"
reportUnnecessaryComparison = "information"
reportUnnecessaryContains = "information"
reportUnnecessaryTypeIgnoreComment = "information"
reportMatchNotExhaustive = "information"
[tool.ty.src]
include = ["src/src_method"]
16 changes: 8 additions & 8 deletions src/src_method/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from collections.abc import Sequence
from types import ModuleType

from numpy.typing import NDArray
from numpy.typing import DTypeLike, NDArray

# Set up logger
setup_logging()
Expand All @@ -61,7 +61,7 @@ def apply(
chi_out: int,
*,
cutoff: float = 0.0,
dtype: type = np.float64,
dtype: DTypeLike = np.float64,
seed: int | None = None,
device: str = "cpu",
) -> list[NDArray]:
Expand Down Expand Up @@ -146,7 +146,7 @@ def _src_mpo_mps(
xp: ModuleType,
*,
cutoff: float = 0.0,
dtype: type = np.float64,
dtype: DTypeLike = np.float64,
) -> list[NDArray]:
"""Computes the compressed product |η> ≈ H|ψ> using the SRC method.

Expand Down Expand Up @@ -233,7 +233,7 @@ def _src_mpo_mps(
eta[j] = Q_trunc.reshape(phys_dim, chi_right, rank).transpose(2, 1, 0)
S = contract(
"acb,debf,ghf,ceh->adg",
eta[j].conj(),
eta[j].conj(), # ty: ignore[unresolved-attribute]
mpo_arrs[j],
mps_arrs[j],
S,
Expand All @@ -248,7 +248,7 @@ def _src_mpo_mps(
logger.debug(LOG_TIME, t_rtl=tms * 1e-9)
logger.info("SRC MPO-MPS complete.")

return [to_numpy(t) for t in eta]
return [to_numpy(t) for t in eta] # ty: ignore[invalid-argument-type]


def _src_mpo_mpo(
Expand All @@ -259,7 +259,7 @@ def _src_mpo_mpo(
xp: ModuleType,
*,
cutoff: float = 0.0,
dtype: type = np.float64,
dtype: DTypeLike = np.float64,
) -> list[NDArray]:
"""Computes the compressed product H_new ≈ H1 @ H2 using the SRC method.

Expand Down Expand Up @@ -354,7 +354,7 @@ def _src_mpo_mpo(
)
S = contract(
"abcd,hecg,ifgd,bef->ahi",
eta[j].conj(),
eta[j].conj(), # ty: ignore[unresolved-attribute]
mpo_left_arrs[j],
mpo_right_arrs[j],
S,
Expand All @@ -369,4 +369,4 @@ def _src_mpo_mpo(
logger.debug(LOG_TIME, t_rtl=tms * 1e-9)
logger.info("SRC MPO-MPO complete.")

return [to_numpy(t) for t in eta]
return [to_numpy(t) for t in eta] # ty: ignore[invalid-argument-type]
16 changes: 8 additions & 8 deletions src/src_method/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from collections.abc import Sequence
from types import ModuleType

from numpy.typing import NDArray
from numpy.typing import DTypeLike, NDArray

# Set up logger
setup_logging()
Expand All @@ -60,7 +60,7 @@ def compress(
chi_out: int,
*,
cutoff: float = 0.0,
dtype: type = np.float64,
dtype: DTypeLike = np.float64,
seed: int | None = None,
device: str = "cpu",
) -> list[NDArray]:
Expand Down Expand Up @@ -129,7 +129,7 @@ def _src_mpo(
xp: ModuleType,
*,
cutoff: float = 0.0,
dtype: type = np.float64,
dtype: DTypeLike = np.float64,
) -> list[NDArray]:
"""Compress an MPO using the SRC method.

Expand Down Expand Up @@ -212,7 +212,7 @@ def _src_mpo(
eta[j] = Q_trunc.reshape(chi_right, phys_up, phys_down, rank).transpose(
3, 0, 1, 2
)
S = contract("abcd,fecd,eb->fa", eta[j].conj(), mpo_arrs[j], S)
S = contract("abcd,fecd,eb->fa", eta[j].conj(), mpo_arrs[j], S) # ty: ignore[unresolved-attribute]
C[j - 1] = None
chi_right = rank

Expand All @@ -223,7 +223,7 @@ def _src_mpo(
logger.debug(LOG_TIME, t_rtl=tms * 1e-9)
logger.info("SRC MPO complete.")

return [to_numpy(t) for t in eta]
return [to_numpy(t) for t in eta] # ty: ignore[invalid-argument-type]


def _src_mps(
Expand All @@ -233,7 +233,7 @@ def _src_mps(
xp: ModuleType,
*,
cutoff: float = 0.0,
dtype: type = np.float64,
dtype: DTypeLike = np.float64,
) -> list[NDArray]:
"""Compress an MPS using the SRC method.

Expand Down Expand Up @@ -306,7 +306,7 @@ def _src_mps(
Q_trunc = truncated_qr(M, cutoff, xp)
rank = Q_trunc.shape[1]
eta[j] = Q_trunc.reshape(phys_dim, chi_right, rank).transpose(2, 1, 0)
S = contract("acb,deb,ec->da", eta[j].conj(), mps_arrs[j], S)
S = contract("acb,deb,ec->da", eta[j].conj(), mps_arrs[j], S) # ty: ignore[unresolved-attribute]
C[j - 1] = None
chi_right = rank

Expand All @@ -317,4 +317,4 @@ def _src_mps(
logger.debug(LOG_TIME, t_rtl=tms * 1e-9)
logger.info("SRC MPS complete.")

return [to_numpy(t) for t in eta]
return [to_numpy(t) for t in eta] # ty: ignore[invalid-argument-type]
2 changes: 1 addition & 1 deletion src/src_method/utils/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_xp(device: str) -> ModuleType:
if device == "cpu":
return np
if device == "gpu":
import cupy # noqa: PLC0415 (lazy: optional dependency)
import cupy # noqa: PLC0415 (lazy: optional dependency) # ty: ignore[unresolved-import]

return cupy
msg = f"Unknown device {device!r}; expected 'cpu' or 'gpu'."
Expand Down
6 changes: 5 additions & 1 deletion src/src_method/utils/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from numpy.typing import NDArray


def truncated_qr(matrix: NDArray, cutoff: float, xp: ModuleType = np) -> NDArray:
def truncated_qr(
matrix: NDArray[np.floating | np.complexfloating],
cutoff: float,
xp: ModuleType = np,
) -> NDArray[np.floating | np.complexfloating]:
"""QR with SVD-based rank truncation, returning only the isometry.

Decomposes ``matrix = Q @ R``, then truncates via SVD on R,
Expand Down
3 changes: 1 addition & 2 deletions src/src_method/utils/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ def setup_logging(default_level: str = "INFO") -> None:
"""
# Configure Python's standard logging
level_name = os.environ.get("LOG_LEVEL_SRC", default_level)
level = logging.getLevelName(level_name.upper())
logging.basicConfig(
level=level,
level=level_name.upper(),
format="%(message)s",
stream=sys.stdout,
)
Expand Down
71 changes: 49 additions & 22 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.