Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Adaptive.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 */
23 #include "Adaptive.h"
24 #include "common.h"
25 
26 
27 
28 //static tbb::spin_mutex my_mutex;
33 
34  // A string labeling the gate operation
35  name = "Adaptive";
36 
37  // A string describing the type of the gate
39 
40  limit = 1;
41 
42 }
43 
44 
45 
54 Adaptive::Adaptive(int qbit_num_in, int target_qbit_in, int control_qbit_in) : CRY(qbit_num_in, target_qbit_in, control_qbit_in) {
55 
56  // A string labeling the gate operation
57  name = "Adaptive";
58 
59  // A string describing the type of the gate
61 
62  limit = 1;
63 }
64 
65 
74 Adaptive::Adaptive(int qbit_num_in, int target_qbit_in, int control_qbit_in, int limit_in) : CRY(qbit_num_in, target_qbit_in, control_qbit_in) {
75 
76  // A string labeling the gate operation
77  name = "Adaptive";
78 
79  // A string describing the type of the gate
81 
82  limit = limit_in;
83 }
84 
89 
90 }
91 
92 
93 
94 
102 void
103 Adaptive::apply_to( Matrix_real& parameters, Matrix& input, int parallel ) {
104 
105 
106  if (input.rows != matrix_size ) {
107  std::string err("Adaptive::apply_to: Wrong input size in Adaptive gate apply.");
108  throw err;
109  }
110 
111  double Phi = parameters[0];
112 
113  Matrix_real Phi_transformed(1,1);
114 // Phi_transformed[0] = Phi;
115 // Phi_transformed[0] = 0.5*(1.0-std::cos(Phi))*M_PI;
116  Phi = activation_function( Phi, limit );
117  Phi_transformed[0] = Phi;
118 
119 
120 /*
121 Phi = Phi + M_PI;
122 Phi = (1.0-std::cos(Phi/2))*M_PI;
123 Phi_transformed[0] = Phi - M_PI;
124 */
125 
126 
127 
128  CRY::apply_to( Phi_transformed, input, parallel );
129 
130 
131 
132 }
133 
134 
135 
141 void
143 
144 
145  if (input.cols != matrix_size ) {
146  std::stringstream sstream;
147  sstream << "Wrong matrix size in Adaptive apply_from_right" << std::endl;
148  print(sstream, 0);
149  exit(-1);
150  }
151 
152  double Phi = parameters[0];
153 
154  Matrix_real Phi_transformed(1,1);
155 // Phi_transformed[0] = Phi;
156 // Phi_transformed[0] = 0.5*(1.0-std::cos(Phi))*M_PI;
157  Phi = activation_function( Phi, limit );
158  Phi_transformed[0] = Phi;
159 /*
160 Phi = Phi + M_PI;
161 Phi = (1.0-std::cos(Phi/2))*M_PI;
162 Phi_transformed[0] = Phi - M_PI;
163 */
164 
165  CRY::apply_from_right( Phi_transformed, input );
166 
167 
168 }
169 
170 
171 
178 std::vector<Matrix>
180 
181  if (input.rows != matrix_size ) {
182  std::stringstream sstream;
183  sstream << "Wrong matrix size in Adaptive gate apply" << std::endl;
184  print(sstream, 0);
185  exit(-1);
186  }
187 
188  double Phi = parameters[0];
189 
190  Matrix_real Phi_transformed(1,1);
191 // Phi_transformed[0] = Phi;
192 // Phi_transformed[0] = 0.5*(1.0-std::cos(Phi))*M_PI;
193  Phi = activation_function( Phi, limit );
194  Phi_transformed[0] = Phi;
195 
196 
197 /*
198 Phi = Phi + M_PI;
199 Phi = (1.0-std::cos(Phi/2))*M_PI;
200 Phi_transformed[0] = Phi - M_PI;
201 */
202 
203 
204 
205  return CRY::apply_derivate_to( Phi_transformed, input, parallel );
206 
207 
208 
209 }
210 
216 
218 
220  ret->set_parents( parents );
221  ret->set_children( children );
222 
223  ret->set_limit( limit );
224 
225 
226  return ret;
227 
228 }
229 
230 
231 
239 
240  if ( get_parameter_start_idx() + get_parameter_num() > parameters.size() ) {
241  std::string err("Adaptive::extract_parameters: Cant extract parameters, since the dinput arary has not enough elements.");
242  throw err;
243  }
244 
245  Matrix_real extracted_parameters(1,1);
246 
247  extracted_parameters[0] = std::fmod( 2*parameters[ get_parameter_start_idx() ], 4*M_PI);
248 
249  return extracted_parameters;
250 
251 }
252 
253 
254 
258 void
259 Adaptive::set_limit( int limit_in ) {
260 
261  limit = limit_in;
262 
263 }
264 
265 
269 int
271 
272  return limit;
273 }
274 
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:95
std::vector< Matrix > apply_derivate_to(Matrix_real &parameters, Matrix &input, int parallel)
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
Definition: Adaptive.cpp:179
void print(const std::stringstream &sstream, int verbose_level=1) const
Call to print output messages in the function of the verbosity level.
Definition: logging.cpp:55
void set_limit(int limit_in)
???????????
Definition: Adaptive.cpp:259
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations...
Definition: Gate.h:87
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:802
void apply_from_right(Matrix_real &parameters, Matrix &input)
Call to apply the gate on the input array/matrix by input*U3.
Definition: Adaptive.cpp:142
virtual void apply_to(Matrix_real &parameters, Matrix &input, int parallel)
Call to apply the gate on the input array/matrix by CRY*input.
Definition: CRY.cpp:90
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:85
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:89
Adaptive * clone()
Call to create a clone of the present class.
Definition: Adaptive.cpp:215
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:83
A class representing a CRY gate.
Definition: CRY.h:37
int rows
The number of rows.
Definition: matrix_base.hpp:42
int cols
The number of columns.
Definition: matrix_base.hpp:44
virtual Matrix_real extract_parameters(Matrix_real &parameters)
Call to extract parameters from the parameter array corresponding to the circuit, in which the gate i...
Definition: Adaptive.cpp:238
void set_parameter_start_idx(int start_idx)
Call to set the starting index of the parameters in the parameter array corresponding to the circuit ...
Definition: Gate.cpp:779
int get_parameter_start_idx()
Call to get the starting index of the parameters in the parameter array corresponding to the circuit ...
Definition: Gate.cpp:814
int limit
Definition: Adaptive.h:43
virtual void apply_from_right(Matrix_real &parameters, Matrix &input)
Call to apply the gate on the input array/matrix by input*CRY.
Definition: CRY.cpp:133
~Adaptive()
Destructor of the class.
Definition: Adaptive.cpp:88
Class to store data of complex arrays and its properties.
Definition: matrix.h:38
Header file for a class representing a gate used in adaptive decomposition.
int size() const
Call to get the number of the allocated elements.
int get_parameter_num()
Call to get the number of free parameters.
Definition: Gate.cpp:486
std::string name
A string labeling the gate operation.
Definition: Gate.h:79
std::vector< Gate * > children
list of child gates to be applied after this current gate
Definition: Gate.h:97
int get_limit()
???????????
Definition: Adaptive.cpp:270
virtual std::vector< Matrix > apply_derivate_to(Matrix_real &parameters, Matrix &input, int parallel)
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
Definition: CRY.cpp:177
Header file for commonly used functions and wrappers to CBLAS functions.
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:790
double activation_function(double Phi, int limit)
?????
Definition: common.cpp:34
A class representing a CRY gate.
Definition: Adaptive.h:36
virtual void apply_to(Matrix_real &parameters, Matrix &input, int parallel)
Call to apply the gate on the input array/matrix.
Definition: Adaptive.cpp:103
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:81
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:39
Adaptive()
Nullary constructor of the class.
Definition: Adaptive.cpp:32