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
/
Copy pathbatch.projector.py
150 lines (136 loc) · 3.71 KB
/
batch.projector.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
138
139
140
141
142
143
144
145
146
147
148
149
# Update: state vector only includes states with proper Hamming weight
import numpy as np
import numpy.linalg as la
import scipy.linalg as sla
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
np.random.seed(123698745)
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):
rank = np.zeros(2**n, dtype=int)
knar = []
now = 0
for z in range(2**n):
if Hamming(z) == k:
rank[z] = now
knar.append(z)
now += 1
cnk = int(comb(n, k))
psi = np.array([cnk**(-0.5) for i in range(cnk)], dtype=complex)
H_B = np.identity(cnk) - np.outer(psi, psi)
H_P = np.diag(np.array([(sum([(expr_g(G[i][j]))*expr_z(Bit(knar[ii], i))*expr_z(Bit(knar[ii], j)) for i in range(n) for j in range(i)])) for ii in range(cnk)], dtype=complex))
for t in np.arange(0.0, T, dt):
H = (1.0 - t/T) * H_B + t/T * H_P
# psi = np.dot(sla.expm(-dt * H), psi)
psi += (-1j) * np.dot(H, psi) * dt
psi /= la.norm(psi)
prob = 0.0
for z in arr:
prob += (abs(psi[rank[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_projector/' + PROB_TYPE + '_' + str(n) + '.txt', 'w')
print('mean: ' + str(np.mean(np.array(ansall))))
print('median: ' + str(np.median(np.array(ansall))))
print(ansall)
print(' ')
for k,v in ans.iteritems():
print(str(k) + ': ' + str(v))