def test_from_problem(self): sample = {0: 1, 1: 0} bqm = dimod.BinaryQuadraticModel({0: 1, 1: 2}, {}, 0.0, 'BINARY') self.assertEqual(State.from_problem(bqm).samples.first.energy, 0.0) self.assertEqual(State.from_problem(bqm, beta=0.5).beta, 0.5) self.assertEqual(State.from_problem(bqm, samples=hybrid.utils.max_sample).samples.first.energy, 3.0) self.assertEqual(State.from_problem(bqm, samples=sample), State.from_samples(sample, bqm)) self.assertEqual(State.from_problem(bqm, samples=[sample]), State.from_samples([sample], bqm))
def test_large_sparse(self): "Total energy is preserved after ICM on random samples over random graph." # random Erdős-Rényi sparse graph with 100 nodes and 10% density graph = nx.generators.fast_gnp_random_graph(n=100, p=0.1) bqm = dimod.generators.uniform(graph=graph, vartype=dimod.SPIN) nodes = sorted(bqm.variables) # random input samples s1 = State.from_problem(bqm, samples=random_sample) s2 = State.from_problem(bqm, samples=random_sample) inp = States(s1, s2) icm = IsoenergeticClusterMove() res = icm.run(inp).result() self.assertAlmostEqual(self.total_energy(inp), self.total_energy(res))
def test_completely_disconnected_constraints(self): bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ca': 1}) size = 2 constraints = ['a', 'b'] with self.assertRaises(ValueError): rcd = RandomConstraintDecomposer(size, constraints) state = State.from_problem(bqm) _ = rcd.run(state).result()
def test_dtype(self): bqm = dimod.BQM.from_ising({'a': 0}, {}) init = State.from_problem(bqm) state = init.updated(subsamples=init.samples) nextstate = SplatComposer().next(state) init_dtype = state.samples.record.sample.dtype next_dtype = nextstate.samples.record.sample.dtype self.assertEqual(init_dtype, next_dtype)
def test_partially_disconnected_constraints(self): bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ca': 1}) size = 2 constraints = ['a', 'b', 'cb'] rcd = RandomConstraintDecomposer(size, constraints) state = State.from_problem(bqm) newstate = rcd.run(state).result() self.assertIn('subproblem', newstate) self.assertTrue(len(newstate.subproblem) <= size) # correct size