From 286da76a3df7b00ca26fc3a86fe8bf927d9e7b6e Mon Sep 17 00:00:00 2001 From: Kaonan Micadei Date: Wed, 13 Nov 2024 12:26:18 +0100 Subject: [PATCH] [Hot fix] Move the list of Herminitian operator to the conftest as fixture (#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. --- tests/conftest.py | 21 +++++++++++++++++++-- tests/test_operators.py | 34 ++++++++++++---------------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3014d28..3eaf1cb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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] diff --git a/tests/test_operators.py b/tests/test_operators.py index 3211242..0bef407 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -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: