Browse Source

prepared qpu solving

master
Tom 5 years ago
parent
commit
460db2369f
9 changed files with 196 additions and 28 deletions
  1. +1
    -0
      .gitignore
  2. +2
    -3
      create_wmis_result_table.sql
  3. +97
    -0
      run_sampler_on_scope.py
  4. +12
    -14
      test_data_extraction.py
  5. +42
    -7
      test_sat_to_qubo_workflow.py
  6. +9
    -0
      util/graph.py
  7. +3
    -0
      util/queries.py
  8. +14
    -3
      util/random_instance_pool.py
  9. +16
    -1
      util/script.py

+ 1
- 0
.gitignore View File

@ -99,6 +99,7 @@ ENV/
#### config files
#
database.config
dwave.conf
### vim
#


+ 2
- 3
create_wmis_result_table.sql View File

@ -1,8 +1,7 @@
create table c42_v5to84_1_wmis_qbsolv_results (
create table c42_vLogistic_6_wmis_siman_results (
result_id char(24),
run int,
instance_id char(24),
number_of_clauses int,
number_of_variables int,
number_of_found_assignments int,
chain_break_fraction float,
num_occurrences int,


+ 97
- 0
run_sampler_on_scope.py View File

@ -0,0 +1,97 @@
#!/usr/bin/env python3
import util.script as script
import util.queries as queries
import util.graph as graph
import networkx as nx
import dwave_networkx as dnx
from neal import SimulatedAnnealingSampler
import dimod
from dwave.system.composites import FixedEmbeddingComposite
from dwave.system.samplers import DWaveSampler
from tqdm import tqdm
def main():
mode = __get_mode()
if mode == "SIMAN":
__run_siman()
elif mode == "QPU":
__run_qpu()
def __get_mode():
print("choose mode:")
print("(1) simulated annealing")
print("(2) qpu")
mode = int(input())
if mode == 1:
return "SIMAN"
elif mode == 2:
return "QPU"
def __run_siman():
db = script.connect_to_instance_pool()
target_graph = dnx.chimera_graph(16, 16, 4)
solver_input_query = __get_solver_input_query(db, target_graph)
base_sampler = SimulatedAnnealingSampler()
chimera_sampler = dimod.StructureComposite(base_sampler,
target_graph.nodes(),
target_graph.edges())
__run_on_scope(solver_input_query, db["wmis_siman_results"], base_sampler)
def __run_qpu():
db = script.connect_to_instance_pool()
base_solver = DWaveSampler()
solver_input_query = __get_solver_input_query(db, solver_graph)
solver_args = {}
solver_args["annealing_time"] = int(input("annealing time (in us): "))
__run_on_scope(solver_input_query, db["wmis_qpu_results"], base_solver, solver_args)
def __get_solver_graph_id(db, solver):
solver_graph = graph.create_qpu_solver_nxgraph(solver)
return queries.get_id_of_solver_graph(db["solver_graphs"],
nx.node_link_data(solver_graph))
def __get_solver_input_query(db, solver):
solver_graph_id = __get_solver_graph_id(db, solver)
scope = input("scope: ")
solver_input_query = queries.WMIS_solver_input_scope_query(db)
solver_input_query.query(scope, solver_graph_id)
def __run_on_scope(solver_input_query, result_collection, base_solver, solver_args=None):
run = int(input("save as run (numbered): "))
for solver_input in tqdm(solver_input_query):
embedding = solver_input["embeddings"][0]
qubo = solver_input["qubo"]
solver = FixedEmbeddingComposite(base_solver, embedding)
res = solver.sample_qubo(qubo, **args)
script.save_result(result_collection,
res,
solver_input,
emb_list_index = 0,
run = run)
if __name__ == "__main__":
main()

+ 12
- 14
test_data_extraction.py View File

@ -7,11 +7,11 @@ import dimod
from tqdm import tqdm
def main():
instance_parameters()
#instance_parameters()
#wmis_results()
#wmis_siman_results_alpha_num_of_assignments()
#wmis_siman_results()
#minisat_runs()
minisat_runs()
def instance_parameters():
edb = script.connect_to_experimetns_db()
@ -20,15 +20,15 @@ def instance_parameters():
idb = script.connect_to_instance_pool()
instances = queries.Instance_scope_query(idb)
instances.query("c42_vLogistic_1")
instances.query("c42_vLogistic_6")
insert_row = ("INSERT INTO c42_vLogistic_1_instances "
insert_row = ("INSERT INTO c42_vLogistic_6_instances "
"(instance_id, "
" number_of_clauses, "
" number_of_variables) "
"VALUES (%s, %s, %s)")
for instance, instance_id in instances:
for instance, instance_id in tqdm(instances):
edb_cursor.execute(insert_row, (str(instance_id),
int(instance.getNumberOfClauses()),
int(instance.getNumberOfVariables())))
@ -60,19 +60,18 @@ def wmis_siman_results_alpha_num_of_assignments():
idb = script.connect_to_instance_pool()
q = queries.WMIS_result_scope_query_raw(idb)
q.query("c42_v[5-84]_1", "wmis_qbsolv_results")
q.query("c42_vLogistic_6", "wmis_siman_results")
insert_row = ("INSERT INTO c42_v5to84_1_wmis_qbsolv_results "
insert_row = ("INSERT INTO c42_vLogistic_6_wmis_siman_results "
"(result_id, "
" run, "
" instance_id, "
" number_of_clauses, "
" number_of_variables, "
" number_of_found_assignments, "
" chain_break_fraction, "
" num_occurrences, "
" energy, "
" satisfiable) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ")
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ")
for result in tqdm(q):
sample_set = queries.read_raw_wmis_sample_set(result["data"])
@ -86,9 +85,8 @@ def wmis_siman_results_alpha_num_of_assignments():
isSatisfiable = sat.checkAssignment(model)
edb_cursor.execute(insert_row, (str(result["_id"]),
int(result["run"]),
str(result["instance"]),
int(sat.getNumberOfClauses()),
int(sat.getNumberOfVariables()),
int(data["number_of_assignments"]),
float(data["chain_break_fraction"]),
int(data["num_occurrences"]),
@ -106,9 +104,9 @@ def minisat_runs():
idb = script.connect_to_instance_pool()
runs = queries.Minisat_run_scope_query_raw(idb)
runs.query("c42_v[5-84]_1", "minisat_runs")
runs.query("c42_vLogistic_6", "minisat_runs")
insert_row = ("INSERT INTO c42_v5to84_1_minisat_runs "
insert_row = ("INSERT INTO c42_vLogistic_6_minisat_runs "
"(run_id, "
" instance_id, "
" satisfiable) "


+ 42
- 7
test_sat_to_qubo_workflow.py View File

@ -5,6 +5,7 @@ import util.randomSAT as rand_sat
import util.SAT2QUBO as SAT2QUBO
import util.script as script
import util.queries as queries
import util.graph as graph
from dwave.system.composites import FixedEmbeddingComposite
import dimod
@ -14,6 +15,10 @@ import networkx as nx
import dwave_networkx as dnx
import dwave.embedding
from dwave.cloud import Client
from dwave.cloud import Future
from dwave.system.samplers import DWaveSampler
from dwave_qbsolv import QBSolv
import numpy as np
@ -63,23 +68,53 @@ def __wmis3():
nx.node_link_data(target_graph))
solver_input_query = queries.WMIS_solver_input_scope_query(db)
solver_input_query.query("c42_v[5-84]_1", target_graph_id)
solver_input_query.query("c42_vLogistic_6", target_graph_id)
#base_sampler = SimulatedAnnealingSampler()
base_sampler = QBSolv()
base_sampler = SimulatedAnnealingSampler()
#base_sampler = QBSolv()
chimera_sampler = dimod.StructureComposite(base_sampler,
target_graph.nodes(),
target_graph.edges())
for solver_input in tqdm(solver_input_query):
sampler = FixedEmbeddingComposite(chimera_sampler, solver_input["embeddings"][0])
res = sampler.sample_qubo(__negate_qubo(solver_input["qubo"]), solver_limit=2048)
#res = sampler.sample_qubo(solver_input["qubo"], solver_limit=2048)
res = sampler.sample_qubo(solver_input["qubo"])
script.save_result(db["wmis_siman_results"],
res,
solver_input,
0,
1)
def __wmis_qpu():
db = script.connect_to_instance_pool()
base_solver = DWaveSampler()
solver_graph = graph.create_qpu_solver_nxgraph(base_solver)
solver_graph_id = queries.get_id_of_solver_graph(db["solver_graphs"],
nx.node_link_data(solver_graph))
solver_input_query = queries.WMIS_solver_input_scope_query(db)
solver_input_query.query("c42_vLogistic_6", solver_graph_id)
for solver_input in tqdm(solver_input_query):
embedding = solver_input["embeddings"][0]
qubo = solver_input["qubo"]
solver = FixedEmbeddingComposite(base_solver, embedding)
res = solver.sample_qubo(qubo, annealing_time=5)
script.save_simulated_annealing_result(db["wmis_qbsolv_results"],
script.save_result(db["wmis_qpu_results"],
res,
solver_input,
0)
emb_list_index = 0,
run = 1)


+ 9
- 0
util/graph.py View File

@ -16,3 +16,12 @@ def negate_qubo(qubo):
negative_qubo[coupler] = -1 * energy
return negative_qubo
def create_qpu_solver_nxgraph(solver):
graph = nx.Graph()
graph.name = solver.id
graph.add_edges_from(solver.edges)
return graph

+ 3
- 0
util/queries.py View File

@ -398,6 +398,9 @@ def read_raw_qubo(raw_qubo):
def read_raw_embedding(raw_embedding):
emb = {}
if "embedding" in raw_embedding:
raw_embedding = raw_embedding["embedding"]
for entry in raw_embedding:
emb[tuple(entry[0])] = entry[1]


+ 14
- 3
util/random_instance_pool.py View File

@ -116,12 +116,12 @@ class Random_logistic_variable_distribution:
min_variables,
max_variables,
alpha_point_of_interest,
steepnesss):
steepness):
self.__number_of_clauses = number_of_clauses
self.__min_variables = min_variables
self.__max_variables = max_variables
self.__alpha_point_of_interest = alpha_point_of_interest
self.__steepnesss = steepnesss
self.__steepness = steepness
def random(self):
number_of_variables = 0
@ -131,7 +131,7 @@ class Random_logistic_variable_distribution:
alpha = inv_logistic(random.random(),
1,
self.__steepnesss,
self.__steepness,
self.__alpha_point_of_interest)
number_of_variables = int(self.__number_of_clauses / alpha)
@ -140,6 +140,17 @@ class Random_logistic_variable_distribution:
def inv_logistic(x, L, k, x0):
return math.log(math.pow(x / (L-x), 1/k)) + x0
def create_random_logistic_pool(logistic_variable_distr_params, number_of_instances,
instance_params):
logist_distr = Random_logistic_variable_distribution(**logistic_variable_distr_params)
rnd_range = Random_range(logist_distr, number_of_instances)
param_range = Instance_parameter_variable_range(instance_params, rnd_range)
return Random_instance_pool(param_range)
def test():
sns.set()


+ 16
- 1
util/script.py View File

@ -7,12 +7,14 @@ import mysql.connector
import networkx as nx
from . import queries
from . import graph
from . import SAT2QUBO
import minorminer
from tqdm import tqdm
import numpy as np
import random
import sys
def readConfig(configFilePath):
config = configparser.ConfigParser()
@ -177,6 +179,15 @@ def write_qubo_to_pool_db(collection, qubo, sat_instance_id):
collection.insert_one(doc)
def create_wmis_qubos_for_scope(db, scope):
instances = queries.Instance_scope_query(db)
instances.query(scope)
for instance, instance_id in instances:
qubo = SAT2QUBO.WMISdictQUBO(instance)
write_qubo_to_pool_db(db["wmis_qubos"], qubo, instance_id)
def __qubo_to_JSON(qubo):
quboJSON = []
@ -295,7 +306,7 @@ def find_wmis_embeddings_for_scope(db, scope, solver_graph):
percentage))
print("{} new embeddigns found".format(new_embeddings_found))
def save_simulated_annealing_result(collection, result, solver_input, emb_list_index):
def save_sample_set(collection, result, solver_input, emb_list_index, run):
doc = {}
doc["data"] = result.to_serializable()
@ -304,9 +315,13 @@ def save_simulated_annealing_result(collection, result, solver_input, emb_list_i
"embedding_id": solver_input["embeddings_id"],
"list_index": emb_list_index
}
doc["run"] = run
collection.insert_one(doc)
def save_qpu_result():
doc = {}
def analyze_wmis_sample(sample):
data = {}


Loading…
Cancel
Save