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 Mar 29, 2023
1 parent bdd49de commit 1b866c6
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 @@ -402,7 +402,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 @@ -495,7 +495,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 @@ -259,6 +259,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 @@ -268,6 +269,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)


if __name__ == "__main__":
Expand Down

0 comments on commit 1b866c6

Please sign in to comment.