Beginner’s Tutorial 4 - Cloud Platform and Real Machine Use Cases#
Use case description#
The whole process from modeling, submitting Qubo matrix to the cloud platform, and obtaining calculation results from the cloud platform is demonstrated through the Traveling Salesman Problem (TSP).
Code modeling and generating qubo matrix#
# 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)
Log in to the cloud platform to upload the matrix#
After logging into the Quantum Cloud Computing Platform, enter the console, select the real machine and click Create Task
After entering the task configuration page, fill in the task name, upload the matrix, and click Next after confirmation.
Enter the Confirm Configuration page, confirm the task and real machine information, and click the OK button
Enter the task submission page, and it will show that the submission is successful.
Return to the console, the task is being verified
After verification is successful, the task enters the queued state
After the task is completed, click Details to enter the result details page
View result details (qubo solution vector, qubo value evolution curve, task execution time, etc.)