29 #include <tbb/scalable_allocator.h> 75 void fread_wrapper(
void * buffer,
size_t size,
size_t count, FILE * stream) {
77 size_t fread_status = fread(buffer, size, count, stream);
79 if ( fread_status < count ) {
80 std::string err(
"Reached the end of the file before finishing reading");
93 void*
qgd_calloc(
int element_num,
int size,
int alignment ) {
95 void* ret = scalable_aligned_malloc( size*element_num,
CACHELINE);
96 memset(ret, 0, element_num*size);
109 void*
qgd_realloc(
void* aligned_ptr,
int element_num,
int size,
int alignment ) {
111 void* ret = scalable_aligned_realloc(aligned_ptr, size*element_num,
CACHELINE);
125 scalable_aligned_free(ptr);
137 if (n == 0)
return 1;
151 if ( involved_qbits.size() == 0 ) {
152 involved_qbits.push_back( qbit );
155 for(std::vector<int>::iterator it = involved_qbits.begin(); it != involved_qbits.end(); ++it) {
157 int current_val = *it;
159 if (current_val == qbit) {
162 else if (current_val > qbit) {
163 involved_qbits.insert( it, qbit );
170 involved_qbits.push_back( qbit );
190 int element_index = idx*matrix_size + idx;
191 mtx[element_index].real = 1.0;
211 int element_index = idx*mtx.
stride + idx;
212 mtx[element_index].real = 1.0f;
232 if (mtxs.size() == 0 ) {
248 for(std::vector<Matrix>::iterator it=++mtxs.begin(); it != mtxs.end(); ++it) {
282 int element_idx = idx*matrix_size+idx;
283 mtx[element_idx].real = mtx[element_idx].real - scalar.
real;
284 mtx[element_idx].imag = mtx[element_idx].imag - scalar.
imag;
365 int element_num = b.
size();
367 for (
int idx=0; idx<element_num; idx++) {
384 int element_num = b.
size();
386 for (
int idx=0; idx<element_num; idx++) {
406 std::string error(
"mult: The input argument b should be a single-column vector.");
413 tbb::parallel_for( tbb::blocked_range<int>(0, n_rows, 32), [&](tbb::blocked_range<int> r) {
415 for (
int idx=r.begin(); idx<r.end(); ++idx){
419 int nz_start = a.
indptr[idx];
420 int nz_end = a.
indptr[idx+1];
422 for (
int nz_idx=nz_start; nz_idx<nz_end; nz_idx++){
428 ret[idx].real += result.
real;
429 ret[idx].imag += result.
imag;
453 else if ( a.
real > 0 && a.
imag <= 0 ) {
457 else if ( a.
real < 0 && a.
imag > 0 ) {
461 else if ( a.
real < 0 && a.
imag <= 0 ) {
465 else if ( std::abs(a.
real) < 1e-8 && a.
imag > 0 ) {
487 int samples = b.
cols;
492 for (
int rdx=0; rdx<samples; rdx++){
495 for(
int cdx=0; cdx<samples; cdx++){
496 d[rdx] = d[rdx] - A[rdx*samples+cdx]*x0[cdx];
500 sk = sk + d[rdx]*d[rdx];
504 while (std::sqrt(sk/b.
cols) > tol && iter<1000){
508 for (
int rdx=0; rdx<samples; rdx++){
512 for(
int cdx=0; cdx<samples; cdx++){
513 Ad[rdx] = Ad[rdx] + A[rdx*samples+cdx]*d[cdx];
516 dAd = dAd + d[rdx]*Ad[rdx];
519 double mu_k = sk / dAd;
522 for(
int idx=0; idx<samples; idx++){
524 x0[idx] = x0[idx] + mu_k*d[idx];
525 g[idx] = g[idx] + mu_k*Ad[idx];
526 sk_new = sk_new + g[idx]*g[idx];
530 for (
int idx=0; idx<samples;idx++){
531 d[idx] = (sk_new/sk)*d[idx] - g[idx];
Matrix dot(Matrix &A, Matrix &B)
Call to calculate the product of two complex matrices by calling method zgemm3m from the CBLAS librar...
Header file of single-precision complex array storage with automatic and thread safe reference counti...
void * qgd_calloc(int element_num, int size, int alignment)
custom defined memory allocation function.
void conjugate_gradient(Matrix_real A, Matrix_real b, Matrix_real &x0, double tol)
void qgd_free(void *ptr)
custom defined memory release function.
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)
Structure type representing single-precision complex numbers.
QGD_Complex16 mult(QGD_Complex16 &a, QGD_Complex16 &b)
Call to calculate the product of two complex scalars.
Class to store data of complex arrays and its properties.
scalar * get_data() const
Call to get the pointer to the stored data.
Matrix reduce_zgemm(std::vector< Matrix > &mtxs)
Calculate the product of several square shaped complex matrices stored in a vector.
double arg(const QGD_Complex16 &a)
Call to retrieve the phase of a complex number.
int rows
The number of rows.
int cols
The number of columns.
Structure type representing complex numbers in the SQUANDER package.
int Power_of_2(int n)
Calculates the n-th power of 2.
Double-precision complex matrix (float64).
int size() const
Call to get the number of the allocated elements.
void subtract_diag(Matrix &mtx, QGD_Complex16 scalar)
Call to subtract a scalar from the diagonal of a complex matrix.
Single-precision complex matrix (float32).
Matrix create_identity(int matrix_size)
Call to create an identity matrix.
Header file for commonly used functions and wrappers to CBLAS functions.
double activation_function(double Phi, int limit)
?????
void add_unique_elelement(std::vector< int > &involved_qbits, int qbit)
Add an integer to a vector of integers if the integer is not already an element of the vector...
double real
the real part of a complex number
void * qgd_realloc(void *aligned_ptr, int element_num, int size, int alignment)
custom defined memory reallocation function.
void fread_wrapper(void *buffer, size_t size, size_t count, FILE *stream)
Wrapper function aound fread with error handling.
Class to store data of complex arrays and its properties.
double imag
the imaginary part of a complex number
Matrix_float create_identity_float(int matrix_size)
Call to create a single-precision complex identity matrix.