コード例 #1
0
 def test_int_pair_distance_change(self):
     qpu = Grid2dQPU(2, 2)
     bqm = dimod.generators.randint(qpu.graph, dimod.SPIN)
     route = Routing(bqm, qpu)
     log_qb0 = route.mapping.hard2log[cirq.GridQubit(0, 0)]
     log_qb1 = route.mapping.hard2log[cirq.GridQubit(0, 1)]
     int_pairs = {frozenset((log_qb0, log_qb1))}
     swap = frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)))
     self.assertEqual(
         0,
         int_pair_distance_change(route, int_pairs, swap),
         msg='swap distance did change after swap on interaction pair')
     log_qb0 = route.mapping.hard2log[cirq.GridQubit(0, 0)]
     log_qb1 = route.mapping.hard2log[cirq.GridQubit(1, 1)]
     int_pairs = {frozenset((log_qb0, log_qb1))}
     swap = frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)))
     self.assertEqual(1,
                      int_pair_distance_change(route, int_pairs, swap),
                      msg='swap distance change is incorrect')
     log_qb0 = route.mapping.hard2log[cirq.GridQubit(0, 1)]
     log_qb1 = route.mapping.hard2log[cirq.GridQubit(1, 0)]
     int_pairs.add(frozenset((log_qb0, log_qb1)))
     self.assertEqual(2,
                      int_pair_distance_change(route, int_pairs, swap),
                      msg='swap distance change is incorrect')
コード例 #2
0
 def test_execute_all_possible_int_gates(self):
     qpu = Grid2dQPU(2, 2)
     linear = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0}
     quadratic = {(0, 1): 1, (1, 2): 1, (2, 3): 1, (3, 0): 1}
     bqm = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, dimod.SPIN)
     initial_mapping_dict = {
         cirq.GridQubit(0, 0): 0,
         cirq.GridQubit(1, 0): 1,
         cirq.GridQubit(0, 1): 2,
         cirq.GridQubit(1, 1): 3
     }
     mapping = Mapping(qpu,
                       bqm,
                       partial_initial_mapping=initial_mapping_dict)
     int_pairs = {frozenset((0, 1)), frozenset((0, 2)), frozenset((2, 3))}
     route = Routing(bqm, qpu, initial_mapping=mapping)
     gate_executed = execute_all_possible_int_gates(route, int_pairs)
     self.assertTrue(
         gate_executed,
         msg=
         'did not execute a single int gate even though log qubits are adjacent on the qpu'
     )
     test = route.layers[-1].gates[cirq.GridQubit(0, 0)][cirq.GridQubit(
         1, 0)]['int'] and route.layers[-1].gates[cirq.GridQubit(
             0, 1)][cirq.GridQubit(1, 1)]['int']
     self.assertTrue(
         test, msg='did not execute maximal possible set of int gates')
コード例 #3
0
def greedy_router_benchmark():
    data = {n**2: regular_graph_bqm(4, n**2, 20) for n in range(3, 7)}
    layer_count_avg = np.zeros(len(data))
    layer_count_20perc = np.zeros(len(data))
    layer_count_80perc = np.zeros(len(data))
    i = -1
    for size, data_point in data.items():
        print(size)
        i += 1
        qpu = Grid2dQPU(int(np.sqrt(size)), int(np.sqrt(size)))
        layer_counts = np.zeros(len(data_point))
        for bqm_index, bqm in enumerate(data_point):
            route = greedy_router(bqm, qpu)
            assert route.remaining_interactions.size(
            ) == 0, 'routing not finished with success'
            layer_counts[bqm_index] = len(route.layers)
        layer_count_avg[i] = np.mean(layer_counts)
        layer_count_20perc[i] = np.percentile(layer_counts, 20)
        layer_count_80perc[i] = np.percentile(layer_counts, 80)
    sys_size = np.zeros(len(data))
    for ind, sze in enumerate(data):
        sys_size[ind] = sze
    plt.plot(sys_size, layer_count_avg, color='blue')
    plt.fill_between(sys_size,
                     layer_count_20perc,
                     layer_count_80perc,
                     facecolor='blue',
                     alpha=0.5)
    plt.show()
    data = np.column_stack(
        (sys_size, layer_count_avg, layer_count_20perc, layer_count_80perc))
    header = 'system size, layer count average, layer count 20th percentile, layer count 80th percentile'
    np.savetxt('./Data/results/greedy_router_v2.dat', data, header=header)
コード例 #4
0
 def setUp(self) -> None:
     self.bqm_arr = [
         dimod.generators.uniform(nx.random_regular_graph(4, 16),
                                  dimod.SPIN,
                                  low=0.5,
                                  high=1.0) for _ in range(10)
     ]
     self.qpu = Grid2dQPU(4, 4)
コード例 #5
0
ファイル: twoColorMapper_test.py プロジェクト: MLeib/QAOA
 def setUp(self) -> None:
     self.test_bqms = [
         dimod.generators.uniform(nx.random_regular_graph(4, 36),
                                  dimod.SPIN,
                                  low=0.5,
                                  high=1.0) for _ in range(40)
     ]
     self.qpu = Grid2dQPU(6, 6)
コード例 #6
0
ファイル: routing_test.py プロジェクト: MLeib/QAOA
 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))
コード例 #7
0
ファイル: routing_test.py プロジェクト: MLeib/QAOA
 def setUp(self) -> None:
     qpu = Grid2dQPU(5, 4)
     self.layer = Layer(qpu)
     self.graph = qpu.graph
     matching = nx.maximal_matching(self.graph)
     while not nx.is_perfect_matching(self.graph, matching):
         self.graph = nx.random_regular_graph(4, 20)
         matching = nx.maximal_matching(self.graph)
     self.matching_gates = set()
     for gate in matching:
         self.matching_gates.add(frozenset(gate))
コード例 #8
0
 def test_greedy_pair_mapper(self):
     qpu = Grid2dQPU(2, 2)
     linear = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0}
     quadratic = {
         (0, 1): 1,
         (1, 2): 1,
         (2, 3): 1,
         (3, 0): 1,
         (0, 2): 1,
         (1, 3): 1
     }
     bqm = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, dimod.SPIN)
     int_pairs = {frozenset((0, 2)), frozenset((1, 3))}
     route = Routing(bqm, qpu)
     greedy_pair_mapper(route, int_pairs)
     for pair in int_pairs:
         self.assertFalse(
             route.remaining_interactions.has_edge(*pair),
             msg='did not execute interaction given in int pairs')
コード例 #9
0
ファイル: test_mapping.py プロジェクト: MLeib/QAOA
 def setUp(self) -> None:
     self.qpu = Grid2dQPU(4, 6)
     problem_graph = nx.random_regular_graph(3, 24)
     self.problem = max_cut(problem_graph)
     self.mapping = Mapping(self.qpu, self.problem)