Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
RL_experience.cpp
Go to the documentation of this file.
1 /*
2 Created on Fri Jun 26 14:13:26 2020
3 Copyright 2020 Peter Rakyta, Ph.D.
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/.
19 
20 @author: Peter Rakyta, Ph.D.
21 */
26 #include "RL_experience.h"
27 #include "tbb/tbb.h"
28 
29 
30 #include <cfloat>
31 
36 
37  gates = NULL;
38  iteration_num = 0;
39 
40  exploration_rate = 0.25;
41 
42  reset();
43 
44 
45 }
46 
47 
55 RL_experience::RL_experience( Gates_block* gates_in, unsigned long long iteration_num_in ) {
56 
57 
58  gates = gates_in;
59 
60  iteration_num = iteration_num_in;
61 
62 
63  exploration_rate = 0.25;
64 
65 
66  reset();
67 
68 
69 }
70 
71 
78 
79  gates = experience.gates;
80  iteration_num = experience.iteration_num;
81 
83 
84  parameter_probs = experience.parameter_probs.copy();
85  parameter_counts = experience.parameter_counts.copy();
86  total_counts = experience.total_counts.copy();
88 
90 
91  history = experience.history;
92 
93 }
94 
95 
102 
103  gates = experience.gates;
104  iteration_num = experience.iteration_num;
105 
107 
108 
109  parameter_probs = experience.parameter_probs.copy();
110  parameter_counts = experience.parameter_counts.copy();
111  total_counts = experience.total_counts.copy();
113 
114  exploration_rate = experience.exploration_rate;
115 
116  history = experience.history;
117 
118 
119  return *this;
120 }
121 
122 
123 
124 
130 
131  RL_experience experience( gates, iteration_num );
132 
133 
134 
135  experience.parameter_probs = parameter_probs.copy();
136  experience.parameter_counts = parameter_counts.copy();
137 
138  experience.exploration_rate = exploration_rate;
139 
140  experience.history = history;
141 
142  return experience;
143 }
144 
145 
146 
152 
153 }
154 
155 
156 
161 
162  if (gates == NULL ) {
163  parameter_num = 0;
164 
165  parameter_probs = Matrix_real( 0, 0 );
167  total_counts = matrix_base<int>( 0 ,0 );
169  return;
170  }
171 
173 
178 
179 
180  // initial set of the counts and probabilities --- equally weighted probabilities and counts
181 
182 
183  double init_prob = 1.0/parameter_num;
184  for ( int row_idx=0; row_idx<parameter_num; row_idx++ ) {
185 
186  total_counts_probs[ row_idx ] = parameter_num*20;
187 
188  for ( int col_idx=0; col_idx<parameter_num; col_idx++ ) {
189  parameter_probs[row_idx*parameter_probs.stride + col_idx] = init_prob;
190  }
191  }
192 
193  memset( parameter_counts.get_data(), 0, parameter_counts.size()*sizeof( int ) );
194  memset( total_counts.get_data(), 0, total_counts.size()*sizeof( int ) );
195 
197  history.clear();
198 }
199 
200 
201 
207 int RL_experience::draw( const int& curent_index, std::mt19937& gen ) {
208 
209 
210  // decide whether explore the action space or draw from trained probabilities
211  std::uniform_real_distribution<> distrib_to_choose(0.0, 1.0);
212  double random_num = distrib_to_choose( gen );
213 
214  int selected_idx = 0;
215 
216  if ( random_num > exploration_rate ) {
217 
218  // draw from trained probabilities
219 
220  // probabilities in the current row of the table
221  Matrix_real current_probs = Matrix_real( parameter_probs.get_data() + parameter_probs.stride*curent_index, 1, parameter_num );
222 
223 
224  random_num = distrib_to_choose( gen );
225 
226  double prob_tmp = 0.0;
227  for ( int idx=0; idx<parameter_num; idx++ ) {
228 
229  if ( prob_tmp >= random_num ) {
230  selected_idx = idx;
231  break;
232  }
233 
234  prob_tmp += current_probs[idx];
235 
236  }
237 
238  }
239  else {
240  // random selection
241 
242  std::uniform_int_distribution<> distrib_int(0, parameter_num-1);
243  selected_idx = distrib_int( gen );
244  }
245 
246  // update the selection counts table
247 
248  parameter_counts[ curent_index*parameter_counts.stride + selected_idx]++;
249  total_counts[selected_idx]++;
250 
251  //history.push_back( selected_idx );
252 
253 //std::cout << "uuuuuuuuu " << parameter_counts[ curent_index*parameter_counts.stride + selected_idx] << std::endl;
254  return selected_idx;
255 
256 
257 
258 }
259 
260 
261 
266 
267 
268  for ( int row_idx=0; row_idx<parameter_num; row_idx++ ) {
269 
270 
271  double prob_sum = 0.0;
272 
273 
274  for ( int col_idx=0; col_idx<parameter_num; col_idx++ ) {
275 
276  unsigned long long counts_loc_old = (unsigned long long) (parameter_probs[row_idx*parameter_counts.stride + col_idx] * total_counts_probs[ row_idx ]);
277  unsigned long long counts_loc_new = (unsigned long long) parameter_counts[row_idx*parameter_counts.stride + col_idx];
278 
279 
280  counts_loc_old += counts_loc_new;
281 
282  total_counts_probs[row_idx] += (unsigned long long) total_counts[row_idx];
283 
284  double prob_new = (double)counts_loc_old/total_counts_probs[row_idx];
285 
286 
287 //if ( parameter_counts[row_idx*parameter_counts.stride + col_idx] > 0 ) {
288 //std::cout << prob_new << " " << counts_loc_old << " " << counts_loc_new << " " << total_counts_probs[row_idx] << " " << total_counts[row_idx] << std::endl;
289 
290 //}
291 
292  parameter_probs[row_idx*parameter_counts.stride + col_idx] = prob_new;
293  prob_sum += prob_new;
294 
295 
296  parameter_counts[row_idx*parameter_counts.stride + col_idx] = 0;
297 
298  }
299 
300  total_counts[row_idx] = 0;
301 
302  // renormalize the probabilities
303  for ( int col_idx=0; col_idx<parameter_num; col_idx++ ) {
304  parameter_probs[row_idx*parameter_counts.stride + col_idx] = parameter_probs[row_idx*parameter_counts.stride + col_idx]/prob_sum;
305  }
306 
307 
308  }
309 
310 
311 //parameter_probs.print_matrix();
312 
313 }
314 
315 
316 
317 
318 
323 void
325 
326  FILE* pFile;
327 std::string filename = "probabilities.bin";
328 
329  const char* c_filename = filename.c_str();
330  pFile = fopen(c_filename, "wb");
331  if (pFile==NULL) {
332  fputs ("File error",stderr);
333  std::string error("Cannot open file.");
334  throw error;
335  }
336 
337 
338  fwrite(&iteration_num, sizeof(unsigned long long), 1, pFile);
339  fwrite(&parameter_num, sizeof(int), 1, pFile);
340 
341  int element_num = parameter_probs.size();
342 
343  fwrite( &element_num, sizeof(int), 1, pFile);
344  fwrite(parameter_probs.get_data(), sizeof(double), element_num, pFile);
345 
346  element_num = total_counts_probs.size();
347  fwrite( &element_num, sizeof(int), 1, pFile);
348  fwrite(total_counts_probs.get_data(), sizeof(unsigned long long), element_num, pFile);
349 
350 
351 
352  fclose(pFile);
353  return;
354 }
355 
356 
357 
362 void
364 
365  FILE* pFile;
366 std::string filename = "probabilities.bin";
367 
368  const char* c_filename = filename.c_str();
369  pFile = fopen(c_filename, "rb");
370  if (pFile==NULL) {
371  fputs ("File error",stderr);
372  std::string error("Cannot open file.");
373  throw error;
374  }
375 
376  fread(&iteration_num, sizeof(unsigned long long), 1, pFile);
377  fread(&parameter_num, sizeof(int), 1, pFile);
378 
379  int element_num;
380 
381  fread( &element_num, sizeof(int), 1, pFile);
382  parameter_probs = Matrix_real( element_num, 1 );
383  fread(parameter_probs.get_data(), sizeof(double), element_num, pFile);
384 
385  fread( &element_num, sizeof(int), 1, pFile);
387  fread(total_counts_probs.get_data(), sizeof(unsigned long long), element_num, pFile);
388 
389 
390  fclose(pFile);
391  return;
392 }
393 
394 
matrix_base< int > total_counts
total counts in one row of parameter_counts — reset when probabilites are updated ...
Definition: RL_experience.h:66
A class for RL_experience optimization according to https://towardsdatascience.com/how-to-implement-a...
Definition: RL_experience.h:38
Matrix_real copy() const
Call to create a copy of the matrix.
void import_probabilities()
???????????
int stride
The column stride of the array. (The array elements in one row are a_0, a_1, ... a_{cols-1}, 0, 0, 0, 0. The number of zeros is stride-cols)
Definition: matrix_base.hpp:46
Matrix_real parameter_probs
Definition: RL_experience.h:59
int parameter_num
number of involved parameters
Definition: RL_experience.h:50
matrix_base< scalar > copy() const
Call to create a copy of the matrix.
double exploration_rate
Definition: RL_experience.h:77
std::vector< int > history
Definition: RL_experience.h:73
scalar * get_data() const
Call to get the pointer to the stored data.
void reset()
?????????????
RL_experience copy()
Call to make a copy of the current instance.
int get_parameter_num()
Call to get the number of free parameters.
RL_experience()
Nullary constructor of the class.
RL_experience & operator=(const RL_experience &experience)
Assignment operator.
matrix_base< unsigned long long > total_counts_probs
total counts used to evasluate one row in parameter_probs
Definition: RL_experience.h:69
Gates_block * gates
attribute stroing the gate structure
Definition: RL_experience.h:56
int draw(const int &curent_index, std::mt19937 &gen)
Call to draw the next index.
int size() const
Call to get the number of the allocated elements.
A class responsible for grouping two-qubit (CNOT,CZ,CH) and one-qubit gates into layers.
Definition: Gates_block.h:41
unsigned long long iteration_num
Definition: RL_experience.h:53
virtual ~RL_experience()
Destructor of the class.
Header file for a class ???
void update_probs()
Call to update the trained probabilities and reset the counts.
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:39
void export_probabilities()
???????????
matrix_base< int > parameter_counts
array containing the counts of successive parameters used in the optimization. Element (i...
Definition: RL_experience.h:63