コード例 #1
0
def show_bqm_sampleset(bqm, sampleset, sampler, embedding_context=None,
                       warnings=None, params=None):
    """
    Visualize a returned sampleset and binary quadratic model.
    """
    data = from_bqm_sampleset(bqm=bqm, sampleset=sampleset, sampler=sampler,
                              embedding_context=embedding_context,
                              warnings=warnings, params=params)
    id_ = push_inspector_data(data)
    return open_problem(id_)
コード例 #2
0
    def test_sampler_graph_validation(self):
        """All data adapters should fail on non-Chimera/Pegasus solvers."""

        # sample
        qpu = DWaveSampler()
        sampler = FixedEmbeddingComposite(qpu, self.embedding)
        sampleset = sampler.sample(self.bqm,
                                   return_embedding=True,
                                   **self.params)

        # resolve it before we mangle with it
        sampleset.info['problem_id']
        # change solver topology to non-chimera/pegasus to test solver validation
        sampler.child.solver.properties['topology']['type'] = 'unknown'

        # ensure `from_bqm_sampleset` adapter fails on unstructured solver
        with self.assertRaises(TypeError):
            from_bqm_sampleset(self.bqm,
                               sampleset,
                               sampler,
                               params=self.params)
コード例 #3
0
    def test_sampler_type_validation(self):
        """All data adapters should fail on non-StructuredSolvers."""

        # sample
        qpu = DWaveSampler()
        sampler = FixedEmbeddingComposite(qpu, self.embedding)
        sampleset = sampler.sample(self.bqm,
                                   return_embedding=True,
                                   **self.params)

        # resolve it before we mangle with it
        sampleset.info['problem_id']
        # change solver to unstructured to test solver validation
        sampler.child.solver = unstructured_solver_mock

        # ensure `from_bqm_sampleset` adapter fails on unstructured solver
        with self.assertRaises(TypeError):
            from_bqm_sampleset(self.bqm,
                               sampleset,
                               sampler,
                               params=self.params)
コード例 #4
0
    def test_problem_label_in_sampleset(self):
        """All data adapters should propagate problem label."""

        # sample bqm -> sampleset
        qpu = DWaveSampler()
        sampler = FixedEmbeddingComposite(qpu, self.embedding)
        sampleset = sampler.sample(self.bqm, label=self.label, **self.params)

        # ensure `from_bqm_sampleset` adapter propagates label
        data = from_bqm_sampleset(self.bqm,
                                  sampleset,
                                  sampler,
                                  params=self.params)
        self.assertEqual(data['details']['label'], self.label)
コード例 #5
0
    def _test_from_bqm_sampleset(self, bqm):
        # sample
        qpu = DWaveSampler()
        sampler = FixedEmbeddingComposite(qpu, self.embedding)
        sampleset = sampler.sample(bqm,
                                   return_embedding=True,
                                   chain_strength=self.chain_strength,
                                   **self.params)

        # convert
        data = from_bqm_sampleset(bqm, sampleset, sampler, params=self.params)

        # construct (unembedded) response with chain breaks resolved
        # NOTE: for bqm/sampleset adapter, this is the best we can expect :(

        # inverse the embedding
        var_to_idx = {var: idx for idx, var in enumerate(sampleset.variables)}
        unembedding = {
            q: var_to_idx[v]
            for v, qs in self.embedding.items() for q in qs
        }

        # embed sampleset
        solutions_without_chain_breaks = [[
            int(sample[unembedding[q]]) if q in unembedding else val
            for q, val in enumerate(solution)
        ] for solution, sample in zip(self.response['solutions'],
                                      sampleset.record.sample)]

        with mock.patch.dict(self.response._result,
                             {'solutions': solutions_without_chain_breaks}):

            # validate data encoding
            self.verify_data_encoding(problem=self.problem,
                                      response=self.response,
                                      solver=self.solver,
                                      params=self.params,
                                      data=data,
                                      embedding_context=self.embedding_context)