Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1764 (variance type in Estimator) #1767

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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