2
$\begingroup$

Suppose I have a PauliSumOp as follows:

from qiskit.opflow import X, Z, I, Y

h = (X ^ X ^ I) + (I ^ Y ^ Y) + (Z ^ Z ^ X)

I would like to go over the Pauli strings and do something with each string (for example, I need to know the positions of nontrivial Pauli matrices, but that's beside the point). I can iterate over h:

for k in h:
    ...

In this case, k will be a PauliSumOp with one Pauli string and one weight. The trouble is, I can't understand how to get the string itself (for example as a str, but a PauliOp will also do).

Is there a recommended way to extract individual Pauli strings from a PauliSumOp?

$\endgroup$

1 Answer 1

2
$\begingroup$

You can convert PauliSumOp into PauliOp with to_pauli_op.

from qiskit.opflow import X, Z, I, Y

h = (X ^ X ^ I) + (I ^ Y ^ Y) + (Z ^ Z ^ X)
for k in h.to_pauli_op():
    print(k)
XXI
IYY
ZZX
$\endgroup$
1
  • $\begingroup$ Thanks! I somehow overlooked that PauliSumOp has this function. $\endgroup$ Commented Oct 31, 2022 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.