新手教程4-云平台和真机使用用例#

用例描述#

通过旅行商问题(TravelingSalesmanProblem,TSP)展示从建模、提交Qubo矩阵到云平台、从云平台获取计算结果的全流程

代码建模并生成qubo矩阵#

# Import distance matrix
w = np.array([[0, 1, 2],
              [1, 0, 0],
              [2, 0, 0]])

# Get the number of nodes
n = w.shape[0]

# Create qubo variable matrix
x = kw.qubo.ndarray((n, n), "x", kw.qubo.Binary)

# Get sets of edge and non-edge pairs
edges = [(u, v) for u in range(n) for v in range(n) if w[u, v] != 0]
no_edges = [(u, v) for u in range(n) for v in range(n) if w[u, v] == 0]

def is_edge_used(x, u, v):
    return kw.qubo.quicksum([x[u, j] * x[v, j + 1] for j in range(-1, n - 1)])

qubo_model = kw.qubo.QuboModel()
# TSP path cost
qubo_model.set_objective(kw.qubo.quicksum([w[u, v] * is_edge_used(x, u, v) for u, v in edges]))

# Node constraint: Each node must belong to exactly one position
qubo_model.add_constraint(x.sum(axis=0) == 1, "sequence_cons", penalty=5)

# Position constraint: Each position can have only one node
qubo_model.add_constraint(x.sum(axis=1) == 1, "node_cons", penalty=5)

# Edge constraint: Pairs without edges cannot appear in the path
qubo_model.add_constraint(kw.qubo.quicksum([is_edge_used(x, u, v) for u, v in no_edges]),
    "connect_cons", penalty=20)

qubo_mat = qubo_model.get_qubo_matrix()
pd.DataFrame(qubo_mat).to_csv("tsp.csv", index=False, header=False)

登录云平台上传矩阵#

  1. 登录光量子云计算平台后进入控制台,选择真机后点击新建任务

    ../_images/new_task.png
  2. 进入任务配置页面后,填写任务名称、上传矩阵,确认后点击下一步

    ../_images/new_task1.png
  3. 进入确认配置页面,确认任务和真机信息后点击确定按钮

    ../_images/new_task2.png
  4. 进入提交任务页面,显示提交成功

    ../_images/new_task3.png
  5. 返回控制台,任务正在校验中

    ../_images/new_task4.jpg
  6. 校验成功后任务进入排队中状态

    ../_images/new_task5.jpg
  7. 任务完成后点击详情进入结果详情页面

    ../_images/new_task6.png
  8. 查看结果详情(qubo解向量、qubo value演化曲线、任务执行时间等)

    ../_images/new_task7.png