def test_smoke(self): nodelist = [0, 1, 2, 3] edgelist = [(0, 1), (1, 2), (2, 3), (0, 3)] child = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) sampler = AutoEmbeddingComposite(child) sampler.sample_ising({}, {(0, 1): -1}) sampler.sample_ising({}, {('a', 0): -1})
def test_embedding_parameters_sample(self): child = dimod.StructureComposite(dimod.NullSampler(), [0, 1], [(0, 1)]) def my_find_embedding(S, T, a): assert a == -1 return {v: [v] for v in set().union(*S)} sampler = EmbeddingComposite(child, find_embedding=my_find_embedding) # nothing breaks sampler.sample_ising({0: -1}, {}, embedding_parameters={'a': -1})
def test_find_embedding_kwarg(self): child = dimod.StructureComposite(dimod.NullSampler(), [0, 1], [(0, 1)]) def my_find_embedding(S, T): # does nothing return {v: [v] for v in set().union(*S)} sampler = EmbeddingComposite(child, find_embedding=my_find_embedding) # nothing breaks sampler.sample_ising({0: -1}, {})
def test_composed_sampler(self): nodelist = list(range(5)) edgelist = list(itertools.combinations(nodelist, 2)) structured_sampler = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) sampler = dimod.TrackingComposite(structured_sampler) structure = dimod.child_structure_dfs(sampler) self.assertEqual(structure.nodelist, nodelist) self.assertEqual(structure.edgelist, edgelist)
def test_broken_find_embedding(self): nodelist = [0, 1, 2, 3] edgelist = [(0, 1), (1, 2), (2, 3), (0, 3)] child = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) def find_embedding(*args, **kwargs): raise NotImplementedError sampler = AutoEmbeddingComposite(child, find_embedding=find_embedding) sampler.sample_ising({}, {(0, 1): -1}) with self.assertRaises(NotImplementedError): sampler.sample_ising({}, {('a', 0): -1})
def test_numpy_variable_labels(self): h = { 0: 0, 1: 1, np.int64(2): 2, np.float(3): 3, fractions.Fraction(4, 1): 4, fractions.Fraction(5, 2): 5, '6': 6 } sampleset = dimod.NullSampler().sample_ising(h, {}) json.dumps(sampleset.to_serializable())
def test_keyer(self): C4 = dnx.chimera_graph(4) nodelist = sorted(C4.nodes) edgelist = sorted(sorted(edge) for edge in C4.edges) child = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) embedding = {0: [49, 53], 1: [52], 2: [50]} sampler = FixedEmbeddingComposite(child, embedding) with self.assertRaises(dwave.embedding.exceptions.MissingChainError): sampler.sample_qubo({(1, 4): 1}) sampler.sample_qubo({(1, 2): 1}).record # sample and resolve future sampler.sample_qubo({(1, 1): 1}).record # sample and resolve future
def test_return_embedding(self): nodelist = [0, 1, 2] edgelist = [(0, 1), (1, 2), (0, 2)] sampler = EmbeddingComposite( dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist)) sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}, return_embedding=True) self.assertIn('embedding', sampleset.info) embedding = sampleset.info['embedding'] self.assertEqual(set(embedding), {'a', 'c'}) # default False sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}) self.assertNotIn('embedding', sampleset.info)
def test_scale_aware_scale_composite(self): nodelist = [0, 1, 2] edgelist = [(0, 1), (1, 2), (0, 2)] embedding = {'a': [0], 'b': [1, 2]} sampler = FixedEmbeddingComposite(dimod.TrackingComposite( ScaleComposite( dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist))), embedding=embedding, scale_aware=True) _ = sampler.sample_ising({'a': 100, 'b': -100}, {'ab': 300}) self.assertIn('ignored_interactions', sampler.child.input) ignored = sampler.child.input['ignored_interactions'] self.assertTrue(ignored == [(1, 2)] or ignored == [(2, 1)])
def test_intermediate_composites(self): child = dimod.StructureComposite(dimod.NullSampler(), [0, 1], [(0, 1)]) intermediate = dimod.TrackingComposite(child) sampler = EmbeddingComposite(intermediate) self.assertEqual(sampler.target_structure.nodelist, [0, 1])
def test_nonempty_bqm(self): bqm = dimod.BinaryQuadraticModel.from_ising({'a': -1}, {'ab': 1}) ss = dimod.NullSampler().sample(bqm) self.assertEqual(len(ss), 0) self.assertEqual(ss.record.sample.shape, (0, 2))
def test_empty_bqm(self): bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN) ss = dimod.NullSampler().sample(bqm) self.assertEqual(len(ss), 0) self.assertEqual(ss.record.sample.shape, (0, 0))
def test_parameters_dict(self): sampler = dimod.NullSampler(parameters={'a': [1.5]}) self.assertEqual(sampler.parameters, {'a': [1.5]})
def test_parameters_iterable(self): sampler = dimod.NullSampler(parameters=['a']) self.assertEqual(sampler.parameters, {'a': []})
def test_construction(self): sampler = dimod.NullSampler() dimod.testing.assert_sampler_api(sampler)
from dimod import BinaryQuadraticModel from dimod import FixedVariableComposite, ExactSolver, RoofDualityComposite from dimod import SampleSet try: import dwave.preprocessing as preprocessing except ImportError: preprocessing = False if preprocessing: @dimod.testing.load_sampler_bqm_tests(FixedVariableComposite(ExactSolver()) ) @dimod.testing.load_sampler_bqm_tests( FixedVariableComposite(dimod.NullSampler())) class TestFixedVariableComposite(unittest.TestCase): def test_instantiation_smoketest(self): with self.assertWarns(DeprecationWarning): sampler = FixedVariableComposite(ExactSolver()) dtest.assert_sampler_api(sampler) def test_sample(self): bqm = BinaryQuadraticModel({ 1: -1.3, 4: -0.5 }, {(1, 4): -0.6}, 0, vartype=Vartype.SPIN)
def setUp(self): # get a sampler that accepts an initial_state base = dimod.NullSampler(parameters=['initial_state']) self.tracker = dimod.TrackingComposite(base)
# 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. import unittest import dimod import dimod.testing as dtest from dwave.preprocessing.composites import ScaleComposite @dtest.load_sampler_bqm_tests(ScaleComposite(dimod.ExactSolver())) @dtest.load_sampler_bqm_tests(ScaleComposite(dimod.NullSampler())) class TestScaleComposite(unittest.TestCase): def test_api(self): sampler = ScaleComposite(dimod.ExactSolver()) dtest.assert_sampler_api(sampler) def test_bias_range(self): bqm = dimod.BQM.from_ising({ 'a': -4.0, 'b': -4.0 }, {('a', 'b'): 3.2}, 1.5) sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver())) sampleset = sampler.sample(bqm, bias_range=[-2, 2])
# See the License for the specific language governing permissions and # limitations under the License. # # ================================================================================================ import unittest import dimod from dimod import ClipComposite from dimod import BinaryQuadraticModel from dimod import ExactSolver, NullSampler @dimod.testing.load_sampler_bqm_tests(ClipComposite(dimod.ExactSolver())) @dimod.testing.load_sampler_bqm_tests(ClipComposite(dimod.NullSampler())) class TestClipCompositeClass(unittest.TestCase): def test_instantiation_smoketest(self): sampler = ClipComposite(NullSampler()) dimod.testing.assert_sampler_api(sampler) def test_no_bounds(self): bqm = BinaryQuadraticModel({ 0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0 }, { (0, 1): -2.0, (0, 2): -5.0,
import unittest import dimod.testing as dtest from dimod.vartypes import Vartype import dimod from dimod import BinaryQuadraticModel from dimod import FixedVariableComposite, ExactSolver, RoofDualityComposite from dimod import SampleSet @dimod.testing.load_sampler_bqm_tests(FixedVariableComposite(ExactSolver())) @dimod.testing.load_sampler_bqm_tests( FixedVariableComposite(dimod.NullSampler())) class TestFixedVariableComposite(unittest.TestCase): def test_instantiation_smoketest(self): sampler = FixedVariableComposite(ExactSolver()) dtest.assert_sampler_api(sampler) def test_sample(self): bqm = BinaryQuadraticModel({ 1: -1.3, 4: -0.5 }, {(1, 4): -0.6}, 0, vartype=Vartype.SPIN) fixed_variables = {1: -1}