Skip to content

Left-addition of cirq.GateOperation with cirq.Circuit raises TypeError #7706

@bharat-thotakura

Description

@bharat-thotakura

Describe the issue

Creating a standalone cirq.GateOperation (so not wrapped in a list or cirq.FrozenCircuit, for example) and adding it first to some other cirq.Circuit object now (in 1.7.0.dev20251014004317) raises a TypeError when it did not do so in previous cirq versions (cirq-core<=1.6.1).

Explain how to reproduce the bug or problem

Previously (testing in cirq-core==1.6.1), a snippet like

import cirq
h_gate = cirq.H(cirq.q(0))
circuit = h_gate + cirq.Circuit(cirq.CNOT(cirq.q(0), cirq.q(1)))

would work the same as if:

cirq.Circuit(cirq.H(cirq.q(0)), cirq.CNOT(cirq.q(0), cirq.q(1)))

However, cirq-core==1.7.0.dev20251014004317 throws

h_gate = cirq.H(cirq.q(0))
circuit = h_gate + cirq.Circuit(cirq.CNOT(cirq.q(0), cirq.q(1)))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 circuit = h_gate + cirq.Circuit(cirq.CNOT(cirq.q(0), cirq.q(1)))

File ~/.venvs/ex_env/lib/python3.13/site-packages/cirq/ops/gate_operation.py:335, in GateOperation.__add__(self, other)
    334 def __add__(self, other):
--> 335     return 1 * self + other

An error is not raised, though, with these cases:

circuit = cirq.Circuit(cirq.X(cirq.q(0))) + h_gate + cirq.Circuit(cirq.CNOT(cirq.q(0), cirq.q(1)))
# or 
circuit = cirq.Circuit(cirq.CNOT(cirq.q(0), cirq.q(1))) + h_gate
# or
h_gate = cirq.H.on_each(cirq.q(0)) # A `list` essentially
circuit = h_gate + cirq.Circuit(cirq.CNOT(cirq.q(0), cirq.q(1)))

Tell us the version of Cirq where this happens

This is happening in at least cirq-core==1.7.0.dev20250925223930 onwards, and looks to be stemming from 6816824

Metadata

Metadata

Assignees

Labels

area/circuitsarea/gatesgood first issueThis issue can be resolved by someone who is not familiar with the codebase. A good starting issue.kind/bug-reportSomething doesn't seem to work.triage/acceptedA consensus emerged that this bug report, feature request, or other action should be worked on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions