Skip to content

Commit be1d024

Browse files
authored
Greedy router (#3360)
Fixes issue #3056. Fixes hanging greedy router on some circuit instances by partially reverting changes from 6d81ddb.
1 parent 46f8df7 commit be1d024

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

cirq/contrib/routing/greedy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,12 @@ def apply_next_swaps(self, require_frontier_adjacency: bool = False):
301301
]
302302
if len(candidate_swap_sets) == 1:
303303
self.apply_swap(*candidate_swap_sets[0])
304-
return
304+
if list(
305+
self.remaining_dag.findall_nodes_until_blocked(
306+
self.acts_on_nonadjacent_qubits)):
307+
return
308+
else:
309+
break
305310

306311
self.apply_next_swaps(True)
307312

cirq/contrib/routing/greedy_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from multiprocessing import Process
16+
1517
import pytest
1618

1719
import cirq
@@ -20,10 +22,47 @@
2022

2123

2224
def test_bad_args():
25+
"""Test zero valued arguments in greedy router."""
2326
circuit = cirq.testing.random_circuit(4, 2, 0.5, random_state=5)
2427
device_graph = ccr.get_grid_device_graph(3, 2)
2528
with pytest.raises(ValueError):
2629
route_circuit_greedily(circuit, device_graph, max_search_radius=0)
2730

2831
with pytest.raises(ValueError):
2932
route_circuit_greedily(circuit, device_graph, max_num_empty_steps=0)
33+
34+
35+
def create_circuit_and_device():
36+
"""Construct a small circuit and a device with line connectivity
37+
to test the greedy router. This instance hangs router in Cirq 8.2.
38+
"""
39+
num_qubits = 6
40+
gate_domain = {cirq.ops.CNOT: 2}
41+
circuit = cirq.testing.random_circuit(num_qubits,
42+
15,
43+
0.5,
44+
gate_domain,
45+
random_state=37)
46+
device_graph = ccr.get_linear_device_graph(num_qubits)
47+
return circuit, device_graph
48+
49+
50+
def create_hanging_routing_instance(circuit, device_graph):
51+
"""Create a test problem instance."""
52+
route_circuit_greedily(circuit,
53+
device_graph,
54+
max_search_radius=2,
55+
random_state=1)
56+
57+
58+
def test_router_hanging():
59+
"""Run a separate process and check if greedy router hits timeout (5s)."""
60+
circuit, device_graph = create_circuit_and_device()
61+
process = Process(target=create_hanging_routing_instance,
62+
args=[circuit, device_graph])
63+
process.start()
64+
process.join(timeout=5)
65+
try:
66+
assert not process.is_alive(), "Greedy router timeout"
67+
finally:
68+
process.terminate()

0 commit comments

Comments
 (0)