def test_sample_instantiation(self): # Check that graph related values have not been instantiated sampler = LazyFixedEmbeddingComposite(MockDWaveSampler()) self.assertIsNone(sampler.embedding) self.assertIsNone(sampler.nodelist) self.assertIsNone(sampler.edgelist) self.assertIsNone(sampler.adjacency) # Set up an and_gate BQM and sample Q = { ('a', 'a'): 0.0, ('c', 'c'): 6.0, ('b', 'b'): 0.0, ('b', 'a'): 2.0, ('c', 'a'): -4.0, ('c', 'b'): -4.0 } sampler.sample_qubo(Q) # Check that values have been populated self.assertIsNotNone(sampler.embedding) self.assertEqual(sampler.nodelist, ['a', 'b', 'c']) self.assertEqual(sampler.edgelist, [('a', 'b'), ('a', 'c'), ('b', 'c')]) self.assertEqual(sampler.adjacency, { 'a': {'b', 'c'}, 'b': {'a', 'c'}, 'c': {'a', 'b'} })
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)
def test_sparse_qubo(self): # There is no relationship between nodes 2 and 3 Q = {(1, 1): 1, (2, 2): 2, (3, 3): 3, (1, 2): 4, (1, 3): 6} sampler = LazyFixedEmbeddingComposite(MockDWaveSampler()) response = sampler.sample_qubo(Q) # Check embedding self.assertIsNotNone(sampler.embedding) self.assertEqual(sampler.nodelist, [1, 2, 3]) self.assertEqual(sampler.edgelist, [(1, 2), (1, 3)]) # Check that at least one response was found self.assertGreaterEqual(len(response), 1)
def test_qubo(self): Q = {(1, 1): 1, (2, 2): 2, (3, 3): 3, (1, 2): 4, (2, 3): 5, (1, 3): 6} sampler = LazyFixedEmbeddingComposite(MockDWaveSampler()) response = sampler.sample_qubo(Q) # Check embedding self.assertIsNotNone(sampler.embedding) self.assertEqual(sampler.nodelist, [1, 2, 3]) self.assertEqual(sampler.edgelist, [(1, 2), (1, 3), (2, 3)]) self.assertEqual(sampler.adjacency, {1: {2, 3}, 2: {1, 3}, 3: {1, 2}}) # Check that at least one response was found self.assertGreaterEqual(len(response), 1)
# Copyright 2020 D-Wave Systems, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from dwave.system.samplers import DWaveSampler from dwave.system.composites import LazyFixedEmbeddingComposite # Set up the QUBO. Start with the equations from the slides: chainstrength = 4 numruns = 100 Q = {(0, 0): 2, (0, 1): -2, (0, 2): -2, (1, 2): 2} sampler = LazyFixedEmbeddingComposite(DWaveSampler()) response = sampler.sample_qubo(Q, chain_strength=chainstrength, num_reads=numruns) print(sampler.properties['embedding']) for smpl, energy in response.data(['sample', 'energy']): print(smpl, energy)