Skip to content

Commit

Permalink
solves a bug in the rescaling of the standard deviation in the quasi-…
Browse files Browse the repository at this point in the history
…static noise trace generator.
  • Loading branch information
JDTeske committed Jun 16, 2022
1 parent 454b21c commit 7e3558b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions qopt/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion qopt_tests/unittests/test_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7e3558b

Please sign in to comment.