Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

Cannot Add/Subtract Identity Gate in Cirq

Open RevanthGundala opened this issue 8 months ago • 1 comments

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).

RevanthGundala avatar Apr 17 '25 20:04 RevanthGundala

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.

mhucka avatar Apr 30 '25 17:04 mhucka