Exemplo n.º 1
0
    def test_deprecated_sweeps_name(self):
        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': 1})
        sampler = Neal()

        with warnings.catch_warnings():
            warnings.simplefilter("error")

            with self.assertRaises(DeprecationWarning):
                sampler.sample(bqm, sweeps=10)
Exemplo n.º 2
0
    def test_soft_num_reads(self):
        """Number of reads adapts to initial_states size, if provided."""

        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1, 'bc': 1, 'ac': 1})
        init = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': 1, 'c': 1},
                                                 {'a': -1, 'b': -1, 'c': -1}], bqm)
        sampler = Neal()

        # default num_reads == 1
        self.assertEqual(len(sampler.sample(bqm)), 1)
        self.assertEqual(len(sampler.sample(bqm, initial_states_generator="random")), 1)

        # with initial_states, num_reads == len(initial_states)
        self.assertEqual(len(sampler.sample(bqm, initial_states=init)), 2)

        # ... but explicit truncation works too
        self.assertEqual(len(sampler.sample(bqm, initial_states=init, num_reads=1)), 1)

        # if num_reads explicitly given together with initial_states, they are expanded
        self.assertEqual(len(sampler.sample(bqm, initial_states=init, num_reads=3)), 3)

        # if num_reads explicitly given together without initial_states, they are generated
        self.assertEqual(len(sampler.sample(bqm, num_reads=4)), 4)
Exemplo n.º 3
0
    def test_job_shop_scheduling_with_linear(self):
        # Set up a job shop scheduling BQM
        #
        # Provide hardcode version of the bqm of "jobs"
        #   jobs = {'b': [(1,1), (3,1)],
        #           'o': [(2,2), (4,1)],
        #           'g': [(1,2)]}
        #
        #   There are three jobs: 'b', 'o', 'g'
        #   Each tuple represents a task that runs on a particular machine for a given amount of
        #   time. I.e. (machine_id, duration_on_machine)
        #
        #   Variables below are labelled as '<job_name>_<task_index>,<task_start_time>'.
        linear = {
            'b_0,0': -2.0,
            'b_0,1': -2.0,
            'b_0,2': -2.0,
            'b_0,3': -2.0,
            'b_1,0': 0.125,
            'b_1,1': -1.5,
            'b_1,2': 0.0,
            'g_0,0': -1.875,
            'g_0,1': -1.5,
            'g_0,2': 0.0,
            'o_0,0': -2.0,
            'o_0,1': -2.0,
            'o_0,2': -2.0,
            'o_1,0': 0.03125,
            'o_1,1': -1.875,
            'o_1,2': -1.5,
            'o_1,3': 0.0
        }

        quadratic = {
            ('b_0,0', 'g_0,0'): 4,
            ('b_0,1', 'b_0,0'): 4.0,
            ('b_0,1', 'g_0,0'): 2,
            ('b_0,2', 'b_0,0'): 4.0,
            ('b_0,2', 'b_0,1'): 4.0,
            ('b_0,2', 'b_1,2'): 2,
            ('b_0,2', 'g_0,1'): 2,
            ('b_0,2', 'g_0,2'): 4,
            ('b_0,3', 'b_0,0'): 4.0,
            ('b_0,3', 'b_0,1'): 4.0,
            ('b_0,3', 'b_0,2'): 4.0,
            ('b_0,3', 'b_1,2'): 2,
            ('b_0,3', 'g_0,2'): 2,
            ('b_1,1', 'b_0,1'): 2,
            ('b_1,1', 'b_0,2'): 2,
            ('b_1,1', 'b_0,3'): 2,
            ('b_1,1', 'b_1,2'): 4.0,
            ('g_0,1', 'b_0,1'): 4,
            ('g_0,1', 'g_0,0'): 4.0,
            ('g_0,2', 'g_0,0'): 4.0,
            ('g_0,2', 'g_0,1'): 4.0,
            ('o_0,0', 'o_1,1'): 2,
            ('o_0,1', 'o_0,0'): 4.0,
            ('o_0,1', 'o_1,1'): 2,
            ('o_0,1', 'o_1,2'): 2,
            ('o_0,2', 'o_0,0'): 4.0,
            ('o_0,2', 'o_0,1'): 4.0,
            ('o_0,2', 'o_1,1'): 2,
            ('o_1,2', 'o_0,2'): 2,
            ('o_1,2', 'o_1,1'): 4.0,
            ('o_1,3', 'o_0,2'): 2,
            ('o_1,3', 'o_1,1'): 4.0,
            ('o_1,3', 'o_1,2'): 4.0
        }

        jss_bqm = dimod.BinaryQuadraticModel(linear, quadratic, 9.0,
                                             dimod.BINARY)

        # Optimal energy
        optimal_solution = {
            'b_0,0': 1,
            'b_0,1': 0,
            'b_0,2': 0,
            'b_0,3': 0,
            'b_1,0': 0,
            'b_1,1': 1,
            'b_1,2': 0,
            'b_1,3': 0,
            'g_0,0': 0,
            'g_0,1': 1,
            'g_0,2': 0,
            'g_0,3': 0,
            'o_0,0': 1,
            'o_0,1': 0,
            'o_0,2': 0,
            'o_0,3': 0,
            'o_1,0': 0,
            'o_1,1': 0,
            'o_1,2': 1,
            'o_1,3': 0
        }
        optimal_energy = jss_bqm.energy(optimal_solution)  # Evaluates to 0.5

        # Get heuristic solution
        sampler = Neal()
        response = sampler.sample(jss_bqm, beta_schedule_type="linear")
        _, response_energy, _ = next(response.data())

        # Compare energies
        threshold = 0.1  # Arbitrary threshold
        self.assertLess(response_energy, optimal_energy + threshold)
Exemplo n.º 4
0
    def test_initial_states_generator(self):
        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1, 'bc': 1, 'ac': 1})
        init = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': 1, 'c': 1},
                                                 {'a': -1, 'b': -1, 'c': -1}], bqm)
        sampler = Neal()

        # 2 fixed initial state, 8 random
        resp = sampler.sample(bqm, initial_states=init, num_reads=10)
        self.assertEqual(len(resp), 10)

        # 2 fixed initial states, 8 random, explicit
        resp = sampler.sample(bqm, initial_states=init, initial_states_generator='random', num_reads=10)
        self.assertEqual(len(resp), 10)

        # all random
        resp = sampler.sample(bqm, initial_states_generator='random', num_reads=10)
        self.assertEqual(len(resp), 10)

        # all random
        resp = sampler.sample(bqm, num_reads=10)
        self.assertEqual(len(resp), 10)


        # zero-length init states in tuple format, extended by random samples
        zero_init_tuple = (np.empty((0, 3)), None)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            resp = sampler.sample(bqm, initial_states=zero_init_tuple, num_reads=10)
        self.assertEqual(len(resp), 10)

        # explicit None for initial_states should use one random init state
        resp = sampler.sample(bqm, initial_states=None)
        self.assertEqual(len(resp), 1)

        # initial_states truncated to num_reads?
        resp = sampler.sample(bqm, initial_states=init, initial_states_generator='none', num_reads=1)
        self.assertEqual(len(resp), 1)

        resp = sampler.sample(bqm, initial_states=init, initial_states_generator='tile', num_reads=1)
        self.assertEqual(len(resp), 1)

        resp = sampler.sample(bqm, initial_states=init, initial_states_generator='random', num_reads=1)
        self.assertEqual(len(resp), 1)


        # 2 fixed initial states, repeated 5 times
        resp = sampler.sample(bqm, initial_states=init, initial_states_generator='tile', num_reads=10)
        self.assertEqual(len(resp), 10)

        # can't tile empty states
        with self.assertRaises(ValueError):
            resp = sampler.sample(bqm, initial_states_generator='tile', num_reads=10)

        # not enough initial states
        with self.assertRaises(ValueError):
            resp = sampler.sample(bqm, initial_states_generator='none', num_reads=3)

        # initial_states incompatible with the bqm
        init = dimod.SampleSet.from_samples({'a': 1, 'b': 1}, vartype='SPIN', energy=0)
        with self.assertRaises(ValueError):
            resp = sampler.sample(bqm, initial_states=init)