Skip to content

Commit

Permalink
Fix improper handling of BindingsArray in EstimatorPub.coerce (#11871)
Browse files Browse the repository at this point in the history
(cherry picked from commit aba7c0f)
  • Loading branch information
ihincks authored and mergify[bot] committed Feb 26, 2024
1 parent 0a74489 commit a13623e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/primitives/containers/estimator_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions test/python/primitives/containers/test_estimator_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit a13623e

Please sign in to comment.