Exemple #1
0
    def test_initialization(self):
        sampler = dimod.RandomSampler()

        dtest.assert_sampler_api(sampler)

        self.assertEqual(sampler.properties, {})
        self.assertEqual(sampler.parameters, {'num_reads': []})
    def test_dimod_solver(self):
        """Run a set of beasely instances with 50 variables."""
        qbsolv = dwave_qbsolv.QBSolv()
        tests_path = os.path.dirname(os.path.abspath(__file__))

        for index, expected_energy in enumerate(self.beasley50_energies):
            index += 1
            qubo = load_qubo(
                self.beasley50_qubo_format.format(tests_path=tests_path,
                                                  index=index))
            # Python 3 feature
            # with self.subTest(index=index, energy=energy):

            # Give each problem three shots. One should be enough.
            # But this is a non-deterministic test.
            for _ in range(3):
                results = qbsolv.sample_qubo(qubo,
                                             find_max=True,
                                             solver=dimod.RandomSampler())
                self.assertTrue(
                    any(
                        abs(datum.energy - expected_energy) < ENERGY_ERROR
                        for datum in results.data()))
                break
            else:
                self.fail()
    def test_sample_ising_kwargs(self):
        sampler = dimod.TrackingComposite(dimod.RandomSampler())

        h = {'a': -1}
        J = {('a', 'b'): -1}
        ss = sampler.sample_ising(h, J, num_reads=5)

        self.assertEqual(sampler.input, dict(h=h, J=J, num_reads=5))
        self.assertEqual(sampler.output, ss)
Exemple #4
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_warnings(self):
        G = dnx.chimera_graph(12)

        sampler = EmbeddingComposite(
            dimod.StructureComposite(dimod.RandomSampler(), G.nodes, G.edges))

        # this will need chains lengths > 7
        J = {uv: -1 for uv in itertools.combinations(range(40), 2)}

        ss = sampler.sample_ising({}, J, warnings='SAVE')

        self.assertIn('warnings', ss.info)
    def test_warnings_as_class_variable(self):
        G = dnx.chimera_graph(12)

        sampler = EmbeddingComposite(
            dimod.StructureComposite(dimod.RandomSampler(), G.nodes, G.edges))

        # this will need chains lengths > 7
        J = {uv: -1 for uv in itertools.combinations(range(40), 2)}

        EmbeddingComposite.warnings_default = 'SAVE'

        ss = sampler.sample_ising({}, J)

        self.assertIn('warnings', ss.info)

        EmbeddingComposite.warnings_default = 'IGNORE'  # restore default
Exemple #7
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)

        # check their energies
        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(energy, bqm.energy(sample))
    def test_warning_chain_strength(self):
        G = dnx.chimera_graph(12)

        sampler = EmbeddingComposite(
            dimod.StructureComposite(dimod.RandomSampler(), G.nodes, G.edges))

        J = {(0, 1): 100}

        ss = sampler.sample_ising({}, J, warnings='SAVE')
        self.assertIn('warnings', ss.info)

        count = 0
        for warning in ss.info['warnings']:
            if issubclass(warning['type'], ChainStrengthWarning):
                count += 1
        self.assertEqual(count, 1)
Exemple #9
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))
Exemple #10
0
    def test_keyerror(self):
        C16 = dnx.chimera_graph(16)
        child_sampler = dimod.RandomSampler()
        nodelist = sorted(C16.nodes)
        edgelist = sorted(sorted(edge) for edge in C16.edges)
        struc_sampler = dimod.StructureComposite(child_sampler, nodelist,
                                                 edgelist)

        embedding = {0: [391, 386], 1: [390], 2: [385]}

        sampler = FixedEmbeddingComposite(struc_sampler, embedding)

        with self.assertRaises(ValueError):
            sampler.sample_qubo({(1, 4): 1})

        sampler.sample_qubo({(1, 2): 1}).record  # sample and resolve future
        sampler.sample_qubo({(1, 1): 1}).record  # sample and resolve future
Exemple #11
0
 def test_kwargs(self):
     bqm = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)
     with self.assertWarns(SamplerUnknownArgWarning):
         dimod.RandomSampler().sample(bqm, a=5, b=2)
Exemple #12
0
 def setUp(self):
     self.sampler = dimod.RandomSampler()
Exemple #13
0
 def setUp(self):
     self.sampler = dimod.RandomSampler()
     self.sampler_factory = dimod.RandomSampler