Skip to content

Commit

Permalink
Add BindingsArray as allowed input type to BindingsArray.coerce() (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
ihincks authored Jan 26, 2024
1 parent ec859e1 commit ce02f06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qiskit/primitives/containers/bindings_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ def coerce(cls, bindings_array: BindingsArrayLike) -> BindingsArray:
bindings_array = cls(bindings_array)
elif isinstance(bindings_array, Mapping):
bindings_array = cls(kwvals=bindings_array)
elif isinstance(bindings_array, BindingsArray):
return bindings_array
else:
raise TypeError(f"Unsupported type {type(bindings_array)} is given.")
return bindings_array
Expand Down
17 changes: 17 additions & 0 deletions test/python/primitives/containers/test_bindings_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,20 @@ def test_simple_vals(self):
self.assertEqual(ba.shape, (1,))
self.assertEqual(len(ba.vals), 1)
np.testing.assert_allclose(ba.vals[0], [[1, 2, 3]])

def test_coerce(self):
"""Test the coerce() method."""
# BindingsArray passthrough
arg = BindingsArray({"a": np.linspace(0, 1, 5)})
ba = BindingsArray.coerce(arg)
self.assertEqual(ba, arg)

# dict-valued input
arg = {"a": np.linspace(0, 1, 5)}
ba = BindingsArray.coerce(arg)
self.assertEqual(ba.num_parameters, 1)

# None-valued input
arg = None
ba = BindingsArray.coerce(None)
self.assertEqual(ba.num_parameters, 0)

0 comments on commit ce02f06

Please sign in to comment.