本文整理汇总了Python中dimod.SPIN属性的典型用法代码示例。如果您正苦于以下问题:Python dimod.SPIN属性的具体用法?Python dimod.SPIN怎么用?Python dimod.SPIN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类dimod
的用法示例。
在下文中一共展示了dimod.SPIN属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_sample_seq
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def random_sample_seq(size, vartype):
"""Return a random sample.
Args:
size (int):
Sample size (number of variables).
vartype (:class:`dimod.Vartype`):
Variable type; for example, `Vartype.SPIN`, `BINARY`, or `{-1, 1}`.
Returns:
dict: Random sample of `size` in length, with values from `vartype`.
Examples:
>>> random_sample_seq(4, dimod.BINARY) # doctest: +SKIP
{0: 0, 1: 1, 2: 0, 3: 0}
"""
values = list(vartype.value)
return {i: random.choice(values) for i in range(size)}
示例2: test_vstack
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_vstack(self):
a = SampleSet.from_samples(
[{'a': 1, 'b': 0, 'c': 0}, {'a': 0, 'b': 1, 'c': 0}], vartype='BINARY', energy=[3, 1])
b = SampleSet.from_samples(
[{'a': 0, 'b': 0, 'c': 1}, {'a': 1, 'b': 0, 'c': 1}], vartype='BINARY', energy=[2, 0])
c = SampleSet.from_samples(
[{'a': -1, 'b': 1, 'c': -1}], vartype='SPIN', energy=4)
m = a.vstack(b, c)
self.assertEqual(len(m), 5)
self.assertEqual(
list(m.samples()),
[
{'a': 1, 'b': 0, 'c': 1}, # b[1], en=0
{'a': 0, 'b': 1, 'c': 0}, # a[1], en=1
{'a': 0, 'b': 0, 'c': 1}, # b[0], en=2
{'a': 1, 'b': 0, 'c': 0}, # a[0], en=3
{'a': 0, 'b': 1, 'c': 0} # c[0] in BINARY, en=4
]
)
示例3: test_updated
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_updated(self):
a = SampleSet.from_samples([1,0,1], 'SPIN', 0)
b = SampleSet.from_samples([0,1,0], 'SPIN', 0)
s1 = State(samples=a)
s2 = State(samples=b, emb={'a': {'b': 1}}, debug={'x': 1})
s3 = State(debug={'x': {'y': {'z': [1]}}})
# test simple replace
self.assertDictEqual(s1.updated(), s1)
self.assertDictEqual(s1.updated(samples=b), State(samples=b))
self.assertDictEqual(s2.updated(emb={'b': 1}).emb, {'b': 1})
self.assertDictEqual(s1.updated(samples=b, debug=dict(x=1), emb={'a': {'b': 1}}), s2)
# test recursive merge of `debug`
self.assertDictEqual(s1.updated(debug=dict(x=1)).debug, {'x': 1})
self.assertDictEqual(s2.updated(debug=dict(x=2)).debug, {'x': 2})
self.assertDictEqual(s2.updated(debug=dict(y=2)).debug, {'x': 1, 'y': 2})
self.assertDictEqual(s2.updated(debug=dict(y=2)).debug, {'x': 1, 'y': 2})
self.assertDictEqual(s3.updated(debug={'x': {'y': {'z': [2]}}}).debug, {'x': {'y': {'z': [2]}}})
self.assertDictEqual(s3.updated(debug={'x': {'y': {'w': 2}}}).debug, {'x': {'y': {'z': [1], 'w': 2}}})
# test clear
self.assertEqual(s2.updated(emb=None).emb, None)
self.assertEqual(s2.updated(debug=None).debug, None)
示例4: test_pfs_on_impactful_far_subproblem
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [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'))
示例5: test_typical_construction
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [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)
示例6: test_next_on_different_sized_constraints
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [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
示例7: test_spread
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_spread(self):
energies = [1, 2]
occurrences = [3, 2]
sampleset = dimod.SampleSet.from_samples(
[{'a': 1}, {'a': 2}], dimod.SPIN,
energy=energies, num_occurrences=occurrences)
state = State(samples=sampleset)
result = AggregatedSamples(aggregate=False).run(state).result()
# we'll have n=5 samples
n = sum(occurrences)
self.assertEqual(len(result.samples), n)
# samples, energies and num_occurrences must be expanded
np.testing.assert_array_equal(result.samples.record.sample,
np.array([[1], [1], [1], [2], [2]]))
np.testing.assert_array_equal(result.samples.record.energy,
np.array([1, 1, 1, 2, 2]))
np.testing.assert_array_equal(result.samples.record.num_occurrences,
np.ones(n))
# variables should stay the same
self.assertEqual(list(sampleset.variables), list(result.samples.variables))
示例8: test_energy_threshold_termination
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [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)
示例9: test_beta_use
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_beta_use(self):
ss = dimod.SampleSet.from_samples([{'a': 0}], energy=[0], vartype='SPIN')
state = hybrid.State(samples=ss)
# beta not given at all
with self.assertRaises(ValueError):
res = EnergyWeightedResampler().run(state).result()
# beta given on construction
res = EnergyWeightedResampler(beta=0).run(state).result()
self.assertEqual(res.samples.info['beta'], 0)
# beta given on runtime, to run method
res = EnergyWeightedResampler().run(state, beta=1).result()
self.assertEqual(res.samples.info['beta'], 1)
# beta given in state
state.beta = 2
res = EnergyWeightedResampler().run(state).result()
self.assertEqual(res.samples.info['beta'], 2)
示例10: test_all_three_functional
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_all_three_functional(self):
builtin = [0, 'a', [0, 'a']]
num_variables = 100
num_samples = 100
samples = 2*np.triu(np.ones((num_samples, num_variables)), -4) - 1
bqm = dimod.BinaryQuadraticModel.from_ising({v: .1*v for v in range(num_variables)}, {})
sampleset = dimod.SampleSet.from_samples_bqm(samples, bqm)
linear = {'a': -1, 4: 1, ('a', "complex key"): 3}
quadratic = {('a', 'c'): 3, ('b', 'c'): -3., ('a', 3): -1}
bqm = dimod.BinaryQuadraticModel(linear, quadratic, 3, dimod.SPIN)
obj = [builtin, sampleset, bqm]
# no encoder, uses ._asdict
new = simplejson.loads(simplejson.dumps(obj), object_hook=dimod_object_hook)
self.assertEqual(obj, new)
示例11: test_x_vartype
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_x_vartype(self):
samples = {'a': -1, 'b': 1}
bqm = dimod.BQM.from_qubo({'ab': 1})
init = Initialized().parse_initial_states(
bqm=bqm, initial_states=samples, num_reads=10)
self.assertIs(init.initial_states.vartype, dimod.BINARY)
arr = init.initial_states.record.sample
self.assertTrue(((arr == 1) ^ (arr == 0)).all())
samples = {'a': 0, 'b': 1}
bqm = dimod.BQM.from_ising({}, {'ab': 1})
init = Initialized().parse_initial_states(
bqm=bqm, initial_states=samples, num_reads=10)
self.assertIs(init.initial_states.vartype, dimod.SPIN)
arr = init.initial_states.record.sample
self.assertTrue(((arr == 1) ^ (arr == -1)).all())
示例12: test_contains
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_contains(self, name, BQM):
bqm = BQM({0: 1.0}, {(0, 1): 2.0, (2, 1): 0.4}, 0.0, dimod.SPIN)
self.assertIn(0, bqm.adj[1])
self.assertEqual(2.0, bqm.adj[1][0])
self.assertIn(1, bqm.adj[0])
self.assertEqual(2.0, bqm.adj[0][1])
self.assertIn(2, bqm.adj[1])
self.assertEqual(.4, bqm.adj[1][2])
self.assertIn(1, bqm.adj[2])
self.assertEqual(.4, bqm.adj[2][1])
self.assertNotIn(2, bqm.adj[0])
with self.assertRaises(KeyError):
bqm.adj[0][2]
self.assertNotIn(0, bqm.adj[2])
with self.assertRaises(KeyError):
bqm.adj[2][0]
示例13: test_bqm_binary_to_spin
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_bqm_binary_to_spin(self):
linear = {'a': -1, 'b': 1, 0: 1.5}
quadratic = {(0, 1): -1, (0, 0): 1, (1, 2): 1.5, (2, 2): 4, (4, 5): 7}
offset = 0
vartype = dimod.BINARY
for source, target in itertools.product(BQM_SUBCLASSES, repeat=2):
with self.subTest(source=source, target=target):
bqm = source(linear, quadratic, offset, vartype)
new = target(bqm, vartype=dimod.SPIN)
self.assertIsInstance(new, target)
assert_consistent_bqm(new)
self.assertEqual(new.vartype, dimod.SPIN)
# change back for equality check
new.change_vartype(dimod.BINARY)
self.assertEqual(bqm.adj, new.adj)
self.assertEqual(bqm.offset, new.offset)
self.assertEqual(bqm.vartype, new.vartype)
示例14: test_bqm_spin_to_binary
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_bqm_spin_to_binary(self):
linear = {'a': -1, 'b': 1, 0: 1.5}
quadratic = {(0, 1): -1, (0, 0): 1, (1, 2): 1.5, (2, 2): 4, (4, 5): 7}
offset = 0
vartype = dimod.SPIN
for source, target in itertools.product(BQM_SUBCLASSES, repeat=2):
with self.subTest(source=source, target=target):
bqm = source(linear, quadratic, offset, vartype)
new = target(bqm, vartype=dimod.BINARY)
self.assertIsInstance(new, target)
assert_consistent_bqm(new)
self.assertEqual(new.vartype, dimod.BINARY)
# change back for equality check
new.change_vartype(dimod.SPIN)
self.assertEqual(bqm.adj, new.adj)
self.assertEqual(bqm.offset, new.offset)
self.assertEqual(bqm.vartype, new.vartype)
示例15: test_vartype
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import SPIN [as 别名]
def test_vartype(self, name, BQM):
bqm = BQM('SPIN')
self.assertEqual(bqm.vartype, dimod.SPIN)
bqm = BQM(dimod.SPIN)
self.assertEqual(bqm.vartype, dimod.SPIN)
bqm = BQM((-1, 1))
self.assertEqual(bqm.vartype, dimod.SPIN)
bqm = BQM('BINARY')
self.assertEqual(bqm.vartype, dimod.BINARY)
bqm = BQM(dimod.BINARY)
self.assertEqual(bqm.vartype, dimod.BINARY)
bqm = BQM((0, 1))
self.assertEqual(bqm.vartype, dimod.BINARY)