Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Optimization_Interface.h
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 */
23 #ifndef Optimization_Interface_H
24 #define Optimization_Interface_H
25 
26 #include <atomic>
27 
28 #include "Decomposition_Base.h"
29 #include "BFGS_Powell.h"
30 #include "Bayes_Opt.h"
31 #include "Powells_method.h"
32 
33 
34 #if defined __DFE__
35  #include "common_DFE.h"
36 #elif defined __GROQ__
37  #include "common_GROQ.h"
38 #endif
39 
40 
41 
46 
47 
48 
51 
52 
53 
54 
55 
56 
62 
63 
64 public:
65 
71  int id;
72 protected:
73 
74 
77 
79  std::map<int,int> identical_blocks;
91  std::vector<std::vector<int>> use_cuts;
93  int osr_rank = -1;
95  bool use_softmax = false;
96 
97 
99  std::atomic<int> number_of_iters;
100 
104  double radius;
109 
112 
113 
117  double CPU_time;
118 
119 
120 
121 
122 public:
123 
129 
135 
136 
137 
147 Optimization_Interface( Matrix Umtx_in, int qbit_num_in, bool optimize_layer_num_in, std::map<std::string, Config_Element>& config, guess_type initial_guess_in, int accelerator_num_in=0 );
148 
152 Optimization_Interface( Matrix_float Umtx_in, int qbit_num_in, bool optimize_layer_num_in, std::map<std::string, Config_Element>& config, guess_type initial_guess_in, int accelerator_num_in=0 );
153 
154 
155 
159 virtual ~Optimization_Interface();
160 
167 
174 
175 
179 virtual void add_finalyzing_layer();
180 
181 
185 void final_optimization();
186 
187 
188 
195 
196 
203 
204 
211 
212 
219 
220 
227 
228 
229 
235 
236 
243 
244 
251 static void export_current_cost_fnc(double current_minimum, Matrix_real& parameters, void* void_instance);
252 
253 
260 
261 
268 
269 
276 
283 
284 
291 
292 
293 
300 
307 
314 void randomize_parameters( Matrix_real& input, Matrix_real& output, const double f0 );
315 
316 
317 // Bring base class optimization_problem into scope to avoid hiding
319 
325 double optimization_problem( double* parameters);
326 
327 
334 
341 double calculate_cost_function( Matrix& matrix_new, Matrix* ret_temp=NULL );
342 
347 double calculate_cost_function( Matrix_float& matrix_new, Matrix_float* ret_temp=NULL );
348 
349 
350 
351 #ifdef __DFE__
352 
357 Matrix_real optimization_problem_batched_DFE( std::vector<Matrix_real>& parameters_vec);
358 #endif
359 
360 #ifdef __GROQ__
361 
368 virtual double optimization_problem_Groq( Matrix_real& parameters, int chosen_device);
369 
370 
376 Matrix_real optimization_problem_batched_Groq( std::vector<Matrix_real>& parameters_vec);
377 #endif
378 
379 
385 Matrix_real optimization_problem_batched( std::vector<Matrix_real>& parameters_vec);
386 
387 
388 
396 double optimization_problem( Matrix_real parameters, void* void_instance, Matrix ret_temp);
397 
398 
405 static double optimization_problem( Matrix_real parameters, void* void_instance);
406 
407 
413 virtual double optimization_problem_non_static( Matrix_real parameters, void* void_instance);
414 
415 
422 static void optimization_problem_grad( Matrix_real parameters, void* void_instance, Matrix_real& grad );
423 
424 
425 
426 
434 static void optimization_problem_combined( Matrix_real parameters, void* void_instance, double* f0, Matrix_real& grad );
435 
436 
444 virtual void optimization_problem_combined_non_static( Matrix_real parameters, void* void_instance, double* f0, Matrix_real& grad );
445 
446 
454 virtual void optimization_problem_combined( Matrix_real parameters, double* f0, Matrix_real grad );
455 
456 
464 static void optimization_problem_combined_unitary( Matrix_real parameters, void* void_instance, Matrix& Umtx, std::vector<Matrix>& Umtx_deriv );
465 
472 void optimization_problem_combined_unitary( Matrix_real parameters, Matrix& Umtx, std::vector<Matrix>& Umtx_deriv );
473 
474 
481 double optimization_problem_panelty( double* parameters, Gates_block* gates_block );
482 
483 
488 
489 
494 
495 
501 
502 
507 double get_correction1_scale();
508 
509 
510 
511 
512 
517 double get_correction2_scale();
518 
519 
520 
521 
522 
523 
528 void set_max_inner_iterations( int max_inner_iterations_in );
529 
530 
535 void set_random_shift_count_max( int random_shift_count_max_in );
536 
537 
543 
544 
548 int get_trace_offset();
549 
553 void set_trace_offset(int trace_offset_in);
554 
555 
559 int get_num_iters();
560 
565 void increment_num_iters( int delta=1 );
566 
571 void set_custom_gate_structure( Gates_block* gate_structure_in );
572 
573 
574 #ifdef __DFE__
575 
579 void upload_Umtx_to_DFE();
580 
581 
582 #endif
583 
584 
588 int get_accelerator_num();
589 
590 void set_osr_params( std::vector<std::vector<int>> use_cuts_in, int osr_rank_in, bool use_softmax_in );
591 
592 
593 
594 
595 };
596 
597 
598 #endif
optimization_aglorithms alg
The optimization algorithm to be used in the optimization.
bool adaptive_eta
logical variable indicating whether adaptive learning reate is used in the ADAM algorithm ...
void set_osr_params(std::vector< std::vector< int >> use_cuts_in, int osr_rank_in, bool use_softmax_in)
int id
unique id indentifying the instance of the class
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...
int get_num_iters()
Get the number of processed iterations during the optimization process.
void set_optimizer(optimization_aglorithms alg_in)
Call to set the optimizer engine to be used in solving the optimization problem.
void solve_layer_optimization_problem_AGENTS(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via the AGENT algorithm.
Definition: AGENTS.cpp:42
void set_custom_gate_structure(Gates_block *gate_structure_in)
Call to set custom layers to the gate structure that are intended to be used in the subdecomposition...
std::map< int, int > identical_blocks
A map of <int n: int num> indicating that how many identical successive blocks should be used in the ...
static void optimization_problem_combined_unitary(Matrix_real parameters, void *void_instance, Matrix &Umtx, std::vector< Matrix > &Umtx_deriv)
Call to calculate both the effect of the circuit on th eunitary and it&#39;s gradient componets...
double correction1_scale
prefactor of the single-bitflip errors in the cost function. (see Eq. (21) in arXiv:2210.09191)
double get_correction2_scale()
Call to get the prefactor of the two-bitflip errors in the cost function.
cost_function_type cost_fnc
The chosen variant of the cost function.
int get_accelerator_num()
Get the number of accelerators to be reserved on DFEs on users demand.
std::vector< std::vector< int > > use_cuts
cuts used for OSR entanglement cost function
double optimization_problem(double *parameters)
Evaluate the optimization problem of the optimization.
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...
void set_trace_offset(int trace_offset_in)
Set the trace offset used in the evaluation of the cost function.
double prev_cost_fnv_val
the previous value of the cost funtion to be used to evaluate bitflip errors in the cost funtion (see...
std::atomic< int > number_of_iters
number of iterations
int trace_offset
The offset in the first columns from which the "trace" is calculated. In this case Tr(A) = sum_(i-off...
double calculate_cost_function(Matrix &matrix_new, Matrix *ret_temp=NULL)
Calculate the current cost function value from an already transformed matrix.
void set_random_shift_count_max(int random_shift_count_max_in)
Call to set the maximal number of parameter randomization tries to escape a local minimum...
void increment_num_iters(int delta=1)
Atomically increment the tracked number of optimization iterations.
Header file for Groq LPU support in state vector simulation.
virtual double optimization_problem_non_static(Matrix_real parameters, void *void_instance)
The optimization problem of the final optimization.
double correction2_scale
prefactor of the double-bitflip errors in the cost function. (see Eq. (21) in arXiv:2210.09191)
cost_function_type get_cost_function_variant()
Call to get the variant of the cost function used in the calculations.
int get_trace_offset()
Get the trace ffset used in the evaluation of the cost function.
int osr_rank
rank used for OSR entanglement cost function
optimization_aglorithms
implemented optimization strategies
bool use_softmax
logical variable indicating whether to use softmax or average in the OSR entanglement cost function ...
void set_max_inner_iterations(int max_inner_iterations_in)
Call to set the maximal number of iterations for which an optimization engine tries to solve the opti...
void solve_layer_optimization_problem_BAYES_AGENTS(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via Bayes & Agents algorithm.
int accelerator_num
number of utilized accelerators
virtual void add_finalyzing_layer()
Call to add further layer to the gate structure used in the subdecomposition.
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
double randomization_rate
randomization rate
void randomize_parameters(Matrix_real &input, Matrix_real &output, const double f0)
Call to randomize the parameter.
void solve_layer_optimization_problem_BFGS2(int num_of_parameters, Matrix_real solution_guess)
Call to solve layer by layer the optimization problem via BBFG algorithm.
Definition: BFGS2.cpp:41
double optimization_problem_panelty(double *parameters, Gates_block *gates_block)
The optimization problem of the final optimization.
double CPU_time
time spent on optimization
A class containing basic methods for the decomposition process.
double circuit_simulation_time
Time spent on circuit simulation/cost function evaluation.
decomposed_matrix
the unitary matrix from the result object
Definition: example.py:90
void solve_layer_optimization_problem_BFGS(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via BBFG algorithm.
Definition: BFGS.cpp:42
Double-precision complex matrix (float64).
Definition: matrix.h:38
Header file for a class containing basic methods for the decomposition process.
double get_previous_cost_function_value()
Call to retrieve the previous value of the cost funtion to be used to evaluate bitflip errors in the ...
void calc_decomposition_error(Matrix &decomposed_matrix)
Calculate the error of the decomposition according to the spectral norm of , where is the unitary pr...
cost_function_type
Type definition of the different types of the cost function.
A class responsible for grouping two-qubit (CNOT,CZ,CH) and one-qubit gates into layers.
Definition: Gates_block.h:44
Single-precision complex matrix (float32).
Definition: matrix_float.h:41
guess_type
Type definition of the types of the initial guess.
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
void solve_layer_optimization_problem_ADAM(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via ADAM algorithm.
Optimization_Interface & operator=(const Optimization_Interface &other)
Copy assignment operator.
static void optimization_problem_grad(Matrix_real parameters, void *void_instance, Matrix_real &grad)
Calculate the derivative of the cost function with respect to the free parameters.
void solve_layer_optimization_problem(int num_of_parameters, Matrix_real solution_guess)
Call to solve layer by layer the optimization problem via calling one of the implemented algorithms...
void solve_layer_optimization_problem_GRAD_DESCEND_PARAMETER_SHIFT_RULE(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via the GRAD_DESCEND_PARAMETER_SHIFT_RULE algor...
volatile double current_minimum
The current minimum of the optimization problem.
Matrix Umtx
The unitary to be decomposed.
virtual ~Optimization_Interface()
Destructor of the class.
double get_correction1_scale()
Call to get the prefactor of the single-bitflip errors in the cost function.
void solve_layer_optimization_problem_COSINE(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via the COSINE algorithm.
Definition: COSINE.cpp:43
void final_optimization()
final optimization procedure improving the accuracy of the decompositin when all the qubits were alre...
bool optimize_layer_num
logical value. Set true to optimize the minimum number of gate layers required in the decomposition...
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...
void solve_layer_optimization_problem_AGENTS_COMBINED(int num_of_parameters, Matrix_real &solution_guess)
Call to solve layer by layer the optimization problem via the AGENT COMBINED algorithm.
Definition: AGENTS.cpp:914
int random_shift_count_max
the maximal number of parameter randomization tries to escape a local minimum.
Optimization_Interface()
Nullary constructor of the class.
void solve_layer_optimization_problem_ADAM_BATCHED(int num_of_parameters, Matrix_real &solution_guess_)
Call to solve layer by layer the optimization problem via batched ADAM algorithm. ...
void set_cost_function_variant(cost_function_type variant)
Call to set the variant of the cost function used in the calculations.
double radius
parameter to contron the radius of parameter randomization around the curren tminimum ...
Matrix_real optimization_problem_batched(std::vector< Matrix_real > &parameters_vec)
The cost function of the optimization with batched input (implemented only for the Frobenius norm cos...
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:41
virtual void optimization_problem_combined_non_static(Matrix_real parameters, void *void_instance, double *f0, Matrix_real &grad)
Call to calculate both the cost function and the its gradient components.
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.
virtual double optimization_problem(const double *parameters)
This is an abstact definition of function giving the cost functions measuring the entaglement of the ...