Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIxes push to dev branch #297

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mrmustard/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions tests/test_physics/test_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from mrmustard.physics import gaussian as gp

math = Math()
hbar0 = settings.HBAR


class TestGaussianStates:
Expand Down Expand Up @@ -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
Expand Down
27 changes: 9 additions & 18 deletions tests/test_physics/test_wigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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|`,
Expand Down Expand Up @@ -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):
Expand All @@ -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()
ziofil marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.parametrize("alpha", [0 + 0j, 3 + 3j])
@pytest.mark.parametrize("hbar", [2, 3])
@pytest.mark.parametrize("method", ["iterative", "clenshaw"])
Expand All @@ -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"])
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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()