Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CH.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 "CH.h"
24 
25 
26 
27 using namespace std;
28 
29 
33 CH::CH() {
34 
35  // A string labeling the gate operation
36  name = "CH";
37 
38  // number of qubits spanning the matrix of the gate
39  qbit_num = -1;
40 
41  // the size of the matrix
42  matrix_size = -1;
43 
44  // A string describing the type of the gate
46 
47  // The number of free parameters
48  parameter_num = 0;
49 
50  // The index of the qubit on which the gate acts (target_qbit >= 0)
51  target_qbit = -1;
52 
53  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
54  control_qbit = -1;
55 
56 
57 }
58 
59 
66 CH::CH(int qbit_num_in, int target_qbit_in, int control_qbit_in) {
67 
68 
69  // A string labeling the gate operation
70  name = "CH";
71 
72  // number of qubits spanning the matrix of the gate
73  qbit_num = qbit_num_in;
74 
75  // the size of the matrix
77 
78  // A string describing the type of the gate
80 
81  // The number of free parameters
82  parameter_num = 0;
83 
84  if (target_qbit_in >= qbit_num) {
85  std::stringstream sstream;
86  sstream << "The index of the target qubit is larger than the number of qubits" << std::endl;
87  print(sstream, 0);
88  throw sstream.str();
89  }
90 
91  // The index of the qubit on which the gate acts (target_qbit >= 0)
92  target_qbit = target_qbit_in;
93 
94 
95  if (control_qbit_in >= qbit_num) {
96  std::stringstream sstream;
97  sstream << "The index of the control qubit is larger than the number of qubits" << std::endl;
98  print(sstream, 0);
99  throw sstream.str();
100  }
101 
102  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
103  control_qbit = control_qbit_in;
104 
105 
106 }
107 
112 }
113 
118 Matrix
120 
121  return get_matrix( false );
122 }
123 
124 
130 Matrix
131 CH::get_matrix(int parallel) {
132 
133  Matrix CH_matrix = create_identity(matrix_size);
134  apply_to(CH_matrix, parallel);
135 
136  return CH_matrix;
137 }
138 
139 
140 
146 void
147 CH::apply_to( Matrix& input, int parallel ) {
148 
149  if (input.rows != matrix_size ) {
150  std::string err("CH::apply_to: Wrong input size in CH gate apply.");
151  throw err;
152  }
153 
154  Matrix u3_1qbit = calc_one_qubit_u3();
155  apply_kernel_to(u3_1qbit, input, false, parallel);
156 
157 }
158 
159 
160 
165 void
167 
168 
169  Matrix u3_1qbit = calc_one_qubit_u3();
170  apply_kernel_from_right(u3_1qbit, input);
171 
172 }
173 
174 
180  // setting the number of qubits
181  Gate::set_qbit_num(qbit_num);
182 
183 }
184 
185 
186 
191 void CH::reorder_qubits( vector<int> qbit_list) {
192 
193  Gate::reorder_qubits(qbit_list);
194 
195 }
196 
197 
198 
204 Matrix
206 
207  Matrix u3_1qbit = Matrix(2,2);
208  u3_1qbit[0].real = 1.0/sqrt(2); u3_1qbit[0].imag = 0.0;
209  u3_1qbit[1].real = 1.0/sqrt(2); u3_1qbit[1].imag = 0.0;
210  u3_1qbit[2].real = 1.0/sqrt(2); u3_1qbit[2].imag = 0.0;
211  u3_1qbit[3].real = -1.0/sqrt(2);u3_1qbit[3].imag = 0.0;
212  return u3_1qbit;
213 
214 }
215 
216 
221 CH* CH::clone() {
222 
223  CH* ret = new CH( qbit_num, target_qbit, control_qbit );
224 
225  ret->set_parameter_start_idx( get_parameter_start_idx() );
226  ret->set_parents( parents );
227  ret->set_children( children );
228 
229  return ret;
230 
231 }
232 
233 
234 
parameter_num
[set adaptive gate structure]
CH * clone()
Call to create a clone of the present class.
Definition: CH.cpp:221
virtual void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the operation.
Definition: Gate.cpp:102
Header file for a class representing a CH operation.
void set_qbit_num(int qbit_num)
Call to set the number of qubits spanning the matrix of the operation.
Definition: CH.cpp:179
name
Definition: setup.py:33
int rows
The number of rows.
Definition: matrix_base.hpp:42
Matrix calc_one_qubit_u3()
Set static values for matrix of the gates.
Definition: CH.cpp:205
void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the operation.
Definition: CH.cpp:191
matrix_size
[load Umtx]
Definition: example.py:58
~CH()
Destructor of the class.
Definition: CH.cpp:111
Matrix get_matrix()
Call to retrieve the operation matrix.
Definition: CH.cpp:119
def apply_to(self, parameters_mtx, unitary_mtx, parallel=1)
Definition: qgd_Circuit.py:302
void apply_from_right(Matrix &input)
Call to apply the gate on the input array/matrix by input*CH.
Definition: CH.cpp:166
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 create_identity(int matrix_size)
Call to create an identity matrix.
Definition: common.cpp:164
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 apply_to(Matrix &input, int parallel)
Call to apply the gate on the input array/matrix CH*input.
Definition: CH.cpp:147
CH()
Nullary constructor of the class.
Definition: CH.cpp:33