def test_advance_no_schedules(self):
        sampler = ReverseAdvanceComposite(MockReverseSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}

        response = sampler.sample_ising(h, J)
        self.assertIn('schedule_index', response.record.dtype.names)
    def test_sample_ising_advance(self):
        sampler = ReverseAdvanceComposite(MockReverseSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}
        schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]],
                     [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]]
        response = sampler.sample_ising(h, J, anneal_schedules=schedules)

        # nothing failed and we got at least two response back for each schedule point
        self.assertGreaterEqual(len(response), 2)
    def test_correct_initial_state_input(self):
        sampler = ReverseAdvanceComposite(MockReverseSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}
        anneal_schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]],
                            [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]]

        with self.assertRaises(TypeError):
            response = sampler.sample_ising(h,
                                            J,
                                            anneal_schedules=anneal_schedules,
                                            initial_state=[1, 1])
    def test_advance_correct_schedules(self):
        sampler = ReverseAdvanceComposite(MockReverseSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}
        anneal_schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]],
                            [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]]
        response = sampler.sample_ising(h,
                                        J,
                                        anneal_schedules=anneal_schedules)

        anneal_schedule_list = response.record.schedule_index

        for schedule_idx in range(len(anneal_schedules)):
            self.assertIn(schedule_idx, anneal_schedule_list)
    def test_combination(self):
        sampler = ReverseBatchStatesComposite(
            ReverseAdvanceComposite(MockReverseSampler()))

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}
        anneal_schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]],
                            [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]]
        initial_states = [{0: 1, 4: 1}, {0: -1, 4: -1}, {0: 1, 4: -1}]
        response = sampler.sample_ising(h,
                                        J,
                                        anneal_schedules=anneal_schedules,
                                        initial_states=initial_states)

        anneal_schedule_list = response.record.schedule_index
        variables = response.variables
        initial_state_list = response.record.initial_state

        for state in initial_states:
            state = [state[var] for var in variables]
            self.assertIn(state, initial_state_list)

        for state_idx in range(len(anneal_schedules)):
            self.assertIn(state_idx, anneal_schedule_list)

        self.assertGreaterEqual(len(response), 6)
Пример #6
0
    def test_correct_initial_state_used(self):
        sampler = ReverseAdvanceComposite(MockReverseSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}
        anneal_schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]],
                            [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]]
        initial = {0: -1, 4: -1}
        response = sampler.sample_ising(h,
                                        J,
                                        anneal_schedules=anneal_schedules,
                                        initial_state=initial)

        vars = response.variables
        for datum in response.data(fields=['initial_state', 'schedule_index']):
            if datum.schedule_index == 0:
                self.assertListEqual([initial[v] for v in vars],
                                     list(datum.initial_state))
            if datum.schedule_index > 1:
                self.assertListEqual([1, -1], list(datum.initial_state))
    def test_correct_initial_state_used_reinit(self):
        sampler = ReverseAdvanceComposite(MockReverseSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}
        anneal_schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]],
                            [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]]
        initial = {0: -1, 4: -1}

        response = sampler.sample_ising(h,
                                        J,
                                        anneal_schedules=anneal_schedules,
                                        initial_state=initial,
                                        reinitialize_state=False)

        vars = response.variables

        init = [initial[v] for v in vars]
        for datum in response.data(fields=['sample', 'initial_state'],
                                   sorted_by=None):
            self.assertListEqual(init, list(datum.initial_state))
            init = [datum.sample[v] for v in vars
                    ]  # sample should be the initial state of the next sample
 def test_instantiation_smoketest_advance(self):
     sampler = ReverseAdvanceComposite(dimod.ExactSolver())
     dtest.assert_sampler_api(sampler)