Skip to content

Commit

Permalink
Fix SamplerPub.coerce too
Browse files Browse the repository at this point in the history
  • Loading branch information
ihincks committed Feb 28, 2024
1 parent ba8d671 commit fc76ec3
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 fc76ec3

Please sign in to comment.