# How does Qiskit Aqua commutator work?

Commutation operation is essential in quantum mechanics, but when I was trying to use the commutator of IBM qiskit I find something confusing.

Here comes the problematic code.

from qiskit import QuantumCircuit
from qiskit.quantum_info import DensityMatrix
from qiskit.aqua.operators.legacy import commutator

qr0=QuantumCircuit(1)
qr1=QuantumCircuit(1)
circ0.x(0)
dm0=DensityMatrix.from_instruction(circ0)
circ1.z(0)
dm1=DensityMatrix.from_instruction(circ1)


The upper codes generates two Density Matrices, and since they are matrices, mathematically we can compute their commutator, but then after you run commutator(dm0,dm1) you'll find that it's not working. Even if you try some other types like input two Operators or input the detailed matrices(dm.data) but it is just raised an error. E.g. in the lower code when the detailed matrices case returns an error.

dm0=Operator(dm0)
dm1=Operator(dm1)
commutator(dm0.data,dm1.data)


The errors that qiskit reports are nearly identical so I just put one of them as an example.

I know numpy or scipy can help me in circumventing the trouble, but I still want to ask: have anyone meet this before? Do you know how to fix this? Thanks!

According with the commutator documentation, it works on WeightedPauliOperator, not on numpy arrays.

from qiskit.aqua.operators.legacy import commutator
from qiskit.aqua.operators import WeightedPauliOperator
from qiskit.quantum_info import Pauli

pauli_a = 'X'
pauli_b = 'Z'
coeff_a = 0.5
coeff_b = 0.5
pauli_term_a = [coeff_a, Pauli.from_label(pauli_a)]
pauli_term_b = [coeff_b, Pauli.from_label(pauli_b)]
op_a = WeightedPauliOperator(paulis=[pauli_term_a])
op_b = WeightedPauliOperator(paulis=[pauli_term_b])

print(commutator(op_a, op_b))

Representation: paulis, qubits: 1, size: 1

• Thanks, I can see this will work. – Yitian Wang Nov 16 '20 at 0:51