Ejemplo n.º 1
0
    def test_measurement_error_mitigation_qaoa(self):
        """measurement error mitigation test with QAOA"""
        algorithm_globals.random_seed = 167

        # build noise model
        noise_model = noise.NoiseModel()
        read_err = noise.errors.readout_error.ReadoutError([[0.9, 0.1],
                                                            [0.25, 0.75]])
        noise_model.add_all_qubit_readout_error(read_err)

        backend = Aer.get_backend("aer_simulator")
        quantum_instance = QuantumInstance(
            backend=backend,
            seed_simulator=algorithm_globals.random_seed,
            seed_transpiler=algorithm_globals.random_seed,
            noise_model=noise_model,
            measurement_error_mitigation_cls=CompleteMeasFitter,
        )
        w = rx.adjacency_matrix(
            rx.undirected_gnp_random_graph(5,
                                           0.5,
                                           seed=algorithm_globals.random_seed))
        qubit_op, _ = self._get_operator(w)
        qaoa = QAOA(
            optimizer=COBYLA(maxiter=1),
            quantum_instance=quantum_instance,
            initial_point=np.asarray([0.0, 0.0]),
        )
        result = qaoa.compute_minimum_eigenvalue(operator=qubit_op)
        self.assertAlmostEqual(result.eigenvalue.real, 3.49, delta=0.05)
Ejemplo n.º 2
0
 def test_graph_empty_dicts(self):
     graph = retworkx.undirected_gnp_random_graph(3, 0.9, seed=42)
     dot_str = graph.to_dot(lambda _: {}, lambda _: {})
     self.assertEqual(
         "graph {\n0 ;\n1 ;\n2 ;\n1 -- 0 ;\n2 -- 0 ;\n"
         "2 -- 1 ;\n}\n",
         dot_str,
     )
Ejemplo n.º 3
0
 def test_graph_graph_attrs(self):
     graph = retworkx.undirected_gnp_random_graph(3, 0.9, seed=42)
     dot_str = graph.to_dot(lambda _: {}, lambda _: {}, {"bgcolor": "red"})
     self.assertEqual(
         "graph {\nbgcolor=red ;\n0 ;\n1 ;\n2 ;\n1 -- 0 ;\n"
         "2 -- 0 ;\n2 -- 1 ;\n}\n",
         dot_str,
     )
Ejemplo n.º 4
0
    def test_qaoa_random_initial_point(self):
        """QAOA random initial point"""
        w = rx.adjacency_matrix(
            rx.undirected_gnp_random_graph(5, 0.5, seed=algorithm_globals.random_seed)
        )
        qubit_op, _ = self._get_operator(w)
        qaoa = QAOA(optimizer=NELDER_MEAD(disp=True), reps=1, quantum_instance=self.qasm_simulator)
        result = qaoa.compute_minimum_eigenvalue(operator=qubit_op)

        self.assertLess(result.eigenvalue, -0.97)
Ejemplo n.º 5
0
 def test_gnp_random_against_networkx_max_cardinality(self):
     rx_graph = retworkx.undirected_gnp_random_graph(10, 0.78, seed=428)
     nx_graph = networkx.Graph(list(rx_graph.edge_list()))
     nx_matches = networkx.max_weight_matching(nx_graph,
                                               maxcardinality=True)
     rx_matches = retworkx.max_weight_matching(rx_graph,
                                               max_cardinality=True,
                                               verify_optimum=True)
     self.compare_rx_nx_sets(rx_graph, rx_matches, nx_matches, 428,
                             nx_graph)
Ejemplo n.º 6
0
 def test_gnp_random_against_networkx(self):
     for i in range(1024):
         # TODO: add back subTest usage on new testtools release
         rx_graph = retworkx.undirected_gnp_random_graph(10,
                                                         0.75,
                                                         seed=42 + i)
         nx_graph = networkx.Graph(list(rx_graph.edge_list()))
         nx_matches = networkx.max_weight_matching(nx_graph)
         rx_matches = retworkx.max_weight_matching(rx_graph,
                                                   verify_optimum=True)
         self.compare_rx_nx_sets(rx_graph, rx_matches, nx_matches, 42 + i,
                                 nx_graph)
Ejemplo n.º 7
0
 def test_gnp_random_against_networkx(self):
     for i in range(1024):
         with self.subTest(i=i):
             rx_graph = retworkx.undirected_gnp_random_graph(10,
                                                             0.75,
                                                             seed=42 + i)
             nx_graph = networkx.Graph(list(rx_graph.edge_list()))
             nx_matches = networkx.max_weight_matching(nx_graph)
             rx_matches = retworkx.max_weight_matching(rx_graph,
                                                       verify_optimum=True)
             self.compare_rx_nx_sets(rx_graph, rx_matches, nx_matches,
                                     42 + i, nx_graph)
Ejemplo n.º 8
0
    def test_qaoa_random_initial_point(self):
        """ QAOA random initial point """
        w = rx.adjacency_matrix(
            rx.undirected_gnp_random_graph(5,
                                           0.5,
                                           seed=algorithm_globals.random_seed))
        qubit_op, _ = self._get_operator(w)
        qaoa = QAOA(optimizer=NELDER_MEAD(disp=True),
                    reps=1,
                    quantum_instance=self.qasm_simulator)
        _ = qaoa.compute_minimum_eigenvalue(operator=qubit_op)

        np.testing.assert_almost_equal([-0.560, 0.315],
                                       qaoa.optimal_params,
                                       decimal=3)
