def _check(self, results, linear, quad, offset=0, num_reads=1): # Did we get the right number of samples? self.assertEqual(num_reads, sum(results.num_occurrences)) # verify num_occurrences sum corresponds to num_reads self.assertEqual(100, sum(results.num_occurrences)) # Make sure energies are correct in raw results for energy, state in zip(results.energies, results.samples): self.assertEqual( energy, evaluate_ising(linear, quad, state, offset=offset)) # skip sampleset test if dimod is not installed if not dimod: return # Make sure the sampleset matches raw results for record, energy, num_occurrences, state in \ zip(results.sampleset.record, results['energies'], results['num_occurrences'], results['solutions']): recalc_energy = evaluate_ising(linear, quad, state, offset=offset) self.assertEqual(energy, recalc_energy) self.assertEqual(energy, float(record.energy)) self.assertEqual(num_occurrences, int(record.num_occurrences)) self.assertEqual(state, list(record.sample))
def _check(self, results, linear, quad, offset=0, num_reads=1): # Did we get the right number of samples? self.assertEqual(num_reads, sum(results.num_occurrences)) # verify .occurrences property still works, although is deprecated with warnings.catch_warnings(): warnings.simplefilter("ignore") self.assertEqual(100, sum(results.occurrences)) # Make sure energies are correct in raw results for energy, state in zip(results.energies, results.samples): self.assertEqual( energy, evaluate_ising(linear, quad, state, offset=offset)) # skip sampleset test if dimod is not installed if not dimod: return # Make sure the sampleset matches raw results for record, energy, num_occurrences, state in \ zip(results.sampleset.record, results['energies'], results['num_occurrences'], results['solutions']): recalc_energy = evaluate_ising(linear, quad, state, offset=offset) self.assertEqual(energy, recalc_energy) self.assertEqual(energy, float(record.energy)) self.assertEqual(num_occurrences, int(record.num_occurrences)) self.assertEqual(state, list(record.sample))
def test_wait_many(self): """Submit a batch of problems then use `wait_multiple` to wait on all of them.""" with Client(**config) as client: solver = client.get_solver() linear, quad = generate_random_ising_problem(solver) result_list = [] for _ in range(100): results = solver.sample_ising(linear, quad, num_reads=40) result_list.append([results, linear, quad]) dwave.cloud.computation.Future.wait_multiple( [f[0] for f in result_list]) for results, _, _ in result_list: self.assertTrue(results.done()) for results, linear, quad in result_list: # Did we get the right number of samples? self.assertEqual(40, sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertAlmostEqual(energy, evaluate_ising(linear, quad, state))
def test_cancel_batch(self): """Submit batch of problems, then cancel them.""" with Client(**config) as client: solver = client.get_solver() linear, quad = generate_random_ising_problem(solver) max_num_reads = max( solver.properties.get('num_reads_range', [1, 100])) result_list = [] for _ in range(1000): results = solver.sample_ising(linear, quad, num_reads=max_num_reads) result_list.append([results, linear, quad]) [r[0].cancel() for r in result_list] for results, linear, quad in result_list: # Responses must be canceled or correct try: # Did we get the right number of samples? self.assertEqual(max_num_reads, sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertAlmostEqual( energy, evaluate_ising(linear, quad, state)) except CanceledFutureError: pass
def test_as_completed(self): """Submit a batch of problems then use `as_completed` to iterate over all of them.""" # Connect with Client(**config) as client: solver = client.get_solver() # Build a problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) quad = { key: random.choice([-1, 1]) for key in solver.undirected_edges } # Sample the solution 100x40 times computations = [ solver.sample_ising(linear, quad, num_reads=40) for _ in range(100) ] # Go over computations, one by one, as they're done and check they're OK for computation in dwave.cloud.computation.Future.as_completed( computations): self.assertTrue(computation.done()) self.assertTrue(40 == sum(computation.occurrences)) for energy, state in zip(computation.energies, computation.samples): self.assertTrue( energy == evaluate_ising(linear, quad, state))
def test_submit_batch(self): """Submit batch of problems.""" # Connect with Client(**config) as client: solver = client.get_solver() result_list = [] for _ in range(100): # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) # Build a quad = { key: random.choice([-1, 1]) for key in solver.undirected_edges } results = solver.sample_ising(linear, quad, num_reads=10) result_list.append([results, linear, quad]) for results, linear, quad in result_list: # Did we get the right number of samples? self.assertTrue(10 == sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertTrue( energy == evaluate_ising(linear, quad, state))
def test_wait_many(self): """Submit a batch of problems then use `wait_multiple` to wait on all of them.""" # Connect with Client(**config) as client: solver = client.get_solver() # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) # Build a quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} result_list = [] for _ in range(100): results = solver.sample_ising(linear, quad, num_reads=40) result_list.append([results, linear, quad]) dwave.cloud.computation.Future.wait_multiple([f[0] for f in result_list]) for results, _, _ in result_list: self.assertTrue(results.done()) for results, linear, quad in result_list: # Did we get the right number of samples? self.assertTrue(40 == sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertTrue(energy == evaluate_ising(linear, quad, state))
def _check(self, results, linear, quad, num): # Did we get the right number of samples? self.assertTrue(100 == sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertTrue(energy == evaluate_ising(linear, quad, state))
def _submit_and_check(self, solver, linear, quad, **kwargs): results = solver.sample_ising(linear, quad, num_reads=100, **kwargs) # Did we get the right number of samples? self.assertEqual(100, sum(results.num_occurrences)) # verify .occurrences property still works, although is deprecated with warnings.catch_warnings(): warnings.simplefilter("ignore") self.assertEqual(100, sum(results.occurrences)) # offset is optional offset = kwargs.get('offset', 0) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertAlmostEqual( energy, evaluate_ising(linear, quad, state, offset=offset)) # label is optional label = kwargs.get('label') if label is not None: self.assertEqual(results.label, label) return results
def test_submit_bqm_qubo_ok_reply(self): """Handle a normal query and response.""" qubo_msg_diff = dict(type="qubo") qubo_answer_diff = { 'energies': 'AAAAAAAAAAA=', 'solutions': 'AA==', 'active_variables': 'AAAAAAQAAAA=' } # each thread can have its instance of a session because # the mocked responses are stateless def create_mock_session(client): session = mock.Mock() session.post = lambda a, _: choose_reply(a, { 'problems/': '[%s]' % complete_no_answer_reply( '123', 'abc123')}) session.get = lambda a: choose_reply(a, { 'problems/123/': complete_reply( '123', 'abc123', answer=qubo_answer_diff, msg=qubo_msg_diff)}) return session with mock.patch.object(Client, 'create_session', create_mock_session): with Client('endpoint', 'token') as client: solver = Solver(client, solver_data('abc123')) qubo = {(0, 0): 4.0, (0, 4): -4, (4, 4): 4.0} offset = -2.0 params = dict(num_reads=100) results = solver.sample_qubo(qubo, offset, **params) # make sure energies are correct in raw results for energy, sample in zip(results.energies, results.samples): self.assertEqual(energy, evaluate_ising({}, qubo, sample, offset=offset))
def _submit_and_check(self, solver, linear, quad, **param): results = solver.sample_ising(linear, quad, num_reads=100, **param) # Did we get the right number of samples? self.assertEqual(100, sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertAlmostEqual(energy, evaluate_ising(linear, quad, state)) return results
def test_submit_bqm_problem(self): """Submit a problem with all supported coefficients set.""" with Client(**config) as client: solver = client.get_solver() linear, quad = generate_random_ising_problem(solver) bqm = dimod.BinaryQuadraticModel.from_ising(linear, quad) results = solver.sample_bqm(bqm, num_reads=100) # Did we get the right number of samples? self.assertEqual(100, sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertAlmostEqual(energy, evaluate_ising(linear, quad, state))
def test_as_completed(self): """Submit a batch of problems then use `as_completed` to iterate over all of them.""" with Client(**config) as client: solver = client.get_solver() linear, quad = generate_random_ising_problem(solver) # Sample the solution 100x40 times computations = [solver.sample_ising(linear, quad, num_reads=40) for _ in range(100)] # Go over computations, one by one, as they're done and check they're OK for computation in dwave.cloud.computation.Future.as_completed(computations): self.assertTrue(computation.done()) self.assertEqual(40, sum(computation.occurrences)) for energy, state in zip(computation.energies, computation.samples): self.assertAlmostEqual(energy, evaluate_ising(linear, quad, state))
def test_submit_batch(self): """Submit batch of problems.""" with Client(**config) as client: solver = client.get_solver() result_list = [] for _ in range(100): linear, quad = generate_random_ising_problem(solver) results = solver.sample_ising(linear, quad, num_reads=10) result_list.append([results, linear, quad]) for results, linear, quad in result_list: # Did we get the right number of samples? self.assertEqual(10, sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertAlmostEqual(energy, evaluate_ising(linear, quad, state))
def test_cancel_batch(self): """Submit batch of problems, then cancel them.""" # Connect with Client(**config) as client: solver = client.get_solver() # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) quad = { key: random.choice([-1, 1]) for key in solver.undirected_edges } max_num_reads = max( solver.properties.get('num_reads_range', [1, 100])) result_list = [] for _ in range(1000): results = solver.sample_ising(linear, quad, num_reads=max_num_reads) result_list.append([results, linear, quad]) [r[0].cancel() for r in result_list] for results, linear, quad in result_list: # Responses must be canceled or correct try: # Did we get the right number of samples? self.assertTrue(max_num_reads == sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertTrue( energy == evaluate_ising(linear, quad, state)) except CanceledFutureError: pass