Skip to content

Commit

Permalink
[Hot fix] Move the list of Herminitian operator to the conftest as fi…
Browse files Browse the repository at this point in the history
…xture (#38)

Clean the `test_idempotency_unitary_hermitian_operators`,
`test_int_power_unitary_hermitian_operators` and
`test_fractional_power_unitary_hermitian_operators` by move the
parameter list to the conftest as a fixture.
  • Loading branch information
kaosmicadei authored Nov 13, 2024
1 parent 23c2ceb commit 286da76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
21 changes: 19 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# use this file for configuring test fixtures and
# functions common to every test
from __future__ import annotations

from typing import Callable
import pytest

from qadence2_expressions import (
CZ,
H,
NOT,
SWAP,
X,
Y,
Z,
)


@pytest.fixture
def unitary_hermitian_operators() -> list[Callable]:
return [CZ, H, X, Y, Z, NOT, SWAP]
34 changes: 12 additions & 22 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
from __future__ import annotations

import pytest
from typing import Callable

from qadence2_expressions import (
CZ,
H,
NOT,
RX,
SWAP,
X,
Y,
Z,
sqrt,
value,
variable,
)


@pytest.mark.parametrize("operator", [CZ, H, X, Y, Z, NOT, SWAP])
def test_idempotency_unitary_hermitian_operators(operator: Callable) -> None:
assert operator() * operator() == value(1)
def test_idempotency_unitary_hermitian_operators(unitary_hermitian_operators: list) -> None:
for operator in unitary_hermitian_operators:
assert operator() * operator() == value(1)


@pytest.mark.parametrize("operator", [CZ, H, X, Y, Z, NOT, SWAP])
def test_int_power_unitary_hermitian_operators(operator: Callable) -> None:
assert operator() ** 2 == value(1)
assert operator() ** 3 == operator()
def test_int_power_unitary_hermitian_operators(unitary_hermitian_operators: list) -> None:
for operator in unitary_hermitian_operators:
assert operator() ** 2 == value(1)
assert operator() ** 3 == operator()


@pytest.mark.parametrize("operator", [CZ, H, X, Y, Z, NOT, SWAP])
def test_fractional_power_unitary_hermitian_operators(operator: Callable) -> None:
# Simplify only acting on same subspace.
assert sqrt(operator()) * sqrt(operator()) == operator()
assert sqrt(operator(0)) * sqrt(operator(1)) == sqrt(operator(0)) * sqrt(operator(1))
def test_fractional_power_unitary_hermitian_operators(unitary_hermitian_operators: list) -> None:
for operator in unitary_hermitian_operators:
# Simplify only acting on same subspace.
assert sqrt(operator()) * sqrt(operator()) == operator()
assert sqrt(operator(0)) * sqrt(operator(1)) == sqrt(operator(0)) * sqrt(operator(1))


def test_parametric_opertor() -> None:
Expand Down

0 comments on commit 286da76

Please sign in to comment.