Example #1
0
 def test_from_subproblem(self):
     subsample = {0: 1, 1: 0}
     bqm = dimod.BinaryQuadraticModel({0: 1, 1: 2}, {}, 0.0, 'BINARY')
     self.assertEqual(State.from_subproblem(bqm).subsamples.first.energy, 0.0)
     self.assertEqual(State.from_subproblem(bqm, beta=0.5).beta, 0.5)
     self.assertEqual(State.from_subproblem(bqm, subsamples=hybrid.utils.max_sample).subsamples.first.energy, 3.0)
     self.assertEqual(State.from_subproblem(bqm, subsamples=subsample), State.from_subsamples(subsample, bqm))
     self.assertEqual(State.from_subproblem(bqm, subsamples=[subsample]), State.from_subsamples([subsample], bqm))
Example #2
0
    def test_custom_qpu_params(self):
        bqm = dimod.BinaryQuadraticModel.from_ising({'a': 1}, {})
        init = State.from_subproblem(bqm)

        # define a mock sampler that exposes some parameters of interest
        mock_sampler = mock.MagicMock()
        mock_sampler.parameters = {
            'num_reads': [],
            'chain_strength': [],
            'future_proof': []
        }

        qpu_params = dict(chain_strength=2, future_proof=True)

        workflow = QPUSubproblemAutoEmbeddingSampler(num_reads=10,
                                                     qpu_sampler=mock_sampler,
                                                     qpu_params=qpu_params)

        # run mock sampling
        workflow.run(init).result()

        # verify mock sampler received custom kwargs
        mock_sampler.sample.assert_called_once_with(bqm,
                                                    num_reads=10,
                                                    **qpu_params)
Example #3
0
    def test_external_embedding_sampler(self):
        bqm = dimod.BinaryQuadraticModel.from_ising({'a': 1}, {})
        init = State.from_subproblem(bqm, embedding={'a': [0]})

        sampler = dimod.StructureComposite(
            SimulatedAnnealingSampler(), nodelist=[0], edgelist=[])

        workflow = QPUSubproblemExternalEmbeddingSampler(qpu_sampler=sampler)

        # run mock sampling
        res = workflow.run(init).result()

        # verify mock sampler received custom kwargs
        self.assertEqual(res.subsamples.first.energy, -1)
Example #4
0
    def test_clique_embedder(self):
        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ca': 1})
        init = State.from_subproblem(bqm)

        sampler = MockDWaveSampler()

        workflow = SubproblemCliqueEmbedder(sampler=sampler)

        # run embedding
        res = workflow.run(init).result()

        # verify mock sampler received custom kwargs
        self.assertIn('embedding', res)
        self.assertEqual(len(res.embedding.keys()), 3)
        # embedding a triangle onto a chimera produces 3 x 2-qubit chains
        self.assertTrue(all(len(e) == 2 for e in res.embedding.values()))