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))
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)
def test_diff_mf(self): h = np.random.normal() j = np.random.normal() model = IsingModel(2) model.import_uniform01(h, j) 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))
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))
def test_rbm_hamiltonian(self): nvis, nhid = 10, 5 vishid = np.random.normal(size=(nvis, nhid)) model = IsingModel(nvis + nhid) model.import_rbm01(nvis, nhid, np.zeros(nvis), np.zeros(nhid), vishid) state = np.random.choice([True, False], size=nvis + nhid) vis, hid = state[:nvis], state[nvis:] energy = -np.dot(vis, np.dot(hid, vishid.T)) self.assertAlmostEqual(model.hamiltonian(state), energy)
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)
def sample_e_with_beta(beta): model = IsingModel(numspin) model.import_uniform01(beta * h, beta * j) sampled_states = model.sample(n) states = np.empty([n, numspin], dtype=bool) energies = np.empty(n) for i, state in enumerate(sampled_states): states[i] = state energies[i] = model.hamiltonian(state) np.save(resdir + "states_" + "beta" + str(beta) + "_n" + str(numspin) + ".npy", states) np.save(resdir + "energies_" + "beta" + str(beta) + "_n" + str(numspin) + ".npy", energies)