コード例 #1
0
    def test_chain_break_method_customization(self):
        sampler = LazyFixedEmbeddingComposite(MockDWaveSampler())

        with mock.patch('dwave.system.composites.embedding.unembed_sampleset'
                        ) as mock_unembed:
            sampler.sample_ising({'a': 1}, {},
                                 chain_break_method=chain_breaks.discard)

            # assert chain_break_method propagated to unembed_sampleset
            __, kwargs = mock_unembed.call_args
            self.assertEqual(kwargs['chain_break_method'],
                             chain_breaks.discard)
コード例 #2
0
    def test_chain_break_method_customization(self):
        sampler = LazyFixedEmbeddingComposite(MockDWaveSampler())

        def mock_unembed(*args, **kwargs):
            self.assertIn('chain_break_method', kwargs)
            self.assertEqual(kwargs['chain_break_method'], chain_breaks.discard)
            mock_unembed.call_count += 1
            return dwave.embedding.unembed_sampleset(*args, **kwargs)

        mock_unembed.call_count = 0

        with mock.patch('dwave.system.composites.embedding.unembed_sampleset', mock_unembed):
            sampler.sample_ising({'a': 1}, {}, chain_break_method=chain_breaks.discard).resolve()

        self.assertEqual(mock_unembed.call_count, 1)
コード例 #3
0
    def test_ising_sample(self):
        h = {'a': 1, 'b': -2}
        J = {('a', 'b'): -3}
        sampler = LazyFixedEmbeddingComposite(MockDWaveSampler())
        response = sampler.sample_ising(h, J)

        # Check that at least one response was found
        self.assertGreaterEqual(len(response), 1)
コード例 #4
0
    def test_same_embedding(self):
        sampler = LazyFixedEmbeddingComposite(MockDWaveSampler())

        # Set up Ising and sample
        h = {'a': 1, 'b': 1, 'c': 1}
        J = {('a', 'b'): 3, ('b', 'c'): -2, ('a', 'c'): 1}
        sampler.sample_ising(h, J)

        # Store embedding
        prev_embedding = sampler.embedding

        # Set up QUBO of an or_gate
        Q = {('a', 'a'): 2.0, ('c', 'c'): 2.0, ('b', 'b'): 2.0, ('b', 'a'): 2.0, ('c', 'a'): -4.0, ('c', 'b'): -4.0}
        sampler.sample_qubo(Q)

        # Check that the same embedding is used
        self.assertEqual(sampler.embedding, prev_embedding)
コード例 #5
0
    def test_ising(self):
        h = {0: 11, 5: 2}
        J = {(0, 5): -8}
        sampler = LazyFixedEmbeddingComposite(MockDWaveSampler())
        response = sampler.sample_ising(h, J)

        # Check embedding
        self.assertIsNotNone(sampler.embedding)
        self.assertEqual(sampler.nodelist, [0, 5])
        self.assertEqual(sampler.edgelist, [(0, 5)])
        self.assertEqual(sampler.adjacency, {0: {5}, 5: {0}})

        # Check that at least one response was found
        self.assertGreaterEqual(len(response), 1)