How to use the dimod.SPIN 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.py View on Github external
def test_construction(self):
        # spin model
        linear = {0: 1, 1: -1, 2: .5}
        quadratic = {(0, 1): .5, (1, 2): 1.5}
        offset = 1.4
        vartype = dimod.SPIN
        bqm = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

        self.assertConsistentBQM(bqm)

        for v, bias in linear.items():
            self.assertEqual(bqm.linear[v], bias)
        for v in bqm.linear:
            self.assertIn(v, linear)

        for (u, v), bias in quadratic.items():
            self.assertEqual(bqm.adj[u][v], bias)
        for interaction in bqm.quadratic:
            self.assertIn(interaction, quadratic)

        self.assertEqual(bqm.offset, offset)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_from_numpy_vectors(self):
        h = np.array([-1, 1, 5])
        heads = np.array([0, 1])
        tails = np.array([1, 2])
        values = np.array([-1, +1])

        bqm = dimod.BinaryQuadraticModel.from_numpy_vectors(h, (heads, tails, values), 0.0, dimod.SPIN)

        self.assertEqual(bqm, dimod.BinaryQuadraticModel.from_ising([-1, 1, 5], {(0, 1): -1, (1, 2): 1}))
github dwavesystems / dimod / tests / test_embedding_transforms.py View on Github external
def test_embed_bqm_empty(self):
        bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)

        embedded_bqm = dimod.embed_bqm(bqm, {}, {})

        self.assertIsInstance(embedded_bqm, dimod.BinaryQuadraticModel)
        self.assertFalse(embedded_bqm)  # should be empty
github dwavesystems / dwave-hybrid / tests / test_utils.py View on Github external
def test_hstack_from_bqm(self):
        bqm = dimod.BQM.from_ising({'a': 1}, {})
        ss = dimod.SampleSet.from_samples({'a': 0}, vartype='BINARY', energy=0)

        res = hstack_samplesets(ss, bqm=bqm)
        self.assertEqual(res.vartype, dimod.SPIN)
        numpy.testing.assert_array_equal(res.record.energy, numpy.array([-1]))
github dwavesystems / dwavebinarycsp / legacy_tests / test_utils.py View on Github external
def test_sample_vartype(self):
        sample = {'a': -1, 'b': +1}
        self.assertIs(utils.sample_vartype(sample), dimod.SPIN)

        # should default to spin
        sample = {'a': +1, 'b': +1}
        self.assertIs(utils.sample_vartype(sample), dimod.SPIN)

        sample = {'a': 0, 'b': +1}
        self.assertIs(utils.sample_vartype(sample), dimod.BINARY)

        with self.assertRaises(ValueError):
            sample = {'a': 0, 'b': -1}
            utils.sample_vartype(sample)

        with self.assertRaises(ValueError):
            sample = {'a': 3}
            utils.sample_vartype(sample)
github dwavesystems / dimod / tests / test_bqm.py View on Github external
def test_typical(self, name, BQM):
        h = OrderedDict([('a', -1), (1, -1), (3, -1)])
        J = {}
        bqm = BQM(h, J, dimod.SPIN)

        self.assertTrue(bqm.has_variable('a'))
        self.assertTrue(bqm.has_variable(1))
        self.assertTrue(bqm.has_variable(3))

        # no false positives
        self.assertFalse(bqm.has_variable(0))
        self.assertFalse(bqm.has_variable(2))
github dwavesystems / dimod / benchmarks / benchmarks / bench_binary_quadratic_model.py View on Github external
def setup(self):
        h_K100 = {v: 0 for v in range(100)}
        J_K100 = {edge: 0 for edge in itertools.combinations(range(100), 2)}
        self.bqm_k100 = dimod.BinaryQuadraticModel(h_K100, J_K100, 0.0, dimod.SPIN)

        self.bqm_empty = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)
github dwavesystems / dwave-hybrid / hybrid / utils.py View on Github external
Examples:
        This example returns the variable with maximum contribution to energy
        for the given sample.

        >>> import dimod
        >>> bqm = dimod.BQM({}, {'ab': 0, 'bc': 1, 'cd': 2}, 0, 'SPIN')
        >>> flip_energy_gains(bqm, {'a': -1, 'b': 1, 'c': 1, 'd': -1})[0][1]
        'd'

    """

    if bqm.vartype is dimod.BINARY:
        # val 0 flips to 1 => delta +1
        # val 1 flips to 0 => delta -1
        delta = lambda val: 1 - 2 * val
    elif bqm.vartype is dimod.SPIN:
        # val -1 flips to +1 => delta +2
        # val +1 flips to -1 => delta -2
        delta = lambda val: -2 * val
    else:
        raise ValueError("vartype not supported")

    sample = sample_as_dict(sample)

    if variables is None:
        variables = iter(sample)

    if min_gain is None:
        min_gain = float('-inf')

    energy_gains = []
    for idx in variables:
github dwavesystems / dwave-hybrid / hybrid / core.py View on Github external
def empty(cls):
        return cls.from_samples([], vartype=dimod.SPIN, energy=0)
github dwavesystems / dwavebinarycsp / dwave_constraint_compilers / compilers / stitcher.py View on Github external
"""
    widgets = make_widgets_from(constraints)
    linear = {}
    quadratic = {}
    offset = 0
    for widget in widgets:
        for variable, bias in iteritems(widget.model.linear):
            linear[variable] = linear.get(variable, 0) + bias

        for relation, bias in iteritems(widget.model.quadratic):
            quadratic[relation] = quadratic.get(relation, 0) + bias

        offset += widget.model.offset

    return dimod.BinaryQuadraticModel(linear, quadratic, offset, dimod.SPIN)