本文整理匯總了Python中dimod.BinaryQuadraticModel方法的典型用法代碼示例。如果您正苦於以下問題:Python dimod.BinaryQuadraticModel方法的具體用法?Python dimod.BinaryQuadraticModel怎麽用?Python dimod.BinaryQuadraticModel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dimod
的用法示例。
在下文中一共展示了dimod.BinaryQuadraticModel方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: select_random_subgraph
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def select_random_subgraph(bqm, n):
"""Select randomly `n` variables of the specified binary quadratic model.
Args:
bqm (:class:`dimod.BinaryQuadraticModel`):
Binary quadratic model (BQM).
n (int):
Number of requested variables. Must be between 0 and `len(bqm)`.
Returns:
list: `n` variables selected randomly from the BQM.
Examples:
This example returns 2 variables of a 4-variable BQM.
>>> import dimod
>>> bqm = dimod.BQM({}, {'ab': 0, 'bc': 1, 'cd': 2}, 0, 'BINARY')
>>> select_random_subgraph(bqm, 2) # doctest: +SKIP
['d', 'b']
"""
return random.sample(bqm.linear.keys(), n)
示例2: random_sample
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def random_sample(bqm):
"""Return a random sample for a binary quadratic model.
Args:
bqm (:obj:`.BinaryQuadraticModel`):
Binary quadratic model (BQM).
Returns:
dict: A sample with random values for the BQM.
Examples:
>>> import dimod
>>> bqm = dimod.BQM({}, {'ab': -1, 'bc': -1, 'ca': -1}, 0, 'BINARY')
>>> random_sample(bqm) # doctest: +SKIP
{'a': 0, 'b': 1, 'c': 1}
"""
values = list(bqm.vartype.value)
return {i: random.choice(values) for i in bqm.variables}
示例3: min_sample
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def min_sample(bqm):
"""Return a sample with all variables set to the minimal value for a binary
quadratic model.
Args:
bqm (:obj:`.BinaryQuadraticModel`):
Binary quadratic model (BQM).
Returns:
dict: A sample with minimal values for all variables of the BQM.
Examples:
>>> import dimod
>>> bqm = dimod.BQM({}, {'ab': -1, 'bc': -1, 'ca': -1}, 0, 'BINARY')
>>> min_sample(bqm) # doctest: +SKIP
{'a': 0, 'b': 0, 'c': 0}
"""
value = min(bqm.vartype.value)
return {i: value for i in bqm.variables}
示例4: max_sample
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def max_sample(bqm):
"""Return a sample with all variables set to the maximal value for a binary
quadratic model.
Args:
bqm (:obj:`.BinaryQuadraticModel`):
Binary quadratic model (BQM).
Returns:
dict: A sample with maximal values for all variables of the BQM.
Examples:
>>> import dimod
>>> bqm = dimod.BQM({}, {'ab': -1, 'bc': -1, 'ca': -1}, 0, 'BINARY')
>>> max_sample(bqm) # doctest: +SKIP
{'a': 1, 'b': 1, 'c': 1}
"""
value = max(bqm.vartype.value)
return {i: value for i in bqm.variables}
示例5: test_clique_embedder
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_clique_embedder(self):
bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ca': 1})
init = State.from_subproblem(bqm)
sampler = MockDWaveSampler()
workflow = SubproblemCliqueEmbedder(sampler=sampler)
# run embedding
res = workflow.run(init).result()
# verify mock sampler received custom kwargs
self.assertIn('embedding', res)
self.assertEqual(len(res.embedding.keys()), 3)
# embedding a triangle onto a chimera produces 3 x 2-qubit chains
self.assertTrue(all(len(e) == 2 for e in res.embedding.values()))
示例6: test_custom_qpu_params
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_custom_qpu_params(self):
bqm = dimod.BinaryQuadraticModel.from_ising({'a': 1}, {})
init = State.from_subproblem(bqm)
# define a mock sampler that exposes some parameters of interest
mock_sampler = mock.MagicMock()
mock_sampler.parameters = {
'num_reads': [], 'chain_strength': [], 'future_proof': []}
qpu_params = dict(chain_strength=2, future_proof=True)
workflow = QPUSubproblemAutoEmbeddingSampler(
num_reads=10, qpu_sampler=mock_sampler, sampling_params=qpu_params)
# run mock sampling
workflow.run(init).result()
# verify mock sampler received custom kwargs
mock_sampler.sample.assert_called_once_with(
bqm, num_reads=10, **qpu_params)
示例7: test_tabu_problem_sampler_initialization
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_tabu_problem_sampler_initialization(self):
bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, 'SPIN')
sampleset = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
{'a': -1, 'b': 1}], bqm)
state = State(problem=bqm, samples=sampleset)
# with timeout=0, TabuSampler should just return the initial_states
result = TabuProblemSampler(timeout=0).run(state).result()
expected = sampleset.record.sample
self.assertTrue(np.array_equal(result.samples.record.sample, expected))
self.assertEqual(len(result.samples), 2)
# test input samples are tiled
result = TabuProblemSampler(timeout=0, num_reads=4,
initial_states_generator="tile").run(state).result()
expected = np.tile(sampleset.record.sample, (2,1))
self.assertTrue(np.array_equal(result.samples.record.sample, expected))
self.assertEqual(len(result.samples), 4)
示例8: test_tabu_subproblem_sampler_initialization
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_tabu_subproblem_sampler_initialization(self):
bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, 'SPIN')
sampleset = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
{'a': -1, 'b': 1}], bqm)
state = State(subproblem=bqm, subsamples=sampleset)
# with timeout=0, TabuSampler should just return the initial_states
result = TabuSubproblemSampler(timeout=0).run(state).result()
expected = sampleset.record.sample
self.assertTrue(np.array_equal(result.subsamples.record.sample, expected))
self.assertEqual(len(result.subsamples), 2)
# test input samples are tiled
result = TabuSubproblemSampler(timeout=0, num_reads=4,
initial_states_generator="tile").run(state).result()
expected = np.tile(sampleset.record.sample, (2,1))
self.assertTrue(np.array_equal(result.subsamples.record.sample, expected))
self.assertEqual(len(result.subsamples), 4)
示例9: test_sa_problem_sampler_initialization
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_sa_problem_sampler_initialization(self):
bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, 'SPIN')
sampleset = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
{'a': -1, 'b': 1}], bqm)
state = State(problem=bqm, samples=sampleset)
# with timeout=0, TabuSampler should just return the initial_states
result = SimulatedAnnealingProblemSampler(num_sweeps=0).run(state).result()
expected = sampleset.record.sample
self.assertTrue(np.array_equal(result.samples.record.sample, expected))
self.assertEqual(len(result.samples), 2)
# test input samples are tiled
result = SimulatedAnnealingProblemSampler(
num_sweeps=0, num_reads=4, initial_states_generator="tile").run(state).result()
expected = np.tile(sampleset.record.sample, (2,1))
self.assertTrue(np.array_equal(result.samples.record.sample, expected))
self.assertEqual(len(result.samples), 4)
示例10: test_sa_subproblem_sampler_initialization
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_sa_subproblem_sampler_initialization(self):
bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, 'SPIN')
sampleset = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
{'a': -1, 'b': 1}], bqm)
state = State(subproblem=bqm, subsamples=sampleset)
# with timeout=0, TabuSampler should just return the initial_states
result = SimulatedAnnealingSubproblemSampler(num_sweeps=0).run(state).result()
expected = sampleset.record.sample
self.assertTrue(np.array_equal(result.subsamples.record.sample, expected))
self.assertEqual(len(result.subsamples), 2)
# test input samples are tiled
result = SimulatedAnnealingSubproblemSampler(
num_sweeps=0, num_reads=4, initial_states_generator="tile").run(state).result()
expected = np.tile(sampleset.record.sample, (2,1))
self.assertTrue(np.array_equal(result.subsamples.record.sample, expected))
self.assertEqual(len(result.subsamples), 4)
示例11: test_sampling_params
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_sampling_params(self):
# a simple runnable that optionally resets the output samples
class Workflow(Runnable):
def next(self, state, reset=False):
if reset:
return state.updated(samples=hybrid.SampleSet.empty())
return state
bqm = dimod.BinaryQuadraticModel.from_ising({'a': 1}, {})
sampler = HybridSampler(Workflow())
# control run
ss = sampler.sample(bqm)
self.assertEqual(len(list(ss.samples())), 1)
# test runopts propagation
ss = sampler.sample(bqm, reset=True)
self.assertEqual(len(list(ss.samples())), 0)
示例12: test_pfs_on_impactful_far_subproblem
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_pfs_on_impactful_far_subproblem(self):
# problem graph has two components, each one is 4-node cycle graph
# variable flip energy gains order variables: a, h..b
edges = {'ab': 1, 'bc': 1, 'cd': 1, 'da': 1,
'ef': 1, 'fg': 1, 'gh': 1, 'he': 1,
'de': 0}
biases = dict(zip(string.ascii_letters, range(8)))
biases['a'] += 10
bqm = dimod.BinaryQuadraticModel(biases, edges, 0.0, 'SPIN')
sample = {i: -1 for i in bqm.variables}
state = State.from_sample(sample, bqm)
eid = EnergyImpactDecomposer(size=5, traversal='pfs')
result = eid.run(state).result()
# move towards second cluster and pick the highest energy variables from there
self.assertEqual(set(result.subproblem.variables), set('adehg'))
示例13: test_typical_construction
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_typical_construction(self):
bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)
variables = list('abcdefg')
constraints = []
for triplet in itertools.combinations(variables, 3):
for u, v in itertools.combinations(triplet, 2):
bqm.add_interaction(u, v, -1)
constraints.append(triplet)
rcd = RandomConstraintDecomposer(3, constraints)
rcd.init(state=State.from_sample(min_sample(bqm), bqm))
# check that the graph is complete
G = rcd.constraint_graph
for i in range(len(constraints)):
self.assertIn(i, G.nodes)
示例14: test_next
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_next(self):
bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)
variables = list('abcdefg')
constraints = []
for triplet in itertools.combinations(variables, 3):
for u, v in itertools.combinations(triplet, 2):
bqm.add_interaction(u, v, -1)
constraints.append(triplet)
rcd = RandomConstraintDecomposer(3, constraints)
state = State.from_sample(min_sample(bqm), bqm)
newstate = rcd.run(state).result()
self.assertIn('subproblem', newstate)
self.assertTrue(len(newstate.subproblem) <= 3) # correct size
示例15: test_next_on_different_sized_constraints
# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BinaryQuadraticModel [as 別名]
def test_next_on_different_sized_constraints(self):
bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)
variables = list('abcdefg')
fixed_variables = list('abc')
size = 3
constraints = []
# Set BQM and constraints of varying lengths
for triplet in itertools.combinations(variables, size):
for u, v in itertools.combinations(triplet, 2):
bqm.add_interaction(u, v, -1)
non_fixed_variables = set(triplet) - set(fixed_variables)
constraints.append(non_fixed_variables)
for fixed_variable in fixed_variables:
bqm.fix_variable(fixed_variable, 1)
# Get new state
rcd = RandomConstraintDecomposer(size, constraints)
state = State.from_sample(min_sample(bqm), bqm)
newstate = rcd.run(state).result()
self.assertIn('subproblem', newstate)
self.assertTrue(len(newstate.subproblem) <= size) # correct size