# How to solve TSP problem with more than 3 nodes in the tutorial of Max-Cut and Traveling Salesman Problem?

I had to try the example of qiskit’s Traveling Salesman Problem with 3 nodes and executing it at IBM backend called simulator_statevector.Can execute and get the result normally.

But when trying to solve the TSP problem with more than 3 nodes,I changed n = 3 to n = 4.

# Generating a graph of 3 nodes
n = 4
num_qubits = n ** 2
ins = tsp.random_tsp(n, seed=123)
print('distance\n', ins.w)

# Draw the graph
G = nx.Graph()
colors = ['r' for node in G.nodes()]

for i in range(0, ins.dim):
for j in range(i+1, ins.dim):

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

draw_graph(G, colors, pos)


And I changed backend from Aer.get_backend ('statevector_simulator') running on my device to provider.backend.simulator_statevector running on the IBM backend.

aqua_globals.random_seed = np.random.default_rng(123)
seed = 10598
backend = provider.backend.simulator_statevector
#backend = Aer.get_backend('statevector_simulator')

quantum_instance = QuantumInstance(backend, seed_simulator=seed, seed_transpiler=seed)


But the result that comes out with an error.

energy: -1303102.65625
time: 5626.549758911133
feasible: False
solution: [1, 0, 2, []]
solution objective: []
Traceback (most recent call last):
File "<ipython-input-10-bc5619b5292f>", line 14, in <module>
draw_tsp_solution(G, z, colors, pos)
File "<ipython-input-4-999185567031>", line 29, in draw_tsp_solution
File "/opt/conda/lib/python3.8/site-packages/networkx/classes/coreviews.py", line 51, in __getitem__
return self._atlas[key]
TypeError: unhashable type: 'list'

Use %tb to get the full traceback.


• Hello! This error comes from the fact that you don't reach a feasible solution, so the function isn't able to draw this non-feasible solution you have. In order to reach a feasible solution, may I suggest maybe playing with the shots of the simulator by increasing it so you may have a better result? Or increasing the size of the Ansatz, so you have more parameters and will be able to reach more potential solutions? But not too much, otherwise it will be too complex to solve.
– Lena
Apr 6 at 8:36

spsa = SPSA(maxiter=300)