45 inline double rand(
double a,
double b) {
52 std::uniform_real_distribution<> ud(a, b);
54 #ifdef NO_THREAD_LOCAL_ 58 #endif // NO_THREAD_LOCAL_ 79 std::uniform_int_distribution<bigint> uid(a, b);
81 #ifdef NO_THREAD_LOCAL_ 85 #endif // NO_THREAD_LOCAL_ 97 idx b = std::numeric_limits<idx>::max()) {
104 std::uniform_int_distribution<idx> uid(a, b);
106 #ifdef NO_THREAD_LOCAL_ 110 #endif // NO_THREAD_LOCAL_ 124 template <
typename Derived>
125 Derived
rand(
idx rows,
idx cols,
double a = 0,
double b = 1) {
159 if (rows == 0 || cols == 0)
165 return dmat::Zero(rows, cols).unaryExpr([a, b](
double) {
195 if (rows == 0 || cols == 0)
201 return rand<dmat>(rows, cols, a, b).cast<cplx>() +
202 1_i * rand<dmat>(rows, cols, a, b).cast<cplx>();
216 template <
typename Derived>
251 if (rows == 0 || cols == 0)
255 std::normal_distribution<> nd(mean, sigma);
257 return dmat::Zero(rows, cols).unaryExpr([&nd](
double) {
258 #ifdef NO_THREAD_LOCAL_ 262 #endif // NO_THREAD_LOCAL_ 291 if (rows == 0 || cols == 0)
295 return randn<dmat>(rows, cols, mean,
sigma).cast<cplx>() +
296 1_i * randn<dmat>(rows, cols, mean,
sigma).cast<cplx>();
308 std::normal_distribution<> nd(mean,
sigma);
310 #ifdef NO_THREAD_LOCAL_ 314 #endif // NO_THREAD_LOCAL_ 333 cmat X = 1 / std::sqrt(2.) * randn<cmat>(D, D);
334 Eigen::HouseholderQR<cmat> qr(X);
336 cmat Q = qr.householderQ();
340 Eigen::VectorXcd phases = (rand<dmat>(D, 1)).cast<
cplx>();
341 for (
idx i = 0; i < static_cast<idx>(phases.rows()); ++i)
342 phases(i) = std::exp(2 *
pi * 1_i * phases(i));
344 Q = Q * phases.asDiagonal();
359 if (Din == 0 || Dout == 0 || Din > Dout)
363 return randU(Dout).block(0, 0, Dout, Din);
385 std::vector<cmat> result(N);
386 for (
idx i = 0; i < N; ++i)
387 result[i] = cmat::Zero(D, D);
393 #pragma omp parallel for collapse(3) 394 #endif // WITH_OPENMP_ 395 for (
idx k = 0; k < N; ++k)
396 for (
idx a = 0; a < D; ++a)
397 for (
idx b = 0; b < D; ++b)
398 result[k](a, b) = U(a * N + k, b * N);
416 cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
418 return H + adjoint(H);
441 ket kt = randn<cmat>(D, 1);
443 return kt / norm(kt);
460 result = result * adjoint(result);
462 return result / trace(result);
481 std::vector<idx> result(N);
484 std::iota(std::begin(result), std::end(result), 0);
486 #ifdef NO_THREAD_LOCAL_ 487 std::shuffle(std::begin(result), std::end(result),
490 std::shuffle(std::begin(result), std::end(result),
493 #endif // NO_THREAD_LOCAL_ 512 std::vector<double> result(N);
515 std::exponential_distribution<> ed(1);
516 for (
idx i = 0; i < N; ++i) {
517 #ifdef NO_THREAD_LOCAL_ 522 #endif // NO_THREAD_LOCAL_ 526 double sumprob = sum(result);
527 for (
idx i = 0; i < N; ++i)
528 result[i] /= sumprob;
std::vector< idx > randperm(idx N)
Generates a random uniformly distributed permutation.
Definition: random.h:474
idx randidx(idx a=std::numeric_limits< idx >::min(), idx b=std::numeric_limits< idx >::max())
Generates a random index (idx) uniformly distributed in the interval [a, b].
Definition: random.h:96
Not defined for this type exception.
Definition: exception.h:554
cmat randH(idx D=2)
Generates a random Hermitian matrix.
Definition: random.h:409
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:69
cmat randrho(idx D=2)
Generates a random density matrix.
Definition: random.h:452
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:54
Quantum++ main namespace.
Definition: codes.h:35
ket randket(idx D=2)
Generates a random normalized ket (pure state vector)
Definition: random.h:427
double sigma(const std::vector< double > &prob, const Container &X, typename std::enable_if< is_iterable< Container >::value >::type *=nullptr)
Standard deviation.
Definition: statistics.h:197
Invalid dimension(s) exception.
Definition: exception.h:267
Invalid permutation exception.
Definition: exception.h:378
std::vector< double > randprob(idx N)
Generates a random probability vector uniformly distributed over the probability simplex.
Definition: random.h:505
static RandomDevices & get_thread_local_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:103
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:49
Derived randn(idx rows, idx cols, double mean=0, double sigma=1)
Generates a random matrix with entries normally distributed in N(mean, sigma)
Definition: random.h:217
double rand(double a, double b)
Generates a random real number uniformly distributed in the interval [a, b)
Definition: random.h:45
constexpr double pi
Definition: constants.h:78
Parameter out of range exception.
Definition: exception.h:513
static RandomDevices & get_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:92
std::vector< cmat > randkraus(idx N, idx D=2)
Generates a set of random Kraus operators.
Definition: random.h:376
long long int bigint
Big integer.
Definition: types.h:44
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:64
std::size_t idx
Non-negative integer index.
Definition: types.h:39
cmat randV(idx Din, idx Dout)
Generates a random isometry matrix.
Definition: random.h:356
cmat randU(idx D=2)
Generates a random unitary matrix.
Definition: random.h:323
Object has zero size exception.
Definition: exception.h:132