Skip to content

Commit

Permalink
add dtype parameter to PauliSumOp.to_list
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed Oct 13, 2022
1 parent af82b4d commit a49475b
Show file tree
Hide file tree
Showing 2 changed files with 12 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
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 a49475b

Please sign in to comment.