Skip to content

Commit

Permalink
Fix improper handling of BindingsArray in SamplerPub.coerce() (#11913)
Browse files Browse the repository at this point in the history
* 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>
(cherry picked from commit 4e0de54)
  • Loading branch information
ihincks authored and mergify[bot] committed Feb 29, 2024
1 parent 2463f16 commit fa2558d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion qiskit/primitives/containers/sampler_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

This file was deleted.

8 changes: 8 additions & 0 deletions releasenotes/notes/fix-pub-coerce-5d13700e15126421.yaml
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions test/python/primitives/containers/test_sampler_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fa2558d

Please sign in to comment.