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

Parametrized tests #61

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/simulated_bifurcation/polynomial/polynomial_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def from_tensors(
def from_expression(
cls,
expression: Poly,
*,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[str, torch.device]] = None,
) -> Dict[int, torch.Tensor]:
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/core/__init__.py
Empty file.
108 changes: 91 additions & 17 deletions tests/core/test_ising.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest
import torch

from src.simulated_bifurcation.core import Ising

from ..utils import DEVICES, DTYPES

J = torch.tensor(
[
[1, 2, 3],
Expand All @@ -13,44 +16,96 @@
h = torch.tensor([1, 0, -1], dtype=torch.float32)


def test_init_ising_model_from_tensors():
ising = Ising(J, h)
def init_J(dtype: torch.dtype, device: str):
return torch.tensor(
[
[1, 2, 3],
[2, 1, 4],
[3, 4, 1],
],
dtype=dtype,
device=device,
)


def init_h(dtype: torch.dtype, device: str):
return torch.tensor([1, 0, -1], dtype=dtype, device=device)


@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_init_ising_model_from_tensors(dtype: torch.dtype, device: str):
J = init_J(dtype, device)
h = init_h(dtype, device)
ising = Ising(J, h, dtype=dtype, device=device)
assert torch.equal(ising.J, J)
assert torch.equal(ising.h, h)
assert ising.linear_term
assert len(ising) == 3
assert ising.dimension == 3
assert ising.dtype == dtype
assert ising.device == torch.device(device)


def test_init_ising_model_from_arrays():
ising = Ising(J.numpy(), h.numpy())
@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_init_ising_model_from_arrays(dtype: torch.dtype, device: str):
J = init_J(dtype, device)
h = init_h(dtype, device)
ising = Ising(J.numpy(), h.numpy(), dtype=dtype, device=device)
assert torch.equal(ising.J, J)
assert torch.equal(ising.h, h)
assert ising.linear_term
assert len(ising) == 3
assert ising.dimension == 3
assert ising.dtype == dtype
assert ising.device == torch.device(device)


def test_ising_without_linear_term():
ising = Ising(J)
@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_ising_without_linear_term(dtype: torch.dtype, device: str):
J = init_J(dtype, device)
ising = Ising(J, dtype=dtype, device=device)
assert torch.equal(ising.J, J)
assert torch.equal(ising.h, torch.zeros(3))
assert not ising.linear_term
assert len(ising) == 3
assert ising.dimension == 3
assert ising.dtype == dtype
assert ising.device == torch.device(device)


def test_init_ising_with_null_h_vector():
ising = Ising(J, torch.zeros(3))
@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_init_ising_with_null_h_vector(dtype: torch.dtype, device: str):
J = init_J(dtype, device)
ising = Ising(J, torch.zeros(3), dtype=dtype, device=device)
assert torch.equal(ising.J, J)
assert torch.equal(ising.h, torch.zeros(3))
assert not ising.linear_term
assert len(ising) == 3
assert ising.dimension == 3
assert ising.dtype == dtype
assert ising.device == torch.device(device)


def test_clip_vector_to_tensor():
ising = Ising(J, h)
@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_clip_vector_to_tensor(dtype: torch.dtype, device: str):
J = init_J(dtype, device)
h = init_h(dtype, device)
ising = Ising(J, h, dtype=dtype, device=device)
attached = ising.clip_vector_to_tensor()
assert torch.equal(
attached,
Expand All @@ -61,37 +116,56 @@ def test_clip_vector_to_tensor():
[3, 4, 1, 1],
[-1, 0, 1, 0],
],
dtype=torch.float32,
dtype=dtype,
device=device,
),
)


def test_simulated_bifurcation_tensor():
@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_simulated_bifurcation_tensor(dtype: torch.dtype, device: str):
original = torch.tensor(
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
dtype=torch.float32,
dtype=dtype,
device=device,
)
ising = Ising(original)
ising = Ising(original, dtype=dtype, device=device)
expected_result = torch.tensor(
[
[0, 3, 5],
[3, 0, 7],
[5, 7, 0],
],
dtype=torch.float32,
dtype=dtype,
device=device,
)
assert torch.equal(ising.as_simulated_bifurcation_tensor(), expected_result)


def test_negative_ising():
ising = Ising(J, h)
@pytest.mark.parametrize(
"dtype, device",
[(dtype, device) for dtype in DTYPES for device in DEVICES],
)
def test_negative_ising(dtype: torch.dtype, device: str):
J = init_J(dtype, device)
h = init_h(dtype, device)
ising = Ising(J, h, dtype=dtype, device=device)
negative_ising = -ising
assert torch.equal(negative_ising.J, -J)
assert torch.equal(negative_ising.h, -h)
assert negative_ising.linear_term
assert len(negative_ising) == 3
assert negative_ising.dimension == 3
assert negative_ising.dtype == dtype
assert negative_ising.device == torch.device(device)


def test_minimize():
pass
2 changes: 1 addition & 1 deletion tests/core/test_quadratic_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_wrong_domain_to_ising():
QuadraticPolynomial(quadratic).to_ising(domain="int2.5")


def test_wrong_domain_cnvert_spin():
def test_wrong_domain_convert_spin():
ising = Ising(quadratic)
ising.computed_spins = torch.ones(3, 3)
with pytest.raises(ValueError):
Expand Down
Empty file added tests/optimizer/__init__.py
Empty file.
2 changes: 0 additions & 2 deletions tests/optimizer/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

import pytest

from src.simulated_bifurcation import get_env, reset_env, set_env
Expand Down
Loading
Loading