Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Tdg.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 "Tdg.h"
24 
25 static double invsqrt2 = 1.0 / std::sqrt(2.0);
26 
27 //static tbb::spin_mutex my_mutex;
32 
33  // A string labeling the gate operation
34  name = "Tdg";
35 
36  // number of qubits spanning the matrix of the gate
37  qbit_num = -1;
38  // the size of the matrix
39  matrix_size = -1;
40  // A string describing the type of the gate
42 
43  // The index of the qubit on which the gate acts (target_qbit >= 0)
44  target_qbit = -1;
45  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
46  control_qbit = -1;
47 
48  parameter_num = 0;
49 
50 
51 
52 }
53 
54 
55 
64 Tdg::Tdg(int qbit_num_in, int target_qbit_in) {
65 
66  // A string labeling the gate operation
67  name = "Z";
68  // number of qubits spanning the matrix of the gate
69  qbit_num = qbit_num_in;
70  // the size of the matrix
72  // A string describing the type of the gate
73  type = Z_OPERATION;
74 
75 
76  if (target_qbit_in >= qbit_num) {
77  std::stringstream sstream;
78  sstream << "The index of the target qubit is larger than the number of qubits" << std::endl;
79  print(sstream, 0);
80 
81  throw "The index of the target qubit is larger than the number of qubits";
82  }
83 
84  // The index of the qubit on which the gate acts (target_qbit >= 0)
85  target_qbit = target_qbit_in;
86  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
87  control_qbit = -1;
88 
89  parameter_num = 0;
90 
91 
92 }
93 
94 
99 
100 }
101 
102 
107 Matrix
109 
110  return get_matrix( false );
111 
112 }
113 
114 
115 
121 Matrix
122 Tdg::get_matrix( int parallel) {
123 
124  Matrix Z_matrix = create_identity(matrix_size);
125  apply_to(Z_matrix, parallel);
126 
127 #ifdef DEBUG
128  if (Z_matrix.isnan()) {
129  std::stringstream sstream;
130  sstream << "Tdg::get_matrix: Z_matrix contains NaN." << std::endl;
131  print(sstream, 1);
132  }
133 #endif
134 
135  return Z_matrix;
136 
137 }
138 
139 
140 
147 void
148 Tdg::apply_to( Matrix& input, int parallel ) {
149 
150  if (input.rows != matrix_size ) {
151  std::string err("Tdg::apply_to: Wrong input size in Z gate apply");
152  throw(err);
153  }
154 
155 
156  Matrix u3_1qbit = calc_one_qubit_u3();
157 
158  //apply_kernel_to function to Z gate
159  apply_kernel_to( u3_1qbit, input, false, parallel );
160 
161 
162 
163 }
164 
165 
166 
172 void
174 
175  //The stringstream input to store the output messages.
176  std::stringstream sstream;
177 
178  if (input.cols != matrix_size ) {
179  std::stringstream sstream;
180  sstream << "Wrong matrix size in U3 apply_from_right" << std::endl;
181  print(sstream, 0);
182  exit(-1);
183  }
184 
185 
186  Matrix u3_1qbit = calc_one_qubit_u3();
187  //apply_kernel_from_right function to Z gate
188  apply_kernel_from_right(u3_1qbit, input);
189 
190 
191 
192 }
193 
194 
195 
201 
202  Tdg* ret = new Tdg(qbit_num, target_qbit);
203 
205  ret->set_parents( parents );
206  ret->set_children( children );
207 
208  return ret;
209 
210 }
211 
212 
213 
214 
219 void Tdg::reorder_qubits( std::vector<int> qbit_list) {
220 
221  Gate::reorder_qubits(qbit_list);
222 
223 }
224 
225 
230 void Tdg::set_qbit_num(int qbit_num_in) {
231 
232  // setting the number of qubits
233  Gate::set_qbit_num(qbit_num_in);
234 }
235 
240 Matrix
242 
243 
244  Matrix u3_1qbit = Matrix(2,2);
245  u3_1qbit[0].real = 1.0; u3_1qbit[0].imag = 0.0;
246  u3_1qbit[1].real = 0.0; u3_1qbit[1].imag = 0.0;
247  u3_1qbit[2].real = 0.0; u3_1qbit[2].imag = 0.0;
248  u3_1qbit[3].real = invsqrt2; u3_1qbit[3].imag = -invsqrt2;
249  return u3_1qbit;
250 
251 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:95
bool isnan()
Call to check the array for NaN entries.
Definition: matrix.cpp:128
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
virtual void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the operation.
Definition: Gate.cpp:102
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:85
~Tdg()
Destructor of the class.
Definition: Tdg.cpp:98
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
A class representing a U3 gate.
Definition: Tdg.h:35
Header file for a class representing the Tdg gate.
Matrix get_matrix()
Call to retrieve the gate matrix.
Definition: Tdg.cpp:108
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:89
Tdg()
NullaRX constructor of the class.
Definition: Tdg.cpp:31
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
void apply_from_right(Matrix &input)
Call to apply the gate on the input array/matrix by input*U3.
Definition: Tdg.cpp:173
int cols
The number of columns.
Definition: matrix_base.hpp:44
static double invsqrt2
Definition: Tdg.cpp:25
void apply_to(Matrix &input, int parallel)
Call to apply the gate on the input array/matrix by U3*input.
Definition: Tdg.cpp:148
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
Tdg * clone()
Call to create a clone of the present class.
Definition: Tdg.cpp:200
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
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
Matrix calc_one_qubit_u3()
Set static values for matrix of the gates.
Definition: Tdg.cpp:241
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
Matrix create_identity(int matrix_size)
Call to create an identity matrix.
Definition: common.cpp:164
int parameter_num
the number of free parameters of the operation
Definition: Gate.h:91
void set_qbit_num(int qbit_num_in)
Call to set the number of qubits spanning the matrix of the gate.
Definition: Tdg.cpp:230
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:790
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:81
virtual void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the operation.
Definition: Gate.cpp:339
void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the gate.
Definition: Tdg.cpp:219