7 #define PI 3.141592653589792
8 #define DEFAULT_SIZE 10
17 // End of forward decl
27 State () : m_spins(DEFAULT_SIZE
) {}
28 State (int size
) : m_spins(size
) {}
29 State (const State
& other
) : m_spins(other
.m_spins
) {}
31 Change
trialChange () {
33 ret
.i
= (rand()/(RAND_MAX
+1.0))*size();
34 ret
.j
= (rand()/(RAND_MAX
+1.0))*(size()-1);
35 if (ret
.j
>=ret
.i
) ret
.j
++;
39 State
applyChange (const Change
& change
) {
41 ret
.m_spins
[change
.i
] = !ret
.m_spins
[change
.i
];
42 ret
.m_spins
[change
.j
] = !ret
.m_spins
[change
.j
];
46 int size () const { return m_spins
.size(); }
47 int at (int i
) const { return m_spins
[i
]?1:-1; }
48 bool parallel (int i
, int j
) const { return m_spins
[i
] == m_spins
[j
]; }
50 static State
up (int size
= DEFAULT_SIZE
) {
52 for (int i
=0;i
<size
;i
++) {
53 ret
.m_spins
[i
] = true;
58 static State
down (int size
= DEFAULT_SIZE
) {
60 for (int i
=0;i
<size
;i
++) {
61 ret
.m_spins
[i
] = false;
66 static State
neel (int size
= DEFAULT_SIZE
) {
68 for (int i
=0;i
<size
;i
++) {
74 static State
half (int size
= DEFAULT_SIZE
) {
76 for (int i
=0;i
<size
;i
++) {
77 ret
.m_spins
[i
] = 2*i
< size
;
82 static State
random (int size
= DEFAULT_SIZE
) {
84 for (int i
=0;i
<size
;i
++) {
85 ret
.m_spins
[i
] = rand()/(RAND_MAX
+1.0) < 0.5;
90 friend ostream
& operator << (ostream
&, const State
&);
91 const State
& operator = (const State
& other
) { m_spins
= other
.m_spins
; return *this; }
92 State
& swap (State
& other
) { m_spins
.swap(other
.m_spins
); return *this; }
95 std::vector
<bool> m_spins
;
98 ostream
& operator << (ostream
& out
, const State
& s
) {
99 for (std::vector
<bool>::const_iterator iter
= s
.m_spins
.begin();iter
!= s
.m_spins
.end();iter
++) {
100 if (iter
!=s
.m_spins
.begin() && iter
!=s
.m_spins
.end()) out
<< ", ";
108 double distance (double i
, double j
) { return 2*sin(PI
*abs(i
-j
)); }
110 VarProb() : alpha(1.0) {}
111 VarProb(double a
) : alpha(a
) {}
113 double compute (const State
& state
) {
115 int L
= state
.size();
117 for (int i
=0;i
<L
;i
++) {
119 for (int j
=i
+1;j
<L
;j
++) {
120 double d
= distance(double(i
)/L
, double(j
)/L
);
121 x
+= log(d
*d
)*(state
.parallel(i
, j
)?1.0:-1.0);
124 if (ups
!=0) return 0.0;
136 template <typename Probability
>
141 QMC (const Probability
& p
, const Observable
&o
, const State
& s
) : m_probability(p
), m_obs(o
), m_state(s
) {
142 current_probability
= m_probability
.compute(m_state
);
145 State
state () const { return m_state
; }
146 void setState (const State
& s
) { m_state
= s
; }
147 Probability
probability () const { return m_probability
; }
148 void setProbability (const Probability
& p
) { m_probability
= p
; }
152 State::Change trial
= m_state
.trialChange();
153 cout
<< "original state: " << m_state
<< endl
;
154 State t_state
= m_state
.applyChange(trial
);
155 cout
<< " --> proposed: " << t_state
<< endl
;
156 double prob
= m_probability
.compute(t_state
);
157 cout
<< " --> with probability: " << prob
<< endl
;
158 double a
= prob
/current_probability
;
159 cout
<< " --> and acceptance ratio: " << a
<< endl
;
160 if (a
>(rand()/(RAND_MAX
+1.0))) {
163 current_probability
= prob
;
164 cout
<< " --> accepted" << endl
;
168 cout
<< " --> rejected" << endl
;
175 Probability m_probability
;
176 double current_probability
;
180 int main (int argc
, char **argv
) {
182 QMC
<VarProb
> qmc(VarProb(+0.26), Observable(), State::half(100));
183 for (int i
=0;i
<1e3
;i
++) qmc
.step();