Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
- push
- pull_request

permissions:
contents: read

jobs:
pre-commit:
runs-on: ubuntu-latest
Expand Down
17 changes: 15 additions & 2 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ on:
- pull_request
- push

permissions:
contents: read

jobs:
pytest:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.10", "3.11", "3.12", "3.13"]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: '3.12'
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Show Python
run: |
python -c "import sys, platform; print(f'Executable: {sys.executable}\nVersion: {platform.python_version()}\nImplementation: {platform.python_implementation()}\nPlatform: {platform.platform()}')"

- name: Install dependencies
run: pip install '.[dev]'

Expand All @@ -25,3 +37,4 @@ jobs:
- uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: os-${{ matrix.os }}, python-${{ matrix.python-version }}
3 changes: 3 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
- push
- pull_request

permissions:
contents: read

jobs:
build:
name: Build distribution
Expand Down
19 changes: 7 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,15 @@ repos:
- id: sort-simple-yaml
- id: trailing-whitespace

- repo: https://github.com/google/yapf
rev: v0.40.2
hooks:
- id: yapf
language: system

- repo: https://github.com/pylint-dev/pylint
rev: v3.3.1
hooks:
- id: pylint
language: system

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
hooks:
- id: mypy
language: system

- repo: https://github.com/astral-sh/ruff-pre-commit
Comment thread
hzhangxyz marked this conversation as resolved.
rev: v0.12.11
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-format
166 changes: 130 additions & 36 deletions grassmann_tensor/tensor.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions grassmann_tensor/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
except ModuleNotFoundError:
try:
import importlib.metadata

__version__ = importlib.metadata.version("parity")
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"
24 changes: 10 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,35 @@ dynamic = ["version"]
dependencies = [
"torch",
]
requires-python = ">=3"
requires-python = ">=3.10"
authors = [{ email = "hzhangxyz@outlook.com", name = "Hao Zhang" }]
description = "A Grassmann algebra tensor package"
readme = "README.md"
license = "GPL-3.0-or-later"

[project.optional-dependencies]
dev = [
"yapf",
"pylint",
"mypy",
"pytest",
"pytest-cov",
"ruff",
]

[tool.setuptools_scm]
version_file = "grassmann_tensor/_version.py"
version_scheme = "no-guess-dev"
fallback_version = "0.0.0"

[tool.yapf]
based_on_style = "google"
column_limit = 200

[tool.pylint]
max-line-length = 200
ignore-paths = [
"grassmann_tensor/_version.py",
"tests/*",
]

[tool.mypy]
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true

[tool.ruff]
Comment thread
hzhangxyz marked this conversation as resolved.
Comment thread
hzhangxyz marked this conversation as resolved.
line-length = 100
Comment thread
hzhangxyz marked this conversation as resolved.

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F"]

[tool.ruff.format]
106 changes: 64 additions & 42 deletions tests/arithmetic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,69 @@
from grassmann_tensor import GrassmannTensor


@pytest.fixture(params=[
(
GrassmannTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])),
GrassmannTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])),
),
(
GrassmannTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])),
GrassmannTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])),
),
(
GrassmannTensor((True, True, False, False), ((1, 2), (2, 2), (1, 1), (3, 1)), torch.randn([3, 4, 2, 4])),
GrassmannTensor((True, True, False, False), ((1, 2), (2, 2), (1, 1), (3, 1)), torch.randn([3, 4, 2, 4])),
),
])
@pytest.fixture(
params=[
(
GrassmannTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])),
GrassmannTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])),
),
(
GrassmannTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])),
GrassmannTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])),
),
(
GrassmannTensor(
(True, True, False, False),
((1, 2), (2, 2), (1, 1), (3, 1)),
torch.randn([3, 4, 2, 4]),
),
GrassmannTensor(
(True, True, False, False),
((1, 2), (2, 2), (1, 1), (3, 1)),
torch.randn([3, 4, 2, 4]),
),
),
]
)
def tensors(request: pytest.FixtureRequest) -> tuple[GrassmannTensor, GrassmannTensor]:
return request.param


@pytest.fixture(params=[
(
GrassmannTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])),
GrassmannTensor((False,), ((2, 2),), torch.randn([4])),
),
(
GrassmannTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])),
GrassmannTensor((True, False, True), ((1, 2), (2, 2), (3, 1)), torch.randn([3, 4, 4])),
),
(
GrassmannTensor((True, True, False), ((1, 2), (2, 2), (3, 1)), torch.randn([3, 4, 4])),
GrassmannTensor((True, True, False, False), ((3, 2), (2, 2), (1, 1), (3, 1)), torch.randn([5, 4, 2, 4])),
),
])
@pytest.fixture(
params=[
(
GrassmannTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])),
GrassmannTensor((False,), ((2, 2),), torch.randn([4])),
),
(
GrassmannTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])),
GrassmannTensor((True, False, True), ((1, 2), (2, 2), (3, 1)), torch.randn([3, 4, 4])),
),
(
GrassmannTensor((True, True, False), ((1, 2), (2, 2), (3, 1)), torch.randn([3, 4, 4])),
GrassmannTensor(
(True, True, False, False),
((3, 2), (2, 2), (1, 1), (3, 1)),
torch.randn([5, 4, 2, 4]),
),
),
]
)
def mismatch_tensors(request: pytest.FixtureRequest) -> tuple[GrassmannTensor, GrassmannTensor]:
return request.param


