本文整理汇总了Python中dimod.BINARY属性的典型用法代码示例。如果您正苦于以下问题:Python dimod.BINARY属性的具体用法?Python dimod.BINARY怎么用?Python dimod.BINARY使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类dimod
的用法示例。
在下文中一共展示了dimod.BINARY属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: select_random_subgraph
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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_seq
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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)}
示例3: random_sample
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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}
示例4: min_sample
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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}
示例5: max_sample
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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}
示例6: test_string_labels
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def test_string_labels(self):
si, sj, st = 2, 2, 3
ti, tj, tt = 1, 1, 4
alpha = 'abcdefghijklmnopqrstuvwxyz'
bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)
for u, v in reversed(list(dnx.chimera_graph(si, sj, st).edges)):
bqm.add_interaction(alpha[u], alpha[v], 1)
tiles = chimera_tiles(bqm, ti, tj, tt)
self.assertEqual(len(tiles), 4) # we have the correct number of tiles
self.assertEqual(set().union(*tiles.values()), bqm.variables) # all of the nodes are present
for embedding in tiles.values():
self.assertTrue(set(chain[0] for chain in embedding.values()).issubset(set(range(ti*tj*tt*2))))
示例7: generate_random_chimera_problem
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def generate_random_chimera_problem(adjacency, h_range, j_range, offset=0, vartype=dimod.BINARY):
"""Generate a random chimera problem, with
h int chosen randomly from h_range, j int chosen randomly from j_range.
Typically: h_range = [0, 0] and j_range = [-k, +k].
Args:
adjacency (dict[/{node: {neighbor_node_1, ...}}): Adjacency dictionary
h_range (tuple/(upper,lower)): bounds for h
j_range (tuple/(upper,lower)): bounds for j
offset (float): energy offset
vartype (dimod.Vartype): BQM's vartype
Returns:
dimod.BinaryQuadraticModel
"""
h = {n: random.randint(*h_range) for n in adjacency.keys()}
J = {(n,e): random.randint(*j_range)
for n, edges in adjacency.items()
for e in edges
if e > n}
return dimod.BinaryQuadraticModel(h, J, offset, vartype)
示例8: generate_chimera
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def generate_chimera(size, vartype, count, fmt, outdir):
"""Generate `count` of random Chimera-structured problems
with `size` topology, with zero biases and random J's in +/-k range
(where k goes from 1 to `count`).
"""
def store(bqm, fp):
if fmt == 'coo':
fp.write(bqm.to_coo(vartype_header=True))
elif fmt == 'json':
fp.write(bqm.to_json())
ext = {'SPIN': 'ising', 'BINARY': 'qubo'}
adj = dnx.chimera_graph(*size).adj
for k in range(1, count+1):
bqm = generate_random_chimera_problem(adj, (0, 0), (-k, k), vartype=vartype)
if outdir:
path = os.path.join(outdir, '{}.{:0>2}.{}'.format(len(bqm), k, ext[vartype]))
with open(path, 'w') as fp:
store(bqm, fp)
else:
store(bqm, sys.stdout)
示例9: test_x_vartype
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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())
示例10: test_array_like
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def test_array_like(self, name, BQM):
D = np.ones((5, 5)).tolist()
bqm = BQM(D, 'BINARY')
assert_consistent_bqm(bqm)
for u, v in itertools.combinations(range(5), 2):
self.assertEqual(bqm.get_quadratic(u, v), 2) # added
for u in range(5):
self.assertEqual(bqm.get_linear(u), 1)
# with explicit kwarg
bqm = BQM(D, vartype='BINARY')
assert_consistent_bqm(bqm)
for u, v in itertools.combinations(range(5), 2):
self.assertEqual(bqm.get_quadratic(u, v), 2) # added
for u in range(5):
self.assertEqual(bqm.get_linear(u), 1)
示例11: test_bqm_binary_to_spin
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [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)
示例12: test_vartype_only
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def test_vartype_only(self, name, BQM):
bqm = BQM('SPIN')
self.assertEqual(bqm.vartype, dimod.SPIN)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
bqm = BQM(vartype='SPIN')
self.assertEqual(bqm.vartype, dimod.SPIN)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
bqm = BQM('BINARY')
self.assertEqual(bqm.vartype, dimod.BINARY)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
bqm = BQM(vartype='BINARY')
self.assertEqual(bqm.vartype, dimod.BINARY)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
示例13: test_coo_functional_file_empty_BINARY
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def test_coo_functional_file_empty_BINARY(self, name, BQM):
if not BQM.shapeable():
return
bqm = BQM.empty(dimod.BINARY)
tmpdir = tempfile.mkdtemp()
filename = path.join(tmpdir, 'test.qubo')
with open(filename, 'w') as file:
bqm.to_coo(file)
with open(filename, 'r') as file:
new_bqm = BQM.from_coo(file, dimod.BINARY)
shutil.rmtree(tmpdir)
self.assertEqual(bqm, new_bqm)
示例14: test_to_ising_binary_to_ising
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def test_to_ising_binary_to_ising(self, name, BQM):
linear = {0: 7.1, 1: 103}
quadratic = {(0, 1): .97}
offset = 0.3
vartype = dimod.BINARY
model = BQM(linear, quadratic, offset, vartype)
h, J, off = model.to_ising()
for spins in itertools.product((-1, 1), repeat=len(model)):
spin_sample = dict(zip(range(len(spins)), spins))
bin_sample = {v: (s + 1) // 2 for v, s in spin_sample.items()}
# calculate the qubo's energy
energy = off
for (u, v), bias in J.items():
energy += spin_sample[u] * spin_sample[v] * bias
for v, bias in h.items():
energy += spin_sample[v] * bias
# and the energy of the model
self.assertAlmostEqual(energy, model.energy(bin_sample))
示例15: bqm_edges_between_variables
# 需要导入模块: import dimod [as 别名]
# 或者: from dimod import BINARY [as 别名]
def bqm_edges_between_variables(bqm, variables):
"""Return edges connecting specified variables of a binary quadratic model.
Args:
bqm (:class:`dimod.BinaryQuadraticModel`):
Binary quadratic model (BQM).
variables (list/set):
Subset of variables in the BQM.
Returns:
list: All edges connecting `variables` as tuples plus the variables
themselves as tuples (v, v).
Examples:
This example returns connecting edges between 3 nodes of a BQM based on
a 4-variable path graph.
>>> import dimod
>>> bqm = dimod.BQM({}, {(0, 1): 1, (1, 2): 1, (2, 3): 1}, 0, 'BINARY')
>>> bqm_edges_between_variables(bqm, {0, 1, 3})
[(0, 1), (0, 0), (1, 1), (3, 3)]
"""
variables = set(variables)
edges = [(start, end) for (start, end), coupling in bqm.quadratic.items()
if start in variables and end in variables]
edges.extend((v, v) for v in bqm.linear if v in variables)
return edges