예제 #1
0
 def test_probabilities_onlyfields(self):
     h = [np.random.normal()]
     j_matrix = [[0]]
     model = IsingModel(1)
     model.import_ising01(h, j_matrix)
     res = from_shaped_iter(model.sample(30000), bool, (30000, 1))
     p1 = 1 / (1 + np.exp(-h[0]))
     obs_prob = np.mean(res, axis=0)[0]
     self.assertAlmostEqual(p1, obs_prob, places=2)
예제 #2
0
 def test_probabilities_onlyinter(self):
     h = [0, 0]
     j = np.random.normal()
     j_matrix = [[0, j], [j, 0]]
     model = IsingModel(2)
     model.import_ising01(h, j_matrix)
     res = from_shaped_iter(model.sample(30000), bool, (30000, 2))
     prod = res[:, 0] * res[:, 1]
     p1 = np.mean(prod)
     self.assertAlmostEqual(p1, 1 / (3 * np.exp(-j) + 1), places=2)
예제 #3
0
 def test_mf_hamiltonian(self):
     h = np.random.normal()
     j = np.random.normal()
     j_matrix = [[0, j], [j, 0]]
     h_vector = [h, h]
     full_model = IsingModel(2)
     mf_model = IsingModel(2)
     full_model.import_ising01(h_vector, j_matrix)
     mf_model.import_uniform01(h, j)
     state = np.random.choice([True, False], size=2)
     self.assertAlmostEqual(full_model.hamiltonian(state),
                            mf_model.hamiltonian(state))
예제 #4
0
 def test_input_data(self):
     with self.assertRaises(ValueError):
         model = IsingModel(3)
         model.import_ising01([0, 0, 0], [[0, 0], [0, 0]])
     with self.assertRaises(ValueError):
         model = IsingModel(2)
         model.import_ising01([0, 0, 0], [[0, 0], [0, 0]])
     with self.assertRaises(ValueError):
         model = IsingModel(0)
     with self.assertRaises(ValueError):
         model = IsingModel(2)
         list(model.sample(-1))
예제 #5
0
 def test_submodel(self):
     j = np.random.normal(0, 1, size=(10, 10))
     j += j.T
     j /= 2.
     j[np.diag_indices_from(j)] = np.zeros(10)
     h = np.random.random(size=10)
     model = IsingModel(10)
     model.import_ising01(h, j)
     subm = model.submodel(5)
     inp = [True, True, True, True, True, False, False, False, False, False]
     h10 = model.hamiltonian(inp)
     h5 = subm.hamiltonian(inp[:5])
     self.assertAlmostEqual(h5, h10)
예제 #6
0
 def test_simple_hamiltonian(self):
     h = np.random.normal(size=2)
     j = np.random.normal()
     j_matrix = [[0, j], [j, 0]]
     model = IsingModel(2)
     model.import_ising01(h, j_matrix)
     self.assertAlmostEqual(model.hamiltonian(np.array([0, 0])), 0)
     h = np.array([-1, 1, -1])
     j_matrix = np.array([[0, 1, 2], [1, 0, -1], [2, -1, 0]])
     s = np.array([1, 1, 0])
     a = np.dot(h, s) + .5 * np.dot(s, np.dot(j_matrix, s))
     model = IsingModel(3)
     model.import_ising01(h, j_matrix)
     self.assertAlmostEqual(model.hamiltonian(s), -a)
예제 #7
0
    def test_diff_full(self):
        h = np.random.normal(size=2)
        j = np.random.normal()
        j_matrix = [[0, j], [j, 0]]
        model = IsingModel(2)
        model.import_ising01(h, j_matrix)

        spins = np.random.choice([True, False], size=2)
        spin = np.random.choice([0, 1])
        spins[spin] = True
        pos_energy = model.hamiltonian(spins)
        spins[spin] = False
        neg_energy = model.hamiltonian(spins)
        p = neg_energy - pos_energy

        self.assertAlmostEqual(p, model.energydiff(spins, spin))
예제 #8
0
 def test_shape_null(self):
     model = IsingModel(1)
     model.import_ising01([0], [[0]])
     res = from_shaped_iter(model.sample(1), bool, (1, 1))
     self.assertEqual(res.shape[0], 1)
     self.assertEqual(res.shape[1], 1)
예제 #9
0
 def test_res_shapes(self):
     model = IsingModel(2)
     model.import_ising01([0, 0], [[0, 0], [0, 0]])
     res = from_shaped_iter(model.sample(3), bool, (3, 2))
     self.assertEqual(len(res[0]), 2)
     self.assertEqual(len(res[:, 0]), 3)
예제 #10
0
import numpy as np
from time import time


# from pycallgraph import PyCallGraph
# from pycallgraph.output import GraphvizOutput

# with PyCallGraph(output=GraphvizOutput()):
numspin = 30
n = 1000

np.random.seed(56426481)
h = np.random.normal(size=numspin)
j = np.random.normal(size=(numspin, numspin))
j += j.T
j[np.diag_indices_from(j)] = np.zeros(numspin)
# np.random.seed()

ntrials = 20

t = time()
for _ in range(10):
    model = IsingModel(numspin)
    model.import_ising01(h, j)

    for x in model.sample(n):
        pass
t = time() - t
print('Time taken, ' + str(ntrials) + ' trials:')
print(str(t) + ' seconds.')
예제 #11
0
import numpy as np
from time import time

numspin = 300
n = 100
seed = np.random.choice(1000)

j0 = np.random.random()
h0 = np.random.normal()

j = np.ones((numspin, numspin)) * j0
h = np.ones(numspin) * h0
j[np.diag_indices_from(j)] = np.zeros(numspin)

model_full = IsingModel(numspin)
model_full.import_ising01(h, j)
np.random.seed(seed)

t = time()
for x in model_full.sample(n):
    pass
print(time() - t)

model_mf = IsingModel(numspin)
model_mf.import_uniform01(h0, j0)
np.random.seed(seed)

t = time()
for x in model_mf.sample(n):
    pass
print(time() - t)