-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtutorial3_max_cut.py
More file actions
executable file
·29 lines (28 loc) · 1.3 KB
/
tutorial3_max_cut.py
File metadata and controls
executable file
·29 lines (28 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
import kaiwu as kw
kw.common.CheckpointManager.save_dir = "/tmp"
kw.license.init(user_id="xxxxxx", sdk_code="xxxxxxxx")
# Import the plotting library
import matplotlib.pyplot as plt
# invert input graph matrix
matrix = -np.array([
[0, 1, 0, 1, 1, 0, 0, 1, 1, 0],
[1, 0, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 0, 1, 1, 0 ,1, 0],
[1, 0, 1, 0, 0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 1, 1, 0, 1, 0]])
worker = kw.classical.SimulatedAnnealingOptimizer(initial_temperature=100,
alpha=0.99,
cutoff_temperature=0.001,
iterations_per_t=10,
size_limit=100)
output = worker.solve(matrix)
opt = kw.sampler.optimal_sampler(matrix, output, 0)
best = opt[0][0]
max_cut = (np.sum(-matrix)-np.dot(-matrix,best).dot(best))/4
print("The obtained max cut is " + str(max_cut) + ".")