diff --git a/qiskit/primitives/containers/sampler_pub.py b/qiskit/primitives/containers/sampler_pub.py index 586dd4c0785a..9828a74c5013 100644 --- a/qiskit/primitives/containers/sampler_pub.py +++ b/qiskit/primitives/containers/sampler_pub.py @@ -121,7 +121,7 @@ def coerce(cls, pub: SamplerPubLike, shots: int | None = None) -> SamplerPub: if len(pub) > 1 and pub[1] is not None: values = pub[1] - 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 deleted file mode 100644 index 0c22a2a95906..000000000000 --- a/releasenotes/notes/fix-estimator-pub-coerce-5d13700e15126421.yaml +++ /dev/null @@ -1,7 +0,0 @@ ---- - -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/releasenotes/notes/fix-pub-coerce-5d13700e15126421.yaml b/releasenotes/notes/fix-pub-coerce-5d13700e15126421.yaml new file mode 100644 index 000000000000..0d7386b345e7 --- /dev/null +++ b/releasenotes/notes/fix-pub-coerce-5d13700e15126421.yaml @@ -0,0 +1,8 @@ +--- + +fixes: + - | + Fixed a bug where ``qiskit.primitives.containers.estimator_pub.EstimatorPub.coerce()`` and + ``qiskit.primitives.containers.sampler_pub.SamplerPub.coerce()`` + improperly handle the case where the parameter values are 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_sampler_pub.py b/test/python/primitives/containers/test_sampler_pub.py index 44b6596803a8..edb0300aec24 100644 --- a/test/python/primitives/containers/test_sampler_pub.py +++ b/test/python/primitives/containers/test_sampler_pub.py @@ -333,3 +333,15 @@ def test_coerce_tuple_3_trivial_params_shots(self, shots): 0, msg="incorrect num parameters for `parameter_values` property", ) + + def test_coerce_pub_with_exact_types(self): + """Test coercing a SamplerPub with exact types.""" + params = (Parameter("a"), Parameter("b")) + circuit = QuantumCircuit(2) + circuit.rx(params[0], 0) + circuit.ry(params[1], 1) + + params = BindingsArray(data={params: np.ones((10, 2))}) + pub = SamplerPub.coerce((circuit, params)) + self.assertIs(pub.circuit, circuit) + self.assertIs(pub.parameter_values, params)