How to use the dimod.BinaryQuadraticModel function in dimod

To help you get started, we’ve selected a few dimod examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dwavesystems / dimod / tests / test_binary_quadratic_model_convert.py View on Github external
def test_to_qubo_spin_to_qubo(self):
        """Spin model's to_qubo method"""
        linear = {0: .5, 1: 1.3}
        quadratic = {(0, 1): -.435}
        offset = 1.2
        vartype = dimod.SPIN

        model = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        Q, off = dimod.to_qubo(model)

        for spins in itertools.product((-1, 1), repeat=len(model)):
            spin_sample = dict(zip(range(len(spins)), spins))
            bin_sample = {v: (s + 1) // 2 for v, s in spin_sample.items()}

            # calculate the qubo's energy
            energy = off
            for (u, v), bias in Q.items():
                energy += bin_sample[u] * bin_sample[v] * bias

            # and the energy of the model
            self.assertAlmostEqual(energy, model.energy(spin_sample))
github dwavesystems / dwave-hybrid / tests / test_composers.py View on Github external
def test_single(self):
        bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, dimod.SPIN)

        states = States(State.from_sample({'a': 1, 'b': -1}, bqm))

        state = MergeSamples().run(states).result()

        self.assertEqual(state, states[0])
github dwavesystems / dwave-hybrid / tests / test_samplers.py View on Github external
def test_tabu_problem_sampler_initialization(self):
        bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, 'SPIN')
        sampleset = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
                                                      {'a': -1, 'b': 1}], bqm)
        state = State(problem=bqm, samples=sampleset)

        # with timeout=0, TabuSampler should just return the initial_states
        result = TabuProblemSampler(timeout=0).run(state).result()
        expected = sampleset.record.sample

        self.assertTrue(np.array_equal(result.samples.record.sample, expected))
        self.assertEqual(len(result.samples), 2)

        # test input samples are tiled
        result = TabuProblemSampler(timeout=0, num_reads=4,
                                    initial_states_generator="tile").run(state).result()

        expected = np.tile(sampleset.record.sample, (2,1))
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_remove_offset(self):
        linear = {v: v * -.43 for v in range(10)}
        quadratic = {(u, v): u * v * -.021 for u, v in itertools.combinations(linear, 2)}
        offset = -1.2
        vartype = dimod.BINARY
        bqm = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        bqm.remove_offset()

        self.assertAlmostEqual(bqm.offset, 0.0)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_relabel_typical_copy(self):
        linear = {0: .5, 1: 1.3}
        quadratic = {(0, 1): -.435}
        offset = 1.2
        vartype = dimod.SPIN
        model = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        mapping = {0: 'a', 1: 'b'}
        newmodel = model.relabel_variables(mapping, inplace=False)
        self.assertIsNot(model, newmodel)
        self.assertIsNot(model.linear, newmodel.linear)
        self.assertIsNot(model.quadratic, newmodel.quadratic)

        # check that new model is the same as old model
        linear = {'a': .5, 'b': 1.3}
        quadratic = {('a', 'b'): -.435}
        offset = 1.2
        vartype = dimod.SPIN
        testmodel = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        self.assertEqual(newmodel, testmodel)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_binary_property(self):
        linear = {0: 1, 1: -1, 2: .5}
        quadratic = {(0, 1): .5, (1, 2): 1.5}
        offset = -1.4
        vartype = dimod.BINARY
        model = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        self.assertIs(model, model.binary)

        #

        # create a binary model
        linear = {0: 1, 1: -1, 2: .5}
        quadratic = {(0, 1): .5, (1, 2): 1.5}
        offset = -1.4
        vartype = dimod.SPIN
        model = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        binary_model = model.change_vartype(dimod.BINARY, inplace=False)

        self.assertEqual(model.binary, binary_model)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
for av, bv in itertools.product((0, 1), repeat=2):
            self.assertEqual(bqm.energy({'a': 2 * av - 1, 'b': 2 * bv - 1}),
                             bqm.binary.energy({'a': av, 'b': bv}))

        bqm.add_interaction('a', 'b', .5)
        self.assertConsistentBQM(bqm)

        for av, bv in itertools.product((0, 1), repeat=2):
            self.assertEqual(bqm.energy({'a': 2 * av - 1, 'b': 2 * bv - 1}),
                             bqm.binary.energy({'a': av, 'b': bv}))

        #

        # spin
        bqm = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)
        __ = bqm.binary  # create counterpart
        bqm.add_interaction('a', 'b', -1)
        self.assertConsistentBQM(bqm)

        for av, bv in itertools.product((0, 1), repeat=2):
            self.assertEqual(bqm.energy({'a': 2 * av - 1, 'b': 2 * bv - 1}),
                             bqm.binary.energy({'a': av, 'b': bv}))

        #

        # binary
        bqm = dimod.BinaryQuadraticModel({}, {('a', 'b'): -1}, 0.0, dimod.BINARY)
        bqm.add_interaction('a', 'b', .5)
        self.assertConsistentBQM(bqm)

        for av, bv in itertools.product((0, 1), repeat=2):
github dwavesystems / dwave-hybrid / tests / test_composers.py View on Github external
with self.assertRaises(traits.StateTraitMissingError):
            IdentityComposer().run(State()).result()
        with self.assertRaises(traits.StateTraitMissingError):
            IdentityComposer().run(State(problem=True)).result()
        with self.assertRaises(traits.StateTraitMissingError):
            IdentityComposer().run(State(problem=True, samples=True)).result()
        self.assertTrue(
            IdentityComposer().run(State(problem=True, samples=True, subsamples=True)).result())


class TestSplatComposer(unittest.TestCase):
    problem = dimod.BinaryQuadraticModel({}, {'ab': 1, 'bc': 1, 'ca': 1}, 0, dimod.SPIN)
    samples = [{'a': +1, 'b': +1, 'c': +1},
               {'a': -1, 'b': -1, 'c': -1}]
    subproblem = dimod.BinaryQuadraticModel({}, {'bc': 1}, 0, dimod.SPIN)
    subsamples = [{'b': -1, 'c': +1},
                  {'b': +1, 'c': +1}]
    composed = [{'a': +1, 'b': -1, 'c': +1},
                {'a': -1, 'b': +1, 'c': +1}]

    def test_default(self):
        """All subsamples are combined with all the samples."""

        state = State.from_samples(self.samples, self.problem).updated(
            subproblem=self.subproblem,
            subsamples=SampleSet.from_samples_bqm(self.subsamples, self.subproblem))

        nextstate = SplatComposer().next(state)

        self.assertEqual(nextstate.samples,
                         SampleSet.from_samples_bqm(self.composed, self.problem))