How to use the dimod.SampleSet.from_samples 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_sampler.py View on Github external
def sample_qubo(self, Q):
                return dimod.SampleSet.from_samples([[1]], energy=[-3], vartype=dimod.BINARY)
github dwavesystems / dimod / tests / test_sampleset.py View on Github external
def test_3path_with_bytes(self):
        samples = [[-1, -1, -1, 1], [1, 1, 1, -1]]
        sampleset = dimod.SampleSet.from_samples(samples, energy=0,
                                                 vartype=dimod.SPIN)

        dct = sampleset.to_serializable(use_bytes=True)

        new = dimod.SampleSet.from_serializable(dct)

        self.assertEqual(sampleset, new)
github dwavesystems / dimod / tests / test_sampleset.py View on Github external
def test_variables_order(self):
        ss0 = dimod.SampleSet.from_samples(([-1, +1], 'ab'), dimod.SPIN, energy=-1)
        ss1 = dimod.SampleSet.from_samples(([-1, +1], 'ba'), dimod.SPIN, energy=-1)
        ss2 = dimod.SampleSet.from_samples(([[+1, +1], [-1, -1]], 'ab'), dimod.SPIN, energy=[1, 1])

        comb = dimod.concatenate((ss0, ss1, ss2))

        out = dimod.SampleSet.from_samples(([[-1, +1], [+1, -1], [+1, +1], [-1, -1]], 'ab'),
                                           dimod.SPIN, energy=[-1, -1, 1, 1])

        self.assertEqual(comb, out)
        np.testing.assert_array_equal(comb.record.sample, out.record.sample)
github dwavesystems / dimod / tests / test_sampleset.py View on Github external
def test_from_samples_empty(self):

        self.assertEqual(len(dimod.SampleSet.from_samples([], dimod.SPIN, energy=[], a=1)), 0)

        self.assertEqual(len(dimod.SampleSet.from_samples(np.empty((0, 0)), dimod.SPIN, energy=[], a=1)), 0)
github dwavesystems / dimod / tests / test_sampleset.py View on Github external
def test_sampleset1_append1(self):
        sampleset = dimod.SampleSet.from_samples({'a': -1, 'b': 1}, dimod.SPIN, energy=0)
        new = sampleset.append_variables({'c': -1, 'd': -1})

        target = dimod.SampleSet.from_samples({'a': -1, 'b': 1, 'c': -1, 'd': -1}, dimod.SPIN, energy=0)

        self.assertEqual(new, target)
github dwavesystems / dimod / tests / test_sampleset.py View on Github external
def test_overlapping_variables(self):
        sampleset = dimod.SampleSet.from_samples([{'a': -1, 'b': 1}, {'a': -1, 'b': -1}],
                                                 dimod.SPIN, energy=0)

        with self.assertRaises(ValueError):
            sampleset.append_variables([{'c': -1, 'd': -1, 'a': -1}])
github dwavesystems / dimod / tests / test_sampleset.py View on Github external
def test_non_blocking(self):

        future = concurrent.futures.Future()

        sampleset = dimod.SampleSet.from_future(future)

        # shouldn't block or raise
        new = sampleset.change_vartype(dimod.BINARY)

        future.set_result(dimod.SampleSet.from_samples({'a': -1},
                                                       dimod.SPIN,
                                                       energy=1))

        np.testing.assert_array_equal(new.record.sample, [[0]])
github dwavesystems / dwave-cloud-client / tests / test_coders.py View on Github external
def test_bq_response_decoding(self):
        """Answer to simple problem properly decoded."""

        ss = dimod.SampleSet.from_samples(
            ([[0, 1], [1, 0]], 'ab'), vartype='BINARY', energy=0)

        msg = dict(answer=dict(format='bq', data=ss.to_serializable()))

        res = decode_bq(msg)

        self.assertEqual(res.get('problem_type'), 'bqm')
        self.assertEqual(res.get('sampleset'), ss)
github dwavesystems / dwave-hybrid / hybrid / utils.py View on Github external
# prepare empty result sampleset
    samples = numpy.empty((num_samples, len(variables)))

    # copy over samplesets, one by one, from left to right
    for ss in samplesets:
        ss.change_vartype(vartype)
        mask = [variables.index[v] for v in ss.variables]
        samples[:, mask] = ss.record.sample[:num_samples]

    if bqm is None:
        energies = 0
    else:
        energies = bqm.energies((samples, variables))

    return dimod.SampleSet.from_samples((samples, variables), energy=energies, vartype=vartype)