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

[Testing] Adding unit tests to all classes and functions in types.py #7

Merged
merged 23 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8932cc5
Update authors
pimvenderbosch Nov 1, 2024
8f5d548
Add test for testing Alloc repr function
pimvenderbosch Nov 1, 2024
99b5a6b
Add test for Alloc.__eq__
pimvenderbosch Nov 1, 2024
0b27519
Fix error in test_alloc_eq
pimvenderbosch Nov 1, 2024
751dc66
Configure pytest coverage correctly
pimvenderbosch Nov 1, 2024
2fe0c88
Add tests for Assign
pimvenderbosch Nov 1, 2024
f9f58da
Add extra failure cases to test_alloc_eq
pimvenderbosch Nov 1, 2024
a859b09
Add tests for Load
pimvenderbosch Nov 1, 2024
e04231f
Fix uncorrect repr of Call when no arguments are given
pimvenderbosch Nov 1, 2024
456d051
Add tests for Call
pimvenderbosch Nov 1, 2024
9a8bbe5
Remove possibility to add args to Support
pimvenderbosch Nov 1, 2024
032ad83
Add tests for Support
pimvenderbosch Nov 1, 2024
491f3e0
Add tests for QuInstruct
pimvenderbosch Nov 1, 2024
dbdf67d
Use repr() instead of .__repr__() in all tests
pimvenderbosch Nov 4, 2024
5b0443a
Harmonize how arguments and kw-arguments are displayed in repr
pimvenderbosch Nov 5, 2024
8b09ba6
Add tests for AllocQubits
pimvenderbosch Nov 5, 2024
098e10f
Change Model repr method to match more closely to the reprs of other …
pimvenderbosch Nov 5, 2024
d00436e
Add tests for Model
pimvenderbosch Nov 6, 2024
4d1794d
Add test for NotImplemented in Call to reach full coverage
pimvenderbosch Nov 6, 2024
c0db241
Change test_alloc_eq to use deepcopy instead of nested function
pimvenderbosch Nov 6, 2024
c8f4a04
Move fixtures to confeste.py
pimvenderbosch Nov 6, 2024
349aaac
Merge branch 'main' into pv/add-tests
pimvenderbosch Nov 12, 2024
56f6fee
Fix linting
pimvenderbosch Nov 12, 2024
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
12 changes: 4 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ keywords = ["quantum"]
authors = [
{ name = "Kaonan Micadei", email = "[email protected]" },
{ name = "Eduardo Maschio", email = "[email protected]" },
{ name = "Roland Guichard", email = "[email protected]" },
{ name = "Pim Venderbosch", email = "[email protected]" },
]
classifiers = [
"Development Status :: 4 - Beta",
Expand Down Expand Up @@ -57,11 +59,11 @@ dependencies = [
]

[tool.hatch.envs.default.scripts]
test = "pytest -n auto --cov-config=pyproject.toml --ignore=./tests/test_examples.py {args}"
test = "pytest -n auto --cov-config=pyproject.toml --cov=qadence2_ir {args}"

[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = """-vvv --cov-report=term-missing --cov-config=pyproject.toml --cov=template_python --cov=tests"""
addopts = """-vvv --cov-config=pyproject.toml --cov=qadence2_ir"""
xfail_strict = true
filterwarnings = [
"ignore:Call to deprecated create function FieldDescriptor",
Expand Down Expand Up @@ -108,12 +110,6 @@ packages = ["qadence2_ir"]
[tool.coverage.run]
branch = true
parallel = true
# uncomment to omit any file from the
# coverage. Regexps can be used
# to select all files from a folder
#omit = [
# "template_python/to_omit.py",
#]

[tool.coverage.report]
exclude_lines = [
Expand Down
8 changes: 5 additions & 3 deletions qadence2_ir/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ def __init__(self, identifier: str, *args: Any) -> None:
self.args = args

def __repr__(self) -> str:
args = ", ".join(map(repr, self.args))
return f"{self.__class__.__name__}({repr(self.identifier)}, {args})"
if not self.args:
return f"{self.__class__.__name__}({repr(self.identifier)})"
else:
args = ", ".join(map(repr, self.args))
return f"{self.__class__.__name__}({repr(self.identifier)}, {args})"
kaosmicadei marked this conversation as resolved.
Show resolved Hide resolved

def __eq__(self, value: object) -> bool:
if not isinstance(value, Call):
Expand All @@ -103,7 +106,6 @@ class Support:

def __init__(
self,
*,
kaosmicadei marked this conversation as resolved.
Show resolved Hide resolved
target: tuple[int, ...],
control: tuple[int, ...] | None = None,
) -> None:
Expand Down
121 changes: 121 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from __future__ import annotations

import pytest

from qadence2_ir.types import Alloc, Assign, Call, Load, QuInstruct, Support


def test_alloc_repr() -> None:
assert repr(Alloc(1, True)) == "Alloc(1, trainable=True)"
assert repr(Alloc(6, False)) == "Alloc(6, trainable=False)"
assert (
repr(Alloc(1, False, attr1=5, attr2=[5, 6, 3]))
== "Alloc(1, trainable=False, attrs={'attr1': 5, 'attr2': [5, 6, 3]})"
)


def test_alloc_eq() -> None:
def create_alloc() -> Alloc:
return Alloc(3, True, attributes={"test": 8, "bla": (2, "str", 84.2)})

assert create_alloc() == create_alloc()
pimvenderbosch marked this conversation as resolved.
Show resolved Hide resolved
assert create_alloc() != Alloc(1, True)
assert create_alloc() != Alloc(1, False)
assert create_alloc().__eq__([]) is NotImplemented


def test_assign_repr() -> None:
assert repr(Assign("my-var", 8)) == "Assign('my-var', 8)"
assert repr(Assign("my-var", 3.14)) == "Assign('my-var', 3.14)"
assert (
repr(Assign("var*with@non$standard123characters", [3, 2, 1]))
== "Assign('var*with@non$standard123characters', [3, 2, 1])"
)


def test_assign_eq() -> None:
assert Assign("x", 2) == Assign("x", 2)
assert Assign("x", 2) != Assign("y", 2)
assert Assign("x", 1) != Assign("x", 34)
assert Assign("x", 5).__eq__({}) is NotImplemented


def test_load_repr() -> None:
assert repr(Load("my-var")) == "Load('my-var')"


def test_load_eq() -> None:
assert Load("my-var") == Load("my-var")
assert Load("x") != Load("y")
assert Load("x") != "x"


def test_call_repr() -> None:
assert repr(Call("my-func")) == "Call('my-func')"
assert repr(Call("my-func", 9, "str")) == "Call('my-func', 9, 'str')"


def test_call_eq() -> None:
assert Call("my-func") == Call("my-func")
assert Call("my-func", 3) == Call("my-func", 3)
assert Call("my-func") != Call("fibonaci")
assert Call("my-func", 2) != Call("my-func", 4)
assert Call("my-func", []) != Call("my-func", 4)


def test_support_init() -> None:
only_target = Support((0,))
assert only_target.target == (0,)
assert only_target.control == ()

with pytest.raises(TypeError):
Support(control=(0,)) # type: ignore

target_and_control = Support((2, 0), (1,))
assert target_and_control.target == (2, 0)
assert target_and_control.control == (1,)

target_all = Support.target_all()
assert target_all.target == ()
assert target_all.control == ()


def test_support_repr() -> None:
assert repr(Support((0,))) == "Support(target=(0,))"
assert repr(Support((1,), (0,))) == "Support(target=(1,), control=(0,))"
assert repr(Support(())) == "Support.target_all()"
assert repr(Support.target_all()) == "Support.target_all()"


def test_support_eq() -> None:
assert Support((0,)) == Support((0,))
assert Support((0,), (1, 3)) == Support((0,), (1, 3))
assert Support((0,)) != Support((1,))
assert Support((0,), (1,)) != Support((0,))
assert Support((0,), (1,)) != Support((0,), (3, 0))
assert Support((3,)) != "Support((3,))"


@pytest.fixture
pimvenderbosch marked this conversation as resolved.
Show resolved Hide resolved
def support_control_target() -> Support:
return Support((0,), (1,))


def test_qu_instruct_repr(support_control_target: Support) -> None:
assert (
repr(QuInstruct("CNOT", support_control_target))
== f"QuInstruct('CNOT', {repr(support_control_target)})"
)
assert (
repr(QuInstruct("CNOT", support_control_target, 1, "l", [], one=1, two=2.0))
== f"QuInstruct('CNOT', {repr(support_control_target)}, 1, 'l', [], "
f"attrs={{'one': 1, 'two': 2.0}})"
)


def test_qu_instruct_eq(support_control_target: Support) -> None:
assert QuInstruct("CNOT", support_control_target) == QuInstruct("CNOT", support_control_target)
assert QuInstruct("CNOT", support_control_target, 1, "a", k=8.0) == QuInstruct(
"CNOT", support_control_target, 1, "a", k=8.0
)
assert QuInstruct("CNOT", support_control_target) != "CNOT"