Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
optimization_engines/Bayes_Opt.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 @author: Peter Rakyta, Ph.D.
18 */
24 #include "Optimization_Interface.h"
26 #include "Bayes_Opt.h"
27 
28 
29 #include <fstream>
30 
31 
32 #ifdef __DFE__
33 #include "common_DFE.h"
34 #endif
35 
36 
37 
38 
45 
46  if (gates.size() == 0 ) {
47  return;
48  }
49 
50 
51  if (solution_guess.size() == 0 ) {
52  solution_guess = Matrix_real(num_of_parameters,1);
53  }
54 
55 
56  if (optimized_parameters_mtx.size() == 0) {
57  optimized_parameters_mtx = Matrix_real(1, num_of_parameters);
58  memcpy(optimized_parameters_mtx.get_data(), solution_guess.get_data(), num_of_parameters*sizeof(double) );
59  }
60 
61  // maximal number of iteration loops
62  int iteration_loops_max = 1;
63  /*try {
64  iteration_loops_max = std::max(iteration_loops[qbit_num], 1);
65  }
66  catch (...) {
67  iteration_loops_max = 1;
68  }*/
69 
70  // random generator of real numbers
71  std::uniform_real_distribution<> distrib_real(0.0, 2*M_PI);
72 
73  // maximal number of inner iterations overriden by config
74  long long max_inner_iterations_loc;
75  if ( config.count("max_inner_iterations_bayes_opt") > 0 ) {
76  config["max_inner_iterations_bfgs"].get_property( max_inner_iterations_loc );
77  }
78  else if ( config.count("max_inner_iterations") > 0 ) {
79  config["max_inner_iterations"].get_property( max_inner_iterations_loc );
80  }
81  else {
82  max_inner_iterations_loc =max_inner_iterations;
83  }
84 
85  // The number if iterations after which the current results are displed/exported
86  int output_periodicity;
87  if ( config.count("output_periodicity_cosine") > 0 ) {
88  long long value = 1;
89  config["output_periodicity_cosine"].get_property( value );
90  output_periodicity = (int) value;
91  }
92  if ( config.count("output_periodicity") > 0 ) {
93  long long value = 1;
94  config["output_periodicity"].get_property( value );
95  output_periodicity = (int) value;
96  }
97  else {
98  output_periodicity = 0;
99  }
100 
101 
102 
103  Bayes_Opt cBayes_Opt(optimization_problem,this);
104  // do the optimization loops
105  for (long long idx=0; idx<iteration_loops_max; idx++) {
106 
107 
108 
109  double f = cBayes_Opt.Start_Optimization(solution_guess, max_inner_iterations_loc);
110 
111  if (current_minimum > f) {
112  current_minimum = f;
113  memcpy( optimized_parameters_mtx.get_data(), solution_guess.get_data(), num_of_parameters*sizeof(double) );
114  }
115  else {
116  for ( int jdx=0; jdx<num_of_parameters; jdx++) {
117  solution_guess[jdx] = solution_guess[jdx] + distrib_real(gen);
118  }
119  }
120 
121  }
122  std::cout<<current_minimum<<std::endl;
123 
124 }
125 
double current_minimum
The current minimum of the optimization problem.
A class implementing the BayesOpt algorithm as seen in: https://browse.arxiv.org/pdf/1807.02811.pdf.
Definition: Bayes_Opt.h:59
double optimization_problem(double *parameters)
Evaluate the optimization problem of the optimization.
scalar * get_data() const
Call to get the pointer to the stored data.
double Start_Optimization(Matrix_real &x, int max_iterations_in)
std::vector< Gate * > gates
The list of stored gates.
Definition: Gates_block.h:46
int size() const
Call to get the number of the allocated elements.
std::map< std::string, Config_Element > config
config metadata utilized during the optimization
Header file for the paralleized calculation of the cost function of the final optimization problem (s...
Header file for DFE support in unitary simulation.
int max_inner_iterations
the maximal number of iterations for which an optimization engine tries to solve the optimization pro...
Matrix_real optimized_parameters_mtx
The optimized parameters for the gates.
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:39
void solve_layer_optimization_problem_BAYES_OPT(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via Bayes algorithm.
std::mt19937 gen
Standard mersenne_twister_engine seeded with rd()