41 inline double rand(
double a,
double b)
49 std::uniform_real_distribution<> ud(a, b);
51 #ifdef NO_THREAD_LOCAL_ 55 #endif // NO_THREAD_LOCAL_ 77 std::uniform_int_distribution<bigint> uid(a, b);
79 #ifdef NO_THREAD_LOCAL_ 83 #endif // NO_THREAD_LOCAL_ 95 idx b = std::numeric_limits<idx>::max())
103 std::uniform_int_distribution<idx> uid(a, b);
105 #ifdef NO_THREAD_LOCAL_ 109 #endif // NO_THREAD_LOCAL_ 123 template<
typename Derived>
160 if (rows == 0 || cols == 0)
166 return dmat::Zero(rows, cols).unaryExpr(
199 if (rows == 0 || cols == 0)
205 return rand<dmat>(rows, cols, a, b).cast<cplx>() +
206 1_i * rand<dmat>(rows, cols, a, b).cast<cplx>();
220 template<
typename Derived>
257 if (rows == 0 || cols == 0)
261 std::normal_distribution<> nd(mean, sigma);
263 return dmat::Zero(rows, cols).unaryExpr(
266 #ifdef NO_THREAD_LOCAL_ 271 #endif // NO_THREAD_LOCAL_ 301 if (rows == 0 || cols == 0)
305 return randn<dmat>(rows, cols, mean,
sigma).cast<cplx>() +
306 1_i * randn<dmat>(rows, cols, mean,
sigma).cast<cplx>();
319 std::normal_distribution<> nd(mean,
sigma);
321 #ifdef NO_THREAD_LOCAL_ 325 #endif // NO_THREAD_LOCAL_ 344 cmat X = 1 / std::sqrt(2.) * randn<cmat>(D, D);
345 Eigen::HouseholderQR<cmat> qr(X);
347 cmat Q = qr.householderQ();
351 Eigen::VectorXcd phases = (rand<dmat>(D, 1)).cast<
cplx>();
352 for (
idx i = 0; i < static_cast<idx>(phases.rows()); ++i)
353 phases(i) = std::exp(2 *
pi * 1_i * phases(i));
355 Q = Q * phases.asDiagonal();
371 if (Din == 0 || Dout == 0 || Din > Dout)
375 return randU(Dout).block(0, 0, Dout, Din);
398 std::vector<cmat> result(N);
399 for (
idx i = 0; i < N; ++i)
400 result[i] = cmat::Zero(D, D);
406 #pragma omp parallel for collapse(3) 407 #endif // WITH_OPENMP_ 408 for (
idx k = 0; k < N; ++k)
409 for (
idx a = 0; a < D; ++a)
410 for (
idx b = 0; b < D; ++b)
411 result[k](a, b) = U(a * N + k, b * N);
430 cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
456 ket kt = randn<cmat>(D, 1);
458 return kt /
norm(kt);
476 result = result *
adjoint(result);
478 return result /
trace(result);
498 std::vector<idx> result(N);
501 std::iota(std::begin(result), std::end(result), 0);
503 #ifdef NO_THREAD_LOCAL_ 504 std::shuffle(std::begin(result), std::end(result),
507 std::shuffle(std::begin(result), std::end(result),
510 #endif // NO_THREAD_LOCAL_ 530 std::vector<double> result(N);
533 std::exponential_distribution<> ed(1);
534 for (
idx i = 0; i < N; ++i)
536 #ifdef NO_THREAD_LOCAL_ 541 #endif // NO_THREAD_LOCAL_ 545 double sumprob =
sum(result);
546 for (
idx i = 0; i < N; ++i)
547 result[i] /= sumprob;
std::vector< idx > randperm(idx N)
Generates a random uniformly distributed permutation.
Definition: random.h:490
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:94
Not defined for this type exception.
Definition: exception.h:619
cmat randH(idx D=2)
Generates a random Hermitian matrix.
Definition: random.h:422
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:65
cmat randrho(idx D=2)
Generates a random density matrix.
Definition: random.h:467
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:50
Quantum++ main namespace.
Definition: codes.h:30
ket randket(idx D=2)
Generates a random normalized ket (pure state vector)
Definition: random.h:441
double norm(const Eigen::MatrixBase< Derived > &A)
Frobenius norm.
Definition: functions.h:248
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:204
Invalid dimension(s) exception.
Definition: exception.h:287
Invalid permutation exception.
Definition: exception.h:412
dyn_mat< typename Derived::Scalar > adjoint(const Eigen::MatrixBase< Derived > &A)
Adjoint.
Definition: functions.h:81
std::vector< double > randprob(idx N)
Generates a random probability vector uniformly distributed over the probability simplex.
Definition: random.h:522
static RandomDevices & get_thread_local_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:101
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:45
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:221
double rand(double a, double b)
Generates a random real number uniformly distributed in the interval [a, b)
Definition: random.h:41
Derived::Scalar trace(const Eigen::MatrixBase< Derived > &A)
Trace.
Definition: functions.h:124
constexpr double pi
Definition: constants.h:76
Parameter out of range exception.
Definition: exception.h:567
static RandomDevices & get_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:89
std::vector< cmat > randkraus(idx N, idx D=2)
Generates a set of random Kraus operators.
Definition: random.h:388
long long int bigint
Big integer.
Definition: types.h:40
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:60
std::size_t idx
Non-negative integer index.
Definition: types.h:35
cmat randV(idx Din, idx Dout)
Generates a random isometry matrix.
Definition: random.h:367
Derived::Scalar sum(const Eigen::MatrixBase< Derived > &A)
Element-wise sum of A.
Definition: functions.h:205
cmat randU(idx D=2)
Generates a random unitary matrix.
Definition: random.h:334
Object has zero size exception.
Definition: exception.h:134