Program Listing for File utility.hpp¶
↰ Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/qulacs-rtd/checkouts/v0.6.7/src/cppsim/utility.hpp
)
#pragma once
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <cctype>
#include <chrono>
#include <cstdio>
#include <random>
#include <string>
#include <tuple>
#include <vector>
#include "qubit_info.hpp"
#include "type.hpp"
inline static UINT count_population_cpp(ITYPE x) {
x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) + (x & 0x5555555555555555UL);
x = ((x & 0xccccccccccccccccUL) >> 2) + (x & 0x3333333333333333UL);
x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) + (x & 0x0f0f0f0f0f0f0f0fUL);
x = ((x & 0xff00ff00ff00ff00UL) >> 8) + (x & 0x00ff00ff00ff00ffUL);
x = ((x & 0xffff0000ffff0000UL) >> 16) + (x & 0x0000ffff0000ffffUL);
x = ((x & 0xffffffff00000000UL) >> 32) + (x & 0x00000000ffffffffUL);
return (UINT)x;
}
void DllExport get_Pauli_matrix(
ComplexMatrix& matrix, const std::vector<UINT>& pauli_id_list);
class Random {
private:
std::uniform_real_distribution<double> uniform_dist;
std::normal_distribution<double> normal_dist;
std::mt19937_64 mt;
public:
Random() : uniform_dist(0, 1), normal_dist(0, 1) {
std::random_device rd;
mt.seed(rd());
}
void set_seed(uint64_t seed) { mt.seed(seed); }
double uniform() { return uniform_dist(mt); }
double normal() { return normal_dist(mt); }
unsigned long long int64() { return mt(); }
unsigned long int32() { return mt() % ULONG_MAX; }
};
class Timer {
private:
std::chrono::system_clock::time_point last;
long long stock;
bool is_stop;
public:
Timer() {
reset();
is_stop = false;
}
void reset() {
stock = 0;
last = std::chrono::system_clock::now();
}
double elapsed() {
if (is_stop)
return stock * 1e-6;
else {
auto duration = std::chrono::system_clock::now() - last;
return (stock +
std::chrono::duration_cast<std::chrono::microseconds>(
duration)
.count()) *
1e-6;
}
}
void temporal_stop() {
if (!is_stop) {
auto duration = std::chrono::system_clock::now() - last;
stock +=
std::chrono::duration_cast<std::chrono::microseconds>(duration)
.count();
is_stop = true;
}
}
void temporal_resume() {
if (is_stop) {
last = std::chrono::system_clock::now();
is_stop = false;
}
}
};
DllExport std::vector<std::string> split(
const std::string& s, const std::string& delim);
DllExport void chfmt(std::string& ops);
DllExport std::tuple<double, double, std::string> parse_openfermion_line(
std::string line);
bool check_is_unique_index_list(const std::vector<UINT>& index_list);
std::string& rtrim(std::string& str);
namespace ptree {
boost::property_tree::ptree to_ptree(const CPPCTYPE& cnum);
boost::property_tree::ptree to_ptree(const std::vector<UINT>& uarray);
boost::property_tree::ptree to_ptree(const std::vector<CPPCTYPE>& carray);
boost::property_tree::ptree to_ptree(
const std::vector<boost::property_tree::ptree>& pt_array);
boost::property_tree::ptree to_ptree(
const std::vector<TargetQubitInfo>& target_qubit_list);
boost::property_tree::ptree to_ptree(
const std::vector<ControlQubitInfo>& control_qubit_list);
boost::property_tree::ptree to_ptree(const ComplexVector& vector);
boost::property_tree::ptree to_ptree(const ComplexMatrix& matrix);
boost::property_tree::ptree to_ptree(const SparseComplexMatrix& sparse_matrix);
CPPCTYPE complex_from_ptree(const boost::property_tree::ptree& pt);
std::vector<UINT> uint_array_from_ptree(const boost::property_tree::ptree& pt);
std::vector<CPPCTYPE> complex_array_from_ptree(
const boost::property_tree::ptree& pt);
std::vector<boost::property_tree::ptree> ptree_array_from_ptree(
const boost::property_tree::ptree& pt);
std::vector<TargetQubitInfo> target_qubit_list_from_ptree(
const boost::property_tree::ptree& pt);
std::vector<ControlQubitInfo> control_qubit_list_from_ptree(
const boost::property_tree::ptree& pt);
ComplexVector complex_vector_from_ptree(const boost::property_tree::ptree& pt);
ComplexMatrix complex_matrix_from_ptree(const boost::property_tree::ptree& pt);
SparseComplexMatrix sparse_complex_matrix_from_ptree(
const boost::property_tree::ptree& pt);
std::string to_json(const boost::property_tree::ptree& ptree);
boost::property_tree::ptree from_json(const std::string& json);
} // namespace ptree