Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CRY.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 "CRY.h"
24 
25 
26 
27 //static tbb::spin_mutex my_mutex;
31 CRY::CRY() : RY() {
32 
33  // A string labeling the gate operation
34  name = "CRY";
35 
36  // A string describing the type of the gate
38 
39 }
40 
41 
42 
51 CRY::CRY(int qbit_num_in, int target_qbit_in, int control_qbit_in) : RY(qbit_num_in, target_qbit_in) {
52 
53  // A string labeling the gate operation
54  name = "CRY";
55 
56  // A string describing the type of the gate
58 
59 
60  if (control_qbit_in >= qbit_num) {
61  std::stringstream sstream;
62  sstream << "The index of the control qubit is larger than the number of qubits in CRY gate." << std::endl;
63  print(sstream, 0);
64  throw sstream.str();
65  }
66 
67  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
68  control_qbit = control_qbit_in;
69 
70 
71 }
72 
77 
78 }
79 
80 
81 
82 
89 void
90 CRY::apply_to( Matrix_real& parameters, Matrix& input, int parallel ) {
91 
92 
93  if (input.rows != matrix_size ) {
94  std::string err("CRY::apply_to: Wrong matrix size in CRY gate apply.");
95  throw err;
96  }
97 
98 
99  double ThetaOver2, Phi, Lambda;
100 
101  ThetaOver2 = parameters[0];
102  parameters_for_calc_one_qubit(ThetaOver2, Phi, Lambda);
103 
104 /*
105  ThetaOver2 = theta0;
106  Phi = parameters[0];
107  Lambda = lambda0;
108 */
109 /*
110 Phi = Phi + M_PI;
111 Phi = (1.0-std::cos(Phi/2))*M_PI;
112 Phi = Phi - M_PI;
113 */
114 //Phi = 0.5*(1.0-std::cos(Phi))*M_PI;
115 
116  // get the U3 gate of one qubit
117  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
118 
119 
120  // apply the computing kernel on the matrix
121  apply_kernel_to(u3_1qbit, input, false, parallel);
122 
123 }
124 
125 
126 
132 void
134 
135 
136  if (input.cols != matrix_size ) {
137  std::stringstream sstream;
138  sstream << "Wrong matrix size in CRY apply_from_right" << std::endl;
139  print(sstream, 0);
140  throw "Wrong matrix size in CRY apply_from_right";
141  }
142 
143  double ThetaOver2, Phi, Lambda;
144 
145  ThetaOver2 = parameters[0];
146  parameters_for_calc_one_qubit(ThetaOver2, Phi, Lambda);
147 
148 /*
149  ThetaOver2 = theta0;
150  Phi = parameters[0];
151  Lambda = lambda0;
152 */
153 /*
154 Phi = Phi + M_PI;
155 Phi = (1.0-std::cos(Phi/2))*M_PI;
156 Phi = Phi - M_PI;
157 */
158 //Phi = 0.5*(1.0-std::cos(Phi))*M_PI;
159 
160 
161  // get the U3 gate of one qubit
162  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
163 
164  // apply the computing kernel on the matrix
165  apply_kernel_from_right(u3_1qbit, input);
166 
167 }
168 
169 
176 std::vector<Matrix>
177 CRY::apply_derivate_to( Matrix_real& parameters_mtx, Matrix& input, int parallel ) {
178 
179  if (input.rows != matrix_size ) {
180  std::stringstream sstream;
181  sstream << "Wrong matrix size in CRY gate apply" << std::endl;
182  print(sstream, 0);
183  throw "Wrong matrix size in CRY gate apply";
184  }
185 
186  std::vector<Matrix> ret;
187 
188  double ThetaOver2, Phi, Lambda;
189 
190  ThetaOver2 = parameters_mtx[0]+M_PI/2;
191  parameters_for_calc_one_qubit(ThetaOver2, Phi, Lambda);
192 
193  // the resulting matrix
194  Matrix res_mtx = input.copy();
195 
196 
197  // get the U3 gate of one qubit
198  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
199 
200 
201  // apply the computing kernel on the matrix
202  bool deriv = true;
203  apply_kernel_to(u3_1qbit, res_mtx, deriv, parallel);
204 
205  ret.push_back(res_mtx);
206  return ret;
207 
208 
209 }
210 
211 
217 
218  CRY* ret = new CRY(qbit_num, target_qbit, control_qbit);
219 
221  ret->set_parents( parents );
222  ret->set_children( children );
223 
224  return ret;
225 
226 }
227 
228 
236 
237  if ( get_parameter_start_idx() + get_parameter_num() > parameters.size() ) {
238  std::string err("CRY::extract_parameters: Cant extract parameters, since the dinput arary has not enough elements.");
239  throw err;
240  }
241 
242  Matrix_real extracted_parameters(1,1);
243 
244  extracted_parameters[0] = std::fmod( 2*parameters[ get_parameter_start_idx() ], 4*M_PI);
245 
246  return extracted_parameters;
247 
248 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:95
void parameters_for_calc_one_qubit(double &ThetaOver2, double &Phi, double &Lambda)
Calculate the matrix of a U3 gate gate corresponding to the given parameters acting on a single qbit ...
Definition: RY.cpp:208
virtual CRY * clone()
Call to create a clone of the present class.
Definition: CRY.cpp:216
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
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
Header file for a class representing a controlled rotation gate around the Y axis.
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
virtual ~CRY()
Destructor of the class.
Definition: CRY.cpp:76
CRY()
Nullary constructor of the class.
Definition: CRY.cpp:31
void apply_kernel_from_right(Matrix &u3_1qbit, Matrix &input)
Call to apply the gate kernel on the input state or unitary from right (no AVX support) ...
Definition: Gate.cpp:613
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:89
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: CRY.cpp:235
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
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
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
virtual Matrix calc_one_qubit_u3()
Calculate the matrix of the constans gates.
Definition: Gate.cpp:750
Class to store data of complex arrays and its properties.
Definition: matrix.h:38
A class representing a U3 gate.
Definition: RY.h:36
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
void apply_kernel_to(Matrix &u3_1qbit, Matrix &input, bool deriv=false, int parallel=0)
Call to apply the gate kernel on the input state or unitary with optional AVX support.
Definition: Gate.cpp:537
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
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:790
Matrix copy()
Call to create a copy of the matrix.
Definition: matrix.cpp:105
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