|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from multiprocessing import Process |
| 16 | + |
15 | 17 | import pytest |
16 | 18 |
|
17 | 19 | import cirq |
|
20 | 22 |
|
21 | 23 |
|
22 | 24 | def test_bad_args(): |
| 25 | + """Test zero valued arguments in greedy router.""" |
23 | 26 | circuit = cirq.testing.random_circuit(4, 2, 0.5, random_state=5) |
24 | 27 | device_graph = ccr.get_grid_device_graph(3, 2) |
25 | 28 | with pytest.raises(ValueError): |
26 | 29 | route_circuit_greedily(circuit, device_graph, max_search_radius=0) |
27 | 30 |
|
28 | 31 | with pytest.raises(ValueError): |
29 | 32 | 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