diff --git a/qiskit/opflow/primitive_ops/pauli_sum_op.py b/qiskit/opflow/primitive_ops/pauli_sum_op.py index d0c55ee391d4..894554341321 100644 --- a/qiskit/opflow/primitive_ops/pauli_sum_op.py +++ b/qiskit/opflow/primitive_ops/pauli_sum_op.py @@ -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: """ diff --git a/test/python/opflow/test_pauli_sum_op.py b/test/python/opflow/test_pauli_sum_op.py index 3a5b8de3d528..4889b9ca9437 100644 --- a/test/python/opflow/test_pauli_sum_op.py +++ b/test/python/opflow/test_pauli_sum_op.py @@ -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"]