예제 #1
0
    def test_energies(self):
        bqm = dimod.BinaryQuadraticModel({0: 0.0, 1: 0.0, 2: 0.0},
                                         {(0, 1): -1.0, (1, 2): 1.0, (0, 2): 1.0},
                                         1.0,
                                         dimod.SPIN)
        sampler = dimod.RandomSampler()
        response = sampler.sample(bqm, num_reads=10)

        dtest.assert_response_energies(response, bqm)
    def test_roof_duality_triangle(self):
        bqm = BinaryQuadraticModel.from_ising({}, {
            'ab': -1,
            'bc': -1,
            'ac': -1
        })

        # two equally good solutions
        sampler = FixVariablesComposite(ExactSolver(),
                                        algorithm='roof_duality')
        sampleset = sampler.sample(bqm)

        self.assertEqual(set(sampleset.variables), set('abc'))
        dtest.assert_response_energies(sampleset, bqm)
예제 #3
0
    def test_chimera(self):
        C = dnx.chimera_graph(4)
        nodelist = list(C.nodes())
        edgelist = list(C.edges())
        structsampler = dimod.StructureComposite(dimod.RandomSampler(),
                                                 nodelist=nodelist,
                                                 edgelist=edgelist)

        Q = {(v, v): 0.1 for v in nodelist}
        Q.update({edge: -1.0 for edge in edgelist})

        sampler = CheckerboardTransformComposite(structsampler, C)
        response = sampler.sample_qubo(Q, num_reads=1000)

        dit.assert_response_energies(response,
                                     dimod.BinaryQuadraticModel.from_qubo(Q))
    def test_roof_duality_triangle_not_strict(self):
        bqm = BinaryQuadraticModel.from_ising({}, {
            'ab': -1,
            'bc': -1,
            'ac': -1
        })

        # two equally good solutions, but with strict=False, it will pick one
        sampler = FixVariablesComposite(ExactSolver(),
                                        algorithm='roof_duality')

        sampleset = sampler.sample(bqm, strict=False)

        self.assertEqual(set(sampleset.variables), set('abc'))
        self.assertEqual(len(sampleset), 1)  # all should be fixed
        dtest.assert_response_energies(sampleset, bqm)
예제 #5
0
    def test_sample_BINARY(self):
        bqm = dimod.BinaryQuadraticModel({
            0: 0.0,
            1: 0.0,
            2: 0.0
        }, {
            (0, 1): -1.0,
            (1, 2): 1.0,
            (0, 2): 1.0
        }, 1.0, dimod.BINARY)

        response = dimod.ExactSolver().sample(bqm)

        # every possible conbination should be present
        self.assertEqual(len(response), 2**len(bqm))
        self.assertEqual(response.record.sample.shape, (2**len(bqm), len(bqm)))

        # confirm vartype
        self.assertIs(response.vartype, bqm.vartype)

        dtest.assert_response_energies(response, bqm)
예제 #6
0
    def test_transforms_exact(self):

        C = dnx.chimera_graph(2, 2, 2)
        nodelist = list(C.nodes())
        edgelist = list(C.edges())
        structsampler = dimod.StructureComposite(dimod.ExactSolver(),
                                                 nodelist=nodelist,
                                                 edgelist=edgelist)

        sampler = CheckerboardTransformComposite(structsampler,
                                                 C,
                                                 aggregate=True)

        h = {v: 0.1 for v in nodelist}
        J = {edge: -1.0 for edge in edgelist}
        response = sampler.sample_ising(h, J)

        # All 4 gauges must return same samples
        for datum in response.data():
            self.assertEqual(datum.num_occurrences, 4)

        dit.assert_response_energies(
            response, dimod.BinaryQuadraticModel.from_ising(h, J))
예제 #7
0
    def test_transform_embedded(self):
        C = dnx.chimera_graph(1)
        nodelist = list(C.nodes())
        edgelist = list(C.edges())
        structsampler = dimod.StructureComposite(dimod.ExactSolver(),
                                                 nodelist=nodelist,
                                                 edgelist=edgelist)
        gauges_sampler = CheckerboardTransformComposite(structsampler,
                                                        C,
                                                        aggregate=True)
        sampler = FixedEmbeddingComposite(gauges_sampler, {
            'a': [0, 4],
            'b': [1, 5],
            'c': [2, 6]
        })
        h = {'a': .5, 'c': 0}
        J = {('a', 'c'): -1}
        response = sampler.sample_ising(h, J)
        # All 4 gauges must return same samples
        for datum in response.data():
            self.assertEqual(datum.num_occurrences, 4)

        dit.assert_response_energies(
            response, dimod.BinaryQuadraticModel.from_ising(h, J))