@pytest.fixture(params=[
torch.randn([]),
torch.randn([]).item(),
])
@pytest.fixture(
params=[
torch.randn([]),
torch.randn([]).item(),
]
)
def scalar(request: pytest.FixtureRequest) -> torch.Tensor | float:
return request.param


class FakeTensor:

def __init__(self) -> None:
pass

Expand Down Expand Up @@ -106,15 +123,20 @@ def __rtruediv__(self, other: typing.Any) -> FakeTensor:
@pytest.mark.parametrize(
"unsupported_type",
[
"string", #string
None, #NoneType
{"key", "value"}, #dict
[1, 2, 3], #list
{1, 2}, #set
object(), #arbitrary object
FakeTensor(), #an ill defined tensor-like object
])
def test_arithmetic(unsupported_type: typing.Any, tensors: tuple[GrassmannTensor, GrassmannTensor], scalar: torch.Tensor | float) -> None:
"string", # string
None, # NoneType
{"key", "value"}, # dict
[1, 2, 3], # list
{1, 2}, # set
object(), # arbitrary object
FakeTensor(), # an ill defined tensor-like object
],
)
def test_arithmetic(
unsupported_type: typing.Any,
tensors: tuple[GrassmannTensor, GrassmannTensor],
scalar: torch.Tensor | float,
) -> None:
tensor_a, tensor_b = tensors

# Test __pos__ method.
Expand Down
28 changes: 16 additions & 12 deletions tests/attributes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
Initialization = tuple[tuple[bool, ...], tuple[tuple[int, int], ...], torch.Tensor]


@pytest.fixture(params=[
((False, False), ((2, 2), (2, 2)), torch.randn([4, 4])),
((False, True), ((2, 2), (1, 3)), torch.randn([4, 4])),
((False, True), ((2, 0), (1, 3)), torch.randn([2, 4])),
((True, False), ((0, 2), (1, 3)), torch.randn([2, 4])),
((True, False), ((0, 0), (1, 3)), torch.randn([0, 4])),
((True,), ((2, 0),), torch.randn([2])),
((False,), ((0, 2),), torch.randn([2])),
((), (), torch.randn([])),
((False, False, True), ((2, 2), (1, 3), (4, 0)), torch.randn([4, 4, 4])),
])
@pytest.fixture(
params=[
((False, False), ((2, 2), (2, 2)), torch.randn([4, 4])),
((False, True), ((2, 2), (1, 3)), torch.randn([4, 4])),
((False, True), ((2, 0), (1, 3)), torch.randn([2, 4])),
((True, False), ((0, 2), (1, 3)), torch.randn([2, 4])),
((True, False), ((0, 0), (1, 3)), torch.randn([0, 4])),
((True,), ((2, 0),), torch.randn([2])),
((False,), ((0, 2),), torch.randn([2])),
((), (), torch.randn([])),
((False, False, True), ((2, 2), (1, 3), (4, 0)), torch.randn([4, 4, 4])),
]
)
def x(request: pytest.FixtureRequest) -> Initialization:
return request.param

Expand Down Expand Up @@ -50,7 +52,9 @@ def test_mask(x: Initialization) -> None:
tensor = GrassmannTensor(*x)
assert tensor.mask.shape == tensor.tensor.shape
assert tensor.mask.dtype == torch.bool
for indices in zip(*torch.unravel_index(torch.arange(tensor.tensor.numel()), tensor.tensor.shape)):
for indices in zip(
*torch.unravel_index(torch.arange(tensor.tensor.numel()), tensor.tensor.shape)
):
mask = tensor.mask[indices]
expect = False
for rank, parity in enumerate(tensor.parity):
Expand Down
4 changes: 3 additions & 1 deletion tests/clone_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def test_clone(
if parity:
assert cloned_tensor._parity is not None
assert original_tensor._parity is not None
assert all(torch.equal(c, o) for c, o in zip(cloned_tensor._parity, original_tensor._parity))
assert all(
torch.equal(c, o) for c, o in zip(cloned_tensor._parity, original_tensor._parity)
)
else:
assert cloned_tensor._parity is original_tensor._parity
if mask:
Expand Down
5 changes: 4 additions & 1 deletion tests/conversion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def test_conversion(
assert y.arrow == x.arrow
assert y.edges == x.edges
assert y.tensor.dtype == torch.complex128 if dtype_arg != "none" else torch.float32
assert y.tensor.device.type == (torch.device(device_str) if device_arg != "none" else torch.device("cpu:0")).type
assert (
y.tensor.device.type
== (torch.device(device_str) if device_arg != "none" else torch.device("cpu:0")).type
)
assert torch.allclose(y.tensor, x.tensor.to(dtype=y.tensor.dtype, device=y.tensor.device))


Expand Down
Loading
Loading