Skip to content

Commit

Permalink
add dtype parameter to PauliSumOp.from_list (#8896)
Browse files Browse the repository at this point in the history
* add dtype parameter to PauliSumOp.to_list

* add release note

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
kevinsung and mergify[bot] authored Jan 10, 2023
1 parent d3243e9 commit 67a8937
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions qiskit/opflow/primitive_ops/pauli_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,22 @@ def to_spmatrix(self) -> spmatrix:
@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:
"""
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/pauli-sum-op-dtype-cd09b4c6521aeb42.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Added ``dtype`` argument to :meth:`~.PauliSumOp.from_list`
for passing type information to the internal
:class:`~.SparsePauliOp`.
7 changes: 7 additions & 0 deletions test/python/opflow/test_pauli_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ def test_from_list(self):
)
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"]
Expand Down

0 comments on commit 67a8937

Please sign in to comment.