In Qiskit's tutorial 'Max-Cut and Traveling Salesman Problem', below code is used to create the nodes:

tsp = Tsp.create_random_instance(n, seed=123)

What information is being generated by this function and how to replace this function if I have my own set of data?


1 Answer 1


The method Tsp.create_random_instance() creates a random instance of the TSP problem. Internally, it uses NetworkX's function random_geometric_graph().

To have your own graph, you can rely on NetworkX library as well to create it:

import networkx as nx

graph = nx.Graph()
graph.add_edges_from([[0, 2], [0, 3], [1, 2], [1, 3], [1, 4], [2, 4], [3, 4]])
nx.draw(graph, with_labels = True)

enter image description here

Now, pass the graph to Tsp constructor

tsp_instance = Tsp(graph)

Another method to create your own problem instance is by creating a TSPLIB file then use Tsp.parse_tsplib_format() method.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.