Quantum Gate Decomposer  v1.3
Powerful decomposition of almost any unitary into U3 and CNOT gates
Operation.cpp
Go to the documentation of this file.
1 /*
2 Created on Fri Jun 26 14:13:26 2020
3 Copyright (C) 2020 Peter Rakyta, Ph.D.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see http://www.gnu.org/licenses/.
17 
18 @author: Peter Rakyta, Ph.D.
19 */
25 #include "qgd/Operation.h"
26 
27 
33 
34  // number of qubits spanning the matrix of the operation
35  qbit_num = -1;
36  // The size N of the NxN matrix associated with the operations.
37  matrix_size = -1;
38  // The type of the operation (see enumeration operation_type)
40  // The index of the qubit on which the operation acts (target_qbit >= 0)
41  target_qbit = -1;
42  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations
43  control_qbit = -1;
44  // the number of free parameters of the operation
45  parameter_num = 0;
46 }
47 
48 
49 
55 Operation::Operation(int qbit_num_in) {
56 
57  // number of qubits spanning the matrix of the operation
58  qbit_num = qbit_num_in;
59  // the size of the matrix
61  // A string describing the type of the operation
63  // The index of the qubit on which the operation acts (target_qbit >= 0)
64  target_qbit = -1;
65  // The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations
66  control_qbit = -1;
67  // The number of parameters
68  parameter_num = 0;
69 }
70 
71 
76 }
77 
82 void Operation::set_qbit_num( int qbit_num_in ) {
83  // setting the number of qubits
84  qbit_num = qbit_num_in;
85 
86  // update the size of the matrix
88 
89 }
90 
91 
96 Matrix
98 
99  return matrix_alloc;
100 }
101 
102 
108 void
110  matrix_alloc = input;
111 }
112 
113 
118 void Operation::reorder_qubits( std::vector<int> qbit_list ) {
119 
120  // check the number of qubits
121  if ((int)qbit_list.size() != qbit_num ) {
122  printf("Wrong number of qubits\n");
123  exit(-1);
124  }
125 
126 
127  int control_qbit_new = control_qbit;
128  int target_qbit_new = target_qbit;
129 
130  // setting the new value for the target qubit
131  for (int idx=0; idx<qbit_num; idx++) {
132  if (target_qbit == qbit_list[idx]) {
133  target_qbit_new = qbit_num-1-idx;
134  }
135  if (control_qbit == qbit_list[idx]) {
136  control_qbit_new = qbit_num-1-idx;
137  }
138  }
139 
140  control_qbit = control_qbit_new;
141  target_qbit = target_qbit_new;
142 }
143 
144 
150  return target_qbit;
151 }
152 
158  return control_qbit;
159 }
160 
166  return parameter_num;
167 }
168 
169 
175  return type;
176 }
177 
178 
184  return qbit_num;
185 }
186 
187 
193 
194  Operation* ret = new Operation( qbit_num );
195  ret->set_matrix( matrix_alloc );
196 
197  return ret;
198 
199 }
200 
201 
Operation * clone()
Call to create a clone of the present class.
Definition: Operation.cpp:192
Base class for the representation of one- and two-qubit operations.
Definition: Operation.h:40
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Operation.h:50
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations.
Definition: Operation.h:52
Operation()
Default constructor of the class.
Definition: Operation.cpp:32
Header file for a class for the representation of one- and two-qubit operations.
int get_target_qbit()
Call to get the index of the target qubit.
Definition: Operation.cpp:149
Matrix matrix_alloc
Matrix of the operation.
Definition: Operation.h:61
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Operation.h:54
virtual void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the operation.
Definition: Operation.cpp:118
operation_type type
The type of the operation (see enumeration operation_type)
Definition: Operation.h:48
int get_qbit_num()
Call to get the number of qubits composing the unitary.
Definition: Operation.cpp:183
virtual ~Operation()
Destructor of the class.
Definition: Operation.cpp:75
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Operation.h:46
unsigned int parameter_num
the number of free parameters of the operation
Definition: Operation.h:56
int Power_of_2(int n)
Calculates the n-th power of 2.
Definition: common.cpp:81
Matrix get_matrix()
Call to retrieve the operation matrix.
Definition: Operation.cpp:97
Class to store data of complex arrays and its properties.
Definition: matrix.h:12
unsigned int get_parameter_num()
Call to get the number of free parameters.
Definition: Operation.cpp:165
void set_matrix(Matrix input)
Call to set the stored matrix in the operation.
Definition: Operation.cpp:109
operation_type get_type()
Call to get the type of the operation.
Definition: Operation.cpp:174
virtual void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the operation.
Definition: Operation.cpp:82
int get_control_qbit()
Call to get the index of the control qubit.
Definition: Operation.cpp:157
operation_type
Type definition of operation types (also generalized for decomposition classes derived from the class...
Definition: Operation.h:33