def test_already_qubo(self): linear = {0: -0.5, 1: -0.3} quadratic = {(0, 1): -1.7} sampler = HigherOrderComposite(ExactSolver()) response = sampler.sample_hising(linear, quadratic, keep_penalty_variables=True, discard_unsatisfied=False) self.assertEqual(response.first.sample, {0: 1, 1: 1}) self.assertAlmostEqual(response.first.energy, -2.5) self.assertTrue(response.first.penalty_satisfaction)
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_hising(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_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_hising(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)
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)