Skip to content

Commit

Permalink
Improve the NoiseModel unused parameters warning message (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri authored Oct 16, 2024
1 parent a8cd6f1 commit c6e9fbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pulser-core/pulser/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,16 @@ def to_tuple(obj: tuple) -> tuple:
object.__setattr__(
self, "noise_types", tuple(sorted(true_noise_types))
)
non_zero_relevant_params = [
p for p in relevant_params if param_vals[p]
]
for param_, val_ in param_vals.items():
object.__setattr__(self, param_, val_)
if val_ and param_ not in relevant_params:
warnings.warn(
f"{param_!r} is not used by any active noise type "
f"{self.noise_types}.",
f"in {self.noise_types} when the only defined parameters "
f"are {non_zero_relevant_params}.",
stacklevel=2,
)

Expand Down
11 changes: 10 additions & 1 deletion tests/test_noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
from __future__ import annotations

import re

import numpy as np
import pytest

Expand Down Expand Up @@ -86,7 +88,14 @@ def test_init(self, params, noise_types):
)
@pytest.mark.parametrize("unused_param", ["runs", "samples_per_run"])
def test_unused_params(self, unused_param, noise_param):
with pytest.warns(UserWarning, match=f"'{unused_param}' is not used"):
with pytest.warns(
UserWarning,
match=re.escape(
f"'{unused_param}' is not used by any active noise type in"
f" {(_PARAM_TO_NOISE_TYPE[noise_param],)} when the only "
f"defined parameters are {[noise_param]}"
),
):
NoiseModel(**{unused_param: 100, noise_param: 1.0})

@pytest.mark.parametrize(
Expand Down

0 comments on commit c6e9fbf

Please sign in to comment.