mirror of https://github.com/Qiskit/qiskit.git
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:
parent
d3243e9e1d
commit
67a89375e7
|
@ -431,19 +431,22 @@ class PauliSumOp(PrimitiveOp):
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_list(
|
def from_list(
|
||||||
cls,
|
cls,
|
||||||
pauli_list: List[Tuple[str, complex]],
|
pauli_list: List[Tuple[str, Union[complex, ParameterExpression]]],
|
||||||
coeff: Union[complex, ParameterExpression] = 1.0,
|
coeff: Union[complex, ParameterExpression] = 1.0,
|
||||||
|
dtype: type = complex,
|
||||||
) -> "PauliSumOp":
|
) -> "PauliSumOp":
|
||||||
"""Construct from a pauli_list with the form [(pauli_str, coeffs)]
|
"""Construct from a pauli_list with the form [(pauli_str, coeffs)]
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
pauli_list: A list of Tuple of pauli_str and coefficient.
|
pauli_list: A list of Tuple of pauli_str and coefficient.
|
||||||
coeff: A coefficient multiplying the primitive.
|
coeff: A coefficient multiplying the primitive.
|
||||||
|
dtype: The dtype to use to construct the internal SparsePauliOp.
|
||||||
|
Defaults to ``complex``.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The PauliSumOp constructed from the pauli_list.
|
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:
|
def is_zero(self) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
features:
|
||||||
|
- |
|
||||||
|
Added ``dtype`` argument to :meth:`~.PauliSumOp.from_list`
|
||||||
|
for passing type information to the internal
|
||||||
|
:class:`~.SparsePauliOp`.
|
|
@ -305,6 +305,13 @@ class TestPauliSumOp(QiskitOpflowTestCase):
|
||||||
)
|
)
|
||||||
self.assertEqual(target, expected)
|
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):
|
def test_matrix_iter(self):
|
||||||
"""Test PauliSumOp dense matrix_iter method."""
|
"""Test PauliSumOp dense matrix_iter method."""
|
||||||
labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
|
labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
|
||||||
|
|
Loading…
Reference in New Issue