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_benchmark(self):
h = {0: 1}
J = {(0, 1):-1.0, (1,2): -1.0}
def solver(time_param, *args):
sa_samp = oj.SASampler()
sa_samp.step_num = time_param
sa_samp.iteration = 10
return sa_samp.sample_ising(h, J)
# logger setting
ground_state = [-1, -1, -1]
ground_energy = oj.BinaryQuadraticModel(h, J).calc_energy(ground_state)
step_num_list = np.linspace(1, 5, 5, dtype=np.int)
bm_res = oj.solver_benchmark(
solver=solver,
time_list=step_num_list,
solutions=[ground_state])
self.assertTrue(set(bm_res) >= {'time', 'success_prob', 'residual_energy', 'tts', 'info'})
self.assertEqual(len(bm_res) ,len(step_num_list))
bench = oj.solver_benchmark(
solver=solver,
time_list=step_num_list,
ref_energy=ground_energy, measure_with_energy=True)
self.assertTrue(set(bench) >= {'time', 'success_prob', 'residual_energy', 'tts', 'info'})
def test_bqm_constructor(self):
# Test BinaryQuadraticModel constructor
bqm = oj.BinaryQuadraticModel(self.h, self.J)
self.assertEqual(type(bqm.ising_interactions()), np.ndarray)
self.assertEqual(bqm.var_type, oj.SPIN)
dense_graph = bqm.get_cxxjij_ising_graph(sparse=False)
self.assertTrue(isinstance(dense_graph, cj.graph.Dense))
bqm_qubo = oj.BinaryQuadraticModel.from_qubo(self.Q)
self.assertEqual(bqm_qubo.var_type, oj.BINARY)
def test_bqm_calc_energy(self):
# Test to calculate energy
# Test Ising energy
bqm = oj.BinaryQuadraticModel(self.h, self.J)
ising_energy_bqm = bqm.calc_energy(self.spins)
true_ising_e = calculate_ising_energy(self.h, self.J, self.spins)
self.assertEqual(ising_energy_bqm, true_ising_e)
# Test QUBO energy
bqm = oj.BinaryQuadraticModel.from_qubo(Q=self.Q)
qubo_energy_bqm = bqm.calc_energy(self.binaries)
true_qubo_e = calculate_qubo_energy(self.Q, self.binaries)
self.assertEqual(qubo_energy_bqm, true_qubo_e)
# QUBO == Ising
spins = [1, 1, -1, 1]
binary = [1, 1, 0, 1]
qubo_bqm = oj.BinaryQuadraticModel.from_qubo(Q=self.Q)
# ising_mat = qubo_bqm.ising_interactions()
# h, J = {}, {}
bqm = oj.BinaryQuadraticModel(self.h, self.J)
ising_matrix = np.array([
[1, -1, 0, 0],
[-1, -2, -3, 0],
[0, -3, 0, 0.5],
[0, 0, 0.5, 0]
])
np.testing.assert_array_equal(
bqm.ising_interactions(), ising_matrix
)
# check Hij = Jij + Jji
J = self.J.copy()
J[0, 1] /= 3
J[1, 0] = J[0, 1] * 2
bqm = oj.BinaryQuadraticModel(self.h, J)
np.testing.assert_array_equal(
bqm.ising_interactions(), ising_matrix
)
def test_sa_sweeps(self):
iteration = 10
sampler = oj.SASampler()
res = sampler.sample_ising(self.h, self.J, num_reads=iteration)
self.assertEqual(iteration, len(res.energies))
sampler = oj.SASampler(num_reads=iteration)
res = sampler.sample_ising(self.h, self.J)
self.assertEqual(iteration, len(res.energies))
def test_sa(self):
initial_state = [1 for _ in range(self.size)]
response = oj.SASampler().sample_ising(
self.h, self.J, initial_state=initial_state, seed=1)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [-1, -1, -1])
response = oj.SASampler(beta_max=100).sample_qubo(self.Q, seed=1)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [0, 0, 0])
vaild_sche = [(beta, 1) for beta in np.linspace(-1, 1, 5)]
with self.assertRaises(ValueError):
sampler = oj.SASampler(schedule=vaild_sche)
sampler.sample_ising({}, {})
def test_sa_sweeps(self):
iteration = 10
sampler = oj.SASampler()
res = sampler.sample_ising(self.h, self.J, num_reads=iteration)
self.assertEqual(iteration, len(res.energies))
sampler = oj.SASampler(num_reads=iteration)
res = sampler.sample_ising(self.h, self.J)
self.assertEqual(iteration, len(res.energies))
def test_sa(self):
initial_state = [1 for _ in range(self.size)]
response = oj.SASampler().sample_ising(
self.h, self.J, initial_state=initial_state, seed=1)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [-1, -1, -1])
response = oj.SASampler(beta_max=100).sample_qubo(self.Q, seed=1)
self.assertEqual(len(response.states), 1)
self.assertListEqual(response.states[0], [0, 0, 0])
vaild_sche = [(beta, 1) for beta in np.linspace(-1, 1, 5)]
with self.assertRaises(ValueError):
sampler = oj.SASampler(schedule=vaild_sche)
sampler.sample_ising({}, {})
def test_reverse_annealing(self):
seed_for_mc = 1
initial_state = [0, 0, 0]
qubo = {
(0, 0): 1, (1, 1): -1, (2, 2): 2,
(0, 1): 1, (1, 2): -1, (2, 0): -1
}
# solution is [0, 1, 0]
solution = [0, 1, 0]
# Reverse simulated annealing
# beta, step_length
reverse_schedule = [
[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(
def test_swendsenwang(self):
sampler = oj.SASampler()
initial_state = [1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1]
h = {0: -1, 10: -1}
J = {(i, i+1): -1 for i in range(10)}
res = sampler.sample_ising(h, J,
updater="swendsenwang",
seed=1, initial_state=initial_state)
self.assertListEqual(res.states[0], [1]*11)