コード例 #1
0
    def test_docplex_tsp(self):
        """ Docplex tsp test """
        # Generating a graph of 3 nodes
        n = 3
        ins = tsp.random_tsp(n)
        graph = nx.Graph()
        graph.add_nodes_from(np.arange(0, n, 1))
        num_node = ins.dim

        # Create an Ising Hamiltonian with docplex.
        mdl = Model(name='tsp')
        x = {(i, p): mdl.binary_var(name='x_{0}_{1}'.format(i, p))
             for i in range(num_node) for p in range(num_node)}
        tsp_func = mdl.sum(ins.w[i, j] * x[(i, p)] * x[(j, (p + 1) % num_node)]
                           for i in range(num_node) for j in range(num_node)
                           for p in range(num_node))
        mdl.minimize(tsp_func)
        for i in range(num_node):
            mdl.add_constraint(
                mdl.sum(x[(i, p)] for p in range(num_node)) == 1)
        for j in range(num_node):
            mdl.add_constraint(
                mdl.sum(x[(i, j)] for i in range(num_node)) == 1)
        qubit_op, offset = docplex.get_operator(mdl)

        e_e = NumPyMinimumEigensolver(qubit_op)
        result = e_e.run()

        ee_expected = NumPyMinimumEigensolver(QUBIT_OP_TSP)
        expected_result = ee_expected.run()

        # Compare objective
        self.assertAlmostEqual(result.eigenvalue.real + offset,
                               expected_result.eigenvalue.real + OFFSET_TSP)
コード例 #2
0
 def setUp(self):
     super().setUp()
     self.seed = 80598
     aqua_globals.random_seed = self.seed
     self.num_nodes = 3
     self.ins = tsp.random_tsp(self.num_nodes)
     self.qubit_op, self.offset = tsp.get_operator(self.ins)
コード例 #3
0
    nx.draw_networkx(G,
                     node_color=colors,
                     node_size=600,
                     alpha=.8,
                     ax=default_axes,
                     pos=pos)
    edge_labels = nx.get_edge_attributes(G, 'weight')
    nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)


exact = MinimumEigenOptimizer(NumPyMinimumEigensolver())
# setup aqua logging
# Generating a graph of 3 nodes
n = 3
num_qubits = n**2
ins = tsp.random_tsp(n, seed=123)
data = tsp.TspData()
print(data)
# Draw the graph
G = nx.Graph()
G.add_nodes_from(np.arange(0, ins.dim, 1))
colors = ['r' for node in G.nodes()]

for i in range(0, ins.dim):
    for j in range(i + 1, ins.dim):
        G.add_edge(i, j, weight=ins.w[i, j])

pos = {k: v for k, v in enumerate(ins.coord)}

draw_graph(G, colors, pos)