Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CZ.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 "CZ.h"
24 
25 
26 
27 using namespace std;
28 
29 
33 CZ::CZ() {
34 
35  // A string labeling the gate operation
36  name = "CZ";
37 
38  // number of qubits spanning the matrix of the gate
39  qbit_num = -1;
40  // the size of the matrix
41  matrix_size = -1;
42  // A string describing the type of the gate
44  // The number of free parameters
45  parameter_num = 0;
46 
47  // The index of the qubit on which the gate acts (target_qbit >= 0)
48  target_qbit = -1;
49 
50  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
51  control_qbit = -1;
52 
53 
54 }
55 
56 
63 CZ::CZ(int qbit_num_in, int target_qbit_in, int control_qbit_in) {
64 
65 
66  // A string labeling the gate operation
67  name = "CZ";
68 
69  // number of qubits spanning the matrix of the gate
70  qbit_num = qbit_num_in;
71  // the size of the matrix
73  // A string describing the type of the gate
75  // The number of free parameters
76  parameter_num = 0;
77 
78  if (target_qbit_in >= qbit_num) {
79  std::stringstream sstream;
80  sstream << "The index of the target qubit is larger than the number of qubits" << std::endl;
81  print(sstream, 0);
82  throw sstream.str();
83  }
84  // The index of the qubit on which the gate acts (target_qbit >= 0)
85  target_qbit = target_qbit_in;
86 
87 
88  if (control_qbit_in >= qbit_num) {
89  std::stringstream sstream;
90  sstream << "The index of the control qubit is larger than the number of qubits" << std::endl;
91  print(sstream, 0);
92  throw sstream.str();
93  }
94  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled gates
95  control_qbit = control_qbit_in;
96 
97 
98 }
99 
104 }
105 
110 Matrix
112 
113  return get_matrix( false );
114 }
115 
116 
122 Matrix
123 CZ::get_matrix( int parallel) {
124 
125  Matrix CZ_matrix = create_identity(matrix_size);
126  apply_to(CZ_matrix, parallel);
127 
128  return CZ_matrix;
129 }
130 
131 
132 
133 
139 void
140 CZ::apply_to( Matrix& input, int parallel ) {
141 
142  if (input.rows != matrix_size ) {
143  std::string err("CZ::apply_to: Wrong matrix size in CZ gate apply.");
144  throw err;
145  }
146 
147  Matrix u3_1qbit = calc_one_qubit_u3();
148  apply_kernel_to(u3_1qbit, input, false, parallel);
149 
150 }
151 
152 
153 
158 void
160 
161  Matrix u3_1qbit = calc_one_qubit_u3();
162  apply_kernel_from_right(u3_1qbit, input);
163 
164 }
165 
166 
167 
173  // setting the number of qubits
174  Gate::set_qbit_num(qbit_num);
175 
176 }
177 
178 
179 
184 void CZ::reorder_qubits( vector<int> qbit_list) {
185 
186  Gate::reorder_qubits(qbit_list);
187 
188 }
189 
194 Matrix
196 
197  Matrix u3_1qbit = Matrix(2,2);
198  u3_1qbit[0].real = 1.0; u3_1qbit[0].imag = 0.0;
199  u3_1qbit[1].real = 0.0; u3_1qbit[1].imag = 0.0;
200  u3_1qbit[2].real = 0.0; u3_1qbit[2].imag = 0.0;
201  u3_1qbit[3].real = -1.0;u3_1qbit[3].imag = 0.0;
202  return u3_1qbit;
203 
204 }
205 
210 CZ* CZ::clone() {
211 
212  CZ* ret = new CZ( qbit_num, target_qbit, control_qbit );
213 
214  ret->set_parameter_start_idx( get_parameter_start_idx() );
215  ret->set_parents( parents );
216  ret->set_children( children );
217 
218  return ret;
219 
220 }
221 
222 
223 
Matrix get_matrix()
Call to retrieve the operation matrix.
Definition: CZ.cpp:111
void apply_to(Matrix &input, int parallel)
Call to apply the gate on the input array/matrix CZ*input.
Definition: CZ.cpp:140
parameter_num
[set adaptive gate structure]
CZ * clone()
Call to create a clone of the present class.
Definition: CZ.cpp:210
virtual void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the operation.
Definition: Gate.cpp:102
Matrix calc_one_qubit_u3()
Set static values for matrix of the gates.
Definition: CZ.cpp:195
name
Definition: setup.py:33
~CZ()
Destructor of the class.
Definition: CZ.cpp:103
void set_qbit_num(int qbit_num)
Call to set the number of qubits spanning the matrix of the operation.
Definition: CZ.cpp:172
int rows
The number of rows.
Definition: matrix_base.hpp:42
matrix_size
[load Umtx]
Definition: example.py:58
CZ()
Nullary constructor of the class.
Definition: CZ.cpp:33
Header file for a class representing a CZ operation.
def apply_to(self, parameters_mtx, unitary_mtx, parallel=1)
Definition: qgd_Circuit.py:302
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
void apply_from_right(Matrix &input)
Call to apply the gate on the input array/matrix by input*CZ.
Definition: CZ.cpp:159
Matrix create_identity(int matrix_size)
Call to create an identity matrix.
Definition: common.cpp:164
void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the operation.
Definition: CZ.cpp:184
virtual void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the operation.
Definition: Gate.cpp:339