Example #1
0
def test_is_valid_routing_with_bad_args():
    p, q, r = cirq.LineQubit.range(3)
    x, y = cirq.NamedQubit('x'), cirq.NamedQubit('y')
    circuit = cirq.Circuit([cirq.CNOT(x, y), cirq.CZ(x, y)])
    routed_circuit = cirq.Circuit([cirq.CNOT(p, q), cirq.CZ(q, r)])
    initial_mapping = {p: x, q: y}
    swap_network = ccr.SwapNetwork(routed_circuit, initial_mapping)
    assert not ccr.is_valid_routing(circuit, swap_network)

    def equals(*args):
        raise ValueError

    with pytest.raises(ValueError):
        ccr.is_valid_routing(circuit, swap_network, equals=equals)
Example #2
0
def test_route_circuit_via_unitaries(n_moments, algo, seed, make_bad):
    circuit = cirq.testing.random_circuit(4, n_moments, 0.5, random_state=seed)
    device_graph = ccr.get_grid_device_graph(3, 2)

    swap_network = ccr.route_circuit(circuit,
                                     device_graph,
                                     algo_name=algo,
                                     random_state=seed)

    logical_qubits = sorted(circuit.all_qubits())
    if len(logical_qubits) < 2:
        return
    reverse_mapping = {l: p for p, l in swap_network.initial_mapping.items()}
    physical_qubits = [reverse_mapping[l] for l in logical_qubits]
    physical_qubits += list(set(device_graph).difference(physical_qubits))
    n_unused_qubits = len(physical_qubits) - len(logical_qubits)

    if make_bad:
        swap_network.circuit += [cirq.CNOT(*physical_qubits[:2])]
    cca.return_to_initial_mapping(swap_network.circuit)

    logical_unitary = circuit.unitary(qubit_order=logical_qubits)
    logical_unitary = np.kron(logical_unitary, np.eye(1 << n_unused_qubits))
    physical_unitary = swap_network.circuit.unitary(
        qubit_order=physical_qubits)

    assert ccr.is_valid_routing(circuit, swap_network) == (not make_bad)
    assert np.allclose(physical_unitary, logical_unitary) == (not make_bad)
Example #3
0
def test_route_circuit(circuit, device_graph, algo):
    swap_network = ccr.route_circuit(circuit, device_graph, algo_name=algo)
    assert set(swap_network.initial_mapping).issubset(device_graph)
    assert (sorted(swap_network.initial_mapping.values()) == sorted(
        circuit.all_qubits()))
    assert ccr.ops_are_consistent_with_device_graph(
        swap_network.circuit.all_operations(), device_graph)
    assert ccr.is_valid_routing(circuit, swap_network)
Example #4
0
def test_route_circuit(n_moments, algo, circuit_seed, routing_seed):
    circuit = cirq.testing.random_circuit(10,
                                          n_moments,
                                          0.5,
                                          random_state=circuit_seed)
    device_graph = ccr.get_grid_device_graph(4, 3)
    swap_network = ccr.route_circuit(circuit,
                                     device_graph,
                                     algo_name=algo,
                                     random_state=routing_seed)
    assert set(swap_network.initial_mapping).issubset(device_graph)
    assert sorted(swap_network.initial_mapping.values()) == sorted(
        circuit.all_qubits())
    assert ccr.ops_are_consistent_with_device_graph(
        swap_network.circuit.all_operations(), device_graph)
    assert ccr.is_valid_routing(circuit, swap_network)