def test_combination(self): sampler = ReverseBatchStatesComposite( ReverseAdvanceComposite(MockReverseSampler())) h = {0: -1., 4: 2} J = {(0, 4): 1.5} anneal_schedules = [[[0, 1], [1, 0.5], [2, 0.5], [3, 1]], [[0, 1], [1, 0.5], [2, 0.5], [3, 1]]] initial_states = [{0: 1, 4: 1}, {0: -1, 4: -1}, {0: 1, 4: -1}] response = sampler.sample_ising(h, J, anneal_schedules=anneal_schedules, initial_states=initial_states) anneal_schedule_list = response.record.schedule_index variables = response.variables initial_state_list = response.record.initial_state for state in initial_states: state = [state[var] for var in variables] self.assertIn(state, initial_state_list) for state_idx in range(len(anneal_schedules)): self.assertIn(state_idx, anneal_schedule_list) self.assertGreaterEqual(len(response), 6)
def test_batch_truncate_initial_states(self): sampler = ReverseBatchStatesComposite(MockReverseSampler()) h = {0: -1., 4: 2} J = {(0, 4): 1.5} num_reads = 1 initial_states = [{0: 1, 4: 1}, {0: -1, 4: 1}, {0: -1, 4: -1}] response = sampler.sample_ising(h, J, initial_states=initial_states, num_reads=num_reads) self.assertEqual(len(response), 4) response = sampler.sample_ising(h, J, initial_states=initial_states, initial_states_generator='tile', num_reads=num_reads) self.assertEqual(len(response), 4) response = sampler.sample_ising(h, J, initial_states=initial_states, initial_states_generator='none', num_reads=num_reads) self.assertEqual(len(response), 4)
def test_batch_generate_more_initial_states(self): sampler = ReverseBatchStatesComposite(MockReverseSampler()) h = {0: -1., 4: 2} J = {(0, 4): 1.5} num_reads = 2 initial_states = [{0: 1, 4: 1}] response = sampler.sample_ising(h, J, initial_states=initial_states, num_reads=num_reads) self.assertGreaterEqual(len(response), 4) response = sampler.sample_ising(h, J, initial_states=initial_states, initial_states_generator='tile', num_reads=num_reads) self.assertEqual(len(response), 4) with self.assertRaises(ValueError): response = sampler.sample_ising(h, J, initial_states=initial_states, initial_states_generator='none', num_reads=num_reads)
def test_batch_no_initial_states(self): sampler = ReverseBatchStatesComposite(MockReverseSampler()) h = {0: -1., 4: 2} J = {(0, 4): 1.5} # check default generates an initial state response = sampler.sample_ising(h, J) self.assertIn('initial_state', response.record.dtype.names)
def test_batch_correct_states(self): sampler = ReverseBatchStatesComposite(MockReverseSampler()) h = {0: -1., 4: 2} J = {(0, 4): 1.5} initial_states = [{0: 1, 4: 1}, {0: -1, 4: -1}, {0: 1, 4: -1}] response = sampler.sample_ising(h, J, initial_states=initial_states) variables = response.variables initial_state_list = response.record.initial_state for state in initial_states: state = [state[var] for var in variables] self.assertIn(state, initial_state_list)
def test_sample_qubo_batch(self): sampler = ReverseBatchStatesComposite(MockReverseSampler()) Q = {(0, 4): 1.5} response = sampler.sample_qubo(Q, initial_states=[{ 0: 1, 4: 1 }, { 0: 0, 4: 0 }, { 0: 1, 4: 0 }]) # nothing failed and we got at least three responses back for each initial state self.assertGreaterEqual(len(response), 3)
def test_sample_ising_batch(self): sampler = ReverseBatchStatesComposite(MockReverseSampler()) h = {0: -1., 4: 2} J = {(0, 4): 1.5} response = sampler.sample_ising(h, J, initial_states=[{ 0: 1, 4: 1 }, { 0: -1, 4: -1 }, { 0: 1, 4: -1 }]) # nothing failed and we got at least three responses back for each initial state self.assertGreaterEqual(len(response), 3)
def test_instantiation_smoketest_batch(self): sampler = ReverseBatchStatesComposite(dimod.ExactSolver()) dtest.assert_sampler_api(sampler)