Skip to content

Commit

Permalink
Fix __radd__ losing device (#2451)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc authored and CirqBot committed Nov 20, 2019
1 parent f14ed20 commit 52b14db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,10 @@ def __radd__(self, other):
if not isinstance(other, (ops.Operation, Iterable)):
return NotImplemented
# Auto wrap OP_TREE inputs into a circuit.
result = Circuit(other)
return result.__iadd__(self)
result = self.copy()
result._moments[:0] = Circuit(other)._moments
result._device.validate_circuit(result)
return result

def __imul__(self, repetitions: int):
if not isinstance(repetitions, int):
Expand Down
11 changes: 11 additions & 0 deletions cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ def test_radd_op_tree():
[cirq.Moment([cirq.X(a)]),
cirq.Moment([cirq.Y(b)])])

# Preserves device.
c = cirq.Circuit(device=cirq.google.Bristlecone)
c2 = [] + c
assert c2.device is cirq.google.Bristlecone
assert c2 == c

# Validates versus device.
c = cirq.Circuit(device=cirq.google.Bristlecone)
with pytest.raises(ValueError, match='Unsupported qubit'):
_ = [cirq.X(cirq.NamedQubit('a'))] + c


def test_bool():
assert not cirq.Circuit()
Expand Down

0 comments on commit 52b14db

Please sign in to comment.