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.

97 lines
2.7 KiB

5 years ago
5 years ago
  1. #!/usr/bin/env python3
  2. import util.script as script
  3. import util.queries as queries
  4. import util.graph as graph
  5. import networkx as nx
  6. import dwave_networkx as dnx
  7. from neal import SimulatedAnnealingSampler
  8. import dimod
  9. from dwave.system.composites import FixedEmbeddingComposite
  10. from dwave.system.samplers import DWaveSampler
  11. from tqdm import tqdm
  12. def main():
  13. mode = __get_mode()
  14. if mode == "SIMAN":
  15. __run_siman()
  16. elif mode == "QPU":
  17. __run_qpu()
  18. def __get_mode():
  19. print("choose mode:")
  20. print("(1) simulated annealing")
  21. print("(2) qpu")
  22. mode = int(input())
  23. if mode == 1:
  24. return "SIMAN"
  25. elif mode == 2:
  26. return "QPU"
  27. def __run_siman():
  28. db = script.connect_to_instance_pool()
  29. target_graph = dnx.chimera_graph(16, 16, 4)
  30. solver_input_query = __get_solver_input_query(db, target_graph)
  31. base_sampler = SimulatedAnnealingSampler()
  32. chimera_sampler = dimod.StructureComposite(base_sampler,
  33. target_graph.nodes(),
  34. target_graph.edges())
  35. __run_on_scope(solver_input_query, db["wmis_siman_results"], base_sampler)
  36. def __run_qpu():
  37. db = script.connect_to_instance_pool()
  38. base_solver = DWaveSampler()
  39. solver_input_query = __get_solver_input_query(db, solver_graph)
  40. solver_args = {}
  41. solver_args["annealing_time"] = int(input("annealing time (in us): "))
  42. __run_on_scope(solver_input_query, db["wmis_qpu_results"], base_solver, solver_args)
  43. def __get_solver_graph_id(db, solver):
  44. solver_graph = graph.create_qpu_solver_nxgraph(solver)
  45. return queries.get_id_of_solver_graph(db["solver_graphs"],
  46. nx.node_link_data(solver_graph))
  47. def __get_solver_input_query(db, solver):
  48. solver_graph_id = __get_solver_graph_id(db, solver)
  49. scope = input("scope: ")
  50. solver_input_query = queries.WMIS_solver_input_scope_query(db)
  51. solver_input_query.query(scope, solver_graph_id)
  52. def __run_on_scope(solver_input_query, result_collection, base_solver, solver_args={}):
  53. run = int(input("save as run (numbered): "))
  54. for solver_input in tqdm(solver_input_query):
  55. embedding = solver_input["embeddings"][0]
  56. qubo = solver_input["qubo"]
  57. solver = FixedEmbeddingComposite(base_solver, embedding)
  58. res = solver.sample_qubo(qubo, **solver_args)
  59. script.save_result(result_collection,
  60. res,
  61. solver_input,
  62. emb_list_index = 0,
  63. run = run)
  64. if __name__ == "__main__":
  65. main()