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