def test_ignored_interactions(self): bqm = dimod.BQM.from_ising({ 'a': -4.0, 'b': -4.0 }, { ('a', 'b'): 3.2, ('b', 'c'): 1 }, 1.5) with self.assertWarns(DeprecationWarning): sampler = ScaleComposite( dimod.TrackingComposite(dimod.ExactSolver())) sampleset = sampler.sample(bqm, scalar=.5, ignored_interactions=[('b', 'c')]) # check that everything was restored properly dimod.testing.assert_sampleset_energies(sampleset, bqm) self.assertEqual( sampler.child.input['bqm'], dimod.BQM.from_ising({ 'a': -2.0, 'b': -2.0 }, { 'ab': 1.6, 'bc': 1 }, .75))
def test_sample_bias_range(self): linear = {'a': -4.0, 'b': -4.0} quadratic = {('a', 'b'): 3.2} ignored_variables, ignored_interactions = _check_params(None, None) bqm = BinaryQuadraticModel.from_ising(linear, quadratic) comp_parameters = dict(ignored_interactions=ignored_interactions, ignored_variables=ignored_variables, bias_range=2.) sampler = ScaleComposite( ScalingChecker(ExactSolver(), bqm=bqm, **comp_parameters)) response = sampler.sample(bqm, **comp_parameters) self.assertAlmostEqual(response.first.energy, -4.8)
def test_sample_ising_quadratic(self): linear = {'a': -4.0, 'b': -4.0} quadratic = {('a', 'b'): 3.2} offset = 5 ignored_variables, ignored_interactions = _check_params(None, None) comp_parameters = dict(ignored_interactions=ignored_interactions, ignored_variables=ignored_variables) sampler = ScaleComposite(ScalingChecker(ExactSolver(), h=linear, J=quadratic, offset=offset, **comp_parameters)) response = sampler.sample_ising(linear, quadratic, offset=offset) self.assertAlmostEqual(response.first.energy, 0.2)
def test_sample_ignore_variables(self): linear = {'a': -4.0, 'b': -4.0} quadratic = {('a', 'b'): 3.2} offset = 5 bqm = BinaryQuadraticModel.from_ising(linear, quadratic, offset=offset) ignored_variables, ignored_interactions = _check_params( ['a'], None) comp_parameters = dict(ignored_interactions=ignored_interactions, ignored_variables=ignored_variables, ignore_offset=True, scalar=0.5) sampler = ScaleComposite(ScalingChecker(ExactSolver(), h=linear, J=quadratic, offset=offset, **comp_parameters)) response = sampler.sample(bqm, **comp_parameters) self.assertAlmostEqual(response.first.energy, 0.2)
def test_sample_hising_ignore_variables(self): linear = {'a': -4.0, 'b': -4.0, 'c': -4.0} quadratic = {('a', 'b', 'c'): 3.2} offset = 5 ignored_variables, ignored_interactions = _check_params( ['a'], None) comp_parameters = dict(ignored_interactions=ignored_interactions, ignored_variables=ignored_variables, ignore_offset=True, scalar=0.5) sampler = ScaleComposite(ScalingChecker(HigherOrderComposite( ExactSolver()), h=linear, J=quadratic, offset=offset, **comp_parameters)) response = sampler.sample_ising(linear, quadratic, offset=offset, **comp_parameters) self.assertAlmostEqual(response.first.energy, -3.8)
def test_ignored_offset(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, scalar=.5, ignore_offset=True) # check that everything was restored properly dimod.testing.assert_sampleset_energies(sampleset, bqm) self.assertEqual( sampler.child.input['bqm'], dimod.BQM.from_ising({ 'a': -2.0, 'b': -2.0 }, {('a', 'b'): 1.6}, 1.5))
def test_sample_hising_ranges(self): linear = {'a': -4.0, 'b': -4.0, 'c': -4.0} quadratic = {('a', 'b', 'c'): 3.2} offset = 5 ignored_variables, ignored_interactions = _check_params(None, None) comp_parameters = dict(ignored_interactions=ignored_interactions, ignored_variables=ignored_variables, penalty_strength=5., quadratic_range=(-1, 10), bias_range=(-8.0, 5) ) sampler = ScaleComposite( ScalingChecker(HigherOrderComposite(ExactSolver()), h=linear, J=quadratic, offset=offset, **comp_parameters)) response = sampler.sample_ising(linear, quadratic, offset=offset, **comp_parameters) self.assertAlmostEqual(response.first.energy, -3.8)
def test_bias_range(self): bqm = dimod.BQM.from_ising({ 'a': -4.0, 'b': -4.0 }, {('a', 'b'): 3.2}, 1.5) with self.assertWarns(DeprecationWarning): sampler = ScaleComposite( dimod.TrackingComposite(dimod.ExactSolver())) sampleset = sampler.sample(bqm, bias_range=[-2, 2]) # check that everything was restored properly dimod.testing.assert_sampleset_energies(sampleset, bqm) self.assertEqual( sampler.child.input['bqm'], dimod.BQM.from_ising({ 'a': -2.0, 'b': -2.0 }, {('a', 'b'): 1.6}, .75))
def test_instantiation_smoketest(self): sampler = ScaleComposite(ExactSolver()) dtest.assert_sampler_api(sampler)
def test_api(self): with self.assertWarns(DeprecationWarning): sampler = ScaleComposite(dimod.ExactSolver()) dimod.testing.assert_sampler_api(sampler)
# import unittest import dimod from dimod import ScaleComposite try: import dwave.preprocessing as preprocessing except ImportError: preprocessing = False if preprocessing: @dimod.testing.load_sampler_bqm_tests(ScaleComposite(dimod.ExactSolver())) @dimod.testing.load_sampler_bqm_tests(ScaleComposite(dimod.NullSampler())) class TestScaleComposite(unittest.TestCase): def test_api(self): with self.assertWarns(DeprecationWarning): sampler = ScaleComposite(dimod.ExactSolver()) dimod.testing.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) with self.assertWarns(DeprecationWarning): sampler = ScaleComposite(
def test_api(self): sampler = ScaleComposite(dimod.ExactSolver()) dimod.testing.assert_sampler_api(sampler)