当前位置: 首页>>代码示例>>Python>>正文


Python dimod.ExactSolver方法代码示例

本文整理汇总了Python中dimod.ExactSolver方法的典型用法代码示例。如果您正苦于以下问题:Python dimod.ExactSolver方法的具体用法?Python dimod.ExactSolver怎么用?Python dimod.ExactSolver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dimod的用法示例。


在下文中一共展示了dimod.ExactSolver方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_energy_threshold_termination

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_energy_threshold_termination(self):
        class ExactSolver(Runnable):
            def next(self, state):
                return state.updated(
                    samples=dimod.ExactSolver().sample(state.problem))

        bqm = dimod.BinaryQuadraticModel({'a': 1}, {}, 0, dimod.SPIN)
        state = State.from_sample({'a': 1}, bqm)

        w = LoopUntilNoImprovement(ExactSolver(),
                                   key=operator.attrgetter('samples.first.energy'),
                                   terminate=partial(operator.ge, -1))
        s = w.run(state).result()
        self.assertEqual(s.samples.first.energy, -1)

        w = LoopUntilNoImprovement(ExactSolver(),
                                   key='samples.first.energy',
                                   terminate=partial(operator.ge, -1))
        s = w.run(state).result()
        self.assertEqual(s.samples.first.energy, -1)

        w = LoopUntilNoImprovement(ExactSolver(),
                                   terminate=partial(operator.ge, -1))
        s = w.run(state).result()
        self.assertEqual(s.samples.first.energy, -1) 
开发者ID:dwavesystems,项目名称:dwave-hybrid,代码行数:27,代码来源:test_flow.py

示例2: test_sample_SPIN

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_sample_SPIN(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)

        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)

        dimod.testing.assert_response_energies(response, bqm) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:18,代码来源:test_exact_solver.py

示例3: test_sample_BINARY

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
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)

        dimod.testing.assert_response_energies(response, bqm) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:18,代码来源:test_exact_solver.py

示例4: test_sample_ising

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_sample_ising(self):
        h = {0: 0.0, 1: 0.0, 2: 0.0}
        J = {(0, 1): -1.0, (1, 2): 1.0, (0, 2): 1.0}

        response = dimod.ExactSolver().sample_ising(h, J)

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

        # confirm vartype
        self.assertIs(response.vartype, dimod.SPIN)

        # check their energies
        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(energy, dimod.ising_energy(sample, h, J)) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:18,代码来源:test_exact_solver.py

示例5: test_sample_qubo

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_sample_qubo(self):
        Q = {(0, 0): 0.0, (1, 1): 0.0, (2, 2): 0.0}
        Q.update({(0, 1): -1.0, (1, 2): 1.0, (0, 2): 1.0})

        response = dimod.ExactSolver().sample_qubo(Q)

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

        # confirm vartype
        self.assertIs(response.vartype, dimod.BINARY)

        # check their energies
        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(energy, dimod.qubo_energy(sample, Q)) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:18,代码来源:test_exact_solver.py

示例6: test_sample_mixed_labels

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_sample_mixed_labels(self):
        h = {'3': 0.6669921875, 4: -2.0, 5: -1.334375, 6: 0.0, 7: -2.0, '1': 1.3328125,
             '2': -1.3330078125, '0': -0.666796875}
        J = {(5, '2'): 1.0, (7, '0'): 0.9998046875, (4, '0'): 0.9998046875, ('3', 4): 0.9998046875,
             (7, '1'): -1.0, (5, '1'): 0.6671875, (6, '2'): 1.0, ('3', 6): 0.6671875,
             (7, '2'): 0.9986328125, (5, '0'): -1.0, ('3', 5): -0.6671875, ('3', 7): 0.998828125,
             (4, '1'): -1.0, (6, '0'): -0.3328125, (4, '2'): 1.0, (6, '1'): 0.0}

        response = dimod.ExactSolver().sample_ising(h, J)

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

        # confirm vartype
        self.assertIs(response.vartype, dimod.SPIN)

        # check their energies
        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(energy, dimod.ising_energy(sample, h, J)) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:22,代码来源:test_exact_solver.py

