Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_typical(self):
sampler = dimod.SpinReversalTransformComposite(dimod.ExactSolver())
Q = {('a', 'a'): -1, ('b', 'b'): -1, ('a', 'b'): 2}
response = sampler.sample_qubo(Q, num_spin_reversal_transforms=100, spin_reversal_variables={'a'})
dimod.testing.assert_response_energies(response, dimod.BinaryQuadraticModel.from_qubo(Q))
def test_dumps_sortable_BINARY_with_header(self):
bqm = dimod.BinaryQuadraticModel.from_qubo({(0, 0): 1., (0, 1): 2, (2, 3): .4})
s = coo.dumps(bqm, vartype_header=True)
contents = "# vartype=BINARY\n0 0 1.000000\n0 1 2.000000\n2 3 0.400000"
self.assertEqual(s, contents)
def test_empty_bqm(self):
bqm = dimod.BinaryQuadraticModel.from_qubo({})
encoded = bqm_bson_encoder(bqm)
expected_encoding = {
'as_complete': False,
'linear': b'',
'quadratic_vals': b'',
'variable_type': 'BINARY',
'offset': 0.0,
'variable_order': [],
'index_dtype': '
def test_from_qubo(self):
Q = {('a', 'a'): 1, ('a', 'b'): -1}
bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
self.assertEqual(bqm, dimod.BinaryQuadraticModel({'a': 1}, {('a', 'b'): -1}, 0.0, dimod.BINARY))
def test_even_divisor(self):
bqm = dimod.BinaryQuadraticModel.from_qubo({edge: 1 for edge in dnx.chimera_graph(4).edges})
tiles = chimera_tiles(bqm, 2, 2, 4)
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.assertEqual(set(chain[0] for chain in embedding.values()), set(range(2*2*4*2)))
def test_embedding_with_extra_chains(self):
embedding = {0: [0, 1], 1: [2], 2: [3]}
G = nx.cycle_graph(4)
bqm = dimod.BinaryQuadraticModel.from_qubo({(0, 0): 1})
target_bqm = dimod.embed_bqm(bqm, embedding, G)
for v in itertools.chain(*embedding.values()):
self.assertIn(v, target_bqm)
def test_load(self):
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'coo_qubo.qubo')
with open(filepath, 'r') as fp:
bqm = coo.load(fp, dimod.BinaryQuadraticModel, dimod.BINARY)
self.assertEqual(bqm, dimod.BinaryQuadraticModel.from_qubo({(0, 0): -1, (1, 1): -1, (2, 2): -1, (3, 3): -1}))
def sample_qubo(self, Q):
bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
samples = [1]*len(bqm)
return dimod.SampleSet.from_samples_bqm(samples, bqm)
**params:
Parameters for the sampling method, solver-specific.
Returns:
:class:`Future`
Note:
To use this method, dimod package has to be installed.
"""
try:
import dimod
except ImportError: # pragma: no cover
raise RuntimeError("Can't use solver of type 'bqm' without dimod. "
"Re-install the library with 'bqm' support.")
bqm = dimod.BinaryQuadraticModel.from_qubo(qubo)
return self.sample_bqm(bqm, **params)