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.

139 lines
4.3 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
  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. __QUBO__ = 1
  13. __ISING__ = 2
  14. def main():
  15. mode = __get_mode()
  16. ising_qubo_collection = input("ising/qubo collection: ")
  17. model_type = __get_model_type()
  18. result_collection = input("result collection: ")
  19. if mode == "SIMAN":
  20. __run_siman(ising_qubo_collection, model_type, result_collection)
  21. elif mode == "QPU":
  22. __run_qpu(ising_qubo_collection, model_type, result_collection)
  23. def __get_mode():
  24. print("choose mode:")
  25. print("(1) simulated annealing")
  26. print("(2) qpu")
  27. mode = int(input())
  28. if mode == 1:
  29. return "SIMAN"
  30. elif mode == 2:
  31. return "QPU"
  32. def __get_model_type():
  33. print("model types:")
  34. print("(q) qubo")
  35. print("(i) ising")
  36. model_type = input("choose: ")
  37. if model_type == "q":
  38. return __QUBO__
  39. if model_type == "i":
  40. return __ISING__
  41. def __run_siman(ising_qubo_collection, model_type, result_collection):
  42. db = script.connect_to_instance_pool()
  43. target_graph = dnx.chimera_graph(16, 16, 4)
  44. target_graph_id = queries.get_id_of_solver_graph(db["solver_graphs"],
  45. nx.node_link_data(target_graph))
  46. solver_input_query = __get_solver_input_query(db,
  47. target_graph_id,
  48. ising_qubo_collection)
  49. base_sampler = SimulatedAnnealingSampler()
  50. chimera_sampler = dimod.StructureComposite(base_sampler,
  51. target_graph.nodes(),
  52. target_graph.edges())
  53. __run_on_scope(solver_input_query, db[result_collection], chimera_sampler, model_type)
  54. def __run_qpu(ising_qubo_collection, model_type, result_collection):
  55. db = script.connect_to_instance_pool()
  56. base_solver = DWaveSampler()
  57. solver_graph_id = __get_solver_graph_id(db, base_solver.solver)
  58. solver_input_query = __get_solver_input_query(db,
  59. solver_graph_id,
  60. ising_qubo_collection)
  61. solver_args = {}
  62. solver_args["annealing_time"] = int(input("annealing time (in us): "))
  63. solver_args["num_reads"] = int(input("number of reads per sample: "))
  64. __run_on_scope(solver_input_query,
  65. db[result_collection],
  66. base_solver,
  67. model_type,
  68. solver_args)
  69. def __get_solver_graph_id(db, solver):
  70. solver_graph = graph.create_qpu_solver_nxgraph(solver)
  71. return queries.get_id_of_solver_graph(db["solver_graphs"],
  72. nx.node_link_data(solver_graph))
  73. def __get_solver_input_query(db, solver_graph_id, ising_qubo_collection):
  74. scope = input("scope: ")
  75. solver_input_query = queries.WMIS_solver_input_scope_query(db, ising_qubo_collection)
  76. solver_input_query.query(scope, solver_graph_id)
  77. return solver_input_query
  78. def __run_on_scope(solver_input_query,
  79. result_collection,
  80. base_solver,
  81. model_type,
  82. solver_args={}):
  83. run = int(input("save as run (numbered): "))
  84. for solver_input in tqdm(solver_input_query):
  85. embedding = solver_input["embeddings"][0]
  86. qubo = solver_input["qubo"]
  87. solver = FixedEmbeddingComposite(base_solver, embedding)
  88. if model_type == __QUBO__:
  89. res = solver.sample_qubo(qubo, **solver_args)
  90. elif model_type == __ISING__:
  91. h, J = graph.split_ising(qubo)
  92. res = solver.sample_ising(h, J, **solver_args)
  93. script.save_sample_set(result_collection,
  94. res,
  95. solver_input,
  96. emb_list_index = 0,
  97. run = run)
  98. if __name__ == "__main__":
  99. main()