From 12c42d0bd753bae267a5377c6dc37b80d950df25 Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Thu, 24 Sep 2020 18:34:18 -0400 Subject: [PATCH 01/11] Fix greedy router hanging issue --- cirq/contrib/routing/greedy.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cirq/contrib/routing/greedy.py b/cirq/contrib/routing/greedy.py index ff0447cbad4..57301abcfea 100644 --- a/cirq/contrib/routing/greedy.py +++ b/cirq/contrib/routing/greedy.py @@ -301,9 +301,12 @@ def apply_next_swaps(self, require_frontier_adjacency: bool = False): ] if len(candidate_swap_sets) == 1: self.apply_swap(*candidate_swap_sets[0]) - return - - self.apply_next_swaps(True) + if list( + self.remaining_dag.findall_nodes_until_blocked( + self.acts_on_nonadjacent_qubits)): + return + else: + break def route(self): self.apply_possible_ops() From 3b600b7d398133e8617cc9d7c4ec3bc673b52e5f Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Thu, 24 Sep 2020 18:40:25 -0400 Subject: [PATCH 02/11] Cleanup --- cirq/contrib/routing/greedy.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cirq/contrib/routing/greedy.py b/cirq/contrib/routing/greedy.py index 57301abcfea..5eed8a928b2 100644 --- a/cirq/contrib/routing/greedy.py +++ b/cirq/contrib/routing/greedy.py @@ -308,6 +308,8 @@ def apply_next_swaps(self, require_frontier_adjacency: bool = False): else: break + self.apply_next_swaps(True) + def route(self): self.apply_possible_ops() empty_steps_remaining = self.max_num_empty_steps From 0bcf738477cfde5a93598aa997625d7182db978a Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 01:09:47 -0400 Subject: [PATCH 03/11] Add greedy router timeout test (timeout = 1s) --- cirq/contrib/routing/greedy_test.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index b66d98d93d3..a73e67b2600 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -13,6 +13,7 @@ # limitations under the License. import pytest +from multiprocessing import Process, TimeoutError import cirq import cirq.contrib.routing as ccr @@ -27,3 +28,26 @@ def test_bad_args(): with pytest.raises(ValueError): route_circuit_greedily(circuit, device_graph, max_num_empty_steps=0) + + +def create_circuit_and_device(): + num_qubits = 6 + gate_domain = {cirq.ops.CNOT: 2} + circuit = cirq.testing.random_circuit(num_qubits, 15, 0.5, gate_domain, random_state=37) + device_graph = ccr.get_linear_device_graph(num_qubits) + return circuit, device_graph + + +def create_hanging_routing_instance(circuit, device_graph): + route_circuit_greedily(circuit, device_graph, max_search_radius=2, random_state=1) + + +def test_router_hanging(): + circuit, device_graph = create_circuit_and_device() + p = Process(target=create_hanging_routing_instance, args=[circuit, device_graph]) + p.start() + p.join(timeout=1) + alive = p.is_alive() + if alive: + p.terminate() + assert not alive, "Greedy router timeout" From 4febf2e1de30064deeb951c8203ecf297625749e Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 01:12:47 -0400 Subject: [PATCH 04/11] Remove TimeoutError import --- cirq/contrib/routing/greedy_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index a73e67b2600..e20f7640e9f 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -13,7 +13,7 @@ # limitations under the License. import pytest -from multiprocessing import Process, TimeoutError +from multiprocessing import Process import cirq import cirq.contrib.routing as ccr From 1c87a6c7ffc58b8d90416a33db82e21071973937 Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 01:14:49 -0400 Subject: [PATCH 05/11] Fix formatting --- cirq/contrib/routing/greedy_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index e20f7640e9f..e764ea84083 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -33,7 +33,9 @@ def test_bad_args(): def create_circuit_and_device(): num_qubits = 6 gate_domain = {cirq.ops.CNOT: 2} - circuit = cirq.testing.random_circuit(num_qubits, 15, 0.5, gate_domain, random_state=37) + circuit = cirq.testing.random_circuit( + num_qubits, 15, 0.5, gate_domain, random_state=37 + ) device_graph = ccr.get_linear_device_graph(num_qubits) return circuit, device_graph From 5d8e531a2a01e948336a371d278d38373a072176 Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 01:22:29 -0400 Subject: [PATCH 06/11] Pylint formatting --- cirq/contrib/routing/greedy_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index e764ea84083..7ae276d309c 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest from multiprocessing import Process +import pytest import cirq import cirq.contrib.routing as ccr @@ -46,10 +46,10 @@ def create_hanging_routing_instance(circuit, device_graph): def test_router_hanging(): circuit, device_graph = create_circuit_and_device() - p = Process(target=create_hanging_routing_instance, args=[circuit, device_graph]) - p.start() - p.join(timeout=1) - alive = p.is_alive() + process = Process(target=create_hanging_routing_instance, args=[circuit, device_graph]) + process.start() + process.join(timeout=1) + alive = process.is_alive() if alive: - p.terminate() + process.terminate() assert not alive, "Greedy router timeout" From 60c223a3572eef2b51ab3b6ef82db4d85ba52f34 Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 01:37:01 -0400 Subject: [PATCH 07/11] Add comments --- cirq/contrib/routing/greedy_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index 7ae276d309c..8a01cc1de04 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -13,6 +13,7 @@ # limitations under the License. from multiprocessing import Process + import pytest import cirq @@ -21,6 +22,7 @@ def test_bad_args(): + """Test zero valued arguments in greedy router.""" circuit = cirq.testing.random_circuit(4, 2, 0.5, random_state=5) device_graph = ccr.get_grid_device_graph(3, 2) with pytest.raises(ValueError): @@ -31,6 +33,9 @@ def test_bad_args(): def create_circuit_and_device(): + """Construct a small circuit and a device with line connectivity + to test the greedy router. This instance hangs router in Cirq 8.2. + """ num_qubits = 6 gate_domain = {cirq.ops.CNOT: 2} circuit = cirq.testing.random_circuit( @@ -41,10 +46,12 @@ def create_circuit_and_device(): def create_hanging_routing_instance(circuit, device_graph): + """Create a test problem instance.""" route_circuit_greedily(circuit, device_graph, max_search_radius=2, random_state=1) def test_router_hanging(): + """Run a separate process and check if greedy router hits timeout (1s).""" circuit, device_graph = create_circuit_and_device() process = Process(target=create_hanging_routing_instance, args=[circuit, device_graph]) process.start() From a7c280a79cc9a7267b142d6a607ddc430ce9fa28 Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 01:45:13 -0400 Subject: [PATCH 08/11] Fix formatting --- cirq/contrib/routing/greedy_test.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index 8a01cc1de04..8c7b2da0033 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -38,22 +38,28 @@ def create_circuit_and_device(): """ num_qubits = 6 gate_domain = {cirq.ops.CNOT: 2} - circuit = cirq.testing.random_circuit( - num_qubits, 15, 0.5, gate_domain, random_state=37 - ) + circuit = cirq.testing.random_circuit(num_qubits, + 15, + 0.5, + gate_domain, + random_state=37) device_graph = ccr.get_linear_device_graph(num_qubits) return circuit, device_graph def create_hanging_routing_instance(circuit, device_graph): """Create a test problem instance.""" - route_circuit_greedily(circuit, device_graph, max_search_radius=2, random_state=1) + route_circuit_greedily(circuit, + device_graph, + max_search_radius=2, + random_state=1) def test_router_hanging(): """Run a separate process and check if greedy router hits timeout (1s).""" circuit, device_graph = create_circuit_and_device() - process = Process(target=create_hanging_routing_instance, args=[circuit, device_graph]) + process = Process(target=create_hanging_routing_instance, + args=[circuit, device_graph]) process.start() process.join(timeout=1) alive = process.is_alive() From c16fa0708091ad37ac4e467b67a5d3eacde53b99 Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 12:56:57 -0400 Subject: [PATCH 09/11] Comply with coverage test. Increase timout cutoff to 5s. --- cirq/contrib/routing/greedy_test.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index 8c7b2da0033..434d033e1ea 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import time from multiprocessing import Process @@ -56,13 +57,14 @@ def create_hanging_routing_instance(circuit, device_graph): def test_router_hanging(): - """Run a separate process and check if greedy router hits timeout (1s).""" + """Run a separate process and check if greedy router hits timeout (5s).""" circuit, device_graph = create_circuit_and_device() process = Process(target=create_hanging_routing_instance, args=[circuit, device_graph]) process.start() - process.join(timeout=1) + process.join(timeout=5) alive = process.is_alive() - if alive: + try: + assert not process.is_alive(), "Greedy router timeout" + finally: process.terminate() - assert not alive, "Greedy router timeout" From 1d474aa2eb806c6f490830424ca9cc0d749b6f6e Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 13:03:49 -0400 Subject: [PATCH 10/11] Cleanup --- cirq/contrib/routing/greedy_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index 434d033e1ea..0a6ffcba707 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -63,7 +63,6 @@ def test_router_hanging(): args=[circuit, device_graph]) process.start() process.join(timeout=5) - alive = process.is_alive() try: assert not process.is_alive(), "Greedy router timeout" finally: From d64eaab56f4e71a21e6267530b7462d892897b9e Mon Sep 17 00:00:00 2001 From: Yaroslav Kharkov Date: Wed, 30 Sep 2020 13:22:27 -0400 Subject: [PATCH 11/11] Comply with Lint check --- cirq/contrib/routing/greedy_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cirq/contrib/routing/greedy_test.py b/cirq/contrib/routing/greedy_test.py index 0a6ffcba707..1776bcd76f2 100644 --- a/cirq/contrib/routing/greedy_test.py +++ b/cirq/contrib/routing/greedy_test.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import time from multiprocessing import Process