Skip to content

Commit

Permalink
Correct a type of variance from complex to real (Qiskit#1767)
Browse files Browse the repository at this point in the history
Previously Aer Estimator wrongly returned a complex value as variance of estimation values.
This commit fixes this behavior to return a real value.
  • Loading branch information
ikkoham authored and hhorii committed Jun 7, 2023
1 parent 23eee8f commit b11ad28
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions qiskit_aer/primitives/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def _compute_with_approximation(
expectation_values.append(rng.normal(combined_expval, standard_error))
metadata.append(
{
"variance": combined_var,
"variance": np.real_if_close(combined_var).item(),
"shots": shots,
"simulator_metadata": result.results[i].metadata,
}
Expand Down Expand Up @@ -518,7 +518,7 @@ def run(self, results: list[ExperimentResult]) -> tuple[float, dict]:
combined_var += np.dot(variances, coeffs**2)
metadata = {
"shots": shots,
"variance": combined_var,
"variance": np.real_if_close(combined_var).item(),
"simulator_metadata": simulator_metadata,
}
return combined_expval, metadata
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixed a bug where the variance in metadata in EstimatorResult was complex and now returns float.
2 changes: 2 additions & 0 deletions test/terra/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def test_with_shots_option_with_approximation(self, abelian_grouping):
).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [-1.3088991960117797])
self.assertIsInstance(result.metadata[0]["variance"], float)

def test_with_shots_option_without_approximation(self):
"""test with shots option."""
Expand All @@ -284,6 +285,7 @@ def test_with_shots_option_without_approximation(self):
).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [-1.2895828299114598])
self.assertIsInstance(result.metadata[0]["variance"], float)

def test_warn_shots_none_without_approximation(self):
"""Test waning for shots=None without approximation."""
Expand Down

0 comments on commit b11ad28

Please sign in to comment.