forked from connorbybee/hoim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
203 lines (173 loc) · 7.17 KB
/
Copy pathmodels.py
File metadata and controls
203 lines (173 loc) · 7.17 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import dimod
import dwavebinarycsp
import dwavebinarycsp as dbc
import numpy
import numpy as np
from jax import numpy as jnp, jit, grad
from energy import config_energy, ising_energy, kuramoto_config_energy
from utils import quantize_binary
from csp import csp_to_cup
class Model:
def check(self, state):
pass
def check_states(sample_states: numpy.ndarray,
model: Model):
# Samples: time x variable
valid = [model.check(s) for s in sample_states]
return valid
class HuboModel:
def __init__(self,
csp: dwavebinarycsp.ConstraintSatisfactionProblem,
unsat=False,
holomorphic=True,
kuramoto=False,
):
self.csp = csp
self.kuramoto = kuramoto
if unsat:
self.cup = csp_to_cup(csp)
self.configs = [np.array([np.array(c) for c in const.configurations])
for const in self.cup.constraints]
else:
self.configs = [np.array([np.array(c) for c in const.configurations])
for const in csp.constraints]
self.variables = list(csp.variables)
self.var_order = list(csp.variables)
self.num_z = len(csp.variables)
self.const_var_inds = [np.array([self.var_order.index(v) for v in const.variables], dtype=int) for const in csp.constraints]
self.description = {
'csp_size': len(csp.variables),
'model_size': len(csp.variables),
'num_constraints': len(self.csp.constraints),
'kuramoto': kuramoto,
'unsat': unsat
}
self.var_inds = np.array(list(range(len(self.variables))), dtype=int)
self.holomorphic = holomorphic
self.ind_mats = {}
self.config_mats = {}
self.num_configs = {}
self.const_inds = {}
for c, (var_inds, config) in enumerate(zip(self.const_var_inds, self.configs)):
k = len(var_inds)
l = len(config)
if k in self.ind_mats:
if l in self.ind_mats[k]:
self.num_configs[k][l] += 1
self.ind_mats[k][l] = jnp.concatenate([self.ind_mats[k][l], jnp.tile(var_inds, (len(config), 1))[None, :, :]])
self.config_mats[k][l] = jnp.concatenate([self.config_mats[k][l], config[None, :, :]])
else:
self.num_configs[k][l] = 1
self.ind_mats[k][l] = jnp.tile(var_inds, (len(config), 1))[None, :, :]
self.config_mats[k][l] = config[None, :, :]
else:
self.num_configs[k] = {}
self.ind_mats[k] = {}
self.config_mats[k] = {}
self.num_configs[k][l] = 1
self.ind_mats[k][l] = jnp.tile(var_inds, (len(config), 1))[None, :, :]
self.config_mats[k][l] = config[None, :, :]
if kuramoto:
e_func = kuramoto_config_energy
self.holomorphic = False
else:
e_func = config_energy
if unsat:
# @jit
def const_energy(z):
c = jnp.concatenate([-e_func(z.take(self.ind_mats[k][l]), self.config_mats[k][l]).sum(-1)
for k in self.ind_mats for l in self.ind_mats[k]])
return c
else:
# @jit
def const_energy(z):
c = jnp.concatenate([1 + e_func(z.take(self.ind_mats[k][l]), self.config_mats[k][l]).sum(-1)
for k in self.ind_mats for l in self.ind_mats[k]])
return c
# @jit
def energy(z):
e = 0
c = const_energy(z)
e = c.sum()
return e
# @jit
def coupling_func(z):
dz = -grad(energy, holomorphic=self.holomorphic)(z)#.conj()
return dz
# @jit
def venergy(z):
e = const_energy(z).sum()
return e
self.energy = venergy
self.const_energy = const_energy
self.coupling_func = coupling_func
def check(self, state):
if self.kuramoto:
state = jnp.exp(1j * state)
x = quantize_binary(state)[self.var_inds]
x = {v: int(s) for v, s in zip(self.variables, x)}
return self.csp.check(x)
class QuadraticModel(Model):
def __init__(self,
csp: dwavebinarycsp.ConstraintSatisfactionProblem,
min_gap: float,
kuramoto=False,
type=''):
self.csp = csp # type: dwavebinarycsp.ConstraintSatisfactionProblem
self.bqm = csp_to_bqm(csp, min_gap) # type: dimod.BinaryQuadraticModel
# print(self.bqm.num_interactions, self.bqm.num_variables)
self.variables = list(self.bqm.variables)
self.b_lqo = bqm_to_numpy(self.bqm, dimod.BINARY)
self.s_lqo = bqm_to_numpy(self.bqm, dimod.SPIN)
self.bqm_ind_map = {v: i for i, v in enumerate(list(self.bqm.variables))}
self.var_inds = np.array([self.bqm_ind_map[v] for v in list(self.csp.variables)])
self.type = type
self.description = {
'csp_size': len(csp.variables),
'model_size': len(self.bqm.variables),
'min_gap': min_gap,
'num_interactions': self.bqm.num_interactions,
'kuramoto': kuramoto,
}
self.kuramoto = kuramoto
if kuramoto:
def e(z):
return kuramoto_ising_energy(z, *self.s_lqo)
def coupling_func(z):
# Compute energy for csp
return kuramoto_ising_grad(z, *self.s_lqo)
else:
def e(z):
return ising_energy(z, *self.s_lqo)
def coupling_func(z):
# Compute energy for csp
return -grad(e, holomorphic=True)(z).conj()
self.energy = e
self.coupling_func = coupling_func
def check_states(self, states: numpy.ndarray):
return [self.csp.check({
variable: value for variable, value in zip(self.csp.variables, s)
}) for s in states]
def check(self, state):
x = quantize_binary(state)[self.var_inds]
state = {v: int(s) for v, s in zip(self.csp.variables, x)}
try:
check = self.csp.check(state)
except Exception as e:
print(e, state, self.variables, self.csp.variables)
return check
def csp_to_bqm(csp, min_gap=2):
return dbc.stitch(csp, min_classical_gap=min_gap, max_graph_size=16)
def bqm_to_numpy(bqm: dimod.BinaryQuadraticModel, vartype: dimod.Vartype):
variables = list(bqm.variables)
var_map = {v: i for i, v in enumerate(variables)}
if vartype == dimod.BINARY:
linear, quadratic, offset = bqm.linear, bqm.quadratic, bqm.offset
elif vartype == dimod.SPIN:
linear, quadratic, offset = bqm.spin.linear, bqm.spin.quadratic, bqm.spin.offset
linear_n = np.array([linear[v] for v in variables])
quadratic_n = np.zeros(2 * [len(variables)])
for (v1, v2), val in quadratic.items():
quadratic_n[var_map[v1], var_map[v2]] = val
quadratic_n = 0.5 * (quadratic_n + quadratic_n.T)
return linear_n, quadratic_n, offset