コード例 #1
0
        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))
コード例 #2
0
    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, [])
コード例 #3
0
ファイル: test_utilities.py プロジェクト: conta877/dimod
    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)
コード例 #4
0
    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)
コード例 #5
0
    def test_missing_inputs(self):
        sampler = dimod.TrackingComposite(dimod.ExactSolver())

        with self.assertRaises(ValueError):
            sampler.input

        with self.assertRaises(ValueError):
            sampler.output
コード例 #6
0
    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)
コード例 #7
0
    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)
コード例 #8
0
    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)
コード例 #9
0
ファイル: test_utilities.py プロジェクト: conta877/dimod
    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)
コード例 #10
0
    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, [])
コード例 #11
0
    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])
コード例 #12
0
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()
コード例 #13
0
    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))
コード例 #14
0
    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)])
コード例 #15
0
ファイル: test_scalecomposite.py プロジェクト: randomir/dimod
    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))
コード例 #16
0
        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))
コード例 #17
0
 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])
コード例 #18
0
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, [])
コード例 #19
0
 def setUp(self):
     # get a sampler that accepts an initial_state
     base = dimod.NullSampler(parameters=['initial_state'])
     self.tracker = dimod.TrackingComposite(base)