forked from clelidm/MinCompSpin_Greedy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate_data_exact.cpp
More file actions
215 lines (173 loc) · 7.12 KB
/
Copy pathGenerate_data_exact.cpp
File metadata and controls
215 lines (173 loc) · 7.12 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <list>
#include <fstream>
#include <map>
#include <bitset>
#include<cmath>
using namespace std;
/********************************************************************/
/************************** CONSTANTS *************************/
/********************************************************************/
#include "data.h"
/******************************************************************************/
/********************* OPERATOR EVALUATED on a STATE **********************/
/*********************** depend on the convention ************************/
/******************************************************************************/
/**************************** !! Convention !! ****************************/
/**** For Ising: {1 in data <--> -1 in model} ****/
/**** {0 in data <--> +1 in model} ****/
/******************************************************************************/
// s_i in {-1;1} !! Convention !! {0,1} in data <-> {1,-1} in model
int Op_Ising(uint32_t Op, uint32_t state)
{ return ( (bitset<n>(Op & state).count())%2 )?(-1):1; }
/******************************************************************************/
/************* PARTITION FUNCTION and PROBA of ALL STATES *********************/
/******************************************************************************/
// return the probability (not normalised) of all the states and compute the partition function
double* Probability_AllStates_Ising(list<Interaction> list_I, double *Z) // Convention {-1;1}
//!! Convention !!: {1 in data <--> -1 in model} and {0 in data <--> 1 in model}
{
double H = 0; // -energy of the state
int Op_s = 1; // value of the operator for the state s ; \in {-1; 1}
list<Interaction>::iterator I;
double* all_P = (double*)malloc((NOp_tot+1)*sizeof(double));
(*Z) = 0;
for (uint32_t state = 0; state <= NOp_tot; state++)
{
H=0; // here H is (H = -Hamiltonian)
for (I = list_I.begin(); I != list_I.end(); I++)
{
Op_s = Op_Ising((*I).Op, state);
H += (*I).g * Op_s;
}
all_P[state] = exp(H);
(*Z) += all_P[state];
}
return all_P;
}
/******************************************************************************/
/************************** MODEL AVERAGES *****************************/
/******************************************************************************/
void Model_averages_Ising_aux(double *P, double Z, list<Interaction> &list_I)
{
int Op_s = 1; // value of the operator for the state s ; \in {-1; 1}
list<Interaction>::iterator I;
for (I = list_I.begin(); I != list_I.end(); I++)
{
(*I).av_M = 0.;
for (uint32_t state = 0; state <= NOp_tot; state++)
{
Op_s = Op_Ising((*I).Op, state);
(*I).av_M += Op_s * P[state];
}
(*I).av_M = (*I).av_M / Z;
}
}
void Model_averages_Ising(list<Interaction> &list_I)
{
// Compute un-normalized probability distribution and Partition function Z:
double Z=0;
double *P=Probability_AllStates_Ising(list_I, &Z);
Model_averages_Ising_aux(P, Z, list_I);
}
/******************************************************************************/
/************************* EMPIRICAL AVERAGES *************************/
/******************************************************************************/
//Number of time an operator is equal to 1 ( = <phi> in the {0,1} representation )
unsigned int k1_op(map<uint32_t, unsigned int> Nset, uint32_t op) // Complexity = O(|Nset|)
{
unsigned int k1=0;
map<uint32_t, unsigned int>::iterator it; // iterator on Nset
for (it = Nset.begin(); it!=Nset.end(); ++it)
{ k1 += (bitset<n>( ((*it).first) & op ).count() % 2)*((*it).second); }
return k1;
}
double op_av_Ising(map<uint32_t, unsigned int> Nset, uint32_t op, unsigned int N)
{
return ( ((double) N) - 2.*k1_op(Nset, op) ) / ((double) N); // [ [-1 * k1] + [+1 * (N-k1)] ] / N
}
/************************ Empirical averages all op *************************/
void empirical_averages_Ising(map<uint32_t, unsigned int> Nset, list<Interaction> &list_I, unsigned int N)
{
list<Interaction>::iterator I;
for (I = list_I.begin(); I != list_I.end(); I++)
{
(*I).av_D = op_av_Ising(Nset, (*I).Op, N);
}
}
/******************************************************************************/
/************************** SAMPLE DATASET *****************************/
/******************************************************************************/
void Sample_dataset(list<Interaction> list_I, string output_filename, unsigned int N=1000)
{
// Compute un-normalized probability distribution and Partition function Z:
double Z=0;
double *p=Probability_AllStates_Ising(list_I, &Z);
// Compute Cumulative distribution:
double *cumul = (double*)malloc((NOp_tot+1)*sizeof(double));
cumul[0]=p[0]/Z;
//cout << 0 << "\t " << cumul[0] << endl;
for(unsigned int i=1; i <= NOp_tot; i++)
{
cumul[i] = cumul[i-1] + p[i]/Z;
//cout << i << "\t " << cumul[i] << endl;
}
// Randomly sampled "N" data points using the Cumulative:
double eps=0;
__int128_t j=0;
// OUTPUT FILE:
fstream file(output_filename.c_str(), ios::out);
for(int i=0; i<N; i++)
{
eps=(double)rand()/RAND_MAX;
j=0;
while(cumul[j]<eps && j < (1 << n))
{ j++; }
bitset<n> hi{ static_cast<unsigned long long>(j >> 64) },
lo{ static_cast<unsigned long long>(j) },
bits{ (hi << 64) | lo };
file << bits << endl;
}
file.close();
delete[] p;
delete[] cumul;
}
/******************************************************************************/
/************************** SAMPLE DATASET *****************************/
/************* and fill info on model + dataset ***************************/
/******** i.e. compute Model averages and data averages *******************/
/*************** and print them with the model information ****************/
/******************************************************************************/
void Sample_dataset_AND_Print_ModelData_Info(list<Interaction>& list_I, string output_filename, unsigned int N=1000)
{
// Compute un-normalized probability distribution and Partition function Z:
double Z=0;
double *P=Probability_AllStates_Ising(list_I, &Z);
// Compute Cumulative distribution:
double *cumul = (double*)malloc((NOp_tot+1)*sizeof(double));
cumul[0]=P[0]/Z;
for(int i=1; i <= NOp_tot; i++)
{
cumul[i] = cumul[i-1] + P[i]/Z;
}
// Randomly sampled "N" data points using the Cumulative:
double eps=0;
uint32_t datapt=0;
// OUTPUT FILE:
fstream file((OUTPUT_directory + output_filename).c_str(), ios::out);
// ***** data is also stored in Nset: ********************************
map<uint32_t, unsigned int> Nset; // Nset[mu] = #of time state mu appears in the data set
for(unsigned int i=0; i<N; i++)
{
eps=rand();
datapt=0;
while(cumul[datapt]<eps)
{ datapt++; }
file << bitset<n>(datapt) << endl;
Nset[datapt] += 1;
}
// Model averages:
Model_averages_Ising_aux(P, Z, list_I);
// Empirical averages (from the generated dataset):
empirical_averages_Ising(Nset, list_I, N);
file.close();
}