From 38039c10f885840a9e7a71e06da74e107186d6de Mon Sep 17 00:00:00 2001 From: Ian Hincks Date: Fri, 23 Feb 2024 09:49:33 -0500 Subject: [PATCH] Fix improper handling of BindingsArray in EstimatorPub.coerce --- qiskit/primitives/containers/estimator_pub.py | 2 +- .../fix-estimator-pub-coerce-5d13700e15126421.yaml | 7 +++++++ .../primitives/containers/test_estimator_pub.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-estimator-pub-coerce-5d13700e15126421.yaml diff --git a/qiskit/primitives/containers/estimator_pub.py b/qiskit/primitives/containers/estimator_pub.py index 1996e315f443..fed278ae14ae 100644 --- a/qiskit/primitives/containers/estimator_pub.py +++ b/qiskit/primitives/containers/estimator_pub.py @@ -141,7 +141,7 @@ def coerce(cls, pub: EstimatorPubLike, precision: float | None = None) -> Estima if len(pub) > 2 and pub[2] is not None: values = pub[2] - if not isinstance(values, Mapping): + if not isinstance(values, (BindingsArray, Mapping)): values = {tuple(circuit.parameters): values} parameter_values = BindingsArray.coerce(values) else: diff --git a/releasenotes/notes/fix-estimator-pub-coerce-5d13700e15126421.yaml b/releasenotes/notes/fix-estimator-pub-coerce-5d13700e15126421.yaml new file mode 100644 index 000000000000..0c22a2a95906 --- /dev/null +++ b/releasenotes/notes/fix-estimator-pub-coerce-5d13700e15126421.yaml @@ -0,0 +1,7 @@ +--- + +fixes: + - | + Fixed a bug where `qiskit.primitives.containers.estimator_pub.EstimatorPub.coerce()` + improperly handles the case where the third value is a `BindingsArray` instance, giving + rise to a ``ValueError`` whenever it is attempted. \ No newline at end of file diff --git a/test/python/primitives/containers/test_estimator_pub.py b/test/python/primitives/containers/test_estimator_pub.py index ecf82e3347ed..4a1bbc5e2241 100644 --- a/test/python/primitives/containers/test_estimator_pub.py +++ b/test/python/primitives/containers/test_estimator_pub.py @@ -182,6 +182,19 @@ def test_coerce_pub_with_precision(self, precision): pub2 = EstimatorPub.coerce(pub1, precision=precision) self.assertEqual(pub1, pub2) + def test_coerce_pub_with_exact_types(self): + """Test coercing an EstimatorPub""" + params = (Parameter("a"), Parameter("b")) + circuit = QuantumCircuit(2) + circuit.rx(params[0], 0) + circuit.ry(params[1], 1) + obs = ObservablesArray({"XY": 1}) + params = BindingsArray(data={params: np.ones((10, 2))}) + pub = EstimatorPub.coerce((circuit, obs, params)) + self.assertIs(pub.circuit, circuit) + self.assertIs(pub.observables, obs) + self.assertIs(pub.parameter_values, params) + @ddt.data(0.01, 0.02) def test_coerce_pub_without_shots(self, precision): """Test coercing an EstimatorPub"""