From 7e3558b19ead96a351fe50981622b69bdea68a68 Mon Sep 17 00:00:00 2001 From: Julian Teske Date: Thu, 16 Jun 2022 20:09:24 +0200 Subject: [PATCH] solves a bug in the rescaling of the standard deviation in the quasi-static noise trace generator. --- qopt/noise.py | 13 ++++++++++--- qopt_tests/unittests/test_noise.py | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/qopt/noise.py b/qopt/noise.py index c70f9fe..bc81527 100644 --- a/qopt/noise.py +++ b/qopt/noise.py @@ -447,14 +447,21 @@ def __init__(self, standard_deviation: List[float], self.sampling_mode = sampling_mode if correct_std_for_discrete_sampling: - if self.sampling_mode == 'uncorrelated_deterministic': + if self.n_traces == 1: + raise RuntimeWarning('Standard deviation cannot be estimated' + 'for a single trace!') + elif self.sampling_mode == 'uncorrelated_deterministic': for i in range(len(self.standard_deviation)): samples = sample_1dim_gaussian_distribution( std=self.standard_deviation[i], - n_samples=self.n_samples_per_trace + n_samples=self.n_traces ) actual_std = np.std(samples) - self.noise_samples[i] *= self.noise_samples[i] / actual_std + if actual_std < 1e-20: + raise RuntimeError('The standard deviation was ' + 'estimated close to 0!') + self.standard_deviation[i] *= \ + self.standard_deviation[i] / actual_std @property def n_traces(self) -> int: diff --git a/qopt_tests/unittests/test_noise.py b/qopt_tests/unittests/test_noise.py index 293da6f..c177019 100644 --- a/qopt_tests/unittests/test_noise.py +++ b/qopt_tests/unittests/test_noise.py @@ -94,7 +94,8 @@ def test_std_rescaling_quasi_static(self): for std in [1, 2.3, 5]: for n_samples in [5, 10, 200]: ntg = NTGQuasiStatic(standard_deviation=[std, ], - n_samples_per_trace=n_samples, + n_samples_per_trace=10, + n_traces=n_samples, correct_std_for_discrete_sampling=True) samples = ntg.noise_samples assert np.std(samples) - std < 1e-10