Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix improper handling of BindingsArray in SamplerPub.coerce() #11913

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading