本文整理匯總了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)
示例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)
示例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)
示例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))
示例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))
示例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))
示例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)})
示例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)})
示例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)
示例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)
示例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)
示例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)
示例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)
示例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
示例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})