【问题标题】:How to solve TSP problem with more than 3 nodes in the tutorial of Max-Cut and Traveling Salesman Problem Qiskit 0.24.0?Max-Cut and Traveling Salesman Problem Qiskit 0.24.0教程中如何解决超过3个节点的TSP问题?
【发布时间】:2021-06-29 09:52:25
【问题描述】:

我不得不尝试qiskit的Traveling Salesman Problem的例子,有3个节点,并在IBM后端调用simulator_statevector执行。可以正常执行并得到结果。

但是当尝试解决超过 3 个节点的 TSP 问题时,我将 n = 3 更改为 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()
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)

我将后端从在我的设备上运行的 Aer.get_backend ('statevector_simulator') 更改为在 IBM 后端运行的 provider.backend.simulator_statevector。

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)

但是出来的结果有错误。

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
    G2.add_edge(order[i], order[j], weight=G[order[i]][order[j]]['weight'])
  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.

我应该如何解决它?请给我一些建议。

【问题讨论】:

  • link Max-Cut 和旅行商问题
  • 您的解决方案不可行 -- 如果您查看输出,解决方案 feasible 是 False。

标签: python quantum-computing qiskit


【解决方案1】:

我找到了答案,我的方法是将 Ansat 的代表次数从 5 增加到 7。

从解决 TSP 4 节点问题

spsa = SPSA(maxiter=300)
ry = TwoLocal(qubitOp.num_qubits, 'ry', 'cz', reps=7, entanglement='linear')
vqe = VQE(qubitOp, ry, spsa, quantum_instance=quantum_instance)

result = vqe.run(quantum_instance)

【讨论】:

  • 这不是上面的错误 BTW 的原因!
猜你喜欢
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2020-06-28
  • 1970-01-01
  • 2010-12-06
相关资源
最近更新 更多