Example #1
0
    def test_hstack_clamp(self):
        s1 = dimod.SampleSet.from_samples([{'a': 1}, {'a': 0}], vartype='BINARY', energy=0)
        s2 = dimod.SampleSet.from_samples({'b': 1}, vartype='BINARY', energy=0)
        exp = dimod.SampleSet.from_samples({'a': 1, 'b': 1}, vartype='BINARY', energy=0)

        self.assertEqual(hstack_samplesets(s1, s2), exp)
        self.assertEqual(hstack_samplesets(s2, s1), exp)
Example #2
0
    def test_hstack_one(self):
        ss = dimod.SampleSet.from_samples({'a': 1}, vartype='BINARY', energy=0)
        emp = SampleSet.empty()

        self.assertEqual(hstack_samplesets(ss), ss)
        self.assertEqual(hstack_samplesets(ss, emp), ss)
        self.assertEqual(hstack_samplesets(emp, ss), ss)
        self.assertEqual(hstack_samplesets(ss, ss), ss)
Example #3
0
    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]))
Example #4
0
    def test_hstack_multisample_multivar(self):
        ab = dimod.SampleSet.from_samples([{
            'a': 0,
            'b': 1
        }, {
            'a': 1,
            'b': 0
        }],
                                          vartype='BINARY',
                                          energy=0)
        bc = dimod.SampleSet.from_samples([{
            'b': 1,
            'c': 0
        }, {
            'b': 1,
            'c': 1
        }],
                                          vartype='BINARY',
                                          energy=0)
        exp = dimod.SampleSet.from_samples([{
            'a': 0,
            'b': 1,
            'c': 0
        }, {
            'a': 1,
            'b': 1,
            'c': 1
        }],
                                           vartype='BINARY',
                                           energy=0)

        self.assertEqual(hstack_samplesets(ab, bc), exp)
Example #5
0
    def test_hstack_identity(self):
        """hstack_samplesets is identity op on single sampleset's samples"""
        ss = dimod.SampleSet.from_samples({'a': 1}, vartype='BINARY', energy=0)
        hs = hstack_samplesets(ss)

        self.assertEqual(hs, ss)
        self.assertEqual(hs.record.sample.dtype, ss.record.sample.dtype)
Example #6
0
    def test_hstack_empty(self):
        s1 = SampleSet.empty()
        s2 = SampleSet.empty()
        exp = SampleSet.empty()

        res = hstack_samplesets(s1, s2)
        self.assertEqual(res, exp)
Example #7
0
    def test_hstack_correct_dtype(self):
        """Output sampleset has the same dtype"""
        s1 = dimod.SampleSet.from_samples({'a': 1}, vartype='BINARY', energy=0)
        s2 = dimod.SampleSet.from_samples({'b': 1}, vartype='BINARY', energy=0)
        hs = hstack_samplesets(s1, s2)

        self.assertEqual(s1.record.sample.dtype, s2.record.sample.dtype)
        self.assertEqual(hs.record.sample.dtype, s1.record.sample.dtype)
Example #8
0
    def next(self, state, **runopts):
        # update as many samples possible with partial samples from `state.subsamples`
        # the resulting number of samples will be limited with `len(state.subsamples)`

        samples = hstack_samplesets(state.samples,
                                    state.subsamples,
                                    bqm=state.problem)

        logger.debug(
            "{name} subsamples (shape={ss_shape!r}) -> samples (shape={s_shape!r}), "
            "sample energies changed {old_en} -> {new_en}".format(
                name=self.name,
                ss_shape=state.subsamples.record.shape,
                s_shape=state.samples.record.shape,
                old_en=state.samples.record.energy,
                new_en=samples.record.energy))

        return state.updated(samples=samples)
Example #9
0
    def test_hstack_update(self):
        s1 = dimod.SampleSet.from_samples({'a': 1}, vartype='BINARY', energy=0)
        s2 = dimod.SampleSet.from_samples({'a': 0}, vartype='BINARY', energy=0)

        self.assertEqual(hstack_samplesets(s1, s2), s2)
        self.assertEqual(hstack_samplesets(s2, s1), s1)
Example #10
0
 def hstack(self, *others):
     """Combine the first sample in this SampleSet with first samples in all
     other SampleSets. Energy is reset to zero, and vartype is cast to the
     local vartype (first sampleset's vartype).
     """
     return hstack_samplesets(self, *others)