Skip to content

Commit

Permalink
Adding tests for get_system_E().
Browse files Browse the repository at this point in the history
  • Loading branch information
shinmorino committed Nov 11, 2018
1 parent 3519a0d commit 367b511
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
82 changes: 82 additions & 0 deletions sqaodpy/tests/test_bipartite_graph_annealer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_calling_sequence(self) :
an.randomize_spin()
an.anneal_one_step(1, 1)
an.calculate_E()
an.get_system_E(1, 1)
an.make_solution()
an.get_E()
an.get_problem_size()
Expand Down Expand Up @@ -309,6 +310,87 @@ def test_anneal_sa_coloring(self) :
self._test_anneal_maximize(sq.algorithm.sa_coloring, 1)
self._test_anneal_hamiltonian(sq.algorithm.sa_coloring, 1)

def _get_spin_energy_ref(self, qset, G, beta) :
m = len(qset)
E = 0.
for im in range(m) :
imnext = (im + 1) % m
E += np.sum(qset[im][0][:] * qset[imnext][0][:])
E += np.sum(qset[im][1][:] * qset[imnext][1][:])
E *= 1. / (2 * beta) * np.log(1 / np.tanh(G * beta / m))
return E

def test_get_system_E_with_uniform_q(self) :
G = 0.01
beta = 1. / 0.05
N0, N1, m = 5, 5, 5

ann = self.anpkg.bipartite_graph_annealer(dtype = self.dtype)
b0, b1, W = np.ones((N0, ), self.dtype), np.ones((N1, ), self.dtype), np.ones((N1, N0), self.dtype)
ann.set_qubo(b0, b1, W)
ann.set_preferences(n_trotters = m)
ann.prepare()

q0, q1 = np.ones((N0, ), np.int8), np.ones((N1, ), np.int8)
ann.set_q((q0, q1))
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

q0, q1 = - np.ones((N0, ), np.int8), - np.ones((N1, ), np.int8)
ann.set_q((q0, q1))
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

def test_get_system_E_with_non_uniform_q(self) :
G = 0.01
beta = 1. / 0.05
N0, N1, m = 5, 5, 5

ann = self.anpkg.bipartite_graph_annealer(dtype = self.dtype)
b0, b1, W = bipartite_graph_random(N0, N1, self.dtype)
ann.set_qubo(b0, b1, W)
ann.set_preferences(n_trotters = m)
ann.prepare()

# pattern
qset = []
for im in range(m) :
q0, q1 = np.empty((N0, ), np.int8), np.empty((N1, ), np.int8)
for idx in range(N0) :
q0[idx] = -1 if (idx + im) % 2 == 0 else 1
for idx in range(N1) :
q1[idx] = -1 if (idx + + im +1) % 2 == 0 else 1
qset.append((q0, q1))

ann.set_qset(qset)

Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

# random
qset = []
for _ in range(m) :
q0, q1 = - np.ones((N0, ), np.int8), - np.ones((N1, ), np.int8)
sq.common.randomize_spin(q0)
sq.common.randomize_spin(q1)
qset.append((q0, q1))

ann.set_qset(qset)
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

def test_reuse_solver(self) :
# test no errors
ann = self.new_annealer(10, 10, 1)
Expand Down
70 changes: 69 additions & 1 deletion sqaodpy/tests/test_dense_graph_annealer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_calling_sequence(self) :
an.randomize_spin()
an.anneal_one_step(1, 1)
an.calculate_E()
an.get_system_E(1, 1)
an.make_solution()
an.get_E()
an.get_problem_size()
Expand Down Expand Up @@ -175,7 +176,7 @@ def test_get_x(self) :
x = an.get_x()
self.assertEqual(len(x), 1)
self.assertTrue(np.all(x[0] == 1))

def anneal(self, an) :
an.prepare()
an.randomize_spin()
Expand Down Expand Up @@ -272,6 +273,73 @@ def test_set_algorithm(self) :
ann.set_preferences(algorithm = sq.algorithm.sa_naive)
pref = ann.get_preferences()
self.assertEqual(pref['algorithm'], sq.algorithm.sa_naive)

def _get_spin_energy_ref(self, qset, G, beta) :
m = len(qset)
E = 0.
for im in range(m) :
imnext = (im + 1) % m
E += np.sum(qset[im][:] * qset[imnext][:])
E *= 1. / (2 * beta) * np.log(1 / np.tanh(G * beta / m))
return E

def test_get_system_E_with_uniform_q(self) :
G = 0.01
beta = 1. / 0.05
N, m = 10, 5

ann = self.anpkg.dense_graph_annealer(dtype = self.dtype)
W = np.ones((N, N), self.dtype)
ann.set_qubo(W)
ann.set_preferences(n_trotters = m)
ann.prepare()

q = np.ones((N, ), np.int8)
ann.set_q(q)
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

q = - np.ones((N, ), np.int8)
ann.set_q(q)
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

def test_get_system_E_with_non_uniform_q(self) :
G = 0.01
beta = 1. / 0.05
N, m = 10, 5

ann = self.anpkg.dense_graph_annealer(dtype = self.dtype)
W = dense_graph_random(N, self.dtype)
ann.set_qubo(W)
ann.set_preferences(n_trotters = m)
ann.prepare()

qset = np.empty((m, N), np.int8)
it = np.nditer(qset, ['multi_index'], [['writeonly']])
while not it.finished :
qset[it.multi_index] = 1 if (np.sum(it.multi_index) % 2) == 0 else -1
it.iternext()
ann.set_qset(qset)
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

sq.common.randomize_spin(qset)
ann.set_qset(qset)
Esys = ann.get_system_E(G, beta)

Emean = np.mean(ann.get_E())
EspinRef = self._get_spin_energy_ref(ann.get_q(), G, beta)
self.assertTrue(np.allclose(Esys, Emean + EspinRef, atol=self.epu))

def test_reuse_solver(self) :
# test no errors
Expand Down

0 comments on commit 367b511

Please sign in to comment.