Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,7 +2129,7 @@ def earliest_available_moment(
Index of the earliest matching moment. Returns `end_moment_index` if no moment on left
is available.
"""
if end_moment_index is None:
if end_moment_index is None or end_moment_index > len(self.moments):
end_moment_index = len(self.moments)
last_available = end_moment_index
k = end_moment_index
Expand All @@ -2148,10 +2148,7 @@ def earliest_available_moment(
or not moment._control_keys_().isdisjoint(op_measurement_keys)
):
return last_available
if self._can_add_op_at(k, op):
# Note: Remove the if condition after `self._device` is gone and move the method to
# `cirq.AbstractDevice`.
last_available = k
last_available = k
return last_available

def _can_add_op_at(self, moment_index: int, operation: cirq.Operation) -> bool:
Expand Down
1 change: 1 addition & 0 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ def test_earliest_available_moment() -> None:
c.earliest_available_moment(cirq.Y(q[1]).with_classical_controls("m"), end_moment_index=1)
== 1
)
assert c.earliest_available_moment(cirq.Y(q[1]), end_moment_index=4) == 2


@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
Expand Down