Ejemplo n.º 1
0
    def test_bimodal_cluster_sampling_statistics(self):
        bqm = dimod.BQM.from_qubo({'ab': 1, 'bd': 1, 'dc': 1, 'ca': 1})
        nodes = sorted(bqm.variables)

        s1 = State.from_samples(dict(zip(nodes, [0, 1, 0, 0])), bqm)
        s2 = State.from_samples(dict(zip(nodes, [0, 0, 1, 0])), bqm)

        exp1 = SampleSet.from_samples_bqm(dict(zip(nodes, [0, 0, 0, 0])), bqm)
        exp2 = SampleSet.from_samples_bqm(dict(zip(nodes, [0, 1, 1, 0])), bqm)

        icm = IsoenergeticClusterMove(seed=None)
        inp = States(s1, s2)
        exp = [exp1, exp2]

        # split between [exp1, exp2] and [exp2, exp1] as output samples
        # should be ~50%
        cnt = 0
        n = 100
        for _ in range(n):
            res = icm.run(inp).result()
            r1, r2 = pluck(res, 'samples')

            # test responses are valid
            self.assertIn(r1, exp)
            self.assertIn(r2, exp)

            # verify total samples energy doesn't change after ICM
            self.assertEqual(self.total_energy(inp), self.total_energy(res))

            # count responses
            if r1 == exp1 and r2 == exp2:
                cnt += 1

        self.assertLess(cnt, 0.75 * n)
        self.assertGreater(cnt, 0.25 * n)
Ejemplo n.º 2
0
 def test_from_problem(self):
     sample = {0: 1, 1: 0}
     bqm = dimod.BinaryQuadraticModel({0: 1, 1: 2}, {}, 0.0, 'BINARY')
     self.assertEqual(State.from_problem(bqm).samples.first.energy, 0.0)
     self.assertEqual(State.from_problem(bqm, beta=0.5).beta, 0.5)
     self.assertEqual(State.from_problem(bqm, samples=hybrid.utils.max_sample).samples.first.energy, 3.0)
     self.assertEqual(State.from_problem(bqm, samples=sample), State.from_samples(sample, bqm))
     self.assertEqual(State.from_problem(bqm, samples=[sample]), State.from_samples([sample], bqm))
Ejemplo n.º 3
0
 def test_from_samples(self):
     s1 = [0, 1]
     s2 = {0: 1, 1: 0}
     bqm = dimod.BinaryQuadraticModel({0: 1, 1: 2}, {}, 0.0, 'BINARY')
     self.assertEqual(State.from_sample(s1, bqm).samples.first.energy, 2.0)
     self.assertEqual(State.from_sample(s2, bqm).samples.first.energy, 1.0)
     self.assertEqual(State.from_samples([s1, s1], bqm).samples.first.energy, 2.0)
     self.assertEqual(State.from_samples([s2, s2], bqm).samples.first.energy, 1.0)
     self.assertEqual(State.from_samples([sample_as_dict(s1), s2], bqm).samples.first.energy, 1.0)
Ejemplo n.º 4
0
    def test_ising_triangle_flip(self):
        bqm = dimod.BQM.from_ising({}, {'ab': 1, 'bc': 1, 'ca': 1})
        s1 = State.from_samples({'a': -1, 'b': +1, 'c': +1}, bqm)
        s2 = State.from_samples({'a': +1, 'b': -1, 'c': +1}, bqm)

        icm = IsoenergeticClusterMove()
        inp = States(s1, s2)
        res = icm.run(inp).result()

        # Expected: ('a', 'b') identified as (the sole) cluster, selected,
        # resulting in variables {'a', 'b'} flipped. Effectively, input states
        # are simply swapped.
        self.assertEqual(res[0].samples, s2.samples)
        self.assertEqual(res[1].samples, s1.samples)

        # verify total samples energy doesn't change after ICM
        self.assertEqual(self.total_energy(inp), self.total_energy(res))
Ejemplo n.º 5
0
    def test_small_lattice(self):
        graph = nx.generators.lattice.grid_2d_graph(5, 5)
        bqm = dimod.generators.uniform(graph,
                                       vartype=dimod.BINARY,
                                       low=1,
                                       high=1)
        nodes = sorted(bqm.variables)

        s1 = State.from_samples(
            dict(
                zip(nodes, [
                    0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
                    1, 0, 1, 0, 0
                ])), bqm)
        s2 = State.from_samples(
            dict(
                zip(nodes, [
                    0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,
                    1, 0, 0, 1, 0
                ])), bqm)

        exp1 = SampleSet.from_samples_bqm(
            dict(
                zip(nodes, [
                    0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,
                    1, 0, 0, 1, 0
                ])), bqm)
        exp2 = SampleSet.from_samples_bqm(
            dict(
                zip(nodes, [
                    0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
                    1, 0, 1, 0, 0
                ])), bqm)

        icm = IsoenergeticClusterMove(seed=1234)
        inp = States(s1, s2)
        res = icm.run(inp).result()

        self.assertEqual(res[0].samples, exp1)
        self.assertEqual(res[1].samples, exp2)

        # verify total samples energy doesn't change after ICM
        self.assertEqual(self.total_energy(inp), self.total_energy(res))
Ejemplo n.º 6
0
    def test_multiple(self):
        bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, dimod.SPIN)

        states = States(State.from_sample({'a': 1, 'b': -1}, bqm),
                        State.from_sample({'a': -1, 'b': 1}, bqm))

        expected = State.from_samples([{'a': 1, 'b': -1}, {'a': -1, 'b': 1}], bqm)

        state = MergeSamples().run(states).result()

        self.assertEqual(state, expected)
Ejemplo n.º 7
0
    def test_default(self):
        """First subsample is combined with the first sample."""

        state = State.from_samples(self.samples, self.problem).updated(
            subproblem=self.subproblem,
            subsamples=SampleSet.from_samples_bqm(self.subsamples, self.subproblem))

        nextstate = SplatComposer().next(state)

        sample = {'a': 1, 'b': -1, 'c': 1}
        self.assertEqual(nextstate.samples,
                         SampleSet.from_samples_bqm(sample, self.problem))
Ejemplo n.º 8
0
    def test_default(self):
        """All subsamples are combined with all the samples."""

        state = State.from_samples(self.samples, self.problem).updated(
            subproblem=self.subproblem,
            subsamples=SampleSet.from_samples_bqm(self.subsamples,
                                                  self.subproblem))

        nextstate = SplatComposer().next(state)

        self.assertEqual(
            nextstate.samples,
            SampleSet.from_samples_bqm(self.composed, self.problem))
Ejemplo n.º 9
0
    def test_sampling_mode(self):
        bqm = dimod.BinaryQuadraticModel.from_ising({'a': 0}, {})

        init = State.from_samples([{'a': -1}, {'a': 1}], bqm)

        # variable not fixed
        new = RoofDualityDecomposer(sampling_mode=True).run(init).result()
        self.assertEqual(new.problem, bqm)
        self.assertEqual(new.subproblem, bqm)
        self.assertEqual(new.samples, init.samples)

        # variable fixed
        new = RoofDualityDecomposer(sampling_mode=False).run(init).result()
        self.assertEqual(new.problem, bqm)
        self.assertEqual(new.subproblem,
                         dimod.BinaryQuadraticModel.from_ising({}, {}))
        self.assertTrue(len(set(new.samples.record.sample.flatten())), 1)
Ejemplo n.º 10
0
    def test_simple(self):
        "Two output states created for two input samples, in correct order."

        bqm = dimod.BQM.from_ising({}, {'ab': 1})

        inp = State.from_samples([{'a': 1, 'b': 1}, {'a': -1, 'b': 1}], bqm)

        exp = States(State.from_sample({
            'a': 1,
            'b': 1
        }, bqm), State.from_sample({
            'a': -1,
            'b': 1
        }, bqm))

        out = ExplodeSamples().run(inp).result()

        self.assertEqual(out, exp)