A pure-Python implementation of Successive Randomized Compression (SRC) for
tensor networks. apply contracts and compresses MPO-MPS and MPO-MPO products,
compress truncates a single MPO or MPS. Trains are plain lists of per-site
NumPy arrays; MPS against MPO is inferred from the rank of the first site
tensor, so there is no wrapper type.
- Target Python 3.11-3.14. Type hints everywhere,
from __future__ import annotationsat the top of every module. - Google-style docstrings without types (types live in the signature) on every public function, class and module.
ruffis the source of truth for style: the formatter wraps at 88 columns (E501only fires past 120), 4-space indent, the rule set inpyproject.toml. Do not hand-format around it.snake_casefor functions and variables,CamelCasefor classes.N803andN806are off so that matrices can keep their mathematical names (Q,R,A); that licence does not extend to anything else.- Comment invariants, contracts and non-obvious numerical choices only. Never narrate the code.
- Backend-agnostic code: go through
src_method.utils._backendinstead of importingnumpyorcupydirectly in the algorithms, so CPU and GPU paths stay in sync. - Log with
structlogviasrc_method.utils.logging_config, neverprint. - Run
uv run prek run --all-filesand the relevant tests before pushing, and fix every finding.lint.ymlruns the same hooks in CI, so a skipped lint is a red PR. - Changes to the API or to user-facing behavior -- developers included, e.g.
workflows or test layout -- belong in the docs and, when relevant, in
README.md.docs/developer-guide/covers versioning, dependencies and how to write tests; read it before changing any of those.
- Commits and PR titles:
<type>(<optional scope>): <gitmoji> <description>. Types:feat(minor),fix(patch),docs,style,refactor,test,chore;!for breaking changes. - Agent commits carry
Assisted-by: <harness>:<model>and noCo-authored-by. - Prefix agent-authored PR descriptions and comments with
:robot: _AI text below_ :robot:.
Package src/src_method/: apply.py and compress.py are the public entry
points, _tensor_train.py holds the shared train helpers, utils/_backend.py
the NumPy/CuPy dispatch and utils/linalg.py the decompositions. Tests in
tests/, benchmarks in benches/ with recorded results in
baseline-benchmarks/, MkDocs sources in docs/, throwaway scripts in
sandbox/.
src/src_method/_version.py is generated by hatch-vcs -- never edit it.
We use uv for environment management.
uv sync --all-groups --all-extras
uv run prek install --prepare-hooks
uv run pytest -m "not slow" # fast suite
uv run pytest # everything
uv run ruff check src/ tests/
uv run mkdocs servefilterwarnings = ["error"]is on: a stray warning fails the suite. Networks with fewer than three sites intentionally warn and fall back to an exact SVD, so those tests must assert the warning.- Markers:
slow(>=1 minute) andperf(benchmark). Mark anything long, the PR suite runs-m "not slow". - GPU tests in
tests/test_gpu_backend.pyskip without CuPy; the GPU extras aregpu-nvidia(CUDA) andgpu-rocm. - Array layouts follow the
quimbconventions: MPO bulk tensors are('l', 'r', 'u', 'd'), MPS bulk tensors('l', 'r', 'u'), boundary tensors drop the outer bond index. applyandcompressare pure; never mutate the input arrays.