Suppose we have a PauliWeightedOperator object from Qiskit. Is there any built-in method to convert it to the matrix representation in the computational basis? My search in the docs was not successful.
1 Answer
I think this is what you are looking for:
from qiskit.aqua.operators import WeightedPauliOperator
import numpy as np
pauli_dict = {'paulis': [{"coeff": {"imag": -1.0, "real": 1.0 }, "label": "XX"}
]}
operator = WeightedPauliOperator.from_dict(pauli_dict)
Op = operator.to_opflow()
MatrixOp = Op.to_matrix()
MatrixRepresentation = np.real(MatrixOp)
print(MatrixRepresentation)
The output would be:
array([[0., 0., 0., 1.],
[0., 0., 1., 0.],
[0., 1., 0., 0.],
[1., 0., 0., 0.]])