Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.assertEqual(len(response.states), 1)
self.assertEqual(response.var_type, oj.SPIN)
self.assertListEqual(response.states[0], [-1, -1, -1])
self.assertEqual(response.energies[0], -18)
response = oj.SQASampler().sample_qubo(self.Q)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [0, 0, 0])
schedule = [(s, 10) for s in np.arange(0, 1, 5)] + [(0.99, 100)]
response = oj.SQASampler(schedule=schedule).sample_qubo(self.Q, seed=1)
self.assertListEqual(response.states[0], [0, 0, 0])
vaild_sche = [(s, 10) for s in np.linspace(0, 1.1, 5)]
with self.assertRaises(ValueError):
sampler = oj.SQASampler()
_ = sampler.sample_ising({}, {}, schedule=vaild_sche)
def test_sqa(self):
response = oj.SQASampler().sample_ising(self.h, self.J)
self.assertEqual(len(response.states), 1)
self.assertEqual(response.var_type, oj.SPIN)
self.assertListEqual(response.states[0], [-1, -1, -1])
self.assertEqual(response.energies[0], -18)
response = oj.SQASampler().sample_qubo(self.Q)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [0, 0, 0])
schedule = [(s, 10) for s in np.arange(0, 1, 5)] + [(0.99, 100)]
response = oj.SQASampler(schedule=schedule).sample_qubo(self.Q, seed=1)
self.assertListEqual(response.states[0], [0, 0, 0])
vaild_sche = [(s, 10) for s in np.linspace(0, 1.1, 5)]
with self.assertRaises(ValueError):
sampler = oj.SQASampler()
_ = sampler.sample_ising({}, {}, schedule=vaild_sche)
def test_sqa(self):
response = oj.SQASampler().sample_ising(self.h, self.J)
self.assertEqual(len(response.states), 1)
self.assertEqual(response.var_type, oj.SPIN)
self.assertListEqual(response.states[0], [-1, -1, -1])
self.assertEqual(response.energies[0], -18)
response = oj.SQASampler().sample_qubo(self.Q)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [0, 0, 0])
schedule = [(s, 10) for s in np.arange(0, 1, 5)] + [(0.99, 100)]
response = oj.SQASampler(schedule=schedule).sample_qubo(self.Q, seed=1)
self.assertListEqual(response.states[0], [0, 0, 0])
vaild_sche = [(s, 10) for s in np.linspace(0, 1.1, 5)]
with self.assertRaises(ValueError):
sampler = oj.SQASampler()
def test_sqa_response(self):
iteration = 10
trotter = 4
sampler = oj.SQASampler(iteration=iteration, trotter=trotter)
response = sampler.sample_ising(h=self.h, J=self.J)
self.assertEqual(len(response.states), iteration)
self.assertEqual(len(response.q_states), iteration)
self.assertEqual(len(response.q_states[0]), trotter)
self.assertTrue(isinstance(
response.q_states[0][0][0], (int, np.int, np.int64)))
def test_time_sqa(self):
fast_res = oj.SQASampler(
num_sweeps=10, iteration=10).sample_ising(self.h, self.J, seed=1)
slow_res = oj.SQASampler(
num_sweeps=100, iteration=10).sample_ising(self.h, self.J, seed=1)
self.assertEqual(len(fast_res.info['list_exec_times']), 10)
self.assertTrue(fast_res.info['execution_time']
< slow_res.info['execution_time'])
def test_sqa(self):
response = oj.SQASampler().sample_ising(self.h, self.J)
self.assertEqual(len(response.states), 1)
self.assertEqual(response.var_type, oj.SPIN)
self.assertListEqual(response.states[0], [-1, -1, -1])
self.assertEqual(response.energies[0], -18)
response = oj.SQASampler().sample_qubo(self.Q)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [0, 0, 0])
schedule = [(s, 10) for s in np.arange(0, 1, 5)] + [(0.99, 100)]
response = oj.SQASampler(schedule=schedule).sample_qubo(self.Q, seed=1)
self.assertListEqual(response.states[0], [0, 0, 0])
vaild_sche = [(s, 10) for s in np.linspace(0, 1.1, 5)]
with self.assertRaises(ValueError):
sampler = oj.SQASampler()
_ = sampler.sample_ising({}, {}, schedule=vaild_sche)
[10, 3], [1, 3], [0.5, 3], [1, 3], [10, 5]
]
rsa_sampler = oj.SASampler(schedule=reverse_schedule, iteration=10)
res = rsa_sampler.sample_qubo(
qubo, initial_state=initial_state, seed=seed_for_mc)
self.assertListEqual(
solution,
list(res.min_samples['states'][0])
)
# Reverse simulated quantum annealing
# annealing parameter s, step_length
reverse_schedule = [
[1, 1], [0.3, 3], [0.1, 5], [0.3, 3], [1, 3]
]
rqa_sampler = oj.SQASampler(schedule=reverse_schedule, iteration=10)
res = rqa_sampler.sample_qubo(
qubo, initial_state=initial_state, seed=seed_for_mc)
self.assertListEqual(
solution,
list(res.min_samples['states'][0])
)
if __name__ == '__main__':
h = {0: -1}
J = {(0, 1): -1, (1, 2): -1}
# Simulated annealing (classical) 10 times
response = oj.SASampler(iteration=10).sample_ising(h, J)
# show the lowest energy solution in ten times
min_index = np.argmin(response.energies)
print("SA results: ", response.states[min_index])
# > SA results: [1, 1, 1]
# Simulated quantum annealing (quantum simulation) 10 times
response = oj.SQASampler(iteration=10).sample_ising(h, J)
# show the lowest energy solution in ten times
min_index = np.argmin(response.energies)
print("SQA results: ", response.states[min_index])
# > SQA results: [1, 1, 1]