From b11ad282e455cc116b8f1389370d3397509fdf2c Mon Sep 17 00:00:00 2001 From: Ikko Hamamura Date: Thu, 30 Mar 2023 00:03:48 +0900 Subject: [PATCH] Correct a type of variance from complex to real (#1767) Previously Aer Estimator wrongly returned a complex value as variance of estimation values. This commit fixes this behavior to return a real value. --- qiskit_aer/primitives/estimator.py | 4 ++-- .../notes/estimator-variance-type-2b04ff7bcd305920.yaml | 4 ++++ test/terra/primitives/test_estimator.py | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/estimator-variance-type-2b04ff7bcd305920.yaml diff --git a/qiskit_aer/primitives/estimator.py b/qiskit_aer/primitives/estimator.py index ead76af798..c7dd3c4910 100644 --- a/qiskit_aer/primitives/estimator.py +++ b/qiskit_aer/primitives/estimator.py @@ -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, } @@ -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 diff --git a/releasenotes/notes/estimator-variance-type-2b04ff7bcd305920.yaml b/releasenotes/notes/estimator-variance-type-2b04ff7bcd305920.yaml new file mode 100644 index 0000000000..d905d54aa7 --- /dev/null +++ b/releasenotes/notes/estimator-variance-type-2b04ff7bcd305920.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixed a bug where the variance in metadata in EstimatorResult was complex and now returns float. diff --git a/test/terra/primitives/test_estimator.py b/test/terra/primitives/test_estimator.py index 354a4f8bf1..b14a2840e0 100644 --- a/test/terra/primitives/test_estimator.py +++ b/test/terra/primitives/test_estimator.py @@ -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.""" @@ -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."""