45 inline double rand(
double a,
double b) {
52 std::uniform_real_distribution<> ud(a, b);
54 #ifdef NO_THREAD_LOCAL_ 81 std::uniform_int_distribution<bigint> uid(a, b);
84 #ifdef NO_THREAD_LOCAL_ 102 idx b = std::numeric_limits<idx>::max()) {
109 std::uniform_int_distribution<idx> uid(a, b);
111 #ifdef NO_THREAD_LOCAL_ 131 template <
typename Derived>
133 double a QPP_UNUSED_ = 0,
double b QPP_UNUSED_ = 1) {
162 if (rows == 0 || cols == 0)
168 return dmat::Zero(rows, cols).unaryExpr([a, b](
double) {
198 if (rows == 0 || cols == 0)
204 return rand<dmat>(rows, cols, a, b).cast<cplx>() +
205 1_i * rand<dmat>(rows, cols, a, b).cast<cplx>();
219 template <
typename Derived>
221 double mean QPP_UNUSED_ = 0,
double sigma QPP_UNUSED_ = 1) {
250 if (rows == 0 || cols == 0)
254 std::normal_distribution<> nd(mean,
sigma);
256 #ifdef NO_THREAD_LOCAL_ 262 return dmat::Zero(rows, cols).unaryExpr([&nd, &gen](
double) {
292 if (rows == 0 || cols == 0)
296 return randn<dmat>(rows, cols, mean,
sigma).cast<cplx>() +
297 1_i * randn<dmat>(rows, cols, mean,
sigma).cast<cplx>();
309 std::normal_distribution<> nd(mean,
sigma);
311 #ifdef NO_THREAD_LOCAL_ 336 cmat X = 1 / std::sqrt(2.) * randn<cmat>(D, D);
337 Eigen::HouseholderQR<cmat> qr(X);
339 cmat Q = qr.householderQ();
343 Eigen::VectorXcd phases = (rand<dmat>(D, 1)).cast<
cplx>();
344 for (
idx i = 0; i < static_cast<idx>(phases.rows()); ++i)
345 phases(i) = std::exp(2 *
pi * 1_i * phases(i));
347 Q = Q * phases.asDiagonal();
362 if (Din == 0 || Dout == 0 || Din > Dout)
366 return randU(Dout).block(0, 0, Dout, Din);
388 std::vector<cmat> result(N);
389 for (
idx i = 0; i < N; ++i)
390 result[i] = cmat::Zero(D, D);
396 #pragma omp parallel for collapse(3) 397 #endif // WITH_OPENMP_ 398 for (
idx k = 0; k < N; ++k)
399 for (
idx a = 0; a < D; ++a)
400 for (
idx b = 0; b < D; ++b)
401 result[k](a, b) = U(a * N + k, b * N);
419 cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
421 return H + H.adjoint();
444 ket kt = randn<cmat>(D, 1);
446 return kt / kt.norm();
463 result = result * result.adjoint();
465 return result / result.trace();
484 std::vector<idx> result(N);
487 std::iota(std::begin(result), std::end(result), 0);
491 #ifdef NO_THREAD_LOCAL_ 496 std::shuffle(std::begin(result), std::end(result), gen);
515 std::vector<double> result(N);
518 std::exponential_distribution<> ed(1);
520 #ifdef NO_THREAD_LOCAL_ 525 for (
idx i = 0; i < N; ++i)
529 double sumprob = std::accumulate(std::begin(result), std::end(result), 0.0);
530 for (
idx i = 0; i < N; ++i)
531 result[i] /= sumprob;
std::vector< idx > randperm(idx N)
Generates a random uniformly distributed permutation.
Definition: random.h:477
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:101
Not defined for this type exception.
Definition: exception.h:556
cmat randH(idx D=2)
Generates a random Hermitian matrix.
Definition: random.h:412
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:69
std::mt19937 & get_prng()
Returns a reference to the internal PRNG object.
Definition: random_devices.h:61
cmat randrho(idx D=2)
Generates a random density matrix.
Definition: random.h:455
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:54
Quantum++ main namespace.
Definition: circuits.h:35
ket randket(idx D=2)
Generates a random normalized ket (pure state vector)
Definition: random.h:430
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:269
Invalid permutation exception.
Definition: exception.h:380
std::vector< double > randprob(idx N)
Generates a random probability vector uniformly distributed over the probability simplex.
Definition: random.h:508
static RandomDevices & get_thread_local_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:103
Derived randn(idx rows QPP_UNUSED_, idx cols QPP_UNUSED_, double mean QPP_UNUSED_=0, double sigma QPP_UNUSED_=1)
Generates a random matrix with entries normally distributed in N(mean, sigma)
Definition: random.h:220
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:49
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:72
Argument out of range exception.
Definition: exception.h:515
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:379
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, make sure you use an unsigned type.
Definition: types.h:39
cmat randV(idx Din, idx Dout)
Generates a random isometry matrix.
Definition: random.h:359
cmat randU(idx D=2)
Generates a random unitary matrix.
Definition: random.h:326
Object has zero size exception.
Definition: exception.h:134