示例7: test__spin_prod

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test__spin_prod(self):

        bqm = dimod.higherorder.utils._spin_product(['a', 'b', 'p', 'aux'])

        for v in ['a', 'b', 'p', 'aux']:
            self.assertIn(v, bqm)
        self.assertEqual(len(bqm), 4)  # has an auxiliary variable

        seen = set()
        samples = dimod.ExactSolver().sample(bqm)
        for sample, energy in samples.data(['sample', 'energy']):
            if energy == 0:
                self.assertEqual(sample['a'] * sample['b'], sample['p'])
                seen.add((sample['a'], sample['b'], sample['p']))
            if sample['a'] * sample['b'] != sample['p']:
                self.assertGreaterEqual(energy, 1)  # gap 1
        self.assertEqual(seen, {(-1, -1, +1),
                                (-1, +1, -1),
                                (+1, -1, -1),
                                (+1, +1, +1)}) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:22,代码来源:test_higherorder.py

示例8: test__binary_prod

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test__binary_prod(self):

        variables = ['a', 'b', 'p']

        bqm = dimod.higherorder.utils._binary_product(variables)

        for v in variables:
            self.assertIn(v, bqm)
        self.assertEqual(len(bqm), 3)

        seen = set()
        samples = dimod.ExactSolver().sample(bqm)
        for sample, energy in samples.data(['sample', 'energy']):
            if energy == 0:
                self.assertEqual(sample['a'] * sample['b'], sample['p'])
                seen.add((sample['a'], sample['b'], sample['p']))
            if sample['a'] * sample['b'] != sample['p']:
                self.assertGreaterEqual(energy, 1)  # gap 1
        self.assertEqual(seen, {(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 1)}) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:21,代码来源:test_higherorder.py

示例9: test_all_energies

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_all_energies(self):
        sampler = PolyScaleComposite(HigherOrderComposite(ExactSolver()))

        h = {'a': -1, 'b': 4}
        J = {'abc': -1, 'ab': 1, 'aaa': .5}

        sampleset = sampler.sample_hising(h, J, discard_unsatisfied=True)

        for sample, energy in sampleset.data(['sample', 'energy']):
            en = 0
            for v, bias in h.items():
                en += sample[v] * bias
            for term, bias in J.items():
                val = bias
                for v in term:
                    val *= sample[v]
                en += val

            self.assertAlmostEqual(energy, en) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:21,代码来源:test_polyscalecomposite.py

示例10: test_higherorder_spin

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_higherorder_spin(self):
        sampler = HigherOrderComposite(self.tracker)

        J = {'abc': -1, 'ab': 1}

        sampleset = sampler.sample_hising({}, J,
                                          initial_state={'a': 1, 'b': 1, 'c': -1})

        bqm = self.tracker.input['bqm']
        initial_state = self.tracker.input['initial_state']

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

        # make sure that the initial-state is minimzed over the product/aux
        mask = (samples[:, ['a', 'b', 'c']] == [1, 1, -1]).all(axis=1)
        for v, val in initial_state.items():
            if v in ['a', 'b', 'c']:
                continue
            self.assertTrue(samples[mask, [v]][0, 0], val) 
开发者ID:dwavesystems,项目名称:dimod,代码行数:21,代码来源:test_higherordercomposite.py

示例11: test_energies_discard

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_energies_discard(self):
        h = {'a': .1, 'b': 0, 'c': 0}
        J = {('a', 'b'): 1, ('b', 'c'): 1.3, ('a', 'c'): -1}
        bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=1.3)

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

        embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)

        embedded_response = dimod.ExactSolver().sample(embedded_bqm)

        chain_break_method = dwave.embedding.discard
        response = dwave.embedding.unembed_sampleset(embedded_response, embedding, bqm,
                                                   chain_break_method=chain_break_method)

        self.assertEqual(len(embedded_response) / 2, len(response))  # half chains should be broken

        for sample, energy in response.data(['sample', 'energy']):
            self.assertEqual(bqm.energy(sample), energy) 
开发者ID:dwavesystems,项目名称:dwave-system,代码行数:21,代码来源:test_embedding_transforms.py

示例12: test_unembed_sampleset_with_discard_matrix_typical

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_unembed_sampleset_with_discard_matrix_typical(self):
        h = {'a': .1, 'b': 0, 'c': 0}
        J = {('a', 'b'): 1, ('b', 'c'): 1.3, ('a', 'c'): -1}
        bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=1.3)

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

        embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)

        embedded_response = dimod.ExactSolver().sample(embedded_bqm)

        chain_break_method = dwave.embedding.discard
        response = dwave.embedding.unembed_sampleset(embedded_response, embedding, bqm,
                                                   chain_break_method=dwave.embedding.discard)

        self.assertEqual(len(embedded_response) / 2, len(response))  # half chains should be broken

        for sample, energy in response.data(['sample', 'energy']):
            self.assertEqual(bqm.energy(sample), energy) 
开发者ID:dwavesystems,项目名称:dwave-system,代码行数:21,代码来源:test_embedding_transforms.py

示例13: test_triangle_to_empty

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_triangle_to_empty(self):
        h = {'a': 1.2, 'b': 1, 'c': .5}
        J = {'ab': -.5, 'bc': -.5, 'ac': -.5}
        cutoff = .75
        cut = dimod.BinaryQuadraticModel.from_ising({}, {})

        # we cannot check in this case because all variables are isolated
        # this results in exactly one variable being sent to ExactSolver and
        # we don't know which one it will be, so we just check the correctness
        # of the output
        samples = CutOffComposite(dimod.ExactSolver(), cutoff).sample_ising(h, J)

        dimod.testing.assert_response_energies(samples, dimod.BinaryQuadraticModel.from_ising(h, J))

        # check that we picked the right minimizing value for the isolated
        self.assertEqual(samples.first.sample['a'], -1)
        self.assertEqual(samples.first.sample['b'], -1)
        self.assertEqual(samples.first.sample['c'], -1) 
开发者ID:dwavesystems,项目名称:dwave-system,代码行数:20,代码来源:test_cutoffcomposite.py

示例14: test_typical_cases

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_typical_cases(self):

        G = nx.complete_graph(10)

        S = dnx.maximum_cut(G, ExactSolver())
        self.assertTrue(len(S) == 5)  # half of the nodes

        with self.assertRaises(dnx.DWaveNetworkXException):
            S = dnx.weighted_maximum_cut(G, ExactSolver())

        nx.set_edge_attributes(G, 1, 'weight')
        S = dnx.weighted_maximum_cut(G, ExactSolver())
        self.assertTrue(len(S) == 5)  # half of the nodes

        G = nx.Graph()
        G.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (3, 4), (2, 4)])
        S = dnx.maximum_cut(G, ExactSolver())
        self.assertTrue(len(S) in (2, 3))

        # this needs another one for weight 
开发者ID:dwavesystems,项目名称:dwave_networkx,代码行数:22,代码来源:test_max_cut.py

示例15: test_maximum_independent_set_weighted

# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import ExactSolver [as 别名]
def test_maximum_independent_set_weighted(self):
        weight = 'weight'
        G = nx.path_graph(6)

        # favor odd nodes
        nx.set_node_attributes(G, {node: node % 2 + 1 for node in G}, weight)
        indep_set = dnx.maximum_weighted_independent_set(G, weight, dimod.ExactSolver())
        self.assertEqual(set(indep_set), {1, 3, 5})

        # favor even nodes
        nx.set_node_attributes(G, {node: (node + 1) % 2 + 1 for node in G}, weight)
        indep_set = dnx.maximum_weighted_independent_set(G, weight, dimod.ExactSolver())
        self.assertEqual(set(indep_set), {0, 2, 4})

        # make nodes 1 and 4 likely
        nx.set_node_attributes(G, {0: 1, 1: 3, 2: 1, 3: 1, 4: 3, 5: 1}, weight)
        indep_set = dnx.maximum_weighted_independent_set(G, weight, dimod.ExactSolver())
        self.assertEqual(set(indep_set), {1, 4}) 
开发者ID:dwavesystems,项目名称:dwave_networkx,代码行数:20,代码来源:test_independent_set.py


注:本文中的dimod.ExactSolver方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。