def solve_qsage_example(): # use a local solver solver = local_connection.get_solver("c4-sw_sample") num_vars = 12 # the first parameter is an objective function, the second parameter is # the number of variables, the third parameter is the solver, the fourth # parameter is the parameters for solver, the fifth parameter is the # parameters for qsage answer = solve_qsage(ObjClass(), num_vars, solver, {}, {"verbose": 2}) print "solve_qsage ObjClass() answer: ", answer answer = solve_qsage(obj_function, num_vars, solver, {}, {"verbose": 2}) print "solve_qsage obj_function answer: ", answer
def embedding_example(): # formulate k_6 structured graph h = [1, 1, 1, 1, 1, 1] J = {(i, j): 1 for i in range(6) for j in range(i)} solver = local_connection.get_solver("c4-sw_optimize") A = get_hardware_adjacency(solver) # find and print embeddings for problem graph embeddings = find_embedding(J, A, verbose=1) print "embeddings are: ", embeddings # embed the problem into solver graph (h0, j0, jc, new_emb) = embed_problem(h, J, embeddings, A) print "embedded problem result:\nj0: ", j0 print "jc: ", jc # find unembedded results for chain strengths -0.5, -1.0, -2.0 for chain_strength in (-0.5, -1.0, -2.0): # set chain strength values jc = dict.fromkeys(jc, chain_strength) # create new J array concatenating j0 with jc emb_j = j0.copy() emb_j.update(jc) # solve embedded problem answer = solve_ising(solver, h0, emb_j, num_reads=10) # unembed and print result of the form: # solution [solution #] # var [var #] : [var value] ([qubit index] : [original qubit value] ...) result = unembed_answer(answer['solutions'], new_emb, broken_chains="minimize_energy", h=h, j=J) print "result for chain strength = ", chain_strength for i, (embsol, sol) in enumerate(zip(answer['solutions'], result)): print "solution", i for j, emb in enumerate(embeddings): print "var %d: %d (" % (j, sol[j]), for k in emb: print "%d:%d" % (k, embsol[k]), print ")"
from dwave_sapi2.util import get_hardware_adjacency from dwave_sapi2.embedding import embed_problem, unembed_answer from dwave_sapi2.core import solve_ising from dwave_sapi2.local import local_connection #~ Connecting to the solver #~ ^^^^^^^^^^^^^^^^^^^^^^^^ #~ #~ Here, we're going to use a local solver because various users will have #~ access to different systems. When you extend this code, you'll want to #~ modify it to use a remote connection with your API key, and connect to #~ the appropriate solver. #~ solver_name = "c4-sw_sample" solver = local_connection.get_solver(solver_name) # or, if you're using a remote solver, uncomment this block and put your # authentication information here # from dwave_sapi2.core import remote # sapi_url = 'https://cloud.dwavesys.com/sapi' # sapi_token = 'ENTER SAPI TOKEN' # solver_name = 'DW_2000Q_5' # connection = remote.RemoteConnection(sapi_url, sapi_token) # solver = connection.get_solver(solver_name) #~ #~ Now that we're connected, let's pull down the adjacency information from #~ the solver. This is just a set of tuples ``(p, q)`` of qubit labels. Using #~ the ``c4-sw_optimize`` solver, this is simply a :math:`C_{4,4,4}` Chimera #~ graph. If you're using a hardware solver, this will be something more
def __init__(self, solver_name): dimod.TemplateSampler.__init__(self) self.solver = solver = local_connection.get_solver(solver_name) edges = get_hardware_adjacency(solver) self.structure = (set().union(*edges), edges)
from dwave_sapi2.embedding import embed_problem, unembed_answer from dwave_sapi2.core import solve_ising from dwave_sapi2.local import local_connection from random import uniform, randint #~ Connecting to the solver #~ ^^^^^^^^^^^^^^^^^^^^^^^^ #~ #~ Here, we're going to use a local solver because various users will have #~ access to different systems. When you extend this code, you'll want to #~ modify it to use a remote connection with your API key, and connect to #~ the appropriate solver. #~ solver_name = "c4-sw_sample" solver = local_connection.get_solver(solver_name) # or, if you're using a remote solver, uncomment this block and put your # authentication information here # from dwave_sapi2.core import remote # sapi_url = 'https://dw2x.dwavesys.com/sapi' # sapi_token = 'ENTER SAPI TOKEN' # solver_name = 'DW2X' # connection = remote.RemoteConnection(sapi_url, sapi_token) # solver = connection.get_solver(solver_name) #~ #~ Now that we're connected, let's pull down the adjacency information from #~ the solver. This is just a set of tuples ``(p, q)`` of qubit labels. Using #~ the ``c4-sw_optimize`` solver, this is simply a :math:`C_{4,4,4}` Chimera #~ graph. If you're using a hardware solver, this will be something more
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # https://docs.dwavesys.com/docs/latest/c_timing_6.html from dwave_sapi2.local import local_connection from dwave_sapi2.core import async_solve_ising, await_completion import datetime h, J = build_hamiltonian() solver = local_connection.get_solver('example_solver') submitted_qmi = async_solve_ising(solver, h, J, num_reads=100) await_completion([submitted_qmi], min_done=2, timeout=1.0) # QPU and PP times result = submitted_qmi.result() timing = result['timing'] # Service time time_format = "%Y-%m-%dT%H:%M:%S.%fZ" status = submitted_qmi.status() start_time = datetime.datetime.strptime(status['time_received'], time_format) finish_time = datetime.datetime.strptime(status['time_solved'], time_format) service_time = finish_time - start_time
def connect_to_local(): solver = local_connection.get_solver(sys.argv[1]) return solver