Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
common_DFE.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 "common_DFE.h"
25 #include "matrix_base.hpp"
26 
27 #include <dlfcn.h>
28 #include <unistd.h>
29 #include <tbb/queuing_rw_mutex.h>
30 
31 namespace {
32 
33 tbb::queuing_rw_mutex libmutex;
34 
35 void unload_dfe_lib_unlocked();
36 int init_dfe_lib_unlocked(const int accelerator_num, int qbit_num, int initialize_id_in);
37 void uploadMatrix2DFE_unlocked(Matrix& input);
38 
39 }
40 
41 
42 // pointer to the dynamically loaded DFE library
43 void* handle = NULL;
44 
45 extern "C" {
46 
47 size_t (*get_accelerator_avail_num_dll)() = NULL;
48 size_t (*get_accelerator_free_num_dll)() = NULL;
49 int (*calcqgdKernelDFE_dll)(size_t rows, size_t cols, DFEgate_kernel_type* gates, int gatesNum, int gateSetNum, int traceOffset, double* trace) = NULL;
50 int (*load2LMEM_dll)(QGD_Complex16* data, size_t rows, size_t cols) = NULL;
51 void (*releive_DFE_dll)() = NULL;
52 int (*initialize_DFE_dll)( int accelerator_num ) = NULL;
54 
55 }
56 
57 // The ID of the class that has initialized the accelerator lib (used to not initialze again if not necessary)
58 int initialize_id = -1;
59 
60 
62  lock.acquire(libmutex, false);
63 }
64 
65 
67  lock.release();
68 }
69 
70 
71 
76 void uploadMatrix2DFE( Matrix& input ) {
77  tbb::queuing_rw_mutex::scoped_lock lock(libmutex, true);
78  uploadMatrix2DFE_unlocked(input);
79 }
80 
81 
82 namespace {
83 
84 void uploadMatrix2DFE_unlocked( Matrix& input ) {
85 
86  std::cout << "size in bytes of uploading to DFE: " << input.size()*2*sizeof(float) << std::endl;
87 
88  // load the data to LMEM
89  if (load2LMEM_dll( input.get_data(), input.rows, input.cols )) initialize_id = -1;
90 
91 }
92 
93 
94 void unload_dfe_lib_unlocked()
95 {
96  if (handle) {
98  dlclose(handle);
99  handle = NULL;
100  }
101 }
102 
103 
104 }
105 
106 
111 {
112  tbb::queuing_rw_mutex::scoped_lock lock(libmutex, true);
113  unload_dfe_lib_unlocked();
114 }
115 
116 
117 
118 
119 
120 
121 
129 int init_dfe_lib( const int accelerator_num, int qbit_num, int initialize_id_in ) {
130  tbb::queuing_rw_mutex::scoped_lock lock(libmutex, true);
131  return init_dfe_lib_unlocked(accelerator_num, qbit_num, initialize_id_in);
132 }
133 
134 
135 namespace {
136 
137 int init_dfe_lib_unlocked( const int accelerator_num, int qbit_num, int initialize_id_in ) {
138 
139  initialize_id = initialize_id_in;
140 
141 
142  unload_dfe_lib_unlocked();
143 
144 
145  std::string lib_name_DFE = qbit_num > 9 ? DFE_LIB_10QUBITS : DFE_LIB_9QUBITS;
146  std::string lib_name = getenv("SLIC_CONF") ? DFE_LIB_SIM : lib_name_DFE;
147 
148  // dynamic-loading the correct DFE permanent calculator (Simulator/DFE/single or dual) from shared libararies
149  handle = dlopen(lib_name.c_str(), RTLD_NOW); //"MAXELEROSDIR"
150  if (handle == NULL && qbit_num == 10 && !getenv("SLIC_CONF")) {
151  handle = dlopen(DFE_LIB_9QUBITS, RTLD_NOW);
152  }
153  if (handle == NULL) {
154  initialize_id = -1;
155  std::string err("init_dfe_lib: failed to load library " + lib_name + " - " + std::string(dlerror()));
156  throw err;
157  }
158  else {
159 
160  get_accelerator_avail_num_dll = (size_t (*)())dlsym(handle, "get_accelerator_avail_num");
161  get_accelerator_free_num_dll = (size_t (*)())dlsym(handle, "get_accelerator_free_num");
162  calcqgdKernelDFE_dll = (int (*)(size_t, size_t, DFEgate_kernel_type*, int, int, int, double*))dlsym(handle, "calcqgdKernelDFE");
163  load2LMEM_dll = (int (*)(QGD_Complex16*, size_t, size_t))dlsym(handle, "load2LMEM");
164  releive_DFE_dll = (void (*)())dlsym(handle, "releive_DFE");
165  initialize_DFE_dll = (int (*)(int))dlsym(handle, "initialize_DFE");
166  get_chained_gates_num_dll = (int (*)())dlsym(handle, "get_chained_gates_num");
167 
168  if (initialize_DFE_dll(accelerator_num)) initialize_id = -1;
169 
170  }
171  return initialize_id;
172 
173 }
174 
175 }
176 
177 
178 void init_dfe_lib_and_upload(const int accelerator_num, int qbit_num, int initialize_id_in, Matrix& input) {
179  tbb::queuing_rw_mutex::scoped_lock lock(libmutex, true);
180  if (initialize_id != initialize_id_in) {
181  init_dfe_lib_unlocked(accelerator_num, qbit_num, initialize_id_in);
182  }
183  uploadMatrix2DFE_unlocked(input);
184 }
185 
186 
187 
188 
189 
190 
196 
198 
199 }
200 
201 
207 
209 
210 }
211 
217 
218  return initialize_id;
219 
220 }
221 
233 int calcqgdKernelDFE(size_t rows, size_t cols, DFEgate_kernel_type* gates, int gatesNum, int gateSetNum, int traceOffset, double* trace) {
234 
235 
236  return calcqgdKernelDFE_dll(rows, cols, gates, gatesNum, gateSetNum, traceOffset, trace);
237 
238 }
239 
240 
241 
247 
248  return get_chained_gates_num_dll();
249 
250 }
251 
int get_chained_gates_num()
Call to retrieve the number of gates that should be chained up during the execution of the DFE librar...
Definition: common_DFE.cpp:246
int(* load2LMEM_dll)(QGD_Complex16 *data, size_t rows, size_t cols)
Definition: common_DFE.cpp:50
void * handle
Definition: common_DFE.cpp:43
void(* releive_DFE_dll)()
Definition: common_DFE.cpp:51
void uploadMatrix2DFE(Matrix &input)
Call to upload the input matrix to the DFE engine.
Definition: common_DFE.cpp:76
QGD_Complex8 * data
pointer to the stored data
Definition: matrix_base.hpp:48
int(* initialize_DFE_dll)(int accelerator_num)
Definition: common_DFE.cpp:52
scalar * get_data() const
Call to get the pointer to the stored data.
void init_dfe_lib_and_upload(const int accelerator_num, int qbit_num, int initialize_id_in, Matrix &input)
Initialize the DFE library if needed and upload the input matrix while holding the writer lock...
Definition: common_DFE.cpp:178
int init_dfe_lib(const int accelerator_num, int qbit_num, int initialize_id_in)
Call to initialize the DFE library support and allocate the requested devices.
Definition: common_DFE.cpp:129
int rows
The number of rows.
Definition: matrix_base.hpp:42
int cols
The number of columns.
Definition: matrix_base.hpp:44
size_t(* get_accelerator_avail_num_dll)()
Definition: common_DFE.cpp:47
#define DFE_LIB_SIM
Definition: common_DFE.h:45
int(* calcqgdKernelDFE_dll)(size_t rows, size_t cols, DFEgate_kernel_type *gates, int gatesNum, int gateSetNum, int traceOffset, double *trace)
Definition: common_DFE.cpp:49
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
#define DFE_LIB_9QUBITS
Definition: common_DFE.h:43
Double-precision complex matrix (float64).
Definition: matrix.h:38
int size() const
Call to get the number of the allocated elements.
int get_initialize_id()
Call to get the identification number of the inititalization of the library.
Definition: common_DFE.cpp:216
Fixed point data related to a gate operation.
Definition: common_DFE.h:62
void unload_dfe_lib()
Call to unload the DFE libarary and release the allocated devices.
Definition: common_DFE.cpp:110
#define DFE_LIB_10QUBITS
Definition: common_DFE.h:44
int calcqgdKernelDFE(size_t rows, size_t cols, DFEgate_kernel_type *gates, int gatesNum, int gateSetNum, int traceOffset, double *trace)
Call to execute the calculation on the reserved DFE engines.
Definition: common_DFE.cpp:233
size_t get_accelerator_avail_num()
Call to get the available number of accelerators.
Definition: common_DFE.cpp:195
Header file for DFE support in unitary simulation.
size_t get_accelerator_free_num()
Call to get the number of free accelerators.
Definition: common_DFE.cpp:206
int initialize_id
Definition: common_DFE.cpp:58
size_t(* get_accelerator_free_num_dll)()
Definition: common_DFE.cpp:48
int(* get_chained_gates_num_dll)()
Definition: common_DFE.cpp:53