def test_routing_no_cx() -> None:
    circ = Circuit(2, 2)
    circ.H(1)
    # c.CX(1, 2)
    circ.Rx(0.2, 0)
    circ.measure_all()
    coupling = [[1, 0], [2, 0], [2, 1], [3, 2], [3, 4], [4, 2]]
    arc = Architecture(coupling)
    physical_c = route(circ, arc)

    assert len(physical_c.get_commands()) == 4
def test_routing_measurements() -> None:
    qc = get_test_circuit(True)
    circ = qiskit_to_tk(qc)
    sim = AerBackend()
    original_results = sim.get_shots(circ, 10, seed=4)
    coupling = [[1, 0], [2, 0], [2, 1], [3, 2], [3, 4], [4, 2]]
    arc = Architecture(coupling)
    physical_c = route(circ, arc)
    Transform.DecomposeSWAPtoCX().apply(physical_c)
    Transform.DecomposeCXDirected(arc).apply(physical_c)
    Transform.OptimisePostRouting().apply(physical_c)
    assert (sim.get_shots(physical_c, 10) == original_results).all()
Beispiel #3
0
def tket_run(file_name, device_name):
    connection_list = qcdevice(device_name).connection_list
    circ = circuit_from_qasm(file_name)
    circ.measure_all()
    arc = Architecture(connection_list)
    dev = Device(arc)
    routed_circ = route(circ, arc)
    # cu = CompilationUnit(routed_circ)
    Transform.DecomposeBRIDGE().apply(routed_circ)
    # pass1 = DecomposeSwapsToCXs(dev)
    # pass1.apply(cu)
    # circ2 = cu.circuit
    return routed_circ
def route_circuit(tk_circuit, architecture_map):
    """Route the circuit to a given architecture map

    Parameters
    ----------
    tk_circuit : pytket Circuit
        A pytket circuit
    architecture_map : list
        A list of qubit pairings, which are in the form of tuples

    Returns
    -------
    pytket Circuit
        A pytket circuit routed for the given architecture

    Notes
    -----
    IBMQ architectures are provided in the IBMLayouts module, accessible with ALALI.ibm"""
    architecture = Architecture(architecture_map)
    routed_circuit = route(tk_circuit, architecture)
    return routed_circuit
Beispiel #5
0
print(tk_to_qiskit(example_circuit))

# This circuit can not be executed on any of our Architectures without modification. We can see this by looking at the circuits interaction graph, a graph where nodes are logical qubits and edges are some two-qubit gate.

interaction_edges = [(0, 1), (0, 2), (1, 2), (3, 2), (0, 3)]
draw_graph(interaction_edges)

draw_graph(simple_coupling_map)

# Sometimes we can route a circuit just by labelling the qubits to nodes of our Architecture such that the interaction graph matches a subgraph of the Architecture - unfortunately this isn't possible here.
#
# Let's call ```pytket```'s automatic routing method, route our circuit for the first Architecture we made, and have a look at our new circuit:

from pytket.routing import route

simple_modified_circuit = route(example_circuit, simple_architecture)

for gate in simple_modified_circuit:
    print(gate)

print(tk_to_qiskit(simple_modified_circuit))

draw_graph(id_architecture.coupling)

# The route method has relabelled the qubits in our old circuit to nodes in simple_architecture, and has added ```SWAP``` gates that permute logical qubits on nodes of our Architecture.
#
# Let's repeat this for id_architecture:

id_modified_circuit = route(example_circuit, id_architecture)

for gate in id_modified_circuit:
Beispiel #6
0
M = int(sys.argv[1])
graph = nx.random_regular_graph(3, M)
edges = list(graph.edges())
print(edges)

connection_list = [(0, 2), (1, 2), (2, 3), (1, 5), (2, 6), (3, 7), (4, 5),
                   (5, 6), (6, 7), (7, 8), (4, 10), (5, 11), (6, 12), (7, 13),
                   (9, 10), (10, 11), (11, 12), (12, 13), (9, 15), (10, 16),
                   (11, 17), (12, 18), (14, 15), (15, 16), (16, 17), (17, 18),
                   (15, 19), (16, 20), (17, 21), (19, 20), (20, 21), (20, 22)]

circ = pytket.Circuit(23, 0)
for edge in edges:
    circ.ZZPhase(angle=0.75, qubit0=edge[0], qubit1=edge[1])

routed_circ = route(circ, Architecture(connection_list), decompose_swaps=False)
Transform.DecomposeBRIDGE().apply(routed_circ)

cirq_circuit = tk_to_cirq(routed_circ)
num_swap = 0
for layer in cirq_circuit:
    for gate in layer:
        if gate.__str__().startswith('SWAP'):
            num_swap += 1
tket_depth = len(cirq_circuit)
print("num_swap", num_swap, " depth", tket_depth)
for layer in cirq_circuit:
    print(layer)

[our_depth, our_swap] = qaoa_exp_smt(M, edges)
Beispiel #7
0
def schedule_swaps(environment,
                   circuit,
                   qubit_locations=None,
                   safety_checks_on=False,
                   decompose_cnots=False):
    original_circuit = circuit

    circuit = qiskit_to_tk(circuit.to_qiskit_rep())
    architecture = generate_architecture(environment)

    if qubit_locations is None:
        qubit_locations = list(range(environment.number_of_nodes))
        random.shuffle(qubit_locations)

    initial_index_map = {
        qubit: node
        for node, qubit in enumerate(qubit_locations)
    }
    initial_map = convert_index_mapping(circuit, architecture,
                                        initial_index_map)

    initial_qubit_locations = [-1] * len(qubit_locations)

    for k, v in initial_map.items():
        q = k.index[0]
        n = v.index[0]

        initial_qubit_locations[n] = q

    place_with_map(circuit, initial_map)
    routed_circuit = route(circuit,
                           architecture,
                           swap_lookahead=1000,
                           bridge_interactions=0,
                           bridge_lookahead=0)

    node_circuit = NodeCircuit.from_qiskit_rep(tk_to_qiskit(routed_circuit),
                                               decompose=decompose_cnots)

    if decompose_cnots:
        Transform.DecomposeSWAPtoCX().apply(routed_circuit)
        #Transform.DecomposeBRIDGE().apply(routed_circuit)

    tket_depth = routed_circuit.depth()

    calculated_depth = node_circuit.depth()

    if tket_depth != calculated_depth:
        print('Tket depth:', tket_depth)
        print('Calculated depth:', calculated_depth)
        print()

        exit("Tket depth disagrees with calculated depth")

    layers = assemble_timesteps_from_gates(node_circuit.n_nodes,
                                           node_circuit.gates)

    if safety_checks_on:
        verify_circuit(original_circuit, node_circuit, environment,
                       initial_qubit_locations)

    return layers, tket_depth