Ejemplo n.º 9
0
 def test_gnp_random_against_networkx_with_negative_weight(self):
     for i in range(1024):
         with self.subTest(i=i):
             random.seed(i)
             rx_graph = retworkx.undirected_gnp_random_graph(10,
                                                             0.75,
                                                             seed=42 + i)
             for edge in rx_graph.edge_list():
                 rx_graph.update_edge(*edge, random.randint(-5000, 5000))
             nx_graph = networkx.Graph([(x[0], x[1], {
                 "weight": x[2]
             }) for x in rx_graph.weighted_edge_list()])
             nx_matches = networkx.max_weight_matching(nx_graph)
             rx_matches = retworkx.max_weight_matching(
                 rx_graph, weight_fn=lambda x: x, verify_optimum=True)
             self.compare_rx_nx_sets(rx_graph, rx_matches, nx_matches,
                                     42 + i, nx_graph)
Ejemplo n.º 10
0
 def test_gnp_random_against_networkx_with_weight(self):
     for i in range(1024):
         # TODO: add back subTest usage on new testtools release
         random.seed(i)
         rx_graph = retworkx.undirected_gnp_random_graph(10,
                                                         .75,
                                                         seed=42 + i)
         for edge in rx_graph.edge_list():
             rx_graph.update_edge(*edge, random.randint(0, 5000))
         nx_graph = networkx.Graph([(x[0], x[1], {
             'weight': x[2]
         }) for x in rx_graph.weighted_edge_list()])
         nx_matches = networkx.max_weight_matching(nx_graph)
         rx_matches = retworkx.max_weight_matching(rx_graph,
                                                   weight_fn=lambda x: x,
                                                   verify_optimum=True)
         self.compare_rx_nx_sets(rx_graph, rx_matches, nx_matches, 42 + i,
                                 nx_graph)
Ejemplo n.º 11
0
 def test_random_gnp_undirected_invalid_probability(self):
     with self.assertRaises(ValueError):
         retworkx.undirected_gnp_random_graph(23, 123.5)
Ejemplo n.º 12
0
 def test_random_gnp_undirected_invalid_num_nodes(self):
     with self.assertRaises(ValueError):
         retworkx.undirected_gnp_random_graph(-23, .5)
Ejemplo n.º 13
0
 def test_random_gnp_undirected_complete_graph(self):
     graph = retworkx.undirected_gnp_random_graph(20, 1)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 20 * (20 - 1) / 2)
Ejemplo n.º 14
0
 def test_random_gnp_undirected_empty_graph(self):
     graph = retworkx.undirected_gnp_random_graph(20, 0)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 0)
Ejemplo n.º 15
0
 def test_random_gnp_undirected(self):
     graph = retworkx.undirected_gnp_random_graph(20, .5, seed=10)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 105)
Ejemplo n.º 16
0
 def test_random_graph_full_path(self):
     graph = retworkx.undirected_gnp_random_graph(100, 0.95, seed=42)
     adjacency_matrix = retworkx.graph_adjacency_matrix(graph)
     new_graph = retworkx.PyGraph.from_adjacency_matrix(adjacency_matrix)
     new_adjacency_matrix = retworkx.graph_adjacency_matrix(new_graph)
     self.assertTrue(np.array_equal(adjacency_matrix, new_adjacency_matrix))
Ejemplo n.º 17
0
 def setUp(self):
     super().setUp()
     if self.class_type == "PyGraph":
         self.graph = retworkx.undirected_gnp_random_graph(10, 0.5, seed=42)
     else:
         self.graph = retworkx.directed_gnp_random_graph(10, 0.5, seed=42)
Ejemplo n.º 18
0
    for i in range(log_m):
        width = initial_width * pow(2, i)
        for u, v in nx.bfs_beam_edges(a, s, v, width):
            if con(v):
                return v
    print(nx.NodeNotFound("no node"))


def has_high_centrality(v: int) -> bool:
    return centrality[v] >= avg_centrality


seed: int = 89

# conversion of retworkx graph to networkx couldn't find any func in retworkx to calculate centrality
G: rx.PyGraph = rx.undirected_gnp_random_graph(100, 0.5, seed=seed)
n1: numpy.ndarray = rx.graph_adjacency_matrix(G, None)
G1: nx.classes.graph.Graph = nx.from_numpy_matrix(n1)

centrality: dict = nx.eigenvector_centrality(G1)
avg_centrality: float = sum(centrality.values()) / len(G1)

source: int = 0
value = centrality.get
print(type(value))
condition: Callable[[int], bool] = has_high_centrality
found_node: int = progressive_widening_search(G1, source, value, condition)
c: float = centrality[found_node]
print(f"found node {found_node} with centrality {c}")

# plotting
Ejemplo n.º 19
0
 def test_graph_no_args(self):
     graph = retworkx.undirected_gnp_random_graph(3, .95, seed=24)
     dot_str = graph.to_dot()
     self.assertEqual("graph {\n0 ;\n1 ;\n2 ;\n2 -- 0 ;\n2 -- 1 ;\n}\n",
                      dot_str)