Esempio n. 1
0
def is_valid_routing(
    circuit: circuits.Circuit,
    swap_network: SwapNetwork,
    *,
    equals: BINARY_OP_PREDICATE = operator.eq,
    can_reorder: BINARY_OP_PREDICATE = circuits.circuit_dag._disjoint_qubits,
) -> bool:
    """Determines whether a swap network is consistent with a given circuit.

    Args:
        circuit: The circuit.
        swap_network: The swap network whose validity is to be checked.
        equals: The function to determine equality of operations. Defaults to
            `operator.eq`.
        can_reorder: A predicate that determines if two operations may be
            reordered.
    """
    circuit_dag = circuits.CircuitDag.from_circuit(circuit,
                                                   can_reorder=can_reorder)
    logical_operations = swap_network.get_logical_operations()
    try:
        return cca.is_topologically_sorted(circuit_dag, logical_operations,
                                           equals)
    except ValueError as err:
        if re.match(r'Operation .* acts on unmapped qubit .*\.', str(err)):
            return False
        raise
Esempio n. 2
0
def test_topological_sort(circuit_dag, sorted_nodes):
    sorted_nodes = list(sorted_nodes)
    assert cca.is_topologically_sorted(circuit_dag,
                                       (node.val for node in sorted_nodes))

    assert not cca.is_topologically_sorted(circuit_dag,
                                           (node.val
                                            for node in sorted_nodes[:-1]))

    assert not cca.is_topologically_sorted(
        circuit_dag, (node.val for node in sorted_nodes + sorted_nodes[:2]))

    v, w = next(iter(circuit_dag.edges))
    i = sorted_nodes.index(v)
    j = sorted_nodes.index(w, i + 1)
    sorted_nodes[i], sorted_nodes[j] = sorted_nodes[j], sorted_nodes[j]

    assert cca.is_topologically_sorted(
        circuit_dag, (node.val for node in sorted_nodes)) == (v.val == w.val)