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.

168 lines
5.0 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
  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_negation():
  24. print("qubo_negation:")
  25. print("(t)rue ")
  26. print("(f)alse")
  27. mode = input()
  28. if mode == "t":
  29. return True
  30. else:
  31. return False
  32. def __get_mode():
  33. print("choose mode:")
  34. print("(1) simulated annealing")
  35. print("(2) qpu")
  36. mode = int(input())
  37. if mode == 1:
  38. return "SIMAN"
  39. elif mode == 2:
  40. return "QPU"
  41. def __get_model_type():
  42. print("model types:")
  43. print("(q) qubo")
  44. print("(i) ising")
  45. model_type = input("choose: ")
  46. if model_type == "q":
  47. return __QUBO__
  48. if model_type == "i":
  49. return __ISING__
  50. def __run_siman(ising_qubo_collection, model_type, result_collection):
  51. db = script.connect_to_instance_pool()
  52. target_graph = dnx.chimera_graph(16, 16, 4)
  53. target_graph_id = queries.get_id_of_solver_graph(db["solver_graphs"],
  54. nx.node_link_data(target_graph))
  55. solver_input_query = __get_solver_input_query(db,
  56. target_graph_id,
  57. ising_qubo_collection)
  58. base_sampler = SimulatedAnnealingSampler()
  59. chimera_sampler = dimod.StructureComposite(base_sampler,
  60. target_graph.nodes(),
  61. target_graph.edges())
  62. __run_on_scope(solver_input_query,
  63. db[result_collection],
  64. chimera_sampler,
  65. model_type=model_type,
  66. negate=__get_negation())
  67. def __run_qpu(ising_qubo_collection, model_type, result_collection):
  68. db = script.connect_to_instance_pool()
  69. base_solver = DWaveSampler()
  70. solver_graph_id = __get_solver_graph_id(db, base_solver.solver)
  71. solver_input_query = __get_solver_input_query(db,
  72. solver_graph_id,
  73. ising_qubo_collection)
  74. solver_args = {}
  75. solver_args["annealing_time"] = int(input("annealing time (in us): "))
  76. solver_args["num_reads"] = int(input("number of reads per instance: "))
  77. __run_on_scope(solver_input_query,
  78. db[result_collection],
  79. base_solver,
  80. model_type,
  81. negate=__get_negation(),
  82. solver_args=solver_args)
  83. def __get_solver_graph_id(db, solver):
  84. solver_graph = graph.create_qpu_solver_nxgraph(solver)
  85. return queries.get_id_of_solver_graph(db["solver_graphs"],
  86. nx.node_link_data(solver_graph))
  87. def __get_solver_input_query(db, solver_graph_id, ising_qubo_collection):
  88. scope = input("scope: ")
  89. solver_input_query = queries.WMIS_solver_input_scope_query(db, ising_qubo_collection)
  90. solver_input_query.query(scope, solver_graph_id)
  91. return solver_input_query
  92. def __run_on_scope(solver_input_query,
  93. result_collection,
  94. base_solver,
  95. model_type,
  96. negate=False,
  97. solver_args={}):
  98. run = int(input("save as run (numbered): "))
  99. for solver_input in tqdm(solver_input_query):
  100. embedding = solver_input["embeddings"][0]
  101. qubo = __negate_qubo(solver_input["qubo"]) if negate else solver_input["qubo"]
  102. solver = FixedEmbeddingComposite(base_solver, embedding)
  103. res = None
  104. if model_type == __QUBO__:
  105. res = solver.sample_qubo(qubo, **solver_args)
  106. elif model_type == __ISING__:
  107. h, J = graph.split_ising(qubo)
  108. res = solver.sample_ising(h, J, **solver_args)
  109. script.save_sample_set(result_collection,
  110. res,
  111. solver_input,
  112. emb_list_index = 0,
  113. run = run)
  114. def __negate_qubo(qubo):
  115. negative_qubo = {}
  116. for coupler, energy in qubo.items():
  117. negative_qubo[coupler] = -1 * energy
  118. return negative_qubo
  119. if __name__ == "__main__":
  120. main()