From 1b866c6ad01862f441e5ec2896d1649f1f097946 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 157f83b6b5..77c2d551dc 100644 --- a/qiskit_aer/primitives/estimator.py +++ b/qiskit_aer/primitives/estimator.py @@ -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, } @@ -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 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 d0b97a1688..ff1a4edef0 100644 --- a/test/terra/primitives/test_estimator.py +++ b/test/terra/primitives/test_estimator.py @@ -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.""" @@ -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__":