def setUp(self): with BrickedClient() as client: self.solver = client.get_solver(qpu=True) self.ising = ({}, {'ab': 1, 'bc': 1, 'ca': 1}) self.bqm = dimod.BQM.from_ising(*self.ising) self.embedding = {'a': [0], 'b': [4], 'c': [1, 5]} self.chain_strength = 1.0 self.embedding_context = dict(embedding=self.embedding, chain_strength=self.chain_strength) target_edgelist = [[0, 4], [0, 5], [1, 4], [1, 5]] target_adjacency = edgelist_to_adjacency(target_edgelist) self.bqm_embedded = embed_bqm(self.bqm, self.embedding, target_adjacency, chain_strength=self.chain_strength) self.ising_embedded = self.bqm_embedded.to_ising() self.problem = self.ising_embedded[:2] self.params = dict(num_reads=100) self.label = "pretty-label" # get the expected response (from VCR) self.response = self.solver.sample_ising(*self.problem, **self.params)
def try_embedding(n_reads, solver, print_embedding=True): start_total = time.time() # Create test start_test = time.time() _, reads = create_test(num_reads=n_reads) test_creation_time = time.time() - start_test # Prepare the data tspAdjM = reads_to_tspAdjM(reads) quboAdjM = tspAdjM_to_quboAdjM(tspAdjM, -1.6, 1.6, 1.6) Q = quboAdjM_to_quboDict(quboAdjM) # Try to embed Q into a valid graph for the pegasus topology start_embedding = time.time() edgelist = solver.edges adjdict = edgelist_to_adjacency(edgelist) embed = minorminer.find_embedding(Q, edgelist) Q_embeded = embed_qubo(Q, embed, adjdict) embedding_time = time.time() - start_embedding total_time = time.time() - start_total print('Successful: {} reads, {} nodes graph, {:.6f}s test creation time, {:.6f}s embedding time, {:.6f}s total time'.format( len(reads), len(reads)*len(reads), test_creation_time, embedding_time, total_time))
def solve_ising_dwave(hii, Jij): config_file = '/media/sf_QWorld/QWorld/QA_DeNovoAsb/dwcloud.conf' client = Client.from_config(config_file, profile='aritra') solver = client.get_solver( ) # Available QPUs: DW_2000Q_2_1 (2038 qubits), DW_2000Q_5 (2030 qubits) dwsampler = DWaveSampler(config_file=config_file) edgelist = solver.edges adjdict = edgelist_to_adjacency(edgelist) embed = minorminer.find_embedding(Jij.keys(), edgelist) [h_qpu, j_qpu] = embed_ising(hii, Jij, embed, adjdict) response_qpt = dwsampler.sample_ising(h_qpu, j_qpu, num_reads=solver.max_num_reads()) client.close() bqm = dimod.BinaryQuadraticModel.from_ising(hii, Jij) unembedded = unembed_sampleset(response_qpt, embed, bqm, chain_break_method=majority_vote) print("Maximum Sampled Configurations from D-Wave\t===>") solnsMaxSample = sorted(unembedded.record, key=lambda x: -x[2]) for i in range(0, 10): print(solnsMaxSample[i]) print("Minimum Energy Configurations from D-Wave\t===>") solnsMinEnergy = sorted(unembedded.record, key=lambda x: +x[1]) for i in range(0, 10): print(solnsMinEnergy[i])
def setUpClass(cls): with BrickedClient() as client: cls.solver = client.get_solver(qpu=True) cls.ising = ({}, {'ab': 1, 'bc': 1, 'ca': 1}) cls.bqm = dimod.BQM.from_ising(*cls.ising) cls.embedding = {'a': [0], 'b': [4], 'c': [1, 5]} cls.chain_strength = 1.0 cls.embedding_context = dict(embedding=cls.embedding, chain_strength=cls.chain_strength) target_edgelist = [[0, 4], [0, 5], [1, 4], [1, 5]] target_adjacency = edgelist_to_adjacency(target_edgelist) cls.bqm_embedded = embed_bqm(cls.bqm, cls.embedding, target_adjacency, chain_strength=cls.chain_strength) cls.ising_embedded = cls.bqm_embedded.to_ising() cls.problem = cls.ising_embedded[:2] cls.params = dict(num_reads=100) cls.label = "pretty-label" # get the expected response (from VCR) cls.response = cls.solver.sample_ising(*cls.problem, **cls.params)
def solve_qubo_dwave(Q, num_reads=1000): # Create the solver (connecting to D-Wave) and the Sampler config_file = '../dwave.conf' client = Client.from_config(config_file, profile='ocete') solver = client.get_solver( ) # Available QPUs: DW_2000Q_2_1 (2038 qubits), DW_2000Q_5 (2030 qubits) dwsampler = DWaveSampler(config_file=config_file) # We need to embed Q into a valid graph for the D-Wave architecture edgelist = solver.edges adjdict = edgelist_to_adjacency(edgelist) embed = minorminer.find_embedding(Q, edgelist) Q_embeded = embed_qubo(Q, embed, adjdict) # Obtain the response from the solver. This is the actual D-Wave execution! start = time.time() response_qpt = dwsampler.sample_qubo(Q_embeded, num_reads=num_reads) qpu_time = time.time() - start client.close() # Transform the response from the embeded graph to our original architecture bqm = dimod.BinaryQuadraticModel.from_qubo(Q) unembedded = unembed_sampleset(response_qpt, embed, bqm, chain_break_method=majority_vote) # Order the solutions by lower energy return unembedded, qpu_time
def solve_qubo_dwave(Q, n_genome_reads, num_reads=100, label='', annealing_time=20): # Create the solver (connecting to D-Wave) and the Sampler config_file = '../dwave_adv.conf' client = Client.from_config(config_file, profile='ocete') solver = client.get_solver('Advantage_system1.1') dwsampler = DWaveSampler( solver={ 'qpu': True, 'topology__type': 'pegasus', 'annealing_time': annealing_time }, # Watch out, Leap doesn seem to take into account # this parameter, you have to give it as a paramater in the dwsampler.sample() call token=token, endpoint=endpoint) # We need to embed Q into a valid graph for the D-Wave architecture adjdict = edgelist_to_adjacency(solver.edges) embed = minorminer.find_embedding(Q, solver.edges) Q_embeded = embed_qubo(Q, embed, adjdict) # Obtain the response from the solver. This is the actual D-Wave execution! start = time.time() response_qpt = dwsampler.sample_qubo(Q_embeded, num_reads=num_reads, label=label, annealing_time=annealing_time) qpu_time = time.time() - start client.close() # Transform the response from the embeded graph to our original architecture bqm = dimod.BinaryQuadraticModel.from_qubo(Q) unembedded = unembed_sampleset(response_qpt, embed, bqm, chain_break_method=majority_vote) # Sort the solutions from lowest energy and format them to quboDict format unformated_solutions_list = sorted(unembedded.record, key=lambda x: +x[1]) solutions_list = [] for sol, energy, num_appereances in unformated_solutions_list: solutions_list.append([ rebuild_quboDict_from_vector(sol, n_genome_reads), energy, num_appereances ]) return solutions_list, qpu_time, get_max_chain_length(embed)
def from_bqm_sampleset(bqm, sampleset, sampler, embedding_context=None, warnings=None, params=None): """Construct problem data for visualization based on the BQM and sampleset in logical space (both unembedded). In order for the embedded problem/response to be reconstructed, an embedding is required in either the sampleset, or as a standalone argument. Note: This adapter can only provide best-effort estimate of the submitted problem and received samples. Namely, because values of logical variables in `sampleset` are produced by a chain break resolution method, information about individual physical qubit values is lost. Please have in mind you will never see "broken chains" when using this adapter. Args: bqm (:class:`dimod.BinaryQuadraticModel`/:class:`dimod.core.bqm.BQM`): Problem in logical (unembedded) space, given as a BQM. sampleset (:class:`~dimod.sampleset.SampleSet`): Sampling response as a sampleset. sampler (:class:`~dimod.Sampler` or :class:`~dimod.ComposedSampler`): The :class:`~dwave.system.samplers.dwave_sampler.DWaveSampler`- derived sampler used to produce the sampleset off the bqm. embedding_context (dict, optional): A map containing an embedding of logical problem onto the solver's graph (the ``embedding`` key) and embedding parameters used (e.g. ``chain_strength``). It is optional only if ``sampleset.info`` contains it (see `return_embedding` argument of :meth:`~dwave.system.composites.embedding.EmbeddingComposite`). warnings (list[dict], optional): Optional list of warnings. params (dict, optional): Sampling parameters used. """ logger.debug("from_bqm_sampleset({!r})".format( dict(bqm=bqm, sampleset=sampleset, sampler=sampler, warnings=warnings, embedding_context=embedding_context, params=params))) if not isinstance(sampler, dimod.Sampler): raise TypeError("dimod.Sampler instance expected for 'sampler'") # get embedding parameters if embedding_context is None: embedding_context = sampleset.info.get('embedding_context', {}) if embedding_context is None: raise ValueError("embedding_context not given") embedding = embedding_context.get('embedding') if embedding is None: raise ValueError("embedding not given") chain_strength = embedding_context.get('chain_strength') def find_solver(sampler): if hasattr(sampler, 'solver'): return sampler.solver for child in getattr(sampler, 'children', []): try: return find_solver(child) except: pass raise TypeError("'sampler' doesn't use DWaveSampler") solver = find_solver(sampler) if not isinstance(solver, StructuredSolver): raise TypeError("only structured solvers are supported") topology = _get_solver_topology(solver) if topology['type'] not in SUPPORTED_SOLVER_TOPOLOGY_TYPES: raise TypeError("unsupported solver topology type") solver_id = solver.id problem_type = "ising" if sampleset.vartype is dimod.SPIN else "qubo" # bqm vartype must match sampleset vartype if bqm.vartype is not sampleset.vartype: bqm = bqm.change_vartype(sampleset.vartype, inplace=False) # if `embedding` is `dwave.embedding.transforms.EmbeddedStructure`, we don't # need `target_adjacency` emb_params = dict(embedding=embedding) if not hasattr(embedding, 'embed_bqm'): # proxy for detecting dict vs. EmbeddedStructure, without actually # importing EmbeddedStructure (did not exist in dwave-system<0.9.10) target_adjacency = edgelist_to_adjacency(solver.edges) emb_params.update(target_adjacency=target_adjacency) # get embedded bqm bqm_embedded = embed_bqm(bqm, chain_strength=chain_strength, smear_vartype=dimod.SPIN, **emb_params) # best effort reconstruction of (unembedded/qmi) response/solutions # NOTE: we **can not** reconstruct physical qubit values from logical variables # (sampleset we have access to has variable values after chain breaks resolved!) active_variables = sorted(list(bqm_embedded.variables)) active_variables_set = set(active_variables) logical_variables = list(sampleset.variables) var_to_idx = {var: idx for idx, var in enumerate(logical_variables)} unembedding = {q: var_to_idx[v] for v, qs in embedding.items() for q in qs} # sanity check assert set(unembedding) == active_variables_set def expand_sample(sample): return [int(sample[unembedding[q]]) for q in active_variables] solutions = [expand_sample(sample) for sample in sampleset.record.sample] # adjust energies to values returned by SAPI (offset embedding) energies = list(map(float, sampleset.record.energy - bqm_embedded.offset)) num_occurrences = list(map(int, sampleset.record.num_occurrences)) num_variables = solver.num_qubits timing = sampleset.info.get('timing') linear, quadratic, offset = bqm_embedded.to_ising() problem_data = { "format": "qp", # SAPI non-conforming (nulls vs nans) "lin": [ uniform_get(linear, v, 0 if v in active_variables_set else None) for v in solver._encoding_qubits ], "quad": [ quadratic.get((q1, q2), 0) + quadratic.get((q2, q1), 0) for (q1, q2) in solver._encoding_couplers if q1 in active_variables_set and q2 in active_variables_set ], "embedding": embedding } # try to get problem id. if not available, auto-generate one problem_id = sampleset.info.get('problem_id') if problem_id is None: problem_id = "local-%s" % uuid.uuid4() # try to reconstruct sampling params if params is None: params = {'num_reads': int(sum(num_occurrences))} # expand with defaults params = _expand_params(solver, params, timing) # try to get warnings from sampleset.info if warnings is None: warnings = sampleset.info.get('warnings') # construct problem stats problem_stats = _problem_stats(response=None, sampleset=sampleset, embedding_context=embedding_context) data = { "ready": True, "details": { "id": problem_id, "type": problem_type, "solver": solver.id, "label": sampleset.info.get('problem_label'), }, "data": _problem_dict(solver_id, problem_type, problem_data, params, problem_stats), "answer": _answer_dict(solutions, active_variables, energies, num_occurrences, timing, num_variables), "unembedded_answer": _unembedded_answer_dict(sampleset), "warnings": _warnings(warnings), "rel": dict(solver=solver), } logger.trace("from_bqm_sampleset returned %r", data) return data
def from_bqm_response(bqm, embedding_context, response, warnings=None, params=None, sampleset=None): """Construct problem data for visualization based on the unembedded BQM, the embedding used when submitting, and the low-level sampling response. Args: bqm (:class:`dimod.BinaryQuadraticModel`/:class:`dimod.core.bqm.BQM`): Problem in logical (unembedded) space, given as a BQM. embedding_context (dict): A map containing an embedding of logical problem onto the solver's graph (the ``embedding`` key) and embedding parameters used (e.g. ``chain_strength``, ``chain_break_method``, etc). response (:class:`dwave.cloud.computation.Future`): Sampling response, as returned by the low-level sampling interface in the Cloud Client (e.g. :meth:`dwave.cloud.solver.sample_ising` for Ising problems). warnings (list[dict], optional): Optional list of warnings. params (dict, optional): Sampling parameters used. sampleset (:class:`dimod.SampleSet`, optional): Optional unembedded sampleset. """ logger.debug("from_bqm_response({!r})".format( dict(bqm=bqm, response=response, response_energies=response['energies'], embedding_context=embedding_context, warnings=warnings, params=params, sampleset=sampleset))) solver = response.solver if not isinstance(response.solver, StructuredSolver): raise TypeError("only structured solvers are supported") topology = _get_solver_topology(solver) if topology['type'] not in SUPPORTED_SOLVER_TOPOLOGY_TYPES: raise TypeError("unsupported solver topology type") solver_id = solver.id problem_type = response.problem_type active_variables = response['active_variables'] active = set(active_variables) solutions = list(map(itemsgetter(*active_variables), response['solutions'])) energies = response['energies'] num_occurrences = response.num_occurrences num_variables = solver.num_qubits timing = response.timing # bqm vartype must match response vartype if problem_type == "ising": bqm = bqm.change_vartype(dimod.SPIN, inplace=False) else: bqm = bqm.change_vartype(dimod.BINARY, inplace=False) # get embedding parameters if 'embedding' not in embedding_context: raise ValueError("embedding not given") embedding = embedding_context.get('embedding') chain_strength = embedding_context.get('chain_strength') chain_break_method = embedding_context.get('chain_break_method') # if `embedding` is `dwave.embedding.transforms.EmbeddedStructure`, we don't # need `target_adjacency` emb_params = dict(embedding=embedding) if not hasattr(embedding, 'embed_bqm'): # proxy for detecting dict vs. EmbeddedStructure, without actually # importing EmbeddedStructure (did not exist in dwave-system<0.9.10) target_adjacency = edgelist_to_adjacency(solver.edges) emb_params.update(target_adjacency=target_adjacency) # get embedded bqm bqm_embedded = embed_bqm(bqm, chain_strength=chain_strength, smear_vartype=dimod.SPIN, **emb_params) linear, quadratic, offset = bqm_embedded.to_ising() problem_data = { "format": "qp", # SAPI non-conforming (nulls vs nans) "lin": [ uniform_get(linear, v, 0 if v in active else None) for v in solver._encoding_qubits ], "quad": [ quadratic.get((q1, q2), 0) + quadratic.get((q2, q1), 0) for (q1, q2) in solver._encoding_couplers if q1 in active and q2 in active ], "embedding": embedding } # try to reconstruct sampling params if params is None: params = {'num_reads': int(sum(num_occurrences))} # expand with defaults params = _expand_params(solver, params, timing) # TODO: if warnings are missing, calculate them here (since we have the # low-level response) # construct problem stats problem_stats = _problem_stats(response=response, sampleset=sampleset, embedding_context=embedding_context) data = { "ready": True, "details": _details_dict(response), "data": _problem_dict(solver_id, problem_type, problem_data, params, problem_stats), "answer": _answer_dict(solutions, active_variables, energies, num_occurrences, timing, num_variables), "warnings": _warnings(warnings), "rel": dict(solver=solver), } if sampleset is not None: data["unembedded_answer"] = _unembedded_answer_dict(sampleset) logger.trace("from_bqm_response returned %r", data) return data
# or, load it from file (if provided) if len(sys.argv) > 1: path = sys.argv[1] with open(path) as fp: bqm = dimod.BinaryQuadraticModel.from_coo(fp).spin # get solver print("solver init") client = Client.from_config() solver = client.get_solver(qpu=True) # embed print("embedding") source_edgelist = list(bqm.quadratic) + [(v, v) for v in bqm.linear] target_edgelist = solver.edges embedding = find_embedding(source_edgelist, target_edgelist) target_adjacency = edgelist_to_adjacency(target_edgelist) bqm_embedded = embed_bqm(bqm, embedding, target_adjacency) # sample print("sampling") response = solver.sample_ising(bqm_embedded.linear, bqm_embedded.quadratic, num_reads=100) sampleset_embedded = response.sampleset sampleset = unembed_sampleset(sampleset_embedded, embedding, bqm) # inspect print("inspecting") dwave.inspector.show(bqm, dict(embedding=embedding), response)