def test_sample_hising_quadratic_range(self):
        linear = {'a': -4.0, 'b': -4.0, 'c': -4.0}
        quadratic = {('a', 'b', 'c'): 3.2}
        offset = 5
        ignored_variables, ignored_interactions = _check_params(None, None)

        comp_parameters = dict(ignored_interactions=ignored_interactions,
                               ignored_variables=ignored_variables,
                               penalty_strength=5.,
                               quadratic_range=(-1, 2)
                               )
        sampler = ScaleComposite(
            ScalingChecker(HigherOrderComposite(ExactSolver()),
                           h=linear,
                           J=quadratic, offset=offset,
                           **comp_parameters))
        response = sampler.sample_ising(linear, quadratic, offset=offset,
                                        **comp_parameters)

        self.assertAlmostEqual(response.first.energy, -3.8)

        comp_parameters = dict(ignored_interactions=ignored_interactions,
                               ignored_variables=ignored_variables,
                               penalty_strength=5.,
                               quadratic_range=(-1, 0.4)
                               )
        sampler = ScaleComposite(
            ScalingChecker(HigherOrderComposite(ExactSolver()),
                           h=linear,
                           J=quadratic, offset=offset,
                           **comp_parameters))
        response = sampler.sample_ising(linear, quadratic, offset=offset,
                                        **comp_parameters)

        self.assertAlmostEqual(response.first.energy, -3.8)
Exemple #2
0
    def test_sampleset_trim(self):
        h = {'a': -4.0, 'b': -4.0, 'c': 0}
        J = {('a', 'b'): 3.2}

        sampler = PolyTruncateComposite(HigherOrderComposite(ExactSolver()), 6)
        samples = sampler.sample_ising(h, J)

        self.assertEqual(len(samples), 6)
Exemple #3
0
    def test_sampleset_shorter(self):
        h = {'a': -4.0, 'b': -4.0, 'c': 0}
        J = {('a', 'b'): 3.2}

        sampler = PolyTruncateComposite(HigherOrderComposite(ExactSolver()), 10)
        with self.assertWarns(DeprecationWarning):
            samples = sampler.sample_ising(h, J)

        # we should see 2**n < 10 rows
        self.assertEqual(len(samples), 8)
Exemple #4
0
    def test_quadratic(self):

        sampler = HigherOrderComposite(self.tracker)

        Q = {(0, 0): 1, (0, 1): 2}

        sampleset = sampler.sample_hubo(Q, initial_state={0: 1, 1: 1})

        # nothing should change
        self.assertEqual(self.tracker.input['initial_state'], {0: 1, 1: 1})
Exemple #5
0
    def test_with_aggration(self):
        # this is actually just a smoke test, needs better testing in the
        # future...
        h = {'a': -4.0, 'b': -4.0, 'c': 0}
        J = {('a', 'b'): 3.2}

        sampler = PolyTruncateComposite(HigherOrderComposite(ExactSolver()), 6, aggregate=True)
        with self.assertWarns(DeprecationWarning):
            samples = sampler.sample_ising(h, J)

        self.assertEqual(len(samples), 6)
    def test_discard(self):
        linear = {0: -0.5, 1: -0.3, 2: -0.8}
        quadratic = {(0, 1, 2): -1.7}

        sampler = HigherOrderComposite(ExactSolver())
        response = sampler.sample_ising(linear, quadratic, penalty_strength=10,
                                        discard_unsatisfied=True)

        self.assertEqual(response.first.sample, {0: 1, 1: 1, 2: 1})
        self.assertAlmostEqual(response.first.energy, -3.3)
        self.assertTrue(np.prod(response.record.penalty_satisfaction))
    def test_already_qubo_2(self):
        linear = {0: -0.5, 1: -0.3}
        quadratic = {(0, 1): -1.7}

        sampler = HigherOrderComposite(ExactSolver())
        response = sampler.sample_ising(linear, quadratic, penalty_strength=10,
                                        keep_penalty_variables=True,
                                        discard_unsatisfied=True)

        self.assertEqual(response.first.sample, {0: 1, 1: 1})
        self.assertAlmostEqual(response.first.energy, -2.5)
        self.assertTrue(response.first.penalty_satisfaction)
    def test_penalty_variables(self):
        linear = {0: -0.5, 1: -0.3, 2: -0.8}
        quadratic = {(0, 1, 2): -1.7}

        sampler = HigherOrderComposite(ExactSolver())
        response = sampler.sample_ising(linear, quadratic, penalty_strength=10,
                                        keep_penalty_variables=True,
                                        discard_unsatisfied=True)

        self.assertEqual(len(response.first.sample), 5)
        self.assertAlmostEqual(response.first.energy, -3.3)
        self.assertTrue(response.first.penalty_satisfaction)
Exemple #9
0
    def test_higherorder_binary(self):
        sampler = HigherOrderComposite(self.tracker)

        Q = {'abc': -1, 'ab': 1}

        sampleset = sampler.sample_hubo(Q,
                                        initial_state={
                                            'a': 1,
                                            'b': 1,
                                            'c': 0
                                        })

        initial_state = self.tracker.input['initial_state']

        self.assertIn(initial_state, [
            {
                'a': 1,
                'b': 1,
                'c': 0,
                'a*b': 1
            },
            {
                'a': 1,
                'b': 1,
                'c': 0,
                'a*c': 0
            },
            {
                'a': 1,
                'b': 1,
                'c': 0,
                'b*c': 0
            },
            {
                'a': 1,
                'b': 1,
                'c': 0,
                'b*a': 1
            },
            {
                'a': 1,
                'b': 1,
                'c': 0,
                'c*a': 0
            },
            {
                'a': 1,
                'b': 1,
                'c': 0,
                'c*b': 0
            },
        ])
    def test_sample_hising_ignore_variables(self):
        linear = {'a': -4.0, 'b': -4.0, 'c': -4.0}
        quadratic = {('a', 'b', 'c'): 3.2}
        offset = 5
        ignored_variables, ignored_interactions = _check_params(
            ['a'], None)
        comp_parameters = dict(ignored_interactions=ignored_interactions,
                               ignored_variables=ignored_variables,
                               ignore_offset=True,
                               scalar=0.5)

        sampler = ScaleComposite(ScalingChecker(HigherOrderComposite(
            ExactSolver()), h=linear,
            J=quadratic, offset=offset,
            **comp_parameters))
        response = sampler.sample_ising(linear, quadratic, offset=offset,
                                        **comp_parameters)
        self.assertAlmostEqual(response.first.energy, -3.8)
    def test_all_energies(self):
        sampler = PolyScaleComposite(HigherOrderComposite(ExactSolver()))

        h = {'a': -1, 'b': 4}
        J = {'abc': -1, 'ab': 1, 'aaa': .5}

        sampleset = sampler.sample_hising(h, J, discard_unsatisfied=True)

        for sample, energy in sampleset.data(['sample', 'energy']):
            en = 0
            for v, bias in h.items():
                en += sample[v] * bias
            for term, bias in J.items():
                val = bias
                for v in term:
                    val *= sample[v]
                en += val

            self.assertAlmostEqual(energy, en)
Exemple #12
0
    def test_higherorder_spin(self):
        sampler = HigherOrderComposite(self.tracker)

        J = {'abc': -1, 'ab': 1}

        sampleset = sampler.sample_hising({},
                                          J,
                                          initial_state={
                                              'a': 1,
                                              'b': 1,
                                              'c': -1
                                          })

        bqm = self.tracker.input['bqm']
        initial_state = self.tracker.input['initial_state']

        samples = dimod.ExactSolver().sample(bqm).samples()

        # make sure that the initial-state is minimzed over the product/aux
        mask = (samples[:, ['a', 'b', 'c']] == [1, 1, -1]).all(axis=1)
        for v, val in initial_state.items():
            if v in ['a', 'b', 'c']:
                continue
            self.assertTrue(samples[mask, [v]][0, 0], val)
Exemple #13
0
    def test_instantiation_smoketest(self):
        sampler = HigherOrderComposite(ExactSolver())

        dtest.assert_sampler_api(sampler)
Exemple #14
0
 def test_0(self):
     with self.assertRaises(ValueError):
         PolyTruncateComposite(HigherOrderComposite(ExactSolver()), 0)
Exemple #15
0
    def test_10(self):
        sampler = PolyTruncateComposite(HigherOrderComposite(ExactSolver()), 10)
        dtest.assert_composite_api(sampler)

        self.assertEqual(sampler.parameters, sampler.child.parameters)
    def test_typical(self):
        sampler = PolyScaleComposite(HigherOrderComposite(ExactSolver()))

        self.assertTrue(hasattr(sampler, 'sample_poly'))
        self.assertTrue(hasattr(sampler, 'sample_hising'))
        self.assertTrue(hasattr(sampler, 'sample_hubo'))