Esempio n. 1
0
    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)
Esempio n. 2
0
    def test_empty(self):
        sampler = PolyScaleComposite(RangeLimitedSampler())
        samples = sampler.sample_hubo({})
        self.assertEqual(len(samples.variables), 0)

        sampler = PolyScaleComposite(RangeLimitedSampler())
        samples = sampler.sample_hubo({'a': 4}, scalar=.25)
        self.assertEqual(samples.first.energy, 4)
Esempio n. 3
0
 def test_fail_scale(self):
     sampler = PolyScaleComposite(RangeLimitedSampler())
     with self.assertRaises(RuntimeError):
         sampler.sample_hising({'a': 4}, {}, scalar=1)
Esempio n. 4
0
 def test_scale(self):
     sampler = PolyScaleComposite(RangeLimitedSampler())
     samples = sampler.sample_hising({'a': 4}, {}, scalar=.25)
     self.assertEqual(samples.first.energy, 4)
Esempio n. 5
0
 def test_normalizing(self):
     sampler = PolyScaleComposite(RangeLimitedSampler())
     samples = sampler.sample_hising({'a': 4}, {})
     self.assertEqual(samples.first.energy, 4)
Esempio n. 6
0
 def test_empty(self):
     sampler = PolyScaleComposite(RangeLimitedSampler())
     samples = sampler.sample_hising({}, {})
     self.assertEqual(len(samples.variables), 0)
Esempio n. 7
0
 def test_all_zero(self):
     sampler = PolyScaleComposite(RangeLimitedSampler())
     sampler.sample_hising({'a': 0}, {'ab': 0, 'bc': 0, 'abc': 0})
Esempio n. 8
0
 def test_wrap_bqm(self):
     with self.assertRaises(TypeError):
         PolyScaleComposite(ExactSolver())
Esempio n. 9
0
    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'))