Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
apply_kernel_to_input.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 */
24 #include "apply_kernel_to_input.h"
25 //#include <immintrin.h>
26 #include "tbb/tbb.h"
27 
28 
29 
30 
31 
41 void
42 apply_kernel_to_input(Matrix& u3_1qbit, Matrix& input, const bool& deriv, const int& target_qbit, const int& control_qbit, const int& matrix_size) {
43 
44 
45 
46  int index_step_target = 1 << target_qbit;
47  int current_idx = 0;
48 
49 
50  for ( int current_idx_pair=current_idx + index_step_target; current_idx_pair<matrix_size; current_idx_pair=current_idx_pair+(index_step_target << 1) ) {
51 
52  for(int idx=0; idx<index_step_target; idx++) {
53  //tbb::parallel_for(0, index_step_target, 1, [&](int idx) {
54 
55  int current_idx_loc = current_idx + idx;
56  int current_idx_pair_loc = current_idx_pair + idx;
57 
58  int row_offset = current_idx_loc*input.stride;
59  int row_offset_pair = current_idx_pair_loc*input.stride;
60 
61  if ( control_qbit<0 || ((current_idx_loc >> control_qbit) & 1) ) {
62 
63  for ( int col_idx=0; col_idx<input.cols; col_idx++) {
64 
65  int index = row_offset+col_idx;
66  int index_pair = row_offset_pair+col_idx;
67 
68  QGD_Complex16 element = input[index];
69  QGD_Complex16 element_pair = input[index_pair];
70 
71  QGD_Complex16 tmp1 = mult(u3_1qbit[0], element);
72  QGD_Complex16 tmp2 = mult(u3_1qbit[1], element_pair);
73 
74  input[index].real = tmp1.real + tmp2.real;
75  input[index].imag = tmp1.imag + tmp2.imag;
76 
77  tmp1 = mult(u3_1qbit[2], element);
78  tmp2 = mult(u3_1qbit[3], element_pair);
79 
80  input[index_pair].real = tmp1.real + tmp2.real;
81  input[index_pair].imag = tmp1.imag + tmp2.imag;
82 
83  }
84 
85 
86  }
87  else if (deriv) {
88  // when calculating derivatives, the constant element should be zeros
89  memset( input.get_data()+row_offset, 0.0, input.cols*sizeof(QGD_Complex16));
90  memset( input.get_data()+row_offset_pair, 0.0, input.cols*sizeof(QGD_Complex16));
91  }
92  else {
93  // leave the state as it is
94  continue;
95  }
96 
97 
98 
99  //});
100  }
101 
102 
103  current_idx = current_idx + (index_step_target << 1);
104 
105 
106  }
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 }
int stride
The column stride of the array. (The array elements in one row are a_0, a_1, ... a_{cols-1}, 0, 0, 0, 0. The number of zeros is stride-cols)
Definition: matrix_base.hpp:46
QGD_Complex16 mult(QGD_Complex16 &a, QGD_Complex16 &b)
Call to calculate the product of two complex scalars.
Definition: common.cpp:259
scalar * get_data() const
Call to get the pointer to the stored data.
int cols
The number of columns.
Definition: matrix_base.hpp:44
matrix_size
[load Umtx]
Definition: example.py:58
void apply_kernel_to_input(Matrix &u3_1qbit, Matrix &input, const bool &deriv, const int &target_qbit, const int &control_qbit, const int &matrix_size)
Call to apply kernel to apply single qubit gate kernel on an input matrix.
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
Class to store data of complex arrays and its properties.
Definition: matrix.h:38
double real
the real part of a complex number
Definition: QGDTypes.h:40
double imag
the imaginary part of a complex number
Definition: QGDTypes.h:42