Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
optimization_engines/grad_descend.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 "grad_descend.h"
27 
28 
29 #include <fstream>
30 
31 
32 #ifdef __DFE__
33 #include "common_DFE.h"
34 #endif
35 
36 
37 
38 
45 
46 
47 #ifdef __DFE__
48  if ( qbit_num >= 2 && get_accelerator_num() > 0 ) {
49  upload_Umtx_to_DFE();
50  }
51 #endif
52 
53  if (gates.size() == 0 ) {
54  return;
55  }
56 
57 
58  if (solution_guess.size() == 0 ) {
59  solution_guess = Matrix_real(num_of_parameters,1);
60  }
61 
62 
63  if (optimized_parameters_mtx.size() == 0) {
64  optimized_parameters_mtx = Matrix_real(1, num_of_parameters);
65  memcpy(optimized_parameters_mtx.get_data(), solution_guess.get_data(), num_of_parameters*sizeof(double) );
66  }
67 
68  // maximal number of iteration loops
69  /*int iteration_loops_max;
70  try {
71  iteration_loops_max = std::max(iteration_loops[qbit_num], 1);
72  }
73  catch (...) {
74  iteration_loops_max = 1;
75  }*/
76  int iteration_loops_max;
77  long long value;
78  if ( config.count("max_iteration_loops_grad_descend") > 0 ) {
79  config["max_iteration_loops_grad_descend"].get_property( value );
80  iteration_loops_max = (int) value;
81  }
82  else if ( config.count("max_iteration_loops") > 0 ) {
83  config["max_iteration_loops"].get_property( value );
84  iteration_loops_max = (int) value;
85  }
86  else {
87  try {
88  iteration_loops_max = std::max(iteration_loops[qbit_num], 1);
89  }
90  catch (...) {
91  iteration_loops_max = 1;
92  }
93  }
94  // random generator of real numbers
95  std::uniform_real_distribution<> distrib_real(0.0, 2*M_PI);
96 
97  // maximal number of inner iterations overriden by config
98  long long max_inner_iterations_loc;
99  if ( config.count("max_inner_iterations_grad_descend") > 0 ) {
100  config["max_inner_iterations_grad_descend"].get_property( max_inner_iterations_loc );
101  }
102  else if ( config.count("max_inner_iterations") > 0 ) {
103  config["max_inner_iterations"].get_property( max_inner_iterations_loc );
104  }
105  else {
106  max_inner_iterations_loc =max_inner_iterations;
107  }
108 
109 
110  // The number if iterations after which the current results are displed/exported
111  int output_periodicity;
112  if ( config.count("output_periodicity_cosine") > 0 ) {
113  long long value = 1;
114  config["output_periodicity_cosine"].get_property( value );
115  output_periodicity = (int) value;
116  }
117  if ( config.count("output_periodicity") > 0 ) {
118  long long value = 1;
119  config["output_periodicity"].get_property( value );
120  output_periodicity = (int) value;
121  }
122  else {
123  output_periodicity = 0;
124  }
125 
126 
127  // do the optimization loops
128  for (long long idx=0; idx<iteration_loops_max; idx++) {
129 
130 
132  double f = cGrad_Descend.Start_Optimization(solution_guess, max_inner_iterations_loc);
133 
134  if (current_minimum > f) {
135  current_minimum = f;
136  memcpy( optimized_parameters_mtx.get_data(), solution_guess.get_data(), num_of_parameters*sizeof(double) );
137  for ( int jdx=0; jdx<num_of_parameters; jdx++) {
138  solution_guess[jdx] = solution_guess[jdx] + distrib_real(gen)/100;
139  }
140  }
141 
142  else {
143  for ( int jdx=0; jdx<num_of_parameters; jdx++) {
144  solution_guess[jdx] = solution_guess[jdx] + distrib_real(gen);
145  }
146  }
147 
148 
149  if ( output_periodicity>0 && idx % output_periodicity == 0 ) {
151  }
152 
153 #ifdef __MPI__
154  MPI_Bcast( (void*)solution_guess.get_data(), num_of_parameters, MPI_DOUBLE, 0, MPI_COMM_WORLD);
155 #endif
156 
157 
158 
159  }
160 
161 
162 
163 
164 }
165 
166 
167 
void export_current_cost_fnc(double current_minimum)
Call to print out into a file the current cost function and the second Rényi entropy on the subsyste...
A class implementing the BFGS iterations on the.
Definition: grad_descend.h:31
double current_minimum
The current minimum of the optimization problem.
int get_accelerator_num()
Get the number of accelerators to be reserved on DFEs on users demand.
void solve_layer_optimization_problem_GRAD_DESCEND(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via the GRAD_DESCEND (line search in the direct...
scalar * get_data() const
Call to get the pointer to the stored data.
std::vector< Gate * > gates
The list of stored gates.
Definition: Gates_block.h:46
double Start_Optimization(Matrix_real &x, long maximal_iterations_in=5001)
Call this method to start the optimization.
std::map< int, int > iteration_loops
A map of <int n: int num> indicating the number of iteration in each step of the decomposition.
int size() const
Call to get the number of the allocated elements.
static void optimization_problem_combined(Matrix_real parameters, void *void_instance, double *f0, Matrix_real &grad)
Call to calculate both the cost function and the its gradient components.
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...
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:81
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
std::mt19937 gen
Standard mersenne_twister_engine seeded with rd()