Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Decomposition_Base.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 DECOMPOSITION_BASE_H
24 #define DECOMPOSITION_BASE_H
25 
26 
27 #include "Gates_block.h"
28 #include "CZ.h"
29 #include "CH.h"
30 #include "CNOT.h"
31 #include "CROT.h"
32 #include "R.h"
33 #include "U3.h"
34 #include "RX.h"
35 #include "H.h"
36 #include "X.h"
37 #include "Y.h"
38 #include "Z.h"
39 #include "SX.h"
40 #include "RY.h"
41 #include "CRY.h"
42 #include "RZ.h"
43 #include "SYC.h"
44 #include "Adaptive.h"
45 #include <map>
46 #include <cstdlib>
47 #include <time.h>
48 #include <ctime>
49 #include <cfloat>
50 #include <limits>
51 #include <tbb/cache_aligned_allocator.h>
52 
53 #include "config_element.h"
54 
55 #include <random>
56 
59 
60 #ifndef OPENBLAS_CONST
61 # define OPENBLAS_CONST const
62 #endif
63 #if defined(OS_WINDOWS) && defined(__64BIT__)
64 typedef long long BLASLONG;
65 typedef unsigned long long BLASULONG;
66 #else
67 typedef long BLASLONG;
68 typedef unsigned long BLASULONG;
69 #endif
70 typedef BLASLONG blasint;
71 #if defined(OS_WINDOWS) && defined(__64BIT__)
72 #define blasabs(x) llabs(x)
73 #else
74 #define blasabs(x) labs(x)
75 #endif
76 
78 
83 
84 
85 public:
86 
89 
91  static std::map<int,int> max_layer_num_def;
92 
95 
98 
100  std::string project_name;
101 
103  std::map<std::string, Config_Element> config;
104 
105 
106 protected:
107 
109  std::map<int,int> max_layer_num;
110 
112  std::map<int,int> iteration_loops;
113 
116 
119 
122 
125 
127  bool use_float;
128 
131 
133  //double* optimized_parameters;
134 
137 
140 
143 
146 
148  volatile double current_minimum;
149 
152 
155 
158 
161 
164 
167 
168 
170  std::mt19937 gen;
171 
172 
173 
174 public:
175 
180 
188 Decomposition_Base( Matrix Umtx_in, int qbit_num_in, std::map<std::string, Config_Element>& config_in, guess_type initial_guess_in);
189 
193 Decomposition_Base( Matrix_float Umtx_in, int qbit_num_in, std::map<std::string, Config_Element>& config_in, guess_type initial_guess_in);
194 
198 virtual ~Decomposition_Base();
199 
200 
205 void set_optimization_blocks( int optimization_block_in );
206 
211 void set_max_iteration( int max_outer_iterations_in);
212 
213 
218 void list_gates( int start_index );
219 
220 
226 void solve_optimization_problem( double* solution_guess, int solution_guess_num );
227 
228 
234 virtual void solve_layer_optimization_problem( int num_of_parameters, Matrix_real solution_guess_gsl);
235 
236 
237 
242 virtual double optimization_problem( const double* parameters );
243 
249 
250 
255 Matrix get_Umtx();
256 
261 
265 bool get_use_float() const;
266 
271 int get_Umtx_size();
272 
278 
283 
288 void get_optimized_parameters( double* ret );
289 
290 
296 
297 
303 
304 
311 Matrix apply_gate( Matrix& gate_mtx, Matrix& input_matrix );
312 
317 void reorder_qubits( std::vector<int> qbit_list);
318 
325 int set_max_layer_num( int n, int max_layer_num_in );
326 
332 int set_max_layer_num( std::map<int, int> max_layer_num_in );
333 
334 
341 int set_iteration_loops( int n, int iteration_loops_in );
342 
348 int set_iteration_loops( std::map<int, int> iteration_loops_in );
349 
350 
354 static void Init_max_layer_num();
355 
356 
361 void set_optimization_tolerance( double tolerance_in );
362 
363 
368 void set_convergence_threshold( double convergence_threshold_in );
369 
370 
375 double get_decomposition_error( );
376 
377 
382 double get_current_minimum( );
383 
388 std::string get_project_name();
393 void set_project_name(std::string& project_name_new);
394 
399 void calculate_new_global_phase_factor( QGD_Complex16 global_phase_factor_new );
400 
406 
411 void set_global_phase(double new_global_phase);
412 
413 
418 void apply_global_phase_factor(QGD_Complex16 global_phase_factor, Matrix& u3_gate);
419 
425 
430 void export_unitary(std::string& filename);
436 
437 
443 
444 
449 void set_qbit_num( int qbit_num_in );
450 
451 
452 
453 };
454 #endif //DECOMPOSITION_BASE
void list_gates(int start_index)
Call to print the gates decomposing the initial unitary.
std::string get_project_name()
Call to get the current name of the project.
Definition: X.h:11
Class to store single-precision real arrays and properties.
BLASLONG blasint
Matrix get_decomposed_matrix()
Calculate the decomposed matrix resulted by the effect of the optimized gates on the unitary Umtx...
void set_project_name(std::string &project_name_new)
Call to set the name of the project.
bool decomposition_finalized
The optimized parameters for the gates.
Matrix get_Umtx()
Call to retrive a pointer to the unitary to be transformed.
bool optimization_problem_solved
logical value describing whether the optimization problem was solved or not
Header file for a class representing a controlled rotation gate around the Y axis.
int finalizing_gates_num
number of finalizing (deterministic) opertaions rotating the disentangled qubits into state |0>...
bool use_float
Selects float32 circuit application for parameter/unitary/state data.
Header file for a class representing a controlled rotation gate around the Y axis.
int set_max_layer_num(int n, int max_layer_num_in)
Set the maximal number of layers used in the subdecomposition of the n-th qubit.
Matrix_real_float optimized_parameters_mtx_float
Float32 optimized parameters used by the hot gate-application path.
double get_current_minimum()
Call to get the obtained minimum of the cost function.
Header file for a class representing a CH operation.
Matrix_real_float get_optimized_parameters_float()
Return optimized parameters in float32.
Header file for a class responsible for grouping gates into subcircuits. (Subcircuits can be nested) ...
std::map< int, int > max_layer_num
A map of <int n: int num> indicating that how many layers should be used in the subdecomposition proc...
Matrix apply_gate(Matrix &gate_mtx, Matrix &input_matrix)
Apply an gates on the input matrix.
guess_type initial_guess
type to guess the initial values for the optimization. Possible values: ZEROS=0, RANDOM=1, CLOSE_TO_ZERO=2
#define OPENBLAS_CONST
static void Init_max_layer_num()
Initializes default layer numbers.
void sync_optimized_parameters_float()
Synchronize the float32 parameter mirror from the double optimizer storage.
void apply_global_phase_factor()
Call to apply the current global phase to the unitary matrix.
void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the gates stored in the block of gates.
int max_outer_iterations
Maximal number of iterations allowed in the optimization process.
Header file for a class representing a CNOT operation.
std::string project_name
the name of the project
double optimization_tolerance
The maximal allowed error of the optimization problem (The error of the decomposition would scale wit...
void set_global_phase(double new_global_phase)
Call to set global phase.
QGD_Complex16 get_global_phase_factor()
Get the global phase of the Unitary matrix.
double get_decomposition_error()
Call to get the error of the decomposition.
int get_Umtx_size()
Call to get the size of the unitary to be transformed.
double convergence_threshold
The convergence threshold in the optimization process.
Header file for a class describing a universal parameter with name and value stored in a config map...
Matrix_float Umtx_float
Float32 copy of the unitary used when config["use_float"] is true.
Matrix_real get_optimized_parameters()
Call to get the optimized parameters.
double cblas_dznrm2(OPENBLAS_CONST blasint N, OPENBLAS_CONST void *X, OPENBLAS_CONST blasint incX)
std::map< int, int > iteration_loops
A map of <int n: int num> indicating the number of iteration in each step of the decomposition.
void set_optimized_parameters(double *parameters, int num_of_parameters)
Call to set the optimized parameters for initial optimization.
Header file for a class representing a CZ operation.
int num_threads
Store the number of OpenMP threads. (During the calculations OpenMP multithreading is turned off...
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
A class containing basic methods for the decomposition process.
void set_optimization_tolerance(double tolerance_in)
Call to set the tolerance of the optimization processes.
Double-precision complex matrix (float64).
Definition: matrix.h:38
virtual ~Decomposition_Base()
Destructor of the class.
Header file for a class representing a gate used in adaptive decomposition.
QGD_Complex16 global_phase_factor
The global phase.
void set_convergence_threshold(double convergence_threshold_in)
Call to set the threshold of convergence in the optimization processes.
A class responsible for grouping two-qubit (CNOT,CZ,CH) and one-qubit gates into layers.
Definition: Gates_block.h:44
void calculate_new_global_phase_factor(QGD_Complex16 global_phase_factor_new)
Calculate the new global phase of the Unitary matrix after removing a trivial U3 matrix.
void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the gate.
Single-precision complex matrix (float32).
Definition: matrix_float.h:41
guess_type
Type definition of the types of the initial guess.
std::map< std::string, Config_Element > config
config metadata utilized during the optimization
virtual void solve_layer_optimization_problem(int num_of_parameters, Matrix_real solution_guess_gsl)
Abstarct function to be used to solve a single sub-layer optimization problem.
unsigned long BLASULONG
volatile double current_minimum
The current minimum of the optimization problem.
void solve_optimization_problem(double *solution_guess, int solution_guess_num)
This method can be used to solve the main optimization problem which is devidid into sub-layer optimi...
Matrix Umtx
The unitary to be decomposed.
static std::map< int, int > max_layer_num_def
A map of <int n: int num> indicating that how many layers should be used in the subdecomposition proc...
Matrix_float get_Umtx_float()
Return the float32 unitary copy used by float execution.
void export_unitary(std::string &filename)
exports unitary matrix to binary file
int finalizing_parameter_num
the number of the finalizing (deterministic) parameters of gates rotating the disentangled qubits int...
void set_max_iteration(int max_outer_iterations_in)
Call to set the maximal number of the iterations in the optimization process.
bool check_optimization_solution()
check_optimization_solution
int optimization_block
number of gate blocks used in one shot of the optimization process
double decomposition_error
error of the final decomposition
int set_iteration_loops(int n, int iteration_loops_in)
Set the number of iteration loops during the subdecomposition of the n-th qubit.
Matrix import_unitary_from_binary(std::string &filename)
Import a Unitary matrix from a file.
Matrix_real optimized_parameters_mtx
The optimized parameters for the gates.
int get_parallel_configuration()
Get the parallel configuration from the config.
bool get_use_float() const
True when config["use_float"] requests float32 circuit execution.
double global_target_minimum
The global target minimum of the optimization problem.
void set_optimization_blocks(int optimization_block_in)
Call to set the number of gate blocks to be optimized in one shot.
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:41
Decomposition_Base()
Nullary constructor of the class.
long BLASLONG
std::mt19937 gen
Standard mersenne_twister_engine seeded with rd()
Header file for a class representing a Sycamore gate.
virtual double optimization_problem(const double *parameters)
This is an abstact definition of function giving the cost functions measuring the entaglement of the ...