Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CROT.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 "CROT.h"
24 #include "gate_kernel_templates.h"
25 
26 //static tbb::spin_mutex my_mutex;
27 
28 namespace {
29 template<typename RealMatrixT, typename RealT>
30 void read_crot_trig(
31  const RealMatrixT& precomputed_sincos,
32  RealT& s_theta,
33  RealT& c_theta,
34  RealT& s_phi,
35  RealT& c_phi) {
36 
37  const int theta_offset = 0 * precomputed_sincos.stride;
38  const int phi_offset = 1 * precomputed_sincos.stride;
39  s_theta = precomputed_sincos[theta_offset + 0];
40  c_theta = precomputed_sincos[theta_offset + 1];
41  s_phi = precomputed_sincos[phi_offset + 0];
42  c_phi = precomputed_sincos[phi_offset + 1];
43 }
44 }
49 
50  // A string describing the type of the gate
52 
53  // number of qubits spanning the matrix of the gate
54  qbit_num = -1;
55  // the size of the matrix
56  matrix_size = -1;
57 
58  target_qbit = -1;
59  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
60  control_qbit = -1;
61  parameter_num = 2;
62 
63  name = "CROT";
64 
65 }
66 
67 
68 
77 CROT::CROT(int qbit_num_in, int target_qbit_in, int control_qbit_in) {
78 
79  name = "CROT";
80 
81  // number of qubits spanning the matrix of the gate
82  qbit_num = qbit_num_in;
83  // the size of the matrix
85  // A string describing the type of the gate
86 
87  // A string describing the type of the gate
89 
90 
91 
92 
93  if (control_qbit_in >= qbit_num) {
94  std::stringstream sstream;
95  sstream << "The index of the control qubit is larger than the number of qubits in CROT gate." << std::endl;
96  print(sstream, 0);
97  throw sstream.str();
98  }
99 
100  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
101  control_qbit = control_qbit_in;
102 
103 
104  // The index of the qubit on which the gate acts (target_qbit >= 0)
105  target_qbit = target_qbit_in;
106 
107 
108  parameter_num=2;
109 }
110 
115 
116 }
117 
118 Matrix
119 CROT::gate_kernel(const Matrix_real& precomputed_sincos) {
120  Matrix ret;
121  gate_kernel_to(precomputed_sincos, ret);
122  return ret;
123 }
124 
126 CROT::gate_kernel(const Matrix_real_float& precomputed_sincos) {
127  Matrix_float ret;
128  gate_kernel_to(precomputed_sincos, ret);
129  return ret;
130 }
131 
132 Matrix
133 CROT::inverse_gate_kernel(const Matrix_real& precomputed_sincos) {
134  Matrix ret;
135  inverse_gate_kernel_to(precomputed_sincos, ret);
136  return ret;
137 }
138 
140 CROT::inverse_gate_kernel(const Matrix_real_float& precomputed_sincos) {
141  Matrix_float ret;
142  inverse_gate_kernel_to(precomputed_sincos, ret);
143  return ret;
144 }
145 
146 Matrix
147 CROT::derivative_kernel(const Matrix_real& precomputed_sincos, int param_idx) {
148  Matrix ret;
149  derivative_kernel_to(precomputed_sincos, param_idx, ret);
150  return ret;
151 }
152 
154 CROT::derivative_kernel(const Matrix_real_float& precomputed_sincos, int param_idx) {
155  Matrix_float ret;
156  derivative_kernel_to(precomputed_sincos, param_idx, ret);
157  return ret;
158 }
159 
160 Matrix
161 CROT::derivative_aux_kernel(const Matrix_real& precomputed_sincos, int param_idx) {
162  Matrix ret;
163  derivative_aux_kernel_to(precomputed_sincos, param_idx, ret);
164  return ret;
165 }
166 
168 CROT::derivative_aux_kernel(const Matrix_real_float& precomputed_sincos, int param_idx) {
169  Matrix_float ret;
170  derivative_aux_kernel_to(precomputed_sincos, param_idx, ret);
171  return ret;
172 }
173 
174 void
175 CROT::gate_kernel_to(const Matrix_real& precomputed_sincos, Matrix& output) {
176  double s_theta, c_theta, s_phi, c_phi;
177  read_crot_trig<Matrix_real, double>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
178  build_crot_gate_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta, s_phi, c_phi);
179 }
180 
181 void
183  float s_theta, c_theta, s_phi, c_phi;
184  read_crot_trig<Matrix_real_float, float>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
185  build_crot_gate_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta, s_phi, c_phi);
186 }
187 
188 void
190  double s_theta, c_theta, s_phi, c_phi;
191  read_crot_trig<Matrix_real, double>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
192  build_crot_inverse_gate_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta, s_phi, c_phi);
193 }
194 
195 void
197  float s_theta, c_theta, s_phi, c_phi;
198  read_crot_trig<Matrix_real_float, float>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
199  build_crot_inverse_gate_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta, s_phi, c_phi);
200 }
201 
202 void
203 CROT::derivative_kernel_to(const Matrix_real& precomputed_sincos, int param_idx, Matrix& output) {
204  double s_theta, c_theta, s_phi, c_phi;
205  read_crot_trig<Matrix_real, double>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
206  if (param_idx == 0) {
207  build_crot_theta_derivative_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta, s_phi, c_phi);
208  return;
209  }
210  if (param_idx == 1) {
211  build_crot_phi_derivative_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta, s_phi, c_phi);
212  return;
213  }
214  output = Matrix();
215 }
216 
217 void
218 CROT::derivative_kernel_to(const Matrix_real_float& precomputed_sincos, int param_idx, Matrix_float& output) {
219  float s_theta, c_theta, s_phi, c_phi;
220  read_crot_trig<Matrix_real_float, float>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
221  if (param_idx == 0) {
222  build_crot_theta_derivative_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta, s_phi, c_phi);
223  return;
224  }
225  if (param_idx == 1) {
226  build_crot_phi_derivative_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta, s_phi, c_phi);
227  return;
228  }
229  output = Matrix_float();
230 }
231 
232 void
233 CROT::derivative_aux_kernel_to(const Matrix_real& precomputed_sincos, int param_idx, Matrix& output) {
234  double s_theta, c_theta, s_phi, c_phi;
235  read_crot_trig<Matrix_real, double>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
236  if (param_idx == 0) {
237  build_crot_theta_derivative_aux_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta, s_phi, c_phi);
238  return;
239  }
240  if (param_idx == 1) {
241  build_crot_phi_derivative_aux_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta, s_phi, c_phi);
242  return;
243  }
244  output = Matrix();
245 }
246 
247 void
248 CROT::derivative_aux_kernel_to(const Matrix_real_float& precomputed_sincos, int param_idx, Matrix_float& output) {
249  float s_theta, c_theta, s_phi, c_phi;
250  read_crot_trig<Matrix_real_float, float>(precomputed_sincos, s_theta, c_theta, s_phi, c_phi);
251  if (param_idx == 0) {
252  build_crot_theta_derivative_aux_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta, s_phi, c_phi);
253  return;
254  }
255  if (param_idx == 1) {
256  build_crot_phi_derivative_aux_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta, s_phi, c_phi);
257  return;
258  }
259  output = Matrix_float();
260 }
261 
267 
268  CROT* ret = new CROT(qbit_num, target_qbit, control_qbit);
269 
271  ret->set_parents( parents );
272  ret->set_children( children );
273 
274  return ret;
275 
276 }
277 
278 std::vector<double>
280  return {2.0, 1.0};
281 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:112
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 CROT * clone() override
Call to create a clone of the present class.
Definition: CROT.cpp:266
Class to store single-precision real arrays and properties.
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations...
Definition: Gate.h:100
~CROT() override
Destructor of the class.
Definition: CROT.cpp:114
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:2523
Header file for a class representing a controlled rotation gate around the Y axis.
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:98
virtual Matrix derivative_kernel(const Matrix_real &precomputed_sincos, int param_idx) override
Definition: CROT.cpp:147
std::vector< double > get_parameter_multipliers() const override
Returns the per-parameter multipliers relative to 2π used by extract_parameters. ...
Definition: CROT.cpp:279
virtual Matrix gate_kernel(const Matrix_real &precomputed_sincos) override
Compute the gate kernel matrix from precomputed trigonometric values.
Definition: CROT.cpp:119
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:106
A class representing a CROT gate.
Definition: CROT.h:38
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:96
virtual void derivative_aux_kernel_to(const Matrix_real &precomputed_sincos, int param_idx, Matrix &output) override
Definition: CROT.cpp:233
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 Power_of_2(int n)
Calculates the n-th power of 2.
Definition: common.cpp:136
Double-precision complex matrix (float64).
Definition: matrix.h:38
virtual void derivative_kernel_to(const Matrix_real &precomputed_sincos, int param_idx, Matrix &output) override
Definition: CROT.cpp:203
virtual void inverse_gate_kernel_to(const Matrix_real &precomputed_sincos, Matrix &output) override
Definition: CROT.cpp:189
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
virtual void gate_kernel_to(const Matrix_real &precomputed_sincos, Matrix &output) override
Definition: CROT.cpp:175
int parameter_num
the number of free parameters of the operation
Definition: Gate.h:108
CROT()
Nullary constructor of the class.
Definition: CROT.cpp:48
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:2511
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:94
virtual Matrix derivative_aux_kernel(const Matrix_real &precomputed_sincos, int param_idx) override
Definition: CROT.cpp:161
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:41
virtual Matrix inverse_gate_kernel(const Matrix_real &precomputed_sincos) override
Definition: CROT.cpp:133