-
Notifications
You must be signed in to change notification settings - Fork 0
/
localupdate.py
41 lines (29 loc) · 1.4 KB
/
localupdate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# class that performs local update on a given state
# returns the next state in the Markcv chain
#faster version
#import modules
import numpy as np
class LocalUpdater():
def __init__(self, hamiltonian, beta):
self.hamiltonian = hamiltonian
self.beta = beta
def update(self, state):
size = len(state)
#generate random probability
p_rand = np.random.uniform(0, 1, size=(size,size))
for n in range(size**2): #loop N times
#randomly select a site
i,j = np.random.randint(0, size, size=2)
accept = False #whether or not flip is accepted
energy = self.hamiltonian(state, i, j)
state[i,j] *= (-1) #try flipping bit
flipped_energy = self.hamiltonian(state, i, j) #energy of flipped state
if flipped_energy < energy: #accept if lower energy
accept = True
else: #if higher energy
prob_accept = np.exp(-1*self.beta*(flipped_energy-energy)) #probability of accepting
if p_rand[i,j] < prob_accept: #accept if lower than probability
accept = True
if accept==False: #if flip is rejected
state[i,j] *= (-1) #flip bit back
return state #return updated state