Sequential Quantum Gate Decomposer  v1.9.6
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 namespace {
27 
28 Matrix_real transform_adaptive_parameter(const Matrix_real& parameters, int limit) {
29  Matrix_real phi_transformed(1, 1);
30  phi_transformed[0] = activation_function(parameters[0], limit);
31  return phi_transformed;
32 }
33 
34 Matrix_real_float transform_adaptive_parameter(const Matrix_real_float& parameters, int limit) {
35  Matrix_real_float phi_transformed(1, 1);
36  phi_transformed[0] = static_cast<float>(activation_function(static_cast<double>(parameters[0]), limit));
37  return phi_transformed;
38 }
39 
40 }
41 
42 
43 
44 //static tbb::spin_mutex my_mutex;
49 
50  // A string labeling the gate operation
51  name = "Adaptive";
52 
53  // A string describing the type of the gate
55 
56  limit = 1;
57 
58 }
59 
60 
61 
70 Adaptive::Adaptive(int qbit_num_in, int target_qbit_in, int control_qbit_in) : CRY(qbit_num_in, target_qbit_in, control_qbit_in) {
71 
72  // A string labeling the gate operation
73  name = "Adaptive";
74 
75  // A string describing the type of the gate
77 
78  limit = 1;
79 }
80 
81 
90 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) {
91 
92  // A string labeling the gate operation
93  name = "Adaptive";
94 
95  // A string describing the type of the gate
97 
98  limit = limit_in;
99 }
100 
105 
106 }
107 
108 
109 
110 
118 void
119 Adaptive::apply_to( Matrix_real& parameters, Matrix& input, int parallel ) {
120 
121 
122  if (input.rows != matrix_size ) {
123  std::string err("Adaptive::apply_to: Wrong input size in Adaptive gate apply.");
124  throw err;
125  }
126 
127  Matrix_real phi_transformed = transform_adaptive_parameter(parameters, limit);
128  Matrix_real phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
129  CRY::apply_to_inner(phi_transformed, phi_precomputed_sincos, input, parallel);
130 
131 
132 
133 }
134 
135 
136 void
138 
139  if (input.rows != matrix_size ) {
140  std::string err("Adaptive::apply_to: Wrong input size in Adaptive gate apply.");
141  throw err;
142  }
143 
144  Matrix_real_float phi_transformed = transform_adaptive_parameter(parameters, limit);
145  Matrix_real_float phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
146  CRY::apply_to_inner(phi_transformed, phi_precomputed_sincos, input, parallel);
147 }
148 
149 
150 void
151 Adaptive::apply_to_inner( Matrix_real& parameters, const Matrix_real& /*precomputed_sincos*/, Matrix& input, int parallel ) {
152 
153  Matrix_real phi_transformed = transform_adaptive_parameter(parameters, limit);
154  Matrix_real phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
155  CRY::apply_to_inner(phi_transformed, phi_precomputed_sincos, input, parallel);
156 }
157 
158 
159 void
160 Adaptive::apply_to_inner( Matrix_real_float& parameters, const Matrix_real_float& /*precomputed_sincos*/, Matrix_float& input, int parallel ) {
161 
162  Matrix_real_float phi_transformed = transform_adaptive_parameter(parameters, limit);
163  Matrix_real_float phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
164  CRY::apply_to_inner(phi_transformed, phi_precomputed_sincos, input, parallel);
165 }
166 
167 
168 
174 void
176 
177 
178  if (input.cols != matrix_size ) {
179  std::stringstream sstream;
180  sstream << "Wrong matrix size in Adaptive apply_from_right" << std::endl;
181  print(sstream, 0);
182  exit(-1);
183  }
184 
185  Matrix_real phi_transformed = transform_adaptive_parameter(parameters, limit);
186  Matrix_real phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
187  CRY::apply_from_right_inner(phi_transformed, phi_precomputed_sincos, input);
188 
189 
190 }
191 
192 
193 void
195 
196  if (input.cols != matrix_size ) {
197  std::string err("Wrong matrix size in Adaptive apply_from_right");
198  throw err;
199  }
200 
201  Matrix_real_float phi_transformed = transform_adaptive_parameter(parameters, limit);
202  Matrix_real_float phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
203  CRY::apply_from_right_inner(phi_transformed, phi_precomputed_sincos, input);
204 }
205 
206 
207 void
208 Adaptive::apply_from_right_inner( Matrix_real& parameters, const Matrix_real& /*precomputed_sincos*/, Matrix& input ) {
209 
210  Matrix_real phi_transformed = transform_adaptive_parameter(parameters, limit);
211  Matrix_real phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
212  CRY::apply_from_right_inner(phi_transformed, phi_precomputed_sincos, input);
213 }
214 
215 
216 void
218 
219  Matrix_real_float phi_transformed = transform_adaptive_parameter(parameters, limit);
220  Matrix_real_float phi_precomputed_sincos = compute_precomputed_sincos(phi_transformed);
221  CRY::apply_from_right_inner(phi_transformed, phi_precomputed_sincos, input);
222 }
223 
224 
225 
232 std::vector<Matrix>
234 
235  if (input.rows != matrix_size ) {
236  std::stringstream sstream;
237  sstream << "Wrong matrix size in Adaptive gate apply" << std::endl;
238  print(sstream, 0);
239  exit(-1);
240  }
241 
242  Matrix_real Phi_transformed = transform_adaptive_parameter(parameters, limit);
243 // Phi_transformed[0] = Phi;
244 // Phi_transformed[0] = 0.5*(1.0-std::cos(Phi))*M_PI;
245 
246 /*
247 Phi = Phi + M_PI;
248 Phi = (1.0-std::cos(Phi/2))*M_PI;
249 Phi_transformed[0] = Phi - M_PI;
250 */
251 
252  return CRY::apply_derivate_to( Phi_transformed, input, parallel );
253 
254 
255 
256 }
257 
258 
259 std::vector<Matrix_float>
261 
262  if (input.rows != matrix_size ) {
263  std::stringstream sstream;
264  sstream << "Wrong matrix size in Adaptive gate apply" << std::endl;
265  print(sstream, 0);
266  exit(-1);
267  }
268 
269  Matrix_real_float Phi_transformed = transform_adaptive_parameter(parameters, limit);
270 // Phi_transformed[0] = Phi;
271 // Phi_transformed[0] = 0.5*(1.0-std::cos(Phi))*M_PI;
272 
273 /*
274 Phi = Phi + M_PI;
275 Phi = (1.0-std::cos(Phi/2))*M_PI;
276 Phi_transformed[0] = Phi - M_PI;
277 */
278 
279  return CRY::apply_derivate_to( Phi_transformed, input, parallel );
280 }
281 
287 
289 
291  ret->set_parents( parents );
292  ret->set_children( children );
293 
294  ret->set_limit( limit );
295 
296 
297  return ret;
298 
299 }
300 
301 
305 void
306 Adaptive::set_limit( int limit_in ) {
307 
308  limit = limit_in;
309 
310 }
311 
312 
316 int
318 
319  return limit;
320 }
321 
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:112
Matrix_real compute_precomputed_sincos(const Matrix_real &parameters) const
Public wrapper to precompute gate-local sin/cos pairs.
Definition: Gate.cpp:1017
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
Class to store single-precision real arrays and properties.
void set_limit(int limit_in)
???????????
Definition: Adaptive.cpp:306
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations...
Definition: Gate.h:100
virtual std::vector< Matrix > apply_derivate_to(Matrix_real &parameters_mtx_in, Matrix &input, int parallel)
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
Definition: Gate.cpp:784
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:2523
Adaptive * clone() override
Call to create a clone of the present class.
Definition: Adaptive.cpp:286
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:98
void apply_from_right(Matrix_real &parameters, Matrix &input) override
Call to apply the gate on the input array/matrix by input*U3.
Definition: Adaptive.cpp:175
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:106
virtual void apply_from_right_inner(Matrix_real &parameter_mtx, const Matrix_real &precomputed_sincos, Matrix &input)
Internal right-apply entry that consumes already precomputed sin/cos values.
Definition: Gate.cpp:977
virtual void apply_to_inner(Matrix_real &parameter_mtx, const Matrix_real &precomputed_sincos, Matrix &input, int parallel)
Internal apply entry that consumes already precomputed sin/cos values.
Definition: Gate.cpp:549
void apply_to_inner(Matrix_real &parameters, const Matrix_real &precomputed_sincos, Matrix &input, int parallel) override
Internal apply entry that consumes already precomputed sin/cos values.
Definition: Adaptive.cpp:151
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:96
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
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:2500
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:2535
int limit
Definition: Adaptive.h:45
std::vector< Matrix > apply_derivate_to(Matrix_real &parameters, Matrix &input, int parallel) override
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
Definition: Adaptive.cpp:233
~Adaptive()
Destructor of the class.
Definition: Adaptive.cpp:104
Double-precision complex matrix (float64).
Definition: matrix.h:38
Header file for a class representing a gate used in adaptive decomposition.
std::string name
A string labeling the gate operation.
Definition: Gate.h:92
Single-precision complex matrix (float32).
Definition: matrix_float.h:41
std::vector< Gate * > children
list of child gates to be applied after this current gate
Definition: Gate.h:114
void apply_from_right_inner(Matrix_real &parameters, const Matrix_real &precomputed_sincos, Matrix &input) override
Internal right-apply entry that consumes already precomputed sin/cos values.
Definition: Adaptive.cpp:208
int get_limit()
???????????
Definition: Adaptive.cpp:317
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:2511
double activation_function(double Phi, int limit)
?????
Definition: common.cpp:35
A class representing a CRY gate.
Definition: Adaptive.h:38
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:94
virtual void apply_to(Matrix_real &parameters, Matrix &input, int parallel) override
Call to apply the gate on the input array/matrix.
Definition: Adaptive.cpp:119
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:41
Adaptive()
Nullary constructor of the class.
Definition: Adaptive.cpp:48