44 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_
78 std::uniform_int_distribution<bigint> uid(a, b);
80 #ifdef NO_THREAD_LOCAL_
84 #endif // NO_THREAD_LOCAL_
97 idx b = std::numeric_limits<idx>::max())
105 std::uniform_int_distribution<idx> uid(a, b);
107 #ifdef NO_THREAD_LOCAL_
111 #endif // NO_THREAD_LOCAL_
125 template<
typename Derived>
162 if (rows == 0 || cols == 0)
168 return dmat::Zero(rows, cols).unaryExpr(
201 if (rows == 0 || cols == 0)
207 return rand<dmat>(rows, cols, a, b).cast<cplx>() +
208 1_i * rand<dmat>(rows, cols, a, b).cast<cplx>();
222 template<
typename Derived>
257 double mean,
double sigma)
261 if (rows == 0 || cols == 0)
265 std::normal_distribution<> nd(mean, sigma);
267 return dmat::Zero(rows, cols).unaryExpr(
270 #ifdef NO_THREAD_LOCAL_
274 #endif // NO_THREAD_LOCAL_
304 if (rows == 0 || cols == 0)
308 return randn<dmat>(rows, cols, mean,
sigma).cast<cplx>() +
309 1_i * randn<dmat>(rows, cols, mean,
sigma).cast<cplx>();
322 std::normal_distribution<> nd(mean,
sigma);
324 #ifdef NO_THREAD_LOCAL_
328 #endif // NO_THREAD_LOCAL_
347 cmat X = 1 / std::sqrt(2.) * randn<cmat>(D, D);
348 Eigen::HouseholderQR<cmat> qr(X);
350 cmat Q = qr.householderQ();
354 Eigen::VectorXcd phases = (rand<dmat>(D, 1)).cast<
cplx>();
355 for (
idx i = 0; i < static_cast<idx>(phases.rows()); ++i)
356 phases(i) = std::exp(2 *
pi * 1_i * phases(i));
358 Q = Q * phases.asDiagonal();
374 if (Din == 0 || Dout == 0 || Din > Dout)
378 return randU(Dout).block(0, 0, Dout, Din);
401 std::vector<cmat> result(N);
402 for (
idx i = 0; i < N; ++i)
403 result[i] = cmat::Zero(D, D);
409 #pragma omp parallel for collapse(3)
410 #endif // WITH_OPENMP_
411 for (
idx k = 0; k < N; ++k)
412 for (
idx a = 0; a < D; ++a)
413 for (
idx b = 0; b < D; ++b)
414 result[k](a, b) = U(a * N + k, b * N);
433 cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
459 ket kt = randn<cmat>(D, 1);
461 return kt /
norm(kt);
479 result = result *
adjoint(result);
481 return result /
trace(result);
501 std::vector<idx> result(N);
504 std::iota(std::begin(result), std::end(result), 0);
506 #ifdef NO_THREAD_LOCAL_
507 std::shuffle(std::begin(result), std::end(result),
510 std::shuffle(std::begin(result), std::end(result),
513 #endif // NO_THREAD_LOCAL_
533 std::vector<double> result(N);
536 std::exponential_distribution<> ed(1);
537 for (
idx i = 0; i < N; ++i)
539 #ifdef NO_THREAD_LOCAL_
543 #endif // NO_THREAD_LOCAL_
547 double sumprob =
sum(result);
548 for (
idx i = 0; i < N; ++i)
549 result[i] /= sumprob;
std::vector< idx > randperm(idx N)
Generates a random uniformly distributed permutation.
Definition: random.h:493
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
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:66
std::vector< cmat > randkraus(idx N, idx D)
Generates a set of random Kraus operators.
Definition: random.h:391
ket randket(idx D)
Generates a random normalized ket (pure state vector)
Definition: random.h:444
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:51
Quantum++ main namespace.
Definition: codes.h:30
double norm(const Eigen::MatrixBase< Derived > &A)
Frobenius norm.
Definition: functions.h:252
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:207
dyn_mat< typename Derived::Scalar > adjoint(const Eigen::MatrixBase< Derived > &A)
Adjoint.
Definition: functions.h:84
std::vector< double > randprob(idx N)
Generates a random probability vector uniformly distributed over the probability simplex.
Definition: random.h:525
static RandomDevices & get_thread_local_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:102
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:46
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
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:223
double rand(double a, double b)
Generates a random real number uniformly distributed in the interval [a, b)
Definition: random.h:44
cmat randU(idx D)
Generates a random unitary matrix.
Definition: random.h:337
Derived::Scalar trace(const Eigen::MatrixBase< Derived > &A)
Trace.
Definition: functions.h:127
constexpr double pi
Definition: constants.h:79
cmat randH(idx D)
Generates a random Hermitian matrix.
Definition: random.h:425
static RandomDevices & get_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:90
long long int bigint
Big integer.
Definition: types.h:41
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:61
cmat randrho(idx D)
Generates a random density matrix.
Definition: random.h:470
std::size_t idx
Non-negative integer index.
Definition: types.h:36
cmat randV(idx Din, idx Dout)
Generates a random isometry matrix.
Definition: random.h:370
Derived::Scalar sum(const Eigen::MatrixBase< Derived > &A)
Element-wise sum of A.
Definition: functions.h:209