add dtype parameter to PauliSumOp.from_list (#8896)

* add dtype parameter to PauliSumOp.to_list

* add release note

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Kevin J. Sung 2023-01-10 11:02:20 -05:00 committed by GitHub
parent d3243e9e1d
commit 67a89375e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

View File

@ -431,19 +431,22 @@ class PauliSumOp(PrimitiveOp):
@classmethod
def from_list(
cls,
pauli_list: List[Tuple[str, complex]],
pauli_list: List[Tuple[str, Union[complex, ParameterExpression]]],
coeff: Union[complex, ParameterExpression] = 1.0,
dtype: type = complex,
) -> "PauliSumOp":
"""Construct from a pauli_list with the form [(pauli_str, coeffs)]
Args:
pauli_list: A list of Tuple of pauli_str and coefficient.
coeff: A coefficient multiplying the primitive.
dtype: The dtype to use to construct the internal SparsePauliOp.
Defaults to ``complex``.
Returns:
The PauliSumOp constructed from the pauli_list.
"""
return cls(SparsePauliOp.from_list(pauli_list), coeff=coeff)
return cls(SparsePauliOp.from_list(pauli_list, dtype=dtype), coeff=coeff)
def is_zero(self) -> bool:
"""

View File

@ -0,0 +1,6 @@
---
features:
- |
Added ``dtype`` argument to :meth:`~.PauliSumOp.from_list`
for passing type information to the internal
:class:`~.SparsePauliOp`.

View File

@ -305,6 +305,13 @@ class TestPauliSumOp(QiskitOpflowTestCase):
)
self.assertEqual(target, expected)
a = Parameter("a")
target = PauliSumOp.from_list([("X", 0.5 * a), ("Y", -0.5j * a)], dtype=object)
expected = PauliSumOp(
SparsePauliOp.from_list([("X", 0.5 * a), ("Y", -0.5j * a)], dtype=object)
)
self.assertEqual(target.primitive, expected.primitive)
def test_matrix_iter(self):
"""Test PauliSumOp dense matrix_iter method."""
labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]