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_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()))
def test_sample(self):
sampler = dimod.TrackingComposite(dimod.ExactSolver())
bqm = dimod.BinaryQuadraticModel.from_ising({'a': -1}, {})
ss = sampler.sample(bqm)
self.assertEqual(sampler.input, dict(bqm=bqm))
self.assertEqual(sampler.output, ss)
def test_ignore_zero_interactions(self):
h = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
J0 = {'ab': 0, 'bc': -1}
J1 = {'cb': -1, 'cd': 0}
bqm0 = dimod.BinaryQuadraticModel.from_ising(h, J0)
bqm1 = dimod.BinaryQuadraticModel.from_ising(h, J1)
with self.assertRaises(AssertionError):
dimod.testing.assert_bqm_almost_equal(bqm0, bqm1)
dimod.testing.assert_bqm_almost_equal(bqm0, bqm1, ignore_zero_interactions=True)
with self.assertRaises(AssertionError):
dimod.testing.assert_bqm_almost_equal(bqm1, bqm0)
dimod.testing.assert_bqm_almost_equal(bqm1, bqm0, ignore_zero_interactions=True)
def test_unlike_offset(self):
bqm0 = dimod.BinaryQuadraticModel.from_ising({'a': -1}, {}, 1.1)
bqm1 = dimod.BinaryQuadraticModel.from_ising({'a': -1}, {}, 1.2)
with self.assertRaises(AssertionError):
dimod.testing.assert_bqm_almost_equal(bqm0, bqm1)
dimod.testing.assert_bqm_almost_equal(bqm0, bqm1, places=0)
def test_from_numpy_vectors_labels(self):
h = np.array([-1, 1, 5])
heads = np.array([0, 1])
tails = np.array([1, 2])
values = np.array([-1, +1])
bqm = dimod.BinaryQuadraticModel.from_numpy_vectors(h, (heads, tails, values), 0.0, dimod.SPIN,
variable_order=['a', 'b', 'c'])
self.assertEqual(bqm, dimod.BinaryQuadraticModel.from_ising({'a': -1, 'b': 1, 'c': 5},
{('a', 'b'): -1, ('b', 'c'): 1}))
def test_to_numpy_vectors(self):
bqm = dimod.BinaryQuadraticModel.from_ising({}, {(0, 1): .5, (2, 3): -1, (0, 3): 1.5})
h, (i, j, values), off = bqm.to_numpy_vectors()
self.assertEqual(len(h), len(bqm.linear))
for idx, bias in enumerate(h):
self.assertAlmostEqual(bias, bqm.linear[idx])
self.assertEqual(len(i), len(j))
self.assertEqual(len(j), len(values))
self.assertEqual(len(values), len(bqm.quadratic))
for u, v, bias in zip(i, j, values):
self.assertIn(u, bqm.adj)
self.assertIn(v, bqm.adj[u])
self.assertAlmostEqual(bias, bqm.adj[u][v])
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
def test_unlike_interactions(self):
bqm0 = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1})
bqm1 = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1.1})
with self.assertRaises(AssertionError):
dimod.testing.assert_bqm_almost_equal(bqm0, bqm1)
dimod.testing.assert_bqm_almost_equal(bqm0, bqm1, places=0)
def test_from_ising(self):
h = {'a': 1}
J = {('a', 'b'): -1}
bqm = dimod.BinaryQuadraticModel.from_ising(h, J)
self.assertEqual(bqm, dimod.BinaryQuadraticModel({'a': 1}, {('a', 'b'): -1}, 0.0, dimod.SPIN))
#
# h list
h = [-1, 1]
J = {(0, 1): 1}
bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=1)
self.assertEqual(bqm, dimod.BinaryQuadraticModel({0: -1, 1: 1}, {(0, 1): 1}, 1, dimod.SPIN))
**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_ising(linear, quadratic)
return self.sample_bqm(bqm, **params)