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.

288 lines
9.9 KiB

5 years ago
  1. from . import script
  2. from . import queries
  3. import dimod
  4. import random
  5. import numpy as np
  6. from bson.objectid import ObjectId
  7. import pandas as pd
  8. from tqdm import tqdm
  9. def extract_wmis_qpu_results():
  10. edb = script.connect_to_experimetns_db()
  11. edb_cursor = edb.cursor()
  12. idb = script.connect_to_instance_pool()
  13. scope = input("scope: ")
  14. result_collection = input("result collection: ")
  15. table_name = input("table name: ")
  16. q = queries.WMIS_result_scope_query_raw(idb)
  17. q.query(scope, result_collection)
  18. insert_row = ("INSERT INTO {} "
  19. "(result_id, "
  20. " run, "
  21. " instance_id, "
  22. " number_of_found_assignments, "
  23. " chain_break_fraction, "
  24. " num_occurrences, "
  25. " energy, "
  26. " satisfiable, "
  27. " anneal_time, "
  28. " energy_reach, "
  29. " sample_size) "
  30. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ").format(table_name)
  31. run = int(input("run: "))
  32. for result in tqdm(q):
  33. if result["run"] == run:
  34. sample_set = queries.read_raw_wmis_sample_set(result["data"])
  35. data = script.analyze_wmis_sample(sample_set.first)
  36. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  37. model = script.majority_vote_sample(sample_set.first.sample)
  38. isSatisfiable = sat.checkAssignment(model)
  39. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  40. energy_reach = abs(sample_set.record["energy"].max() -
  41. sample_set.record["energy"].min())
  42. sample_size = np.sum(sample_set.record["num_occurrences"])
  43. edb_cursor.execute(insert_row, (str(result["_id"]),
  44. int(result["run"]),
  45. str(result["instance"]),
  46. int(data["number_of_assignments"]),
  47. float(data["chain_break_fraction"]),
  48. int(data["num_occurrences"]),
  49. int(data["energy"]),
  50. isSatisfiable,
  51. int(anneal_time),
  52. int(energy_reach),
  53. int(sample_size)))
  54. edb.commit()
  55. edb_cursor.close()
  56. edb.close()
  57. def extract_wmis_2_qpu_results():
  58. edb = script.connect_to_experimetns_db()
  59. edb_cursor = edb.cursor()
  60. idb = script.connect_to_instance_pool()
  61. scope = input("scope: ")
  62. result_collection = input("result collection: ")
  63. table_name = input("table name: ")
  64. q = queries.WMIS_result_scope_query_raw(idb)
  65. q.query(scope, result_collection)
  66. insert_row = ("INSERT INTO {} "
  67. "(result_id, "
  68. " run, "
  69. " instance_id, "
  70. " chain_break_fraction, "
  71. " num_occurrences, "
  72. " energy, "
  73. " satisfiable, "
  74. " anneal_time, "
  75. " energy_reach, "
  76. " sample_size) "
  77. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ").format(table_name)
  78. run = int(input("run: "))
  79. for result in tqdm(q):
  80. if result["run"] == run:
  81. sample_set = queries.read_raw_primitive_ising_sample_set(result["data"])
  82. data = script.analyze_wmis_sample(sample_set.first)
  83. sat = queries.get_instance_by_id(idb["instances"], result["instance"])
  84. post_process_results = __evaluate_wmis_2_sample(sat,
  85. sample_set.first.sample)
  86. anneal_time = result["data"]["info"]["timing"]["qpu_anneal_time_per_sample"]
  87. energy_reach = abs(sample_set.record["energy"].max() -
  88. sample_set.record["energy"].min())
  89. sample_size = np.sum(sample_set.record["num_occurrences"])
  90. edb_cursor.execute(insert_row, (str(result["_id"]),
  91. int(result["run"]),
  92. str(result["instance"]),
  93. float(data["chain_break_fraction"]),
  94. int(data["num_occurrences"]),
  95. int(data["energy"]),
  96. bool(post_process_results["satisfiable"]),
  97. int(anneal_time),
  98. int(energy_reach),
  99. int(sample_size)))
  100. edb.commit()
  101. edb_cursor.close()
  102. edb.close()
  103. def __evaluate_wmis_2_sample(sat, sample):
  104. post_process_results = {}
  105. assignments = {}
  106. vars = set()
  107. for node, energy in sample.items():
  108. if node[0] == "x":
  109. lit = int(node[1:])
  110. vars.add(abs(lit))
  111. assignments[lit] = energy
  112. model = [True for i in range(len(vars))]
  113. for var in vars:
  114. if var in assignments:
  115. model[var - 1] = True if assignments[var] == 1 else False
  116. elif -var in assignments:
  117. model[var - 1] = True if assignments[-var] == 0 else False
  118. post_process_results["satisfiable"] = sat.checkAssignment(model)
  119. return post_process_results
  120. def extract_minisat_results():
  121. edb = script.connect_to_experimetns_db()
  122. edb_cursor = edb.cursor();
  123. idb = script.connect_to_instance_pool()
  124. scope = input("scope: ")
  125. table_name = input("table name: ")
  126. runs = queries.Minisat_run_scope_query_raw(idb)
  127. runs.query(scope, "minisat_runs")
  128. insert_row = ("INSERT INTO {} "
  129. "(run_id, "
  130. " instance_id, "
  131. " satisfiable) "
  132. "VALUES (%s, %s, %s) ").format(table_name)
  133. for run in tqdm(runs):
  134. data = script.analyde_minisat_run(run)
  135. edb_cursor.execute(insert_row, (str(run["_id"]),
  136. str(run["instance"]),
  137. int(data["satisfiable"])))
  138. edb.commit()
  139. edb_cursor.close()
  140. edb.close()
  141. def extract_instance_parameters():
  142. edb = script.connect_to_experimetns_db()
  143. edb_cursor = edb.cursor()
  144. idb = script.connect_to_instance_pool()
  145. scope = input("scope: ")
  146. table_name = input("table name: ")
  147. instances = queries.Instance_scope_query(idb)
  148. instances.query(scope)
  149. insert_row = ("INSERT INTO {} "
  150. "(instance_id, "
  151. " number_of_clauses, "
  152. " number_of_variables) "
  153. "VALUES (%s, %s, %s)").format(table_name)
  154. for instance, instance_id in tqdm(instances):
  155. edb_cursor.execute(insert_row, (str(instance_id),
  156. int(instance.getNumberOfClauses()),
  157. int(instance.getNumberOfVariables())))
  158. edb.commit()
  159. edb_cursor.close()
  160. edb.close()
  161. def extract_embedding_data():
  162. edb = script.connect_to_experimetns_db()
  163. edb_cursor = edb.cursor()
  164. idb = script.connect_to_instance_pool()
  165. scope = input("scope: ")
  166. table_name = input("table name: ")
  167. qubo_collection = input("qubo collection: ")
  168. solver_graph_id = ObjectId(input("solver graph id: "))
  169. solver_input = queries.WMIS_solver_input_scope_query(idb, qubo_collection)
  170. solver_input.query(scope, solver_graph_id)
  171. results = pd.DataFrame(columns=["instance_id",
  172. "embedding_list_id",
  173. "emb_list_index",
  174. "num_qubits",
  175. "avgr_chain_length",
  176. "median_chain_length"]);
  177. for soin in solver_input:
  178. emb_index = 0;
  179. for emb in soin["embeddings"]:
  180. chain_lengths = []
  181. qubits = set()
  182. for node, chain in emb.items():
  183. chain_lengths.append(len(chain))
  184. qubits = qubits.union(set(chain))
  185. results = results.append({"instance_id": soin["instance_id"],
  186. "embedding_list_id": soin["embeddings_id"],
  187. "emb_list_index": emb_index,
  188. "num_qubits": len(qubits),
  189. "avgr_chain_length": np.mean(chain_lengths),
  190. "median_chain_length": np.median(chain_lengths)},
  191. ignore_index=True)
  192. insert_row = ('''INSERT INTO {}
  193. (instance_id,
  194. embedding_list_id,
  195. emb_list_index,
  196. num_qubits,
  197. avgr_chain_length,
  198. median_chain_length)
  199. VALUES(%s, %s, %s, %s, %s, %s)''').format(table_name)
  200. for index, row in results.iterrows():
  201. edb_cursor.execute(insert_row, (str(row["instance_id"]),
  202. str(row["embedding_list_id"]),
  203. int(row["emb_list_index"]),
  204. int(row["num_qubits"]),
  205. float(row["avgr_chain_length"]),
  206. float(row["median_chain_length"])))
  207. edb.commit()
  208. edb_cursor.close()
  209. edb.close()