Skip to content

Commit

Permalink
test1 and test7 reverted
Browse files Browse the repository at this point in the history
  • Loading branch information
daxfohl committed Feb 7, 2025
1 parent a173a48 commit 577f9c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
15 changes: 9 additions & 6 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2144,11 +2144,14 @@ def insert(
and not isinstance(batch[0], Moment)
and strategy in [InsertStrategy.INLINE, InsertStrategy.EARLIEST]
and not all(
self._can_add_op_at(k, op) or k > 0 and self._can_add_op_at(k - 1, op)
(strategy is InsertStrategy.EARLIEST and self._can_add_op_at(k, op))
or (k > 0 and self._can_add_op_at(k - 1, op))
for op in cast(List['cirq.Operation'], batch)
)
):
self._moments.insert(k, Moment())
if strategy is InsertStrategy.INLINE:
k += 1
max_p = 0
for moment_or_op in batch:
# Determine Placement
Expand All @@ -2159,11 +2162,10 @@ def insert(
elif strategy in [InsertStrategy.NEW, InsertStrategy.NEW_THEN_INLINE]:
self._moments.insert(k, Moment())
p = k
else: # [InsertStrategy.INLINE, InsertStrategy.EARLIEST]
# At least one of k or k-1 is free due to batch's moment insertion check above
p = k if self._can_add_op_at(k, moment_or_op) else k - 1
if strategy is InsertStrategy.EARLIEST:
p = self.earliest_available_moment(moment_or_op, end_moment_index=p)
elif strategy is InsertStrategy.INLINE:
p = k - 1
else: # InsertStrategy.EARLIEST:
p = self.earliest_available_moment(moment_or_op, end_moment_index=k)
# Place
if isinstance(moment_or_op, Moment):
self._moments.insert(p, moment_or_op)
Expand All @@ -2175,6 +2177,7 @@ def insert(
max_p = max(p, max_p)
if strategy is InsertStrategy.NEW_THEN_INLINE:
strategy = InsertStrategy.INLINE
k += 1
k = max(k, max_p + 1)
self._mutated(preserve_placement_cache=True)
return k
Expand Down
19 changes: 13 additions & 6 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def test_insert_op_tree_inline():
op_tree_list = [
(1, 1, [cirq.H(a), cirq.X(b)], [a, b]),
(0, 0, [cirq.X(b)], [b]),
(4, 2, [cirq.H(b)], [b]),
(4, 3, [cirq.H(b)], [b]),
(5, 3, [cirq.H(a)], [a]),
(-2, 0, [cirq.X(b)], [b]),
(-5, 0, [cirq.CZ(a, b)], [a]),
Expand Down Expand Up @@ -865,13 +865,20 @@ def test_insert_inline_near_start():
c = cirq.Circuit([cirq.Moment(), cirq.Moment()])

c.insert(1, cirq.X(a), strategy=cirq.InsertStrategy.INLINE)
assert c == cirq.Circuit(cirq.Moment(), cirq.Moment(cirq.X(a)))
assert c == cirq.Circuit([cirq.Moment([cirq.X(a)]), cirq.Moment()])

c.insert(1, cirq.Y(a), strategy=cirq.InsertStrategy.INLINE)
assert c == cirq.Circuit(cirq.Moment(cirq.Y(a)), cirq.Moment(cirq.X(a)))
assert c == cirq.Circuit([cirq.Moment([cirq.X(a)]), cirq.Moment([cirq.Y(a)]), cirq.Moment()])

c.insert(0, cirq.Z(b), strategy=cirq.InsertStrategy.INLINE)
assert c == cirq.Circuit([cirq.Moment(cirq.Z(b), cirq.Y(a)), cirq.Moment(cirq.X(a))])
assert c == cirq.Circuit(
[
cirq.Moment([cirq.Z(b)]),
cirq.Moment([cirq.X(a)]),
cirq.Moment([cirq.Y(a)]),
cirq.Moment(),
]
)


def test_insert_at_frontier_init():
Expand Down Expand Up @@ -3575,7 +3582,7 @@ def test_insert_to_open_moment():
c1.insert(1, cirq.Y(q), strategy=cirq.InsertStrategy.INLINE)
expected = cirq.Circuit(cirq.X(q), cirq.Y(q), cirq.Z(q))
assert c0 == expected
assert c1 == expected
assert c1 == cirq.Circuit(cirq.X(q), cirq.Y(q), cirq.Moment(), cirq.Moment(cirq.Z(q)))


def test_insert_inline_when_requested_and_previous_moment_free():
Expand All @@ -3584,7 +3591,7 @@ def test_insert_inline_when_requested_and_previous_moment_free():
c = cirq.Circuit(cirq.Moment(cirq.X(q)), cirq.Moment(), cirq.Moment(), cirq.Moment(cirq.Z(q)))
c.insert(2, cirq.Y(q), strategy=cirq.InsertStrategy.INLINE)
assert c == cirq.Circuit(
cirq.Moment(cirq.X(q)), cirq.Moment(), cirq.Moment(cirq.Y(q)), cirq.Moment(cirq.Z(q))
cirq.Moment(cirq.X(q)), cirq.Moment(cirq.Y(q)), cirq.Moment(), cirq.Moment(cirq.Z(q))
)


Expand Down

0 comments on commit 577f9c0

Please sign in to comment.