You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

566 lines
15 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. from .kSAT import kSAT
  2. import bson
  3. import dimod
  4. class Instance_scope_query:
  5. def __init__(self, database):
  6. self.__database = database
  7. self.__query = None
  8. self.__instance_ids = []
  9. self.__instance_id_iterator = None;
  10. def query(self, scope):
  11. self.__query = self.__database["experiment_scopes"].aggregate([
  12. {
  13. "$match": {"_id": scope}
  14. },
  15. {
  16. "$unwind": "$instances"
  17. },
  18. {
  19. "$lookup":
  20. {
  21. "from": "instances",
  22. "localField": "instances",
  23. "foreignField": "_id",
  24. "as": "instance"
  25. }
  26. },
  27. {
  28. "$unwind": "$instance"
  29. },
  30. {
  31. "$project": {"instance_id": "$instance._id"}
  32. }
  33. ])
  34. self.__instance_ids = []
  35. for doc in self.__query:
  36. self.__instance_ids.append(doc["instance_id"])
  37. self.__instance_id_iterator = iter(self.__instance_ids)
  38. def __iter__(self):
  39. return self
  40. def __next__(self):
  41. instance_id = self.__instance_id_iterator.__next__()
  42. document = self.__database["instances"].find_one({"_id": instance_id})
  43. return self.__document_to_sat(document)
  44. def __document_to_sat(self, document):
  45. sat = kSAT()
  46. for clause in document["clauses"]:
  47. sat.addClause(clause);
  48. return sat, document["_id"]
  49. class Qubo_ising_scope_query_raw:
  50. def __init__(self, database, collection):
  51. self.__database = database
  52. self.__collection = collection
  53. self.__query = None
  54. self.__qubo_ids = []
  55. self.__qubo_id_iterator = None
  56. def query(self, scope):
  57. self.__query = self.__database["experiment_scopes"].aggregate([
  58. {
  59. "$match": {"_id": scope}
  60. },
  61. {
  62. "$unwind": "$instances"
  63. },
  64. {
  65. "$lookup":
  66. {
  67. "from": self.__collection,
  68. "localField": "instances",
  69. "foreignField": "instance",
  70. "as": "qubo"
  71. }
  72. },
  73. {
  74. "$unwind": "$qubo"
  75. },
  76. {
  77. "$project": {"qubo_id": "$qubo._id"}
  78. }
  79. ])
  80. self.__qubo_ids = []
  81. for doc in self.__query:
  82. self.__qubo_ids.append(doc["qubo_id"])
  83. self.__qubo_id_iterator = iter(self.__qubo_ids)
  84. def __len__(self):
  85. return self.query.count_documents({})
  86. def __iter__(self):
  87. return self
  88. def __next__(self):
  89. qubo_filter = {"_id": self.__qubo_id_iterator.__next__()}
  90. return self.__database[self.__collection].find_one(qubo_filter)
  91. class WMIS_scope_query_raw:
  92. def __init__(self, database):
  93. self.__database = database
  94. self.__query = None
  95. self.__qubo_ids = []
  96. self.__qubo_id_iterator = None
  97. def query(self, scope):
  98. self.__query = self.__database["experiment_scopes"].aggregate([
  99. {
  100. "$match": {"_id": scope}
  101. },
  102. {
  103. "$unwind": "$instances"
  104. },
  105. {
  106. "$lookup":
  107. {
  108. "from": "wmis_qubos",
  109. "localField": "instances",
  110. "foreignField": "instance",
  111. "as": "qubo"
  112. }
  113. },
  114. {
  115. "$unwind": "$qubo"
  116. },
  117. {
  118. "$project": {"qubo_id": "$qubo._id"}
  119. }
  120. ])
  121. self.__qubo_ids = []
  122. for doc in self.__query:
  123. self.__qubo_ids.append(doc["qubo_id"])
  124. self.__qubo_id_iterator = iter(self.__qubo_ids)
  125. def __len__(self):
  126. return self.query.count_documents({})
  127. def __iter__(self):
  128. return self
  129. def __next__(self):
  130. qubo_filter = {"_id": self.__qubo_id_iterator.__next__()}
  131. return self.__database["wmis_qubos"].find_one(qubo_filter)
  132. class WMIS_scope_query (WMIS_scope_query_raw):
  133. def __next__(self):
  134. doc = super(WMIS_scope_query, self).__next__()
  135. return read_raw_qubo(doc["qubo"]), doc["_id"]
  136. class Ising_scope_query (Qubo_ising_scope_query_raw):
  137. def __next__(self):
  138. doc = super(Ising_scope_query, self).__next__()
  139. return read_raw_ising(doc["qubo"]), doc["_id"]
  140. class Qubo_scope_query (Qubo_ising_scope_query_raw):
  141. def __next__(self):
  142. doc = super(Qubo_scope_query, self).__next__()
  143. return read_raw_qubo(doc["qubo"]), doc["_id"]
  144. class WMIS_solver_input_scope_query_raw:
  145. def __init__(self, database, ising_qubo_collection):
  146. self.__database = database
  147. self.__ising_qubo_collection = ising_qubo_collection
  148. self.__query = None
  149. self.__ids = []
  150. self.__id_iterator = None;
  151. def query(self, scope, solver_graph_id):
  152. self.__query = self.__database["experiment_scopes"].aggregate([
  153. {
  154. "$match": {"_id": scope}
  155. },
  156. {
  157. "$unwind": "$instances"
  158. },
  159. {
  160. "$project": {"instance_id": "$instances"}
  161. },
  162. {
  163. "$lookup":
  164. {
  165. "from": self.__ising_qubo_collection,
  166. "localField": "instance_id",
  167. "foreignField": "instance",
  168. "as": "wmis_qubo"
  169. }
  170. },
  171. {
  172. "$unwind": "$wmis_qubo"
  173. },
  174. {
  175. "$project":
  176. {
  177. "instance_id": True,
  178. "wmis_qubo_id": "$wmis_qubo._id"
  179. }
  180. },
  181. {
  182. "$lookup":
  183. {
  184. "from": "embeddings",
  185. "let":
  186. {
  187. "qubo_id": "$wmis_qubo_id",
  188. "solver_graph_id": bson.ObjectId(solver_graph_id)
  189. },
  190. "pipeline":
  191. [
  192. {
  193. "$project":
  194. {
  195. "qubo": True,
  196. "solver_graph": True,
  197. }
  198. },
  199. {
  200. "$match":
  201. {
  202. "$expr":
  203. {
  204. "$and":
  205. [
  206. {"$eq": ["$qubo", "$$qubo_id"]},
  207. {"$eq": ["$solver_graph", "$$solver_graph_id"]}
  208. ]
  209. }
  210. }
  211. },
  212. {
  213. "$project":
  214. {
  215. "_id": True
  216. }
  217. }
  218. ],
  219. "as": "embeddings_id"
  220. }
  221. },
  222. {
  223. "$unwind": "$embeddings_id"
  224. },
  225. {
  226. "$project":
  227. {
  228. "instance_id": True,
  229. "wmis_qubo_id": True,
  230. "embeddings_id": "$embeddings_id._id"
  231. }
  232. }
  233. ])
  234. self.__ids = []
  235. for doc in self.__query:
  236. self.__ids.append({
  237. "instance_id": doc["instance_id"],
  238. "wmis_qubo_id": doc["wmis_qubo_id"],
  239. "embeddings_id": doc["embeddings_id"]
  240. })
  241. self.__id_iterator = iter(self.__ids)
  242. def __iter__(self):
  243. return self
  244. def __next__(self):
  245. ids = self.__id_iterator.__next__()
  246. doc = {}
  247. doc["instance_id"] = ids["instance_id"]
  248. qubo_filter = {"_id": ids["wmis_qubo_id"]}
  249. ising_qubo_collection = self.__database[self.__ising_qubo_collection]
  250. doc["wmis_qubo"] = ising_qubo_collection.find_one(qubo_filter)
  251. embeddings_filter = {"_id": ids["embeddings_id"]}
  252. doc["embeddings"] = self.__database["embeddings"].find_one(embeddings_filter)
  253. return doc
  254. class WMIS_solver_input_scope_query (WMIS_solver_input_scope_query_raw):
  255. def __next__(self):
  256. doc = super(WMIS_solver_input_scope_query, self).__next__()
  257. data = {}
  258. data["instance_id"] = doc["instance_id"]
  259. data["qubo_id"] = doc["wmis_qubo"]["_id"]
  260. data["qubo"] = read_raw_qubo(doc["wmis_qubo"]["qubo"])
  261. data["embeddings_id"] = doc["embeddings"]["_id"]
  262. data["embeddings"] = []
  263. for raw_emb in doc["embeddings"]["embeddings"]:
  264. data["embeddings"].append(read_raw_embedding(raw_emb))
  265. return data
  266. class Ising_solver_input_scope_query (WMIS_solver_input_scope_query_raw):
  267. def __next__(self):
  268. doc = super(Ising_solver_input_scope_query, self).__next__()
  269. data = {}
  270. data["instance_id"] = doc["instance_id"]
  271. data["qubo_id"] = doc["wmis_qubo"]["_id"]
  272. data["qubo"] = read_raw_ising(doc["wmis_qubo"]["qubo"])
  273. data["embeddings_id"] = doc["embeddings"]["_id"]
  274. data["embeddings"] = []
  275. for raw_emb in doc["embeddings"]["embeddings"]:
  276. data["embeddings"].append(read_raw_ising_embedding(raw_emb))
  277. return data
  278. class WMIS_result_scope_query_raw:
  279. def __init__(self, database):
  280. self.__database = database
  281. self.__query = None
  282. def query(self, scope, collection):
  283. self.__query = self.__database["experiment_scopes"].aggregate([
  284. {
  285. "$match": {"_id": scope}
  286. },
  287. {
  288. "$unwind": "$instances"
  289. },
  290. {
  291. "$project": {"instance_id": "$instances"}
  292. },
  293. {
  294. "$lookup":
  295. {
  296. "from": collection,
  297. "localField": "instance_id",
  298. "foreignField": "instance",
  299. "as": "result"
  300. }
  301. },
  302. {
  303. "$unwind": "$result"
  304. },
  305. {
  306. "$replaceRoot": {"newRoot": "$result"}
  307. }
  308. ])
  309. def __iter__(self):
  310. return self
  311. def __next__(self):
  312. return self.__query.next()
  313. class Minisat_run_scope_query_raw:
  314. def __init__(self, database):
  315. self.__database = database
  316. self.__query = None
  317. def query(self, scope, collection):
  318. self.__query = self.__database["experiment_scopes"].aggregate([
  319. {
  320. "$match": {"_id": scope}
  321. },
  322. {
  323. "$unwind": "$instances"
  324. },
  325. {
  326. "$project": {"instance_id": "$instances"}
  327. },
  328. {
  329. "$lookup":
  330. {
  331. "from": collection,
  332. "localField": "instance_id",
  333. "foreignField": "instance",
  334. "as": "run"
  335. }
  336. },
  337. {
  338. "$unwind": "$run"
  339. },
  340. {
  341. "$replaceRoot": {"newRoot": "$run"}
  342. }
  343. ])
  344. def __iter__(self):
  345. return self
  346. def __next__(self):
  347. return self.__query.next()
  348. def load_embedding(collection, qubo_id, solver_graph_id):
  349. doc = collection.find_one(
  350. {
  351. "qubo": bson.ObjectId(qubo_id),
  352. "solver_graph": bson.ObjectId(solver_graph_id)
  353. }
  354. )
  355. if doc == None:
  356. return None
  357. if doc["embeddings"] == None:
  358. return None
  359. if len(doc["embeddings"]) == 0:
  360. return None
  361. return read_raw_embedding(doc["embeddings"][0])
  362. def get_id_of_solver_graph(collection, nx_graph_data):
  363. doc = collection.find_one({"data": nx_graph_data})
  364. if doc == None:
  365. return None
  366. return doc["_id"]
  367. def load_WMIS_qubo_of_instance(collection, instance_id):
  368. doc = load_WMIS_qubo_of_instance_raw(collection, instance_id)
  369. if doc == None:
  370. return None
  371. return read_raw_qubo(doc["qubo"]), doc["_id"]
  372. def get_WMIS_qubo_id_of_instance(collection, instance_id):
  373. doc = load_WMIS_qubo_of_instance_raw(collection, instance_id)
  374. if doc == None:
  375. return None
  376. return doc["_id"]
  377. def load_WMIS_qubo_of_instance_raw(collection, instance_id):
  378. return collection.find_one({"instance": bson.ObjectId(instance_id)})
  379. def read_raw_qubo(raw_qubo):
  380. qubo = {}
  381. for entry in raw_qubo:
  382. energy = entry[1]
  383. raw_coupler = entry[0]
  384. node1 = tuple(raw_coupler[0])
  385. node2 = tuple(raw_coupler[1])
  386. qubo[(node1, node2)] = energy
  387. return qubo
  388. def read_raw_ising(raw_ising):
  389. ising = {}
  390. for entry in raw_ising:
  391. energy = entry[1]
  392. raw_coupler = entry[0]
  393. node1 = raw_coupler[0]
  394. node2 = raw_coupler[1]
  395. ising[(node1, node2)] = energy
  396. return ising
  397. def read_raw_embedding(raw_embedding):
  398. emb = {}
  399. if "embedding" in raw_embedding:
  400. raw_embedding = raw_embedding["embedding"]
  401. for entry in raw_embedding:
  402. emb[tuple(entry[0])] = entry[1]
  403. return emb
  404. def read_raw_ising_embedding(raw_embedding):
  405. emb = {}
  406. if "embedding" in raw_embedding:
  407. raw_embedding = raw_embedding["embedding"]
  408. for entry in raw_embedding:
  409. emb[entry[0]] = entry[1]
  410. return emb
  411. def read_raw_wmis_sample_set(raw_sample_set):
  412. sample_set_data = raw_sample_set.copy()
  413. sample_set_data["variable_labels"] = []
  414. for label in raw_sample_set["variable_labels"]:
  415. sample_set_data["variable_labels"].append(tuple(label))
  416. return dimod.SampleSet.from_serializable(sample_set_data)
  417. def read_raw_primitive_ising_sample_set(raw_sample_set):
  418. sample_set_data = raw_sample_set.copy()
  419. sample_set_data["variable_labels"] = []
  420. for label in raw_sample_set["variable_labels"]:
  421. sample_set_data["variable_labels"].append("".join(label))
  422. return dimod.SampleSet.from_serializable(sample_set_data)
  423. def get_instance_by_id(collection, id):
  424. doc = collection.find_one({"_id": bson.ObjectId(id)})
  425. sat = kSAT()
  426. for clause in doc["clauses"]:
  427. sat.addClause(clause);
  428. return sat
  429. def extract_primitive_ising_model(sample):
  430. variable_bindings = {}
  431. for node, energy in sample.items():
  432. if node[0] == "x":
  433. var = int(node[1:])
  434. variable_bindings[var] = True if energy > 0 else False
  435. model = [True for i in range(len(variable_bindings))]
  436. for var, binding in variable_bindings.items():
  437. model[var - 1] = binding
  438. return model