def test_uniform_get(self): d = {0: 0, 1: 1} self.assertEqual(uniform_get(d, 0), 0) self.assertEqual(uniform_get(d, 2), None) self.assertEqual(uniform_get(d, 2, default=0), 0) l = [0, 1] self.assertEqual(uniform_get(l, 0), 0) self.assertEqual(uniform_get(l, 2), None) self.assertEqual(uniform_get(l, 2, default=0), 0)
def encode_bqm_as_qp(solver, linear, quadratic): """Encode the binary quadratic problem for submission to a given solver, using the `qp` format for data. Args: solver (:class:`dwave.cloud.solver.Solver`): The solver used. linear (dict[variable, bias]/list[variable, bias]): Linear terms of the model. quadratic (dict[(variable, variable), bias]): Quadratic terms of the model. Returns: encoded submission dictionary """ active = active_qubits(linear, quadratic) # Encode linear terms. The coefficients of the linear terms of the objective # are encoded as an array of little endian 64 bit doubles. # This array is then base64 encoded into a string safe for json. # The order of the terms is determined by the _encoding_qubits property # specified by the server. # Note: only active qubits are coded with double, inactive with NaN nan = float('nan') lin = [ uniform_get(linear, qubit, 0 if qubit in active else nan) for qubit in solver._encoding_qubits ] lin = base64.b64encode(struct.pack('<' + ('d' * len(lin)), *lin)) # Encode the coefficients of the quadratic terms of the objective # in the same manner as the linear terms, in the order given by the # _encoding_couplers property, discarding tailing zero couplings 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 ] quad = base64.b64encode(struct.pack('<' + ('d' * len(quad)), *quad)) # The name for this encoding is 'qp' and is explicitly included in the # message for easier extension in the future. return { 'format': 'qp', 'lin': lin.decode('utf-8'), 'quad': quad.decode('utf-8') }
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
def from_qmi_response(problem, response, embedding_context=None, warnings=None, params=None, sampleset=None): """Construct problem data for visualization based on the low-level sampling problem definition and the low-level response. Args: problem ((list/dict, dict[(int, int), float]) or dict[(int, int), float]): Problem in Ising or QUBO form, conforming to solver graph. Note: if problem is given as tuple, it is assumed to be in Ising variable space, and if given as a dict, Binary variable space is assumed. Zero energy offset is always implied. 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). 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``). 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_qmi_response({!r})".format( dict(problem=problem, response=response, response_energies=response['energies'], embedding_context=embedding_context, warnings=warnings, params=params, sampleset=sampleset))) try: linear, quadratic = problem except: linear, quadratic = reformat_qubo_as_ising(problem) # make sure lin/quad are not dimod views (that handle directed edges) if isinstance(linear, BQMView): linear = dict(linear) if isinstance(quadratic, BQMView): quadratic = dict(quadratic) 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 variables = list(response.variables) active = active_qubits(linear, quadratic) # filter out invalid values (user's error in problem definition), since # SAPI ignores them too active = {q for q in active if q in solver.variables} # sanity check active_variables = response['active_variables'] assert set(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 # note: we can't use encode_problem_as_qp(solver, linear, quadratic) because # visualizer accepts decoded lists (and nulls instead of NaNs) 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 ] } # include optional embedding if embedding_context is not None and 'embedding' in embedding_context: problem_data['embedding'] = embedding_context['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) # 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_qmi_response returned %r", data) return data
def verify_data_encoding(self, problem, response, solver, params, data, embedding_context=None): # avoid persistent data modification data = data.copy() # make sure data correct after JSON decoding (minus the 'rel' data) del data['rel'] data = json.loads(json.dumps(data)) # test structure self.assertIsInstance(data, dict) self.assertTrue( all(k in data for k in 'details data answer warnings'.split())) # .details self.assertIn('id', data['details']) self.assertIn('label', data['details']) self.assertEqual(data['details']['solver'], solver.id) # .problem self.assertEqual(data['data']['type'], response.problem_type) # .problem.params, smoke tests self.assertIn('params', data['data']) self.assertEqual(data['data']['params']['num_reads'], params['num_reads']) self.assertIn('annealing_time', data['data']['params']) self.assertIn('programming_thermalization', data['data']['params']) if response.problem_type == 'ising': linear, quadratic = problem elif response.problem_type == 'qubo': linear, quadratic = reformat_qubo_as_ising(problem) else: self.fail("Unknown problem type") active_variables = response['active_variables'] problem_data = { "format": "qp", "lin": [ uniform_get(linear, v, 0 if v in active_variables 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 and q2 in active_variables ] } if embedding_context is not None: problem_data['embedding'] = embedding_context['embedding'] self.assertDictEqual(data['data']['data'], problem_data) # .answer self.assertEqual(sum(data['answer']['num_occurrences']), params['num_reads']) self.assertEqual(data['answer']['num_occurrences'], response['num_occurrences']) self.assertEqual(data['answer']['num_variables'], response['num_variables']) self.assertEqual(data['answer']['active_variables'], active_variables) solutions = [[sol[idx] for idx in active_variables] for sol in response['solutions']] self.assertEqual(data['answer']['solutions'], solutions) self.assertEqual(data['answer']['energies'], response['energies']) self.assertEqual(data['answer']['timing'], response['timing'])