This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.2.py
138 lines (126 loc) · 3.56 KB
/
batch.2.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
import numpy.linalg as la
from scipy.misc import comb
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages/')
import progressbar
PROB_TYPE = sys.argv[1]
n = int(sys.argv[2])
SAMPLE = 100
PROB = 0.5
dt = 1.0
def expr_g (gij):
return {
'max_clique': 1-gij,
'max_vertex_indep': gij,
'min_vertex_cover': gij,
'min_unknown': 1-gij
}[PROB_TYPE]
def expr_z (z):
return {
'max_clique': z,
'max_vertex_indep': z,
'min_vertex_cover': 1-z,
'min_unknown': 1-z
}[PROB_TYPE]
# Get Hamming weight of binary number
def Hamming (num):
return bin(num).count('1')
# Get idx-th bit from right
def Bit (num, idx):
return (num >> idx) & 1
# Solve problem classically; return size of answer and array of answers
def classical_solve (n, G):
return {
'max_clique': classical_solve_max(n, G),
'max_vertex_indep': classical_solve_max(n, G),
'min_vertex_cover': classical_solve_min(n, G),
'min_unknown': classical_solve_min(n, G)
}[PROB_TYPE]
def classical_solve_max (n, G):
k = 0
arr = []
for z in range(2**n):
flag = True
for i in range(n):
for j in range(n):
if i != j and expr_g(G[i][j])*expr_z(Bit(z, i))*expr_z(Bit(z, j)):
flag = False
if flag:
if k < Hamming(z):
arr = []
k = Hamming(z)
if k == Hamming(z):
arr.append(z)
return k, arr
def classical_solve_min (n, G):
k = n+1
arr = []
for z in range(2**n):
flag = True
for i in range(n):
for j in range(n):
if i != j and expr_g(G[i][j])*expr_z(Bit(z, i))*expr_z(Bit(z, j)):
flag = False
if flag:
if k > Hamming(z):
arr = []
k = Hamming(z)
if k == Hamming(z):
arr.append(z)
return k, arr
# Verify if given computation time guarantees sufficient probability
def run_single (n, k, G, T, arr):
psi = np.array([comb(n, k)**(-0.5) if Hamming(z)==k else 0 for z in range(2**n)], dtype=complex) # uniform superposition of eligible states
H_B = np.identity(2**n) - np.outer(psi, psi)
H_P = np.diag(np.array([(sum([(expr_g(G[i][j]))*expr_z(Bit(z, i))*expr_z(Bit(z, j)) for i in range(n) for j in range(i)])) for z in range(2**n)], dtype=complex))
for t in np.arange(0.0, T, dt):
H = (1.0 - t/T) * H_B + t/T * H_P
H_ = (1.0 - (t+dt/2)/T) * H_B + (t+dt/2)/T * H_P
H__ = (1.0 - (t+dt)/T) * H_B + (t+dt)/T * H_P
k1 = (-1j) * np.dot(H, psi)
k2 = (-1j) * np.dot(H_, psi+(dt/2)*k1)
k3 = (-1j) * np.dot(H_, psi+(dt/2)*k2)
k4 = (-1j) * np.dot(H__, psi+dt*k3)
psi += (k1 + 2*k2 + 2*k3 + k4) / 6 * dt
prob = 0.0
for z in arr:
prob += (abs(psi[z]))**2
return prob >= PROB
# Run algorithm on a single random graph of size n; return computation time to achieve probability threshold
def run_random (n):
# Generate random graph: each edge exists by probability 1/2
# G: adjacency matrix
G = np.random.randint(2, size=(n, n))
G = G ^ G.T # enforce symmetry and zeros on diagonal
k, arr = classical_solve(n, G)
# binary search computation time
T_min = 0
T_max = 1
while run_single(n, k, G, T_max*dt, arr) == False:
T_max *= 2
while T_max - T_min > 1:
T = int((T_min + T_max) / 2)
if run_single(n, k, G, T*dt, arr):
T_max = T
else:
T_min = T
return k, T_max*dt
ansall = []
ans = {}
for k in range(0, n+1):
ans[k] = []
pbar = progressbar.ProgressBar(widgets=[
progressbar.Percentage(), ' ',
progressbar.Bar(marker='>', fill='-'), ' ',
progressbar.ETA(), ' ',
])
for it in pbar(range(SAMPLE)):
k, T = run_random(n)
ansall.append(T)
ans[k].append(T)
sys.stdout = open('output/' + PROB_TYPE + '_' + str(n) + '.txt', 'w')
print(ansall)
print(' ')
for k,v in ans.iteritems():
print(str(k) + ': ' + str(v))