Cirq
Cirq copied to clipboard
Cannot Add/Subtract Identity Gate in Cirq
Description
If one tries to add or subtract the identity gate from another identity gate on a qubit (e.g., I(q) - I(q)), Cirq throws a TypeError.
❌ Failing Example
from cirq import I, LineQubit
q = LineQubit(0)
print(I(q) + I(q))
💥 Error
TypeError: unsupported operand type(s) for +: 'GateOperation' and 'GateOperation'
While adding/subtracting GateOperation objects directly fails, some equivalent transformations succeed:
✅ Passing Alternatives
from cirq import I, PauliString, LineQubit
q = LineQubit(0)
assert PauliString() - I(q) == I(q) + (-1) * I(q)
🔁 How to Reproduce
The following code adds and subtracts the identity gates
from cirq import I, LineQubit
q = LineQubit(0)
I(q) + I(q)
I(q) - I(q)
This snippet outputs:
I+I: error
I-I: error
🧩 Version Info
This happens on Cirq 1.4.1 (latest as of this report).
Discussed in Cirq Cynq 2025-04-30: the example that succeeds may be succeeding due to a different reason (eg type coercion). We need to look at the operations that PauliString supports, and compare it to what Identity gate supports.