From 4e0de54f973b5f67bd03de38314c223d8ffbdbd4 Mon Sep 17 00:00:00 2001 From: Ian Hincks Date: Thu, 29 Feb 2024 05:09:48 -0500 Subject: [PATCH] Fix improper handling of BindingsArray in SamplerPub.coerce() (#11913) * Fix SamplerPub.coerce too * Update releasenotes/notes/fix-pub-coerce-5d13700e15126421.yaml Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> --------- Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> --- qiskit/primitives/containers/sampler_pub.py | 2 +- .../fix-estimator-pub-coerce-5d13700e15126421.yaml | 7 ------- .../notes/fix-pub-coerce-5d13700e15126421.yaml | 8 ++++++++ .../python/primitives/containers/test_sampler_pub.py | 12 ++++++++++++ 4 files changed, 21 insertions(+), 8 deletions(-) delete mode 100644 releasenotes/notes/fix-estimator-pub-coerce-5d13700e15126421.yaml create mode 100644 releasenotes/notes/fix-pub-coerce-5d13700e15126421.yaml diff --git a/qiskit/primitives/containers/sampler_pub.py b/qiskit/primitives/containers/sampler_pub.py index 586dd4c0785..9828a74c501 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 0c22a2a9590..00000000000 --- 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 00000000000..0d7386b345e --- /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 44b6596803a..edb0300aec2 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)