From e007dda6c1599e577c8a2b8001b4edf4efd5816a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:20:55 +0000 Subject: [PATCH] fix: avoid flaky test behavior on macOS (#10) (#11) (cherry picked from commit 5a505913f555e9000641710e75fde6b129179ea9) Co-authored-by: Max Rossmannek --- test/static/test_approximate.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/static/test_approximate.py b/test/static/test_approximate.py index 9eb0dd4..205fd5d 100644 --- a/test/static/test_approximate.py +++ b/test/static/test_approximate.py @@ -13,6 +13,7 @@ """Tests for the ``qiskit_addon_mpf.static.approximate`` module.""" import unittest +from typing import Any, ClassVar import cvxpy as cp import numpy as np @@ -23,6 +24,12 @@ class TestApproximateCoeffs(unittest.TestCase): """Tests for the ``qiskit_addon_mpf.static.approximate`` module.""" + CVXPY_SOLVER_SETTINGS: ClassVar[dict[str, Any]] = { + "warm_start": False, + "eps_abs": 1e-7, + "eps_rel": 1e-7, + } + def test_setup_approximate_model(self): """Tests the :meth:`.setup_approximate_model` method.""" trotter_steps = [1, 2, 4] @@ -30,12 +37,12 @@ def test_setup_approximate_model(self): problem, coeffs = setup_approximate_model(lse) with self.subTest("final cost"): - final_cost = problem.solve() + final_cost = problem.solve(**self.CVXPY_SOLVER_SETTINGS) self.assertAlmostEqual(final_cost, 0) with self.subTest("optimal coefficients"): expected = np.array([0.02222225, -0.44444444, 1.42222216]) - np.testing.assert_allclose(coeffs.value, expected, rtol=1e-5) + np.testing.assert_allclose(coeffs.value, expected, rtol=1e-4) def test_setup_approximate_model_max_l1_norm(self): """Tests the :meth:`.setup_approximate_model` method with ``max_l1_norm``.""" @@ -44,12 +51,12 @@ def test_setup_approximate_model_max_l1_norm(self): problem, coeffs = setup_approximate_model(lse, max_l1_norm=1.5) with self.subTest("final cost"): - final_cost = problem.solve() + final_cost = problem.solve(**self.CVXPY_SOLVER_SETTINGS) self.assertAlmostEqual(final_cost, 0.00035765) with self.subTest("optimal coefficients"): expected = np.array([-0.001143293, -0.2488567, 1.25]) - np.testing.assert_allclose(coeffs.value, expected, rtol=1e-5) + np.testing.assert_allclose(coeffs.value, expected, rtol=1e-4) def test_setup_approximate_model_params(self): """Tests the :meth:`.setup_approximate_model` method with parameters.""" @@ -60,12 +67,12 @@ def test_setup_approximate_model_params(self): ks.value = [1, 2, 4] with self.subTest("final cost"): - final_cost = problem.solve() + final_cost = problem.solve(**self.CVXPY_SOLVER_SETTINGS) self.assertAlmostEqual(final_cost, 0) with self.subTest("optimal coefficients"): expected = np.array([0.02222225, -0.44444444, 1.42222216]) - np.testing.assert_allclose(coeffs.value, expected, rtol=1e-5) + np.testing.assert_allclose(coeffs.value, expected, rtol=1e-4) if __name__ == "__main__":