45 inline double rand(
double a,
double b) {
49 throw exception::OutOfRange(
"qpp::rand()");
52 std::uniform_real_distribution<> ud(a, b);
54 #ifdef NO_THREAD_LOCAL_ 55 RandomDevices::get_instance().get_prng();
57 RandomDevices::get_thread_local_instance().get_prng();
74 inline bigint rand(bigint a, bigint b) {
78 throw exception::OutOfRange(
"qpp::rand()");
81 std::uniform_int_distribution<bigint> uid(a, b);
84 #ifdef NO_THREAD_LOCAL_ 85 RandomDevices::get_instance().get_prng();
87 RandomDevices::get_thread_local_instance().get_prng();
101 inline idx randidx(idx a = std::numeric_limits<idx>::min(),
102 idx b = std::numeric_limits<idx>::max()) {
106 throw exception::OutOfRange(
"qpp::randidx()");
109 std::uniform_int_distribution<idx> uid(a, b);
111 #ifdef NO_THREAD_LOCAL_ 112 RandomDevices::get_instance().get_prng();
114 RandomDevices::get_thread_local_instance().get_prng();
131 template <
typename Derived>
132 Derived rand(idx rows QPP_UNUSED_, idx cols QPP_UNUSED_,
133 double a QPP_UNUSED_ = 0,
double b QPP_UNUSED_ = 1) {
134 throw exception::UndefinedType(
"qpp::rand()");
158 inline dmat rand(idx rows, idx cols,
double a,
double b) {
161 if (rows == 0 || cols == 0)
162 throw exception::ZeroSize(
"qpp::rand()");
164 throw exception::OutOfRange(
"qpp::rand()");
167 return dmat::Zero(rows, cols).unaryExpr([a, b](
double) {
194 inline cmat rand(idx rows, idx cols,
double a,
double b) {
197 if (rows == 0 || cols == 0)
198 throw exception::ZeroSize(
"qpp::rand()");
200 throw exception::OutOfRange(
"qpp::rand()");
203 return rand<dmat>(rows, cols, a, b).cast<cplx>() +
204 1_i * rand<dmat>(rows, cols, a, b).cast<cplx>();
218 template <
typename Derived>
219 Derived randn(idx rows QPP_UNUSED_, idx cols QPP_UNUSED_,
220 double mean QPP_UNUSED_ = 0,
double sigma QPP_UNUSED_ = 1) {
221 throw exception::UndefinedType(
"qpp::randn()");
245 inline dmat randn(idx rows, idx cols,
double mean,
double sigma) {
248 if (rows == 0 || cols == 0)
249 throw exception::ZeroSize(
"qpp::randn()");
252 std::normal_distribution<> nd(mean, sigma);
254 #ifdef NO_THREAD_LOCAL_ 255 RandomDevices::get_instance().get_prng();
257 RandomDevices::get_thread_local_instance().get_prng();
260 return dmat::Zero(rows, cols).unaryExpr([&nd, &gen](
double) {
287 inline cmat randn(idx rows, idx cols,
double mean,
double sigma) {
290 if (rows == 0 || cols == 0)
291 throw exception::ZeroSize(
"qpp::randn()");
294 return randn<dmat>(rows, cols, mean, sigma).cast<cplx>() +
295 1_i * randn<dmat>(rows, cols, mean, sigma).cast<cplx>();
306 inline double randn(
double mean = 0,
double sigma = 1) {
307 std::normal_distribution<> nd(mean, sigma);
309 #ifdef NO_THREAD_LOCAL_ 310 RandomDevices::get_instance().get_prng();
312 RandomDevices::get_thread_local_instance().get_prng();
324 inline cmat randU(idx D = 2)
331 throw exception::DimsInvalid(
"qpp::randU()");
334 cmat X = 1 / std::sqrt(2.) * randn<cmat>(D, D);
335 Eigen::HouseholderQR<cmat> qr(X);
337 cmat Q = qr.householderQ();
341 Eigen::VectorXcd phases = (rand<dmat>(D, 1)).cast<cplx>();
342 for (idx i = 0; i < static_cast<idx>(phases.rows()); ++i)
343 phases(i) = std::exp(2 * pi * 1_i * phases(i));
345 Q = Q * phases.asDiagonal();
357 inline cmat randV(idx Din, idx Dout) {
360 if (Din == 0 || Dout == 0 || Din > Dout)
361 throw exception::DimsInvalid(
"qpp::randV()");
364 return randU(Dout).block(0, 0, Dout, Din);
377 inline std::vector<cmat> randkraus(idx N, idx D = 2) {
381 throw exception::OutOfRange(
"qpp::randkraus()");
383 throw exception::DimsInvalid(
"qpp::randkraus()");
386 std::vector<cmat> result(N);
387 for (idx i = 0; i < N; ++i)
388 result[i] = cmat::Zero(D, D);
391 cmat U = randU(N * D);
394 #pragma omp parallel for collapse(3) 395 #endif // WITH_OPENMP_ 396 for (idx k = 0; k < N; ++k)
397 for (idx a = 0; a < D; ++a)
398 for (idx b = 0; b < D; ++b)
399 result[k](a, b) = U(a * N + k, b * N);
410 inline cmat randH(idx D = 2) {
414 throw exception::DimsInvalid(
"qpp::randH()");
417 cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
419 return H + H.adjoint();
428 inline ket randket(idx D = 2) {
432 throw exception::DimsInvalid(
"qpp::randket()");
442 ket kt = randn<cmat>(D, 1);
444 return kt / kt.norm();
453 inline cmat randrho(idx D = 2) {
457 throw exception::DimsInvalid(
"qpp::randrho()");
460 cmat result = 10 * randH(D);
461 result = result * result.adjoint();
463 return result / result.trace();
475 inline std::vector<idx> randperm(idx N) {
479 throw exception::PermInvalid(
"qpp::randperm()");
482 std::vector<idx> result(N);
485 std::iota(std::begin(result), std::end(result), 0);
489 #ifdef NO_THREAD_LOCAL_ 490 RandomDevices::get_instance().get_prng();
492 RandomDevices::get_thread_local_instance().get_prng();
494 std::shuffle(std::begin(result), std::end(result), gen);
506 inline std::vector<double> randprob(idx N) {
510 throw exception::OutOfRange(
"qpp::randprob()");
513 std::vector<double> result(N);
516 std::exponential_distribution<> ed(1);
518 #ifdef NO_THREAD_LOCAL_ 519 RandomDevices::get_instance().get_prng();
521 RandomDevices::get_thread_local_instance().get_prng();
523 for (idx i = 0; i < N; ++i)
527 double sumprob = std::accumulate(std::begin(result), std::end(result), 0.0);
528 for (idx i = 0; i < N; ++i)
529 result[i] /= sumprob;
Quantum++ main namespace.
Definition: circuits.h:35