Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
RZ.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 "RZ.h"
24 
25 
26 
27 //static tbb::spin_mutex my_mutex;
31 RZ::RZ() {
32 
33  // A string labeling the gate operation
34  name = "RZ";
35  // number of qubits spanning the matrix of the gate
36  qbit_num = -1;
37  // the size of the matrix
38  matrix_size = -1;
39  // A string describing the type of the gate
41 
42  // The index of the qubit on which the gate acts (target_qbit >= 0)
43  target_qbit = -1;
44  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
45  control_qbit = -1;
46 
47  parameter_num = 0;
48 
49 }
50 
51 
52 
61 RZ::RZ(int qbit_num_in, int target_qbit_in) {
62 
63  // A string labeling the gate operation
64  name = "RZ";
65 
66  //The stringstream input to store the output messages.
67  std::stringstream sstream;
68 
69  //Integer value to set the verbosity level of the output messages.
70  int verbose_level;
71 
72  // number of qubits spanning the matrix of the gate
73  qbit_num = qbit_num_in;
74  // the size of the matrix
76  // A string describing the type of the gate
78 
79 
80  if (target_qbit_in >= qbit_num) {
81  verbose_level=1;
82  sstream << "The index of the target qubit is larger than the number of qubits" << std::endl;
83  print(sstream,verbose_level);
84 
85  throw "The index of the target qubit is larger than the number of qubits";
86  }
87 
88  // The index of the qubit on which the gate acts (target_qbit >= 0)
89  target_qbit = target_qbit_in;
90  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
91  control_qbit = -1;
92 
93  parameter_num = 1;
94 
95 }
96 
97 
102 
103 
104 }
105 
106 
107 
108 
115 void
116 RZ::apply_to( Matrix_real& parameters, Matrix& input, int parallel ) {
117 
118 
119  if (input.rows != matrix_size ) {
120  std::string err("RZ::apply_to: Wrong input size in RZ gate apply.");
121  throw err;
122  }
123 
124 
125 
126  double Phi_over_2 = parameters[0];
127 
128 
129 
130  // get the U3 gate of one qubit
131  //Matrix u3_1qbit = calc_one_qubit_u3(theta0, Phi, lambda0 );
132  Matrix u3_1qbit = calc_one_qubit_u3( Phi_over_2 );
133 
134 
135 
136  apply_kernel_to( u3_1qbit, input, false, parallel );
137 
138 
139 }
140 
141 
142 
148 void
150 
151 
152  if (input.cols != matrix_size ) {
153  std::string err("Wrong matrix size in U3 apply_from_right");
154  throw err;
155  }
156 
157  double Phi_over_2 = parameters[0];
158 
159 
160 
161  // get the U3 gate of one qubit
162  //Matrix u3_1qbit = calc_one_qubit_u3(theta0, Phi, lambda0 );
163  Matrix u3_1qbit = calc_one_qubit_u3( Phi_over_2 );
164 
165 
166  apply_kernel_from_right(u3_1qbit, input);
167 
168 
169 
170 }
171 
172 
173 
180 std::vector<Matrix>
181 RZ::apply_derivate_to( Matrix_real& parameters_mtx, Matrix& input, int parallel ) {
182 
183  if (input.rows != matrix_size ) {
184  std::string err("Wrong matrix size in RZ apply_derivate_to");
185  throw err;
186  }
187 
188  std::vector<Matrix> ret;
189 
190  Matrix_real parameters_tmp(1,1);
191 
192  parameters_tmp[0] = parameters_mtx[0] + M_PI/2;
193  Matrix res_mtx = input.copy();
194  apply_to(parameters_tmp, res_mtx, parallel);
195  ret.push_back(res_mtx);
196 
197 
198 
199  return ret;
200 
201 
202 }
203 
204 
212 void
213 RZ::parameters_for_calc_one_qubit( double& ThetaOver2, double& Phi, double& Lambda){
214 
215  ThetaOver2 = 0;
216  Lambda = 0;
217 }
218 
224 
225  RZ* ret = new RZ(qbit_num, target_qbit);
226 
228  ret->set_parents( parents );
229  ret->set_children( children );
230 
231 
232  return ret;
233 
234 }
235 
236 
237 
243 Matrix RZ::calc_one_qubit_u3(double PhiOver2 ) {
244 
245 
246  Matrix u3_1qbit(2, 2);
247  double cos_phi = cos(PhiOver2);
248  double sin_phi = sin(PhiOver2);
249 
250  memset( u3_1qbit.get_data(), 0.0, 4*sizeof(QGD_Complex16) );
251  u3_1qbit[0].real = cos_phi;
252  u3_1qbit[0].imag = -sin_phi;
253 
254  u3_1qbit[3].real = cos_phi;
255  u3_1qbit[3].imag = sin_phi;
256 
257 
258  return u3_1qbit;
259 
260 }
261 
262 
263 
271 
272  if ( get_parameter_start_idx() + get_parameter_num() > parameters.size() ) {
273  std::string err("RZ::extract_parameters: Cant extract parameters, since the dinput arary has not enough elements.");
274  throw err;
275  }
276 
277  Matrix_real extracted_parameters(1,1);
278 
279  extracted_parameters[0] = std::fmod( 2*parameters[ get_parameter_start_idx() ], 4*M_PI);
280 
281  return extracted_parameters;
282 
283 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:95
void apply_to(Matrix_real &parameters, Matrix &input, int parallel)
Call to apply the gate on the input array/matrix by U3*input.
Definition: RZ.cpp:116
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
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:85
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
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: RZ.cpp:270
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:89
scalar * get_data() const
Call to get the pointer to the stored data.
RZ * clone()
Call to create a clone of the present class.
Definition: RZ.cpp:223
void apply_from_right(Matrix_real &parameters, Matrix &input)
Call to apply the gate on the input array/matrix by input*U3.
Definition: RZ.cpp:149
RZ()
NullaRZ constructor of the class.
Definition: RZ.cpp:31
A class representing a RZ gate.
Definition: RZ.h:36
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:83
int rows
The number of rows.
Definition: matrix_base.hpp:42
int cols
The number of columns.
Definition: matrix_base.hpp:44
Header file for a class representing a rotation gate around the Z axis.
~RZ()
Destructor of the class.
Definition: RZ.cpp:101
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
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
virtual Matrix calc_one_qubit_u3()
Calculate the matrix of the constans gates.
Definition: Gate.cpp:750
int Power_of_2(int n)
Calculates the n-th power of 2.
Definition: common.cpp:117
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.
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: RZ.cpp:181
int parameter_num
the number of free parameters of the operation
Definition: Gate.h:91
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
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: RZ.cpp:213
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