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_construction(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) dimod.testing.assert_sampler_api(sampler) dimod.testing.assert_composite_api(sampler) self.assertEqual(sampler.inputs, []) self.assertEqual(sampler.outputs, [])
def test_unstructured_sampler(self): with self.assertRaises(ValueError): dimod.child_structure_dfs(dimod.NullSampler()) nested = dimod.TrackingComposite(dimod.NullSampler()) with self.assertRaises(ValueError): dimod.child_structure_dfs(nested)
def test_sample_qubo(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) Q = {('a', 'b'): -1} ss = sampler.sample_qubo(Q) self.assertEqual(sampler.input, dict(Q=Q)) self.assertEqual(sampler.output, ss)
def test_missing_inputs(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) with self.assertRaises(ValueError): sampler.input with self.assertRaises(ValueError): sampler.output
def test_sample_ising_copy_false(self): sampler = dimod.TrackingComposite(dimod.ExactSolver(), copy=False) h0 = {'a': -1} J0 = {('a', 'b'): -1} ss0 = sampler.sample_ising(h0, J0) self.assertIs(sampler.input['h'], h0) self.assertIs(sampler.output, ss0)
def test_sample(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) bqm = dimod.BinaryQuadraticModel.from_ising({'a': -1}, {}) ss = sampler.sample(bqm) self.assertEqual(sampler.input, dict(bqm=bqm)) self.assertEqual(sampler.output, ss)
def test_sample_ising_kwargs(self): sampler = dimod.TrackingComposite(dimod.RandomSampler()) h = {'a': -1} J = {('a', 'b'): -1} ss = sampler.sample_ising(h, J, num_reads=5) self.assertEqual(sampler.input, dict(h=h, J=J, num_reads=5)) self.assertEqual(sampler.output, ss)
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_clear(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) h0 = {'a': -1} J0 = {('a', 'b'): -1} ss0 = sampler.sample_ising(h0, J0) h1 = {'b': -1} J1 = {('b', 'c'): 2} ss1 = sampler.sample_ising(h1, J1) sampler.clear() self.assertEqual(sampler.inputs, []) self.assertEqual(sampler.outputs, [])
def test_sample_ising(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) h0 = {'a': -1} J0 = {('a', 'b'): -1} ss0 = sampler.sample_ising(h0, J0) h1 = {'b': -1} J1 = {('b', 'c'): 2} ss1 = sampler.sample_ising(h1, J1) self.assertEqual(sampler.input, dict(h=h1, J=J1)) self.assertEqual(sampler.output, ss1) self.assertEqual(sampler.inputs, [dict(h=h0, J=J0), dict(h=h1, J=J1)]) self.assertEqual(sampler.outputs, [ss0, ss1])
def main(sampler_type, region, show, inspect): if sampler_type is None: print("No solver selected, defaulting to hybrid") sampler_type = 'hybrid' # get the appropriate signed social network G = global_signed_social_network(region=region) # choose solver and any tuning parameters needed if sampler_type == 'cpu': params = dict(num_reads=100) sampler = SimulatedAnnealingSampler() elif sampler_type == 'hybrid': params = dict() sampler = LeapHybridSampler() elif sampler_type == 'qpu': params = dict( num_reads=100, chain_strength=2.0, ) sampler = dimod.TrackingComposite(EmbeddingComposite(DWaveSampler())) else: raise RuntimeError("unknown solver type") # use the chosen sampler (passing in the parameters) edges, colors = dnx.structural_imbalance( G, sampler, label='Example - Structural Imbalance', **params) if inspect and sampler_type == 'qpu': dwave.inspector.show(sampler.output) print("Found", len(edges), 'violations out of', len(G.edges), 'edges') draw_social_network(G, colors) if show: plt.show() else: filename = 'structural imbalance {} {}.png'.format( sampler_type, region) plt.savefig(filename, facecolor='white') plt.clf()
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]) # check that everything was restored properly dtest.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_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_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_bias_ranges(self): bqm = dimod.BQM.from_ising({ 'a': -4.0, 'b': -4.0 }, {('a', 'b'): 4}, 1.5) with self.assertWarns(DeprecationWarning): sampler = ScaleComposite( dimod.TrackingComposite(dimod.ExactSolver())) sampleset = sampler.sample(bqm, bias_range=[-3, 3], quadratic_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'): 2}, .75))
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])
import dimod class TestConstruction(unittest.TestCase): def test_construction(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) dimod.testing.assert_sampler_api(sampler) dimod.testing.assert_composite_api(sampler) self.assertEqual(sampler.inputs, []) self.assertEqual(sampler.outputs, []) @dimod.testing.load_sampler_bqm_tests( dimod.TrackingComposite(dimod.ExactSolver())) class TestSample(unittest.TestCase): def test_clear(self): sampler = dimod.TrackingComposite(dimod.ExactSolver()) h0 = {'a': -1} J0 = {('a', 'b'): -1} ss0 = sampler.sample_ising(h0, J0) h1 = {'b': -1} J1 = {('b', 'c'): 2} ss1 = sampler.sample_ising(h1, J1) sampler.clear() self.assertEqual(sampler.inputs, [])
def setUp(self): # get a sampler that accepts an initial_state base = dimod.NullSampler(parameters=['initial_state']) self.tracker = dimod.TrackingComposite(base)