Beispiel #1
0
 def test_default_fold(self):
     bqm = dimod.BinaryQuadraticModel({'a': 1}, {}, 0, dimod.SPIN)
     states = States(
         State.from_sample(min_sample(bqm), bqm),  # energy: -1
         State.from_sample(max_sample(bqm), bqm),  # energy: +1
     )
     best = ArgMin().run(states).result()
     self.assertEqual(best.samples.first.energy, -1)
Beispiel #2
0
    def test_basic(self):
        bqm = dimod.BinaryQuadraticModel({}, {'ab': 1, 'bc': -1, 'ca': 1}, 0, dimod.SPIN)
        state = State.from_sample(min_sample(bqm), bqm)
        antistate = State.from_sample(max_sample(bqm), bqm)

        result = GreedyPathMerge().run(States(state, antistate)).result()

        self.assertEqual(result.samples.first.energy, -3.0)
Beispiel #3
0
 def test_custom_fold(self):
     bqm = dimod.BinaryQuadraticModel({'a': 1}, {}, 0, dimod.SPIN)
     states = States(
         State.from_sample(min_sample(bqm), bqm),  # energy: -1
         State.from_sample(max_sample(bqm), bqm),  # energy: +1
     )
     fold = ArgMin(key=lambda s: -s.samples.first.energy)
     best = fold.run(states).result()
     self.assertEqual(best.samples.first.energy, 1)
Beispiel #4
0
    def test_custom_key(self):
        """Custom key function works, here best state has the highest energy."""

        bqm = dimod.BinaryQuadraticModel({'a': 1}, {}, 0, dimod.SPIN)
        states = States(
            State.from_sample(min_sample(bqm), bqm),  # energy: -1
            State.from_sample(max_sample(bqm), bqm),  # energy: +1
        )

        tracker = TrackMin(key=lambda s: -s.samples.first.energy)
        for state in states:
            tracker.run(state).result()
        self.assertEqual(tracker.best.samples.first.energy, +1)
Beispiel #5
0
    def test_default_tracking(self):
        """Best seen state is kept (default: state with sampleset with the lowest energy)"""

        bqm = dimod.BinaryQuadraticModel({'a': 1}, {}, 0, dimod.SPIN)
        min_state = State.from_sample(min_sample(bqm), bqm)  # energy: -1
        max_state = State.from_sample(max_sample(bqm), bqm)  # energy: +1

        tracker = TrackMin()
        _ = tracker.run(max_state).result()
        self.assertEqual(tracker.best.samples.first.energy, +1)
        _ = tracker.run(min_state).result()
        self.assertEqual(tracker.best.samples.first.energy, -1)
        _ = tracker.run(max_state).result()
        self.assertEqual(tracker.best.samples.first.energy, -1)