def greedy_router(problem_instance: dimod.BinaryQuadraticModel, qpu: Type[QPU]): #find edge coloring for problem graph #color_sets = edge_coloring.find_edge_coloring(problem_instance.to_networkx_graph()) #color_sets = sorted(color_sets, key=lambda color_set: len(color_set), reverse=True) # find initial mapping and execute interaction gates #initial_mapping, int_layer = int_pair_mapper(qpu, problem_instance, color_sets[0]) initial_mapping, int_layers = twoColorMapper(problem_instance, qpu) route = Routing(problem_instance, qpu, initial_mapping=initial_mapping) int_count = 0 for int_layer in int_layers: for int_gate in int_layer: int_count += 1 route.apply_int(int_gate) print(int_count) # find valid edge coloring #route = Routing(problem_instance, qpu) color_sets = edge_coloring.find_edge_coloring(route.remaining_interactions) color_sets = sorted(color_sets, key=lambda color_set: len(color_set), reverse=True) #finish remaining layers of color sets for color_set in color_sets: greedy_pair_mapper(route, color_set) return route
def execute_all_possible_int_gates(routing: Routing, int_pairs: Set[frozenset]) -> bool: int_graph = nx.Graph() gate_executed = False for int_pair in int_pairs: hard_qb0 = routing.mapping.log2hard[list(int_pair)[0]] hard_qb1 = routing.mapping.log2hard[list(int_pair)[1]] if routing.qpu.graph.has_edge(hard_qb0, hard_qb1): if routing.layers[-1].int_gate_applicable(frozenset((hard_qb0, hard_qb1))): int_graph.add_edge(hard_qb1, hard_qb0) gate_executed = True matching = nx.maximal_matching(int_graph) for match in matching: gate = frozenset(match) log_qb0 = routing.mapping.hard2log[list(match)[0]] log_qb1 = routing.mapping.hard2log[list(match)[1]] int_pair = frozenset((log_qb0, log_qb1)) routing.apply_int(gate) int_pairs.remove(int_pair) return gate_executed
class TestRouting(TestCase): def setUp(self) -> None: gr = nx.grid_2d_graph(2, 2) gr.add_edge((0, 0), (1, 1)) self.qpu = Grid2dQPU(2, 2) self.problem_instance = dimod.generators.random.uniform(gr, dimod.SPIN) init_map = {cirq.GridQubit(*loc): loc for loc in self.problem_instance.variables} self.initial_mapping = Mapping(self.qpu, self.problem_instance, initial_mapping=init_map) self.routing = Routing(self.problem_instance, self.qpu, cp.deepcopy(self.initial_mapping)) def test_apply_swap(self) -> None: self.routing.apply_swap(frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)))) self.assertEqual(self.initial_mapping.hard2log[cirq.GridQubit(0, 0)], self.routing.mapping.hard2log[cirq.GridQubit(1, 0)], msg="application of swap gate did not update mapping") self.assertEqual(self.initial_mapping.hard2log[cirq.GridQubit(1, 0)], self.routing.mapping.hard2log[cirq.GridQubit(0, 0)], msg="application of swap gate did not update mapping") self.assertTrue(self.routing.layers[0].gates[cirq.GridQubit(0, 0)][cirq.GridQubit(1, 0)]['swap'], msg="did not add swap gate to layer") self.routing.apply_swap(frozenset((cirq.GridQubit(0, 1), cirq.GridQubit(1, 1)))) self.assertEqual(self.initial_mapping.hard2log[cirq.GridQubit(0, 1)], self.routing.mapping.hard2log[cirq.GridQubit(1, 1)], msg="application of swap gate did not update mapping") self.assertEqual(self.initial_mapping.hard2log[cirq.GridQubit(1, 1)], self.routing.mapping.hard2log[cirq.GridQubit(0, 1)], msg="application of swap gate did not update mapping") self.assertTrue(self.routing.layers[0].gates[cirq.GridQubit(0, 1)][cirq.GridQubit(1, 1)]['swap'], msg="did not add swap gate to layer") self.routing.apply_swap(frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)))) self.assertTrue(self.routing.layers[1].gates[cirq.GridQubit(0, 0)][cirq.GridQubit(0, 1)]['swap'], msg="did not add layer containing new swap gate") self.routing.draw() def test_apply_int(self) -> None: self.routing.apply_int(frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)))) self.assertTrue(self.routing.layers[0].gates[cirq.GridQubit(0, 0)][cirq.GridQubit(1, 0)]['int'], msg="did not add int gate to layer") self.assertFalse(self.routing.remaining_interactions.has_edge(cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)), msg='interaction has not been deleted from routing.remaining_interactions') self.routing.apply_int(frozenset((cirq.GridQubit(0, 1), cirq.GridQubit(1, 1)))) self.assertTrue(self.routing.layers[0].gates[cirq.GridQubit(0, 1)][cirq.GridQubit(1, 1)]['int'], msg="did not add int gate to layer") self.assertFalse(self.routing.remaining_interactions.has_edge(cirq.GridQubit(0, 1), cirq.GridQubit(1, 1)), msg='interaction has not been deleted from routing.remaining_interactions') self.routing.apply_int(frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)))) self.assertTrue(self.routing.layers[1].gates[cirq.GridQubit(0, 0)][cirq.GridQubit(0, 1)]['int'], msg="did not add layer containing new int gate") self.assertFalse(self.routing.remaining_interactions.has_edge(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)), msg='interaction has not been deleted from routing.remaining_interactions')