Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CR.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 "CR.h"
24 
25 
26 
27 //static tbb::spin_mutex my_mutex;
31 CR::CR() : R() {
32 
33  // A string labeling the gate operation
34  name = "CR";
35 
36  // A string describing the type of the gate
38 
39 }
40 
41 
42 
51 CR::CR(int qbit_num_in, int target_qbit_in, int control_qbit_in) : R(qbit_num_in, target_qbit_in) {
52 
53  // A string labeling the gate operation
54  name = "CR";
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 CR 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 CR::apply_to( Matrix_real& parameters, Matrix& input, int parallel ) {
91 
92 
93  if (input.rows != matrix_size ) {
94  std::string err("CR::apply_to: Wrong matrix size in CR gate apply.");
95  throw err;
96  }
97 
98 
99  double ThetaOver2, Phi, Lambda;
100 
101  ThetaOver2 = parameters[0];
102  Phi = parameters[1]-M_PI/2;
103  Lambda = -1.*parameters[1] + M_PI/2;
104 
105  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
106 
107 
108  // apply the computing kernel on the matrix
109  apply_kernel_to(u3_1qbit, input, false, parallel);
110 
111 }
112 
113 
114 
120 void
122 
123 
124  if (input.cols != matrix_size ) {
125  std::stringstream sstream;
126  sstream << "Wrong matrix size in CR apply_from_right" << std::endl;
127  print(sstream, 0);
128  throw "Wrong matrix size in CR apply_from_right";
129  }
130 
131  double ThetaOver2, Phi, Lambda;
132 
133  ThetaOver2 = parameters[0];
134  Phi = parameters[1]-M_PI/2;
135  Lambda = -1.*parameters[1] + M_PI/2;
136 
137 
138 /*
139  ThetaOver2 = theta0;
140  Phi = parameters[0];
141  Lambda = lambda0;
142 */
143 /*
144 Phi = Phi + M_PI;
145 Phi = (1.0-std::cos(Phi/2))*M_PI;
146 Phi = Phi - M_PI;
147 */
148 //Phi = 0.5*(1.0-std::cos(Phi))*M_PI;
149 
150 
151  // get the U3 gate of one qubit
152  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
153 
154  // apply the computing kernel on the matrix
155  apply_kernel_from_right(u3_1qbit, input);
156 
157 }
158 
159 
166 std::vector<Matrix>
167 CR::apply_derivate_to( Matrix_real& parameters_mtx, Matrix& input, int parallel ) {
168 
169  if (input.rows != matrix_size ) {
170  std::stringstream sstream;
171  sstream << "Wrong matrix size in CR gate apply" << std::endl;
172  print(sstream, 0);
173  throw "Wrong matrix size in CR gate apply";
174  }
175 
176  std::vector<Matrix> ret;
177 
178  double ThetaOver2, Phi, Lambda;
179 
180  ThetaOver2 = parameters_mtx[0];
181  Phi = parameters_mtx[1]-M_PI/2;
182  Lambda = -1.*parameters_mtx[1] + M_PI/2;
183 
184 
185  Matrix res_mtx = input.copy();
186  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2 + M_PI/2, Phi, Lambda );
187  apply_kernel_to( u3_1qbit, res_mtx, true, parallel );
188  ret.push_back(res_mtx);
189  Matrix res_mtx2 = input.copy();
190  u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi + M_PI/2, Lambda - M_PI/2 );
191  u3_1qbit[0].real = 0.;
192  u3_1qbit[0].imag = 0.;
193  u3_1qbit[3].real = 0.;
194  u3_1qbit[3].imag = 0.;
195  apply_kernel_to( u3_1qbit, res_mtx2, true, parallel );
196  ret.push_back(res_mtx2);
197 
198  return ret;
199 
200 
201 }
202 
203 
209 
210  CR* ret = new CR(qbit_num, target_qbit, control_qbit);
211 
213  ret->set_parents( parents );
214  ret->set_children( children );
215 
216  return ret;
217 
218 }
219 
220 
228 
229  if ( get_parameter_start_idx() + get_parameter_num() > parameters.size() ) {
230  std::string err("CR::extract_parameters: Cant extract parameters, since the dinput arary has not enough elements.");
231  throw err;
232  }
233 
234  Matrix_real extracted_parameters(1,2);
235 
236  extracted_parameters[0] = std::fmod( 2*parameters[ get_parameter_start_idx() ], 4*M_PI);
237  extracted_parameters[1] = std::fmod( parameters[ get_parameter_start_idx() + 1 ], 2*M_PI);
238 
239  return extracted_parameters;
240 
241 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:96
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
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: CR.cpp:167
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations...
Definition: Gate.h:88
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:802
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:86
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:90
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: CR.cpp:227
virtual void apply_from_right(Matrix_real &parameters, Matrix &input)
Call to apply the gate on the input array/matrix by input*CRY.
Definition: CR.cpp:121
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: CR.cpp:90
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:84
int rows
The number of rows.
Definition: matrix_base.hpp:42
int cols
The number of columns.
Definition: matrix_base.hpp:44
virtual CR * clone()
Call to create a clone of the present class.
Definition: CR.cpp:208
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 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
int size() const
Call to get the number of the allocated elements.
A class representing a CRY gate.
Definition: CR.h:37
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:80
A class representing a U3 gate.
Definition: R.h:36
std::vector< Gate * > children
list of child gates to be applied after this current gate
Definition: Gate.h:98
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
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:790
CR()
Nullary constructor of the class.
Definition: CR.cpp:31
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:82
virtual ~CR()
Destructor of the class.
Definition: CR.cpp:76
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:39