def test_many_upload_failures(self): """Failure handling in high concurrency mode works correctly.""" # build a test problem bqm = dimod.BQM.from_ising({}, {'ab': 1}) # use a global mocked session, so we can modify it on-fly session = mock.Mock() # upload is now part of submit, so we need to mock it mock_upload_exc = ValueError('error') def mock_upload(self, bqm): return Present(exception=mock_upload_exc) # construct a functional solver by mocking client and api response data with mock.patch.object(Client, 'create_session', lambda self: session): with Client('endpoint', 'token') as client: with mock.patch.object(BaseUnstructuredSolver, 'upload_problem', mock_upload): solver = BQMSolver(client, unstructured_solver_data()) futs = [solver.sample_bqm(bqm) for _ in range(100)] for fut in futs: with self.assertRaises(type(mock_upload_exc)): fut.result()
def test_label_is_received(self, name, label): """Problem label is set from response in result/sampleset.""" # build a test problem bqm = dimod.BQM.from_ising({}, {'ab': 1}) # use a global mocked session, so we can modify it on-fly session = mock.Mock() # upload is now part of submit, so we need to mock it mock_problem_id = 'mock-problem-id' def mock_upload(self, bqm): return Present(result=mock_problem_id) # construct a functional solver by mocking client and api response data with mock.patch.multiple(Client, create_session=lambda self: session, upload_problem_encoded=mock_upload): with Client('endpoint', 'token') as client: solver = BQMSolver(client, unstructured_solver_data()) # construct mock response ss = dimod.ExactSolver().sample(bqm) session.post = lambda path, _: choose_reply( path, { 'problems/': complete_reply(ss, id_=mock_problem_id, label=label) }) # sample and verify label fut = solver.sample_bqm(bqm, label=label) info = fut.sampleset.info self.assertIn('problem_id', info) if label is not None: self.assertIn('problem_label', info) self.assertEqual(info['problem_label'], label)
def test_label_is_sent(self, name, label): """Problem label is set on problem submit.""" bqm = dimod.BQM.from_ising({}, {'ab': 1}) # use a global mocked session, so we can modify it on-fly session = mock.Mock() # upload is now part of submit, so we need to mock it mock_problem_id = 'mock-problem-id' def mock_upload(self, bqm): return Present(result=mock_problem_id) # construct a functional solver by mocking client and api response data with mock.patch.multiple(Client, create_session=lambda self: session, upload_problem_encoded=mock_upload): with Client('endpoint', 'token') as client: solver = BQMSolver(client, unstructured_solver_data()) problems = [("sample_ising", (bqm.linear, bqm.quadratic)), ("sample_qubo", (bqm.quadratic, )), ("sample_bqm", (bqm, ))] for method_name, problem_args in problems: with self.subTest(method_name=method_name): sample = getattr(solver, method_name) with mock.patch.object( Client, '_submit', self.on_submit_label_verifier(label)): with self.assertRaises( self.PrimaryAssertionSatisfied): sample(*problem_args, label=label).result()
def test_sample_bqm_immediate_reply(self): """Construction of and sampling from an unstructured BQM solver works.""" # build a test problem bqm = dimod.BQM.from_ising({}, {'ab': 1}) # use a global mocked session, so we can modify it on-fly session = mock.Mock() # upload is now part of submit, so we need to mock it mock_problem_id = 'mock-problem-id' def mock_upload(self, bqm): return Present(result=mock_problem_id) # construct a functional solver by mocking client and api response data with mock.patch.multiple(Client, create_session=lambda self: session, upload_problem_encoded=mock_upload): with Client('endpoint', 'token') as client: solver = BQMSolver(client, unstructured_solver_data()) # make sure this still works _ = UnstructuredSolver(client, unstructured_solver_data()) # direct bqm sampling ss = dimod.ExactSolver().sample(bqm) ss.info.update(problem_id=mock_problem_id) session.post = lambda path, _: choose_reply( path, {'problems/': complete_reply(ss, id_=mock_problem_id)}) fut = solver.sample_bqm(bqm) numpy.testing.assert_array_equal(fut.sampleset, ss) numpy.testing.assert_array_equal(fut.samples, ss.record.sample) numpy.testing.assert_array_equal(fut.energies, ss.record.energy) numpy.testing.assert_array_equal(fut.num_occurrences, ss.record.num_occurrences) # submit of pre-uploaded bqm problem fut = solver.sample_bqm(mock_problem_id) numpy.testing.assert_array_equal(fut.sampleset, ss) numpy.testing.assert_array_equal(fut.samples, ss.record.sample) numpy.testing.assert_array_equal(fut.energies, ss.record.energy) numpy.testing.assert_array_equal(fut.num_occurrences, ss.record.num_occurrences) # ising sampling lin, quad, _ = bqm.to_ising() ss = dimod.ExactSolver().sample_ising(lin, quad) ss.info.update(problem_id=mock_problem_id) session.post = lambda path, _: choose_reply( path, {'problems/': complete_reply(ss, id_=mock_problem_id)}) fut = solver.sample_ising(lin, quad) numpy.testing.assert_array_equal(fut.sampleset, ss) numpy.testing.assert_array_equal(fut.samples, ss.record.sample) numpy.testing.assert_array_equal(fut.energies, ss.record.energy) numpy.testing.assert_array_equal(fut.num_occurrences, ss.record.num_occurrences) # qubo sampling qubo, _ = bqm.to_qubo() ss = dimod.ExactSolver().sample_qubo(qubo) ss.info.update(problem_id=mock_problem_id) session.post = lambda path, _: choose_reply( path, {'problems/': complete_reply(ss, id_=mock_problem_id)}) fut = solver.sample_qubo(qubo) numpy.testing.assert_array_equal(fut.sampleset, ss) numpy.testing.assert_array_equal(fut.samples, ss.record.sample) numpy.testing.assert_array_equal(fut.energies, ss.record.energy) numpy.testing.assert_array_equal(fut.num_occurrences, ss.record.num_occurrences)