From 8872374dc37b87a9ffc2018f15609082950a7c64 Mon Sep 17 00:00:00 2001 From: DanPuzzuoli Date: Tue, 17 Oct 2023 08:18:24 -0700 Subject: [PATCH 1/3] update test_array_backends infrastructure --- test/dynamics/arraylias/test_alias.py | 6 +++--- test/dynamics/common.py | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/test/dynamics/arraylias/test_alias.py b/test/dynamics/arraylias/test_alias.py index a3827e33f..fdba7fae1 100644 --- a/test/dynamics/arraylias/test_alias.py +++ b/test/dynamics/arraylias/test_alias.py @@ -23,11 +23,11 @@ from qiskit_dynamics import DYNAMICS_NUMPY as unp from qiskit_dynamics import DYNAMICS_SCIPY as usp -from ..common import QiskitDynamicsTestCase, test_array_backends +from ..common import test_array_backends @partial(test_array_backends, array_libraries=["numpy", "jax", "array_numpy", "array_jax"]) -class TestDynamicsNumpy(QiskitDynamicsTestCase): +class TestDynamicsNumpy: """Test cases for global numpy configuration.""" def test_simple_case(self): @@ -41,7 +41,7 @@ def test_simple_case(self): @test_array_backends -class TestDynamicsScipy(QiskitDynamicsTestCase): +class TestDynamicsScipy: """Test cases for global scipy configuration.""" def test_simple_case(self): diff --git a/test/dynamics/common.py b/test/dynamics/common.py index 7aecc4bd2..b94571a8f 100644 --- a/test/dynamics/common.py +++ b/test/dynamics/common.py @@ -23,6 +23,7 @@ ``assertArrayType`` method for validating that an array is from that library. These classes can also implement any required setup and teardown methods for working with that library (e.g. ``JAXTestBase`` skips tests if running on windows). +- Each ``TestBase`` subclasses ``QiskitDynamicsTestCase``. - When used on a given ``test_class``, the decorator ``test_array_backends`` creates a series of subclasses inheriting from ``test_class`` and a desired list of ``TestBase``. The desired list is specified via the ``array_libraries`` argument, which are matched against the @@ -76,7 +77,7 @@ def assertAllCloseSparse(self, A, B, rtol=1e-8, atol=1e-8): self.assertTrue(np.allclose(A, B, rtol=rtol, atol=atol)) -class NumpyTestBase(unittest.TestCase): +class NumpyTestBase(QiskitDynamicsTestCase): """Base class for tests working with numpy arrays.""" @classmethod @@ -93,7 +94,7 @@ def assertArrayType(self, a): return isinstance(a, np.ndarray) -class JAXTestBase(unittest.TestCase): +class JAXTestBase(QiskitDynamicsTestCase): """Base class for tests working with JAX arrays.""" @classmethod @@ -121,7 +122,7 @@ def assertArrayType(self, a): return isinstance(a, jnp.ndarray) -class ArrayNumpyTestBase(unittest.TestCase): +class ArrayNumpyTestBase(QiskitDynamicsTestCase): """Base class for tests working with qiskit_dynamics Arrays with numpy backend.""" @classmethod @@ -138,7 +139,7 @@ def assertArrayType(self, a): return isinstance(a, Array) and a.backend == "numpy" -class ArrayJaxTestBase(unittest.TestCase): +class ArrayJaxTestBase(QiskitDynamicsTestCase): """Base class for tests working with qiskit_dynamics Arrays with jax backend.""" @classmethod @@ -178,7 +179,9 @@ def test_array_backends(test_class: Type, array_libraries: Optional[List[str]] = Creates subclasses of ``test_class`` with any class in this file implementing an ``array_library`` class method whose output matches an entry of ``array_libraries``. These - classes are added to the calling module, and the original ``test_class`` is deleted. + classes are added to the calling module, and the original ``test_class`` is deleted. The classes + in this file implementing ``array_library`` are assumed to be a subclass of + ``QiskitDynamicsTestCase``. Read the file doc string for the intended usage. From efc28eef224a8d93d55c184172ff72f1e72f785c Mon Sep 17 00:00:00 2001 From: DanPuzzuoli Date: Tue, 17 Oct 2023 08:22:58 -0700 Subject: [PATCH 2/3] formatting --- test/dynamics/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dynamics/common.py b/test/dynamics/common.py index b94571a8f..8cb562e5d 100644 --- a/test/dynamics/common.py +++ b/test/dynamics/common.py @@ -23,7 +23,7 @@ ``assertArrayType`` method for validating that an array is from that library. These classes can also implement any required setup and teardown methods for working with that library (e.g. ``JAXTestBase`` skips tests if running on windows). -- Each ``TestBase`` subclasses ``QiskitDynamicsTestCase``. +- Each ``TestBase`` subclasses ``QiskitDynamicsTestCase``. - When used on a given ``test_class``, the decorator ``test_array_backends`` creates a series of subclasses inheriting from ``test_class`` and a desired list of ``TestBase``. The desired list is specified via the ``array_libraries`` argument, which are matched against the From 6c9524c57cffe8f526780ff59e16ec6b37ca53df Mon Sep 17 00:00:00 2001 From: DanPuzzuoli Date: Tue, 17 Oct 2023 08:26:25 -0700 Subject: [PATCH 3/3] updating function doc string --- test/dynamics/common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/dynamics/common.py b/test/dynamics/common.py index 8cb562e5d..a369bb6e2 100644 --- a/test/dynamics/common.py +++ b/test/dynamics/common.py @@ -181,7 +181,8 @@ def test_array_backends(test_class: Type, array_libraries: Optional[List[str]] = ``array_library`` class method whose output matches an entry of ``array_libraries``. These classes are added to the calling module, and the original ``test_class`` is deleted. The classes in this file implementing ``array_library`` are assumed to be a subclass of - ``QiskitDynamicsTestCase``. + ``QiskitDynamicsTestCase``, and hence ``test_class`` should not already be a subclass of + ``QiskitDynamicsTestCase`` or ``unittest.TestCase``. Read the file doc string for the intended usage.