I am using Qiskit to measure the Hamiltonian H
, which isbasically sum of Pauli strings, say something like 2*X^X+0.5*Z^Y
. For one variable, I need to get the analytically form of the inverse of H
. It seems that there is no easy way to directly get the inverse observable. So, I am wondering that how can I get this done by using qiskit?
1 Answer
It seems that Qiskit does not have this feature. However, you can get the matrix, inverse it using numpy.linalg.inv()
, then convert it to operator object again:
from qiskit.opflow import X, Y, Z
from qiskit.opflow.primitive_ops import MatrixOp
import numpy as np
op = 2*(X^X)+0.5*(Z^Y)
inv_matrix = np.linalg.inv(op.to_matrix())
operator = MatrixOp(inv_matrix)
print(operator.to_pauli_op())
And to get a PauliSumOp
:
pauli_list = [(p.primitive.to_label(), p.coeff) for p in operator.to_pauli_op().oplist]
pauli_sum_op = PauliSumOp.from_list(pauli_list)
-
-
$\begingroup$ The resulting output is a
SummedOp
object. Is there a way to transform it t aPauliSumOp
object? $\endgroup$ Jul 6, 2021 at 15:51 -
$\begingroup$ I updated the answer to show how to get
PauliSumOp
. $\endgroup$ Jul 6, 2021 at 16:46 -