diff --git a/mrmustard/utils/settings.py b/mrmustard/utils/settings.py index f1df34ee4..1f40736aa 100644 --- a/mrmustard/utils/settings.py +++ b/mrmustard/utils/settings.py @@ -114,6 +114,7 @@ def __init__(self): ] # possible values for settings.PRECISION_BITS_HERMITE_POLY def _force_hbar(self, value): + r"can set the value of HBAR at any time. use with caution." self._hbar._value = value @property diff --git a/tests/test_physics/test_fidelity.py b/tests/test_physics/test_fidelity.py index a5c65ff4e..6ec830aa9 100644 --- a/tests/test_physics/test_fidelity.py +++ b/tests/test_physics/test_fidelity.py @@ -10,7 +10,6 @@ from mrmustard.physics import gaussian as gp math = Math() -hbar0 = settings.HBAR class TestGaussianStates: @@ -79,7 +78,6 @@ def test_fidelity_coherent_state(self, num_modes, hbar): @pytest.mark.parametrize("r2", np.random.rand(3)) def test_fidelity_squeezed_vacuum(self, r1, r2, hbar): """Tests fidelity between two squeezed states""" - hbar0 = settings.HBAR settings._force_hbar(hbar) cov1 = np.diag([np.exp(2 * r1), np.exp(-2 * r1)]) * hbar / 2 cov2 = np.diag([np.exp(2 * r2), np.exp(-2 * r2)]) * hbar / 2 diff --git a/tests/test_physics/test_wigner.py b/tests/test_physics/test_wigner.py index 02811e773..13cd63f5e 100644 --- a/tests/test_physics/test_wigner.py +++ b/tests/test_physics/test_wigner.py @@ -38,14 +38,6 @@ # ~~~~~~~ -def reset_settings(): - r"""Resets `Settings`""" - settings.AUTOCUTOFF_MAX_CUTOFF = autocutoff_max0 - settings.AUTOCUTOFF_MIN_CUTOFF = autocutoff_min0 - settings.DISCRETIZATION_METHOD = method0 - settings._force_hbar(hbar0) - - def distance(W_mm, W_th): r"""Calculates the distance between the discretized Wigner functions W_mm (generated by `mrmustard`) and W_th (computed analytically) as the maximum of `|W_mm-W_th|/|W_th|`, @@ -115,6 +107,15 @@ def generator(q, p, n): class TestWignerDiscretized: r"""Tests discretized Wigner functions (DWF) for various states""" + hbar0: float = settings.HBAR + + def teardown_method(self, method): + r"""Resets `Settings`""" + settings.AUTOCUTOFF_MAX_CUTOFF = autocutoff_max0 + settings.AUTOCUTOFF_MIN_CUTOFF = autocutoff_min0 + settings.DISCRETIZATION_METHOD = method0 + settings._force_hbar(self.hbar0) + @pytest.mark.parametrize("method", ["iterative", "clenshaw"]) @pytest.mark.parametrize("hbar", [1, 2]) def test_cat_state(self, method, hbar): @@ -136,8 +137,6 @@ def test_cat_state(self, method, hbar): assert np.allclose(q_mat.T, q_vec) assert np.allclose(p_mat, p_vec) - reset_settings() - @pytest.mark.parametrize("alpha", [0 + 0j, 3 + 3j]) @pytest.mark.parametrize("hbar", [2, 3]) @pytest.mark.parametrize("method", ["iterative", "clenshaw"]) @@ -163,8 +162,6 @@ def test_coherent_state(self, alpha, hbar, method): assert np.allclose(q_mat.T, q_vec) assert np.allclose(p_mat, p_vec) - reset_settings() - @pytest.mark.parametrize("n", [2, 6]) @pytest.mark.parametrize("hbar", [2, 3]) @pytest.mark.parametrize("method", ["iterative", "clenshaw"]) @@ -184,8 +181,6 @@ def test_fock_state(self, n, hbar, method): assert np.allclose(q_mat.T, q_vec) assert np.allclose(p_mat, p_vec) - reset_settings() - @pytest.mark.parametrize("method", ["iterative", "clenshaw"]) def test_squeezed_vacuum_both_method_succeed(self, method): r"""Tests DWF for a squeezed vacuum state with squeezing s=1. @@ -207,8 +202,6 @@ def test_squeezed_vacuum_both_method_succeed(self, method): assert np.allclose(q_mat.T, q_vec) assert np.allclose(p_mat, p_vec) - reset_settings() - @pytest.mark.parametrize("method", ["iterative", "clenshaw"]) def test_squeezed_vacuum_iterative_fails(self, method): r"""Tests DWF for a squeezed vacuum state with squeezing s=2. @@ -228,5 +221,3 @@ def test_squeezed_vacuum_iterative_fails(self, method): success = np.allclose(distance(W_mm, W_th), 0, atol=10**-1) assert success is False if method == "iterative" else True - - reset_settings()