quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.24k stars 1.01k forks source link

Issue when commuting moments #6659

Open cdbf1 opened 2 months ago

cdbf1 commented 2 months ago

Description of the issue Cirq gives the wrong result when determining whether moments commute if the individual operators within the moments do not commute, but the moment as a whole still commutes. For example, take a pair of single qubit Z gates and a two qubit RXX gate. While [Z, RXX] !=0 (for either single qubit Z), we do in fact have [Z * Z, RXX] = 0. However, when using cirq we have

import cirq
qubits = cirq.LineQubits.range(2)
moment_1 = cirq.Moment([cirq.Z(qubits[0], cirq.Z(qubits[1])])
moment_2 = cirq.XXPowGate(exponent=1 / 2)(*qubits)

print(cirq.commutes(moment_1, moment_2))

Gives False.

Looking through the source code this seems to be because the the commutes() function compares pairs of operators form each moment but doesn't consider the case when the moment as a whole commutes even when the individual operators do not.

How to reproduce the issue See above

Cirq version You can get the cirq version by printing cirq.__version__. From the command line: 1.5.0

dstrain115 commented 2 months ago

cirq cync: This functionality works for pauli strings, but not for moments.

A naive solution would be to decompose the moments (if commutes is initially determined to be false via gate by gate comparison).

From the documentation: Either value has a commutes method that returns 'True', 'False', or 'None' (meaning indeterminate).

So, this should either return None (not sure) or should perform an alternative strategy like decomposing the moment into a unitary.