Quantum Gate Decomposer  v1.3
Powerful decomposition of almost any unitary into U3 and CNOT gates
N_Qubit_Decomposition_Cost_Function.cpp
Go to the documentation of this file.
1 /*
2 Created on Fri Jun 26 14:13:26 2020
3 Copyright (C) 2020 Peter Rakyta, Ph.D.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see http://www.gnu.org/licenses/.
17 
18 @author: Peter Rakyta, Ph.D.
19 */
25 
26 
27 
28 
34 double get_cost_function(Matrix matrix) {
35 
36  size_t matrix_size = matrix.rows;
37 
38  tbb::combinable<double> priv_partial_cost_functions{[](){return 0;}};
39  tbb::parallel_for( tbb::blocked_range<size_t>(0, matrix_size, 1), functor_cost_fnc( matrix, &priv_partial_cost_functions ));
40 
41 /*
42  //sequential version
43  functor_cost_fnc tmp = functor_cost_fnc( matrix, matrix_size, partial_cost_functions, matrix_size );
44  #pragma omp parallel for
45  for (int idx=0; idx<matrix_size; idx++) {
46  tmp(idx);
47  }
48 */
49 
50  // calculate the final cost function
51  double cost_function = 0;
52  priv_partial_cost_functions.combine_each([&cost_function](double a) {
53  cost_function = cost_function + a;
54  });
55 
56  return cost_function;
57 
58 }
59 
60 
61 
62 
71 functor_cost_fnc::functor_cost_fnc( Matrix matrix_in, tbb::combinable<double>* partial_cost_functions_in ) {
72 
73  matrix = matrix_in;
74  data = matrix.get_data();
75  partial_cost_functions = partial_cost_functions_in;
76 }
77 
82 void functor_cost_fnc::operator()( tbb::blocked_range<size_t> r ) const {
83 
84  size_t matrix_size = matrix.rows;
85  double &cost_function_priv = partial_cost_functions->local();
86 
87  for ( size_t row_idx = r.begin(); row_idx != r.end(); row_idx++) {
88 
89  if ( row_idx > matrix_size ) {
90  printf("Error: row idx should be less than the number of roes in the matrix\n");
91  exit(-1);
92  }
93 
94  // getting the corner element
95  QGD_Complex16 corner_element = data[0];
96 
97 
98  // Calculate the |x|^2 value of the elements of the matrix and summing them up to calculate the partial cost function
99  double partial_cost_function = 0;
100  size_t idx_offset = row_idx*matrix_size;
101  size_t idx_max = idx_offset + row_idx;
102  for ( size_t idx=idx_offset; idx<idx_max; idx++ ) {
103  partial_cost_function = partial_cost_function + data[idx].real*data[idx].real + data[idx].imag*data[idx].imag;
104  }
105 
106  size_t diag_element_idx = row_idx*matrix_size + row_idx;
107  double diag_real = data[diag_element_idx].real - corner_element.real;
108  double diag_imag = data[diag_element_idx].imag - corner_element.imag;
109  partial_cost_function = partial_cost_function + diag_real*diag_real + diag_imag*diag_imag;
110 
111 
112  idx_offset = idx_max + 1;
113  idx_max = row_idx*matrix_size + matrix_size;
114  for ( size_t idx=idx_offset; idx<idx_max; idx++ ) {
115  partial_cost_function = partial_cost_function + data[idx].real*data[idx].real + data[idx].imag*data[idx].imag;
116  }
117 
118  // storing the calculated partial cost function
119  cost_function_priv = cost_function_priv + partial_cost_function;
120 
121  }
122 }
123 
124 
125 
126 
127 
128 
129 
130 
size_t rows
The number of rows.
Definition: matrix_base.h:23
QGD_Complex16 * data
Pointer to the data stored in the matrix.
scalar * get_data()
Call to get the pointer to the stored data.
Definition: matrix_base.h:221
void operator()(tbb::blocked_range< size_t > r) const
Operator to calculate the partial cost function derived from the row of the matrix labeled by row_idx...
Function operator class to calculate the partial cost function of the final optimization process.
matrix_size
Definition: example.py:41
Matrix matrix
Array stroing the matrix.
functor_cost_fnc(Matrix matrix_in, tbb::combinable< double > *partial_cost_functions_in)
Constructor of the class.
Structure type representing complex numbers in the QGD package.
Definition: QGDTypes.h:39
Class to store data of complex arrays and its properties.
Definition: matrix.h:12
double get_cost_function(Matrix matrix)
Call co calculate the cost function during the final optimization process.
Header file for the paralleized calculation of the cost function of the final optimization problem (s...
double real
the real part of a complex number
Definition: QGDTypes.h:41
tbb::combinable< double > * partial_cost_functions
array storing the partial cost functions
double imag
the imaginary part of a complex number
Definition: QGDTypes.h:43