Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
GrayCode_base.hpp
Go to the documentation of this file.
1 
17 #ifndef GRAYCODE_BASE_H
18 #define GRAYCODE_BASE_H
19 
20 #include "matrix_base.hpp"
21 
22 
23 
24 
28 template <typename intType>
29 class GrayCode_base : public matrix_base<intType> {
30 
31 public:
32 
33 #if CACHELINE>=64
34 private:
36  uint8_t padding[CACHELINE-sizeof(matrix_base<intType>)-sizeof(intType)];
37 #endif
38 
41 
42 
43 public:
44 
49 GrayCode_base() : matrix_base<intType>() {
50  this->rows = 0;
51  this->cols = 0;
52 
53  n_ary_limits = matrix_base<intType>();
54 }
55 
62 GrayCode_base( intType* data_in, matrix_base<intType> n_ary_limits_) : matrix_base<intType>(data_in, 1, n_ary_limits_.size()) {
63 
64  n_ary_limits = n_ary_limits_;
65 
66 }
67 
68 
74 GrayCode_base( matrix_base<intType> n_ary_limits_) : matrix_base<intType>(1, n_ary_limits_.size()) {
75 
76  n_ary_limits = n_ary_limits_;
77 
78 }
79 
80 
86 GrayCode_base( intType value, matrix_base<intType>& n_ary_limits_) : matrix_base<intType>(1, n_ary_limits_.size()) {
87 
88  if (value == 0) {
89  memset(this->data, value, n_ary_limits_.size()*sizeof(intType));
90  }
91  else {
92  for (size_t idx=0; idx < this->size(); idx ++) {
93  this->data[idx] = value;
94  }
95  }
96 
97  n_ary_limits = n_ary_limits_;
98 
99 }
100 
101 
106 GrayCode_base(const GrayCode_base &in) : matrix_base<intType>(in) {
107 
108  n_ary_limits = in.n_ary_limits;
109 
110 }
111 
117 bool
118 operator==( const GrayCode_base &gcode) const {
119 
120  if (this->cols != gcode.cols) {
121  return false;
122  }
123 
124 
125  for (intType idx=0; idx<this->cols; idx++) {
126  if ( this->data[idx] != gcode[idx] ) {
127  return false;
128  }
129 
130  if ( n_ary_limits[idx] != gcode.n_ary_limits[idx] ) {
131  return false;
132  }
133  }
134 
135  return true;
136 
137 }
138 
139 
145 intType&
146 operator[](size_t idx) {
147  return this->data[idx];
148 }
149 
155 const intType&
156 operator[](size_t idx) const {
157  return this->data[idx];
158 }
159 
160 
161 
167 void
168 operator= (const GrayCode_base &gcode ) {
169 
171 
172  n_ary_limits = gcode.n_ary_limits;
173 
174 }
175 
176 
182 copy() const {
183 
184  GrayCode_base ret = GrayCode_base(this->n_ary_limits);
185 
186  // logical variable indicating whether the matrix needs to be conjugated in CBLAS operations
187  ret.conjugated = this->conjugated;
188  // logical variable indicating whether the matrix needs to be transposed in CBLAS operations
189  ret.transposed = this->transposed;
190  // logical value indicating whether the class instance is the owner of the stored data or not. (If true, the data array is released in the destructor)
191  ret.owner = true;
192 
193  memcpy( ret.data, this->data, this->rows*this->cols*sizeof(intType));
194 
195 
196  ret.n_ary_limits = n_ary_limits.copy();
197 
198  return ret;
199 
200 }
201 
202 
203 
204 
210 get_Limits() const {
211 
212  return n_ary_limits.copy();
213 
214 }
215 
216 
217 
224 add_Digit( const intType n_ary_limit) const {
225 
226  matrix_base<intType> n_ary_limits_new( 1, n_ary_limits.size() + 1 );
227  memcpy( n_ary_limits_new.get_data(), n_ary_limits.get_data(), n_ary_limits.size()*sizeof(intType) );
228 
229  n_ary_limits_new[ n_ary_limits_new.size() -1 ] = n_ary_limit;
230 
231  GrayCode_base<intType> gcode_ret( n_ary_limits_new );
232 
233  memcpy( gcode_ret.get_data(), this->data, this->size()*sizeof(intType) );
234  gcode_ret[ gcode_ret.size()-1 ] = 0;
235 
236  return gcode_ret;
237 
238 
239 }
240 
241 
242 
249 remove_Digit( const int idx) const {
250 
251  matrix_base<intType> n_ary_limits_new( 1, n_ary_limits.size() - 1 );
252  memcpy( n_ary_limits_new.get_data(), n_ary_limits.get_data(), idx*sizeof(intType) );
253  memcpy( n_ary_limits_new.get_data()+idx, n_ary_limits.get_data()+idx+1, (n_ary_limits.size()-idx-1)*sizeof(intType) );
254 
255 
256  GrayCode_base<intType> gcode_ret( n_ary_limits_new );
257 
258  memcpy( gcode_ret.get_data(), this->data, idx*sizeof(intType) );
259  memcpy( gcode_ret.get_data()+idx, this->data+idx+1, (this->size()-idx-1)*sizeof(intType) );
260 
261  return gcode_ret;
262 
263 
264 }
265 
266 
267 
268 
269 
270 };
271 
272 
273 
274 #endif // GRAYCODE_BASE_HPP
GrayCode_base(const GrayCode_base &in)
Copy constructor of the class.
uint8_t padding[CACHELINE-sizeof(matrix_base< intType >) -sizeof(intType)]
padding class object to cache line borders
matrix_base< intType > get_Limits() const
Call to get the n-ary limits of the Gray code.
Copyright 2021 Budapest Quantum Computing Group.
bool owner
logical value indicating whether the class instance is the owner of the stored data or not...
Definition: matrix_base.hpp:57
matrix_base< intType > n_ary_limits
the limits of the gray code digits
matrix_base< scalar > copy() const
Call to create a copy of the matrix.
intType * data
pointer to the stored data
Definition: matrix_base.hpp:48
intType & operator[](size_t idx)
Operator [] to access elements in array style.
GrayCode_base()
Default constructor of the class.
bool transposed
logical variable indicating whether the matrix needs to be transposed in CBLAS operations ...
Definition: matrix_base.hpp:55
bool conjugated
logical variable indicating whether the matrix needs to be conjugated in CBLAS operations ...
Definition: matrix_base.hpp:53
scalar * get_data() const
Call to get the pointer to the stored data.
#define CACHELINE
Definition: QGDTypes.h:33
Base Class to store data of arrays and its properties.
Definition: matrix_base.hpp:38
void operator=(const GrayCode_base &gcode)
Overloaded assignment operator to create a copy of the state.
void operator=(const matrix_base &mtx)
Assignment operator.
GrayCode_base remove_Digit(const int idx) const
Call to add a new digit to the Gray code.
int rows
The number of rows.
Definition: matrix_base.hpp:42
int cols
The number of columns.
Definition: matrix_base.hpp:44
GrayCode_base add_Digit(const intType n_ary_limit) const
Call to add a new digit to the Gray code.
GrayCode_base(matrix_base< intType > n_ary_limits_)
Constructor of the class.
const intType & operator[](size_t idx) const
Operator [] to access a constant element in array style of constant instance.
int size() const
Call to get the number of the allocated elements.
GrayCode_base copy() const
Call to create a copy of the state.
GrayCode_base(intType value, matrix_base< intType > &n_ary_limits_)
Constructor of the class.
bool operator==(const GrayCode_base &gcode) const
Operator to compare two keys made of PicState_base class instances.
GrayCode_base(intType *data_in, matrix_base< intType > n_ary_limits_)
Constructor of the class.