utility.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <chrono>
10 #include <random>
11 #include <string>
12 #include <vector>
13 #include "type.hpp"
14 
21 inline static UINT count_population_cpp(ITYPE x)
22 {
23  x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1)
24  + (x & 0x5555555555555555UL);
25  x = ((x & 0xccccccccccccccccUL) >> 2)
26  + (x & 0x3333333333333333UL);
27  x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4)
28  + (x & 0x0f0f0f0f0f0f0f0fUL);
29  x = ((x & 0xff00ff00ff00ff00UL) >> 8)
30  + (x & 0x00ff00ff00ff00ffUL);
31  x = ((x & 0xffff0000ffff0000UL) >> 16)
32  + (x & 0x0000ffff0000ffffUL);
33  x = ((x & 0xffffffff00000000UL) >> 32)
34  + (x & 0x00000000ffffffffUL);
35  return (UINT)x;
36 }
37 
44 void DllExport get_Pauli_matrix(ComplexMatrix& matrix, const std::vector<UINT>& pauli_id_list) ;
45 
49 class Random{
50 private:
51  std::uniform_real_distribution<double> uniform_dist;
52  std::normal_distribution<double> normal_dist;
53  std::mt19937_64 mt;
54 public:
58  Random(): uniform_dist(0,1), normal_dist(0,1){
59  std::random_device rd;
60  mt.seed(rd());
61  }
62 
68  void set_seed(uint64_t seed){
69  mt.seed(seed);
70  }
76  double uniform() {return uniform_dist(mt);}
77 
83  double normal(){return normal_dist(mt);}
84 
90  unsigned long long int64() { return mt(); }
91 
97  unsigned long int32() { return mt() % ULONG_MAX; }
98 };
99 
105 class Timer{
106 private:
107  std::chrono::system_clock::time_point last;
108  long long stock;
109  bool is_stop;
110 public:
114  Timer(){
115  reset();
116  is_stop = false;
117  }
118 
124  void reset(){
125  stock=0;
126  last = std::chrono::system_clock::now();
127  }
128 
135  double elapsed(){
136  if (is_stop) return stock*1e-6;
137  else {
138  auto duration = std::chrono::system_clock::now() - last;
139  return (stock + std::chrono::duration_cast<std::chrono::microseconds>(duration).count())*1e-6;
140  }
141  }
142 
149  if (!is_stop) {
150  auto duration = std::chrono::system_clock::now() - last;
151  stock += std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
152  is_stop = true;
153  }
154  }
155 
162  if (is_stop) {
163  last = std::chrono::system_clock::now();
164  is_stop = false;
165  }
166  }
167 };
168 
169 
177 DllExport std::vector<std::string> split(const std::string &s, const std::string &delim);
178 
184 DllExport void chfmt(std::string& ops);
void temporal_resume()
Definition: utility.hpp:161
Eigen::Matrix< CPPCTYPE, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > ComplexMatrix
Definition: type.hpp:17
Random()
Definition: utility.hpp:58
Timer()
Definition: utility.hpp:114
#define DllExport
Definition: type.hpp:30
Definition: utility.hpp:105
void DllExport get_Pauli_matrix(ComplexMatrix &matrix, const std::vector< UINT > &pauli_id_list)
Definition: utility.cpp:4
Definition: utility.hpp:49
void set_seed(uint64_t seed)
Definition: utility.hpp:68
double normal()
Definition: utility.hpp:83
double elapsed()
Definition: utility.hpp:135
void temporal_stop()
Definition: utility.hpp:148
unsigned long long int64()
Definition: utility.hpp:90
static UINT count_population_cpp(ITYPE x)
Definition: utility.hpp:21
DllExport void chfmt(std::string &ops)
Definition: utility.cpp:51
DllExport std::vector< std::string > split(const std::string &s, const std::string &delim)
Definition: utility.cpp:32
unsigned long int32()
Definition: utility.hpp:97
void reset()
Definition: utility.hpp:124
double uniform()
Definition: utility.hpp:76