Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
U2.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 "U2.h"
24 
25 
29 U2::U2() {
30 
31  // A string labeling the gate operation
32  name = "U2";
33 
34  // number of qubits spanning the matrix of the gate
35  qbit_num = -1;
36  // the size of the matrix
37  matrix_size = -1;
38  // A string describing the type of the gate
40 
41  // The index of the qubit on which the gate acts (target_qbit >= 0)
42  target_qbit = -1;
43  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
44  control_qbit = -1;
45 
46  parameter_num = 0;
47 
48 }
49 
50 
56 U2::U2(int qbit_num_in, int target_qbit_in) {
57 
58  // A string labeling the gate operation
59  name = "U2";
60 
61  // number of qubits spanning the matrix of the gate
62  qbit_num = qbit_num_in;
63  // the size of the matrix
65  // A string describing the type of the gate
67 
68  if (target_qbit_in >= qbit_num) {
69  std::stringstream sstream;
70  sstream << "The index of the target qubit is larger than the number of qubits" << std::endl;
71  print(sstream, 0);
72  throw "The index of the target qubit is larger than the number of qubits";
73  }
74 
75  // The index of the qubit on which the gate acts (target_qbit >= 0)
76  target_qbit = target_qbit_in;
77  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
78  control_qbit = -1;
79 
80  parameter_num = 2;
81 
82 }
83 
84 
89 
90 }
91 
92 
99 void
100 U2::apply_to( Matrix_real& parameters, Matrix& input, int parallel ) {
101 
102  if (input.rows != matrix_size ) {
103  std::string err("U2::apply_to: Wrong input size in U2 gate apply.");
104  throw err;
105  }
106 
107 
108  double ThetaOver2, Phi, Lambda;
109 
110  Phi = parameters[0];
111  Lambda = parameters[1];
112  parameters_for_calc_one_qubit(ThetaOver2, Phi, Lambda);
113 
114  // get the U3 gate of one qubit
115  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
116 
117 
118  apply_kernel_to( u3_1qbit, input, false, parallel );
119 
120 
121 }
122 
123 
129 void
131 
132  if (input.cols != matrix_size ) {
133  std::string err("U2::apply_from_right: Wrong matrix size in U2 apply_from_right.");
134  throw err;
135  }
136 
137 
138  double ThetaOver2, Phi, Lambda;
139 
140  Phi = parameters[0];
141  Lambda = parameters[1];
142  parameters_for_calc_one_qubit(ThetaOver2, Phi, Lambda);
143 
144  // get the U3 gate of one qubit
145  Matrix u3_1qbit = calc_one_qubit_u3(ThetaOver2, Phi, Lambda );
146 
147 
148  apply_kernel_from_right(u3_1qbit, input);
149 
150 
151 }
152 
153 
160 std::vector<Matrix> U2::apply_derivate_to( Matrix_real& parameters_mtx, Matrix& input, int parallel ) {
161 
162  if (input.rows != matrix_size ) {
163  std::string err("U2::apply_derivate_to: Wrong matrix size in U2 gate apply.");
164  throw err;
165  }
166 
167 
168  std::vector<Matrix> ret;
169 
170  Matrix_real parameters_tmp(1,2);
171 
172  parameters_tmp[0] = parameters_mtx[0] + M_PI/2;
173  parameters_tmp[1] = parameters_mtx[1];
174  Matrix res_mtx_phi = input.copy();
175  apply_to(parameters_tmp, res_mtx_phi, parallel);
176  ret.push_back(res_mtx_phi);
177 
178 
179  parameters_tmp[0] = parameters_mtx[0];
180  parameters_tmp[1] = parameters_mtx[1] + M_PI/2;
181  Matrix res_mtx_lambda = input.copy();
182  apply_to(parameters_tmp, res_mtx_lambda, parallel);
183  ret.push_back(res_mtx_lambda);
184 
185 
186 
187  return ret;
188 
189 
190 }
191 
192 
200 void
201 U2::parameters_for_calc_one_qubit( double& ThetaOver2, double& Phi, double& Lambda){
202 
203  ThetaOver2 = M_PI/4;
204  // Phi is passed through unchanged
205  // Lambda is passed through unchanged
206 
207 }
208 
209 
215 
216  U2* ret = new U2(qbit_num, target_qbit);
217 
219  ret->set_parents( parents );
220  ret->set_children( children );
221 
222  return ret;
223 }
224 
225 
233 
234  if ( get_parameter_start_idx() + get_parameter_num() > parameters.size() ) {
235  std::string err("U2::extract_parameters: Cant extract parameters, since the dinput arary has not enough elements.");
236  throw err;
237  }
238 
239  Matrix_real extracted_parameters(1,2);
240 
241  extracted_parameters[0] = std::fmod( parameters[ get_parameter_start_idx() ], 2*M_PI);
242  extracted_parameters[1] = std::fmod( parameters[ get_parameter_start_idx() + 1 ], 2*M_PI);
243 
244  return extracted_parameters;
245 
246 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:95
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
void apply_to(Matrix_real &parameters, Matrix &input, int parallel)
Call to apply the gate on the input array/matrix by U2*input.
Definition: U2.cpp:100
A class representing a U2 gate.
Definition: U2.h:36
~U2()
Destructor of the class.
Definition: U2.cpp:88
U2()
Nullary constructor of the class.
Definition: U2.cpp:29
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
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: U2.cpp:232
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
void parameters_for_calc_one_qubit(double &ThetaOver2, double &Phi, double &Lambda)
Calculate the matrix of a U2 gate gate corresponding to the given parameters acting on a single qbit ...
Definition: U2.cpp:201
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
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
Header file for a class representing a U2 gate.
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
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
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:81
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: U2.cpp:160
U2 * clone()
Call to create a clone of the present class.
Definition: U2.cpp:214
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:39
void apply_from_right(Matrix_real &parameters, Matrix &input)
Call to apply the gate on the input array/matrix by input*U2.
Definition: U2.cpp:130