Ejemplo n.º 1
0
    def test_energy_minimization(self):
        sample0 = {0: -1, 1: -1, 2: +1, 3: +1}
        sample1 = {0: +1, 1: -1, 2: +1, 3: -1}
        samples = [sample0, sample1]

        embedding = {'a': {0, 1}, 'b': {2}, 'c': {3}}

        linear = {'a': -1, 'b': 0, 'c': 0}
        quadratic = {}

        chain_break_method = dimod.embedding.MinimizeEnergy(linear=linear, quadratic=quadratic)
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=chain_break_method))

        source0, source1 = source_samples

        # no broken chains
        self.assertEqual(source0, {'a': -1, 'b': +1, 'c': +1})

        # in this case 'a' being spin-up minimizes the energy
        self.assertEqual(source1, {'a': +1, 'b': +1, 'c': -1})

        linear = {'a': 1, 'b': 0, 'c': 0}
        quadratic = {('a', 'b'): -5}

        chain_break_method = dimod.embedding.MinimizeEnergy(linear=linear, quadratic=quadratic)
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=chain_break_method))

        source0, source1 = source_samples

        # no broken chains
        self.assertEqual(source0, {'a': -1, 'b': +1, 'c': +1})

        # in this case 'a' being spin-up minimizes the energy due to the quadratic bias
        self.assertEqual(source1, {'a': +1, 'b': +1, 'c': -1})

        # now we need two broken chains
        sample = {0: +1, 1: -1, 2: +1, 3: -1, 4: +1}
        samples = [sample]

        embedding = {'a': {0, 1}, 'b': {2, 3}, 'c': {4}}

        quadratic = {('a', 'b'): -1, ('b', 'c'): 1}

        chain_break_method = dimod.embedding.MinimizeEnergy(quadratic=quadratic)
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=chain_break_method))

        source, = source_samples

        self.assertEqual(source, {'b': -1, 'c': +1, 'a': -1})
Ejemplo n.º 2
0
    def test_discard(self):
        sample0 = {0: -1, 1: -1, 2: +1}
        sample1 = {0: +1, 1: -1, 2: +1}
        samples = [sample0, sample1]

        embedding = {'a': {0, 1, 2}}

        # specify that majority vote should be used
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=dimod.embedding.discard))

        # no samples should be returned because they are all broken
        self.assertEqual(len(source_samples), 0)

        # now set up an embedding that works for one sample and not the other
        embedding = {'a': {0, 1}, 'b': {2}}

        # specify that discard should be used
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=dimod.embedding.discard))

        # only the first sample should be returned
        self.assertEqual(len(source_samples), 1)
        self.assertEqual(source_samples, [{'a': -1, 'b': +1}])
Ejemplo n.º 3
0
    def test_majority_vote(self):
        """should return the most common value in the chain"""

        sample0 = {0: -1, 1: -1, 2: +1}
        sample1 = {0: +1, 1: -1, 2: +1}
        samples = [sample0, sample1]

        embedding = {'a': {0, 1, 2}}

        # specify that majority vote should be used
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=dimod.embedding.majority_vote))

        self.assertEqual(source_samples, [{'a': -1}, {'a': +1}])
Ejemplo n.º 4
0
    def test_majority_vote_with_response(self):
        sample0 = {0: -1, 1: -1, 2: +1}
        sample1 = {0: +1, 1: -1, 2: +1}
        samples = [sample0, sample1]

        # now set up an embedding that works for one sample and not the other
        embedding = {'a': {0, 1, 2}}

        # load the samples into a dimod response
        response = dimod.Response.from_dicts(samples, {'energy': [0 for __ in samples]})

        # specify that majority vote should be used
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=dimod.embedding.majority_vote))

        self.assertEqual(len(source_samples), 2)
        self.assertEqual(source_samples, [{'a': -1}, {'a': +1}])
Ejemplo n.º 5
0
    def test_discard_with_response(self):
        sample0 = {0: -1, 1: -1, 2: +1}
        sample1 = {0: +1, 1: -1, 2: +1}
        samples = [sample0, sample1]

        # now set up an embedding that works for one sample and not the other
        embedding = {'a': {0, 1}, 'b': {2}}

        # load the samples into a dimod response
        response = dimod.Response.from_dicts(samples, {'energy': [0 for __ in samples]})

        # specify that discard should be used
        source_samples = list(dimod.iter_unembed(samples, embedding, chain_break_method=dimod.embedding.discard))

        # only the first sample should be returned
        self.assertEqual(len(source_samples), 1)
        self.assertEqual(source_samples, [{'a': -1, 'b': +1}])