Quantum++  v0.8.6
C++11 quantum computing library
random.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef RANDOM_H_
28 #define RANDOM_H_
29 
30 // random matrices/states
31 
32 namespace qpp
33 {
34 
43 inline double rand(double a = 0, double b = 1)
44 {
45  std::uniform_real_distribution<> ud(a, b);
46 
47 #ifdef _NO_THREAD_LOCAL
48  return ud(RandomDevices::get_instance()._rng);
49 #else
51 #endif // _NO_THREAD_LOCAL
52 }
53 
62 inline bigint rand(bigint a = std::numeric_limits<bigint>::min(),
63  bigint b = std::numeric_limits<bigint>::max())
64 {
65  std::uniform_int_distribution<bigint> uid(a, b);
66 
67 #ifdef _NO_THREAD_LOCAL
68  return uid(RandomDevices::get_instance()._rng);
69 #else
71 #endif // _NO_THREAD_LOCAL
72 }
73 
82 inline ubigint rand(ubigint a = std::numeric_limits<ubigint>::min(),
83  ubigint b = std::numeric_limits<ubigint>::max())
84 {
85  std::uniform_int_distribution<ubigint> uid(a, b);
86 
87 #ifdef _NO_THREAD_LOCAL
88  return uid(RandomDevices::get_instance()._rng);
89 #else
91 #endif // _NO_THREAD_LOCAL
92 }
93 
102 inline idx randidx(idx a = std::numeric_limits<idx>::min(),
103  idx b = std::numeric_limits<idx>::max())
104 {
105  std::uniform_int_distribution<idx> uid(a, b);
106 
107 #ifdef _NO_THREAD_LOCAL
108  return uid(RandomDevices::get_instance()._rng);
109 #else
110  return uid(RandomDevices::get_thread_local_instance()._rng);
111 #endif // _NO_THREAD_LOCAL
112 }
113 
125 template<typename Derived>
126 Derived rand(idx rows, idx cols, double a = 0, double b = 1)
127 {
128  // silence -Wunused-parameter in clang++
129  (void) rows;
130  (void) cols;
131  (void) a;
132  (void) b;
133  throw Exception("qpp::rand()", Exception::Type::UNDEFINED_TYPE);
134 }
135 
157 template<>
158 inline dmat rand(idx rows, idx cols, double a, double b)
159 {
160  // EXCEPTION CHECKS
161 
162  if (rows == 0 || cols == 0)
163  throw Exception("qpp::rand()", Exception::Type::ZERO_SIZE);
164  // END EXCEPTION CHECKS
165 
166  return dmat::Zero(rows, cols).unaryExpr(
167  [a, b](double)
168  {
169  return rand(a, b);
170  });
171 }
172 
194 template<>
195 inline cmat rand(idx rows, idx cols, double a, double b)
196 {
197  // EXCEPTION CHECKS
198 
199  if (rows == 0 || cols == 0)
200  throw Exception("qpp::rand()", Exception::Type::ZERO_SIZE);
201  // END EXCEPTION CHECKS
202 
203  return rand<dmat>(rows, cols, a, b).cast<cplx>() +
204  1_i * rand<dmat>(rows, cols, a, b).cast<cplx>();
205 }
206 
218 template<typename Derived>
219 Derived randn(idx rows, idx cols, double mean = 0,
220  double sigma = 1)
221 {
222  // silence -Wunused-parameter in clang++
223  (void) rows;
224  (void) cols;
225  (void) mean;
226  (void) sigma;
227  throw Exception("qpp::randn()", Exception::Type::UNDEFINED_TYPE);
228 }
229 
251 template<>
252 inline dmat randn(idx rows, idx cols,
253  double mean, double sigma)
254 {
255  // EXCEPTION CHECKS
256 
257  if (rows == 0 || cols == 0)
258  throw Exception("qpp::randn()", Exception::Type::ZERO_SIZE);
259  // END EXCEPTION CHECKS
260 
261  std::normal_distribution<> nd(mean, sigma);
262 
263  return dmat::Zero(rows, cols).unaryExpr(
264  [&nd](double)
265  {
266 #ifdef _NO_THREAD_LOCAL
267  return nd(RandomDevices::get_instance()._rng);
268 #else
270 #endif // _NO_THREAD_LOCAL
271  });
272 }
273 
295 template<>
296 inline cmat randn(idx rows, idx cols,
297  double mean, double sigma)
298 {
299  // EXCEPTION CHECKS
300 
301  if (rows == 0 || cols == 0)
302  throw Exception("qpp::randn()", Exception::Type::ZERO_SIZE);
303  // END EXCEPTION CHECKS
304 
305  return randn<dmat>(rows, cols, mean, sigma).cast<cplx>() +
306  1_i * randn<dmat>(rows, cols, mean, sigma).cast<cplx>();
307 }
308 
317 inline double randn(double mean = 0, double sigma = 1)
318 {
319  std::normal_distribution<> nd(mean, sigma);
320 
321 #ifdef _NO_THREAD_LOCAL
322  return nd(RandomDevices::get_instance()._rng);
323 #else
325 #endif // _NO_THREAD_LOCAL
326 }
327 
334 inline cmat randU(idx D)
335 // ~3 times slower than Toby Cubitt's MATLAB corresponding routine,
336 // because Eigen 3 QR algorithm is not parallelized
337 {
338  // EXCEPTION CHECKS
339 
340  if (D == 0)
341  throw Exception("qpp::randU()", Exception::Type::DIMS_INVALID);
342  // END EXCEPTION CHECKS
343 
344  cmat X = 1 / std::sqrt(2.) * randn<cmat>(D, D);
345  Eigen::HouseholderQR<cmat> qr(X);
346 
347  cmat Q = qr.householderQ();
348  // phase correction so that the resultant matrix is
349  // uniformly distributed according to the Haar measure
350 
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));
354 
355  Q = Q * phases.asDiagonal();
356 
357  return Q;
358 }
359 
367 inline cmat randV(idx Din, idx Dout)
368 {
369  // EXCEPTION CHECKS
370 
371  if (Din == 0 || Dout == 0 || Din > Dout)
372  throw Exception("qpp::randV()", Exception::Type::DIMS_INVALID);
373  // END EXCEPTION CHECKS
374 
375  return randU(Dout).block(0, 0, Dout, Din);
376 }
377 
388 inline std::vector<cmat> randkraus(idx N, idx D)
389 {
390  // EXCEPTION CHECKS
391 
392  if (N == 0)
393  throw Exception("qpp::randkraus()", Exception::Type::OUT_OF_RANGE);
394  if (D == 0)
395  throw Exception("qpp::randkraus()", Exception::Type::DIMS_INVALID);
396  // END EXCEPTION CHECKS
397 
398  std::vector<cmat> result(N);
399  for (idx i = 0; i < N; ++i)
400  result[i] = cmat::Zero(D, D);
401 
402  cmat Fk(D, D);
403  cmat U = randU(N * D);
404 
405 #pragma omp parallel for collapse(3)
406  for (idx k = 0; k < N; ++k)
407  for (idx a = 0; a < D; ++a)
408  for (idx b = 0; b < D; ++b)
409  result[k](a, b) = U(a * N + k, b * N);
410 
411  return result;
412 }
413 
420 inline cmat randH(idx D)
421 {
422  // EXCEPTION CHECKS
423 
424  if (D == 0)
425  throw Exception("qpp::randH()", Exception::Type::DIMS_INVALID);
426  // END EXCEPTION CHECKS
427 
428  cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
429 
430  return H + adjoint(H);
431 }
432 
439 inline ket randket(idx D)
440 {
441  // EXCEPTION CHECKS
442 
443  if (D == 0)
444  throw Exception("qpp::randket()", Exception::Type::DIMS_INVALID);
445  // END EXCEPTION CHECKS
446 
447  /* slow
448  ket kt = ket::Ones(D);
449  ket result = static_cast<ket>(randU(D) * kt);
450 
451  return result;
452  */
453 
454  ket kt = randn<cmat>(D, 1);
455 
456  return kt / norm(kt);
457 }
458 
465 inline cmat randrho(idx D)
466 {
467  // EXCEPTION CHECKS
468 
469  if (D == 0)
470  throw Exception("qpp::randrho()", Exception::Type::DIMS_INVALID);
471  // END EXCEPTION CHECKS
472 
473  cmat result = 10 * randH(D);
474  result = result * adjoint(result);
475 
476  return result / trace(result);
477 }
478 
488 inline std::vector<idx> randperm(idx n)
489 {
490  // EXCEPTION CHECKS
491 
492  if (n == 0)
493  throw Exception("qpp::randperm()", Exception::Type::PERM_INVALID);
494  // END EXCEPTION CHECKS
495 
496  std::vector<idx> result(n);
497 
498  // fill in increasing order
499  std::iota(std::begin(result), std::end(result), 0);
500  // shuffle
501 #ifdef _NO_THREAD_LOCAL
502  std::shuffle(std::begin(result), std::end(result),
504 #else
505  std::shuffle(std::begin(result), std::end(result),
507 
508 #endif // _NO_THREAD_LOCAL
509 
510  return result;
511 }
512 
513 } /* namespace qpp */
514 
515 #endif /* RANDOM_H_ */
static thread_local RandomDevices & get_thread_local_instance() noexcept(std::is_nothrow_constructible< RandomDevices >::value)
Definition: singleton.h:102
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:102
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:71
unsigned long long int ubigint
Non-negative big integer.
Definition: types.h:46
std::vector< cmat > randkraus(idx N, idx D)
Generates a set of random Kraus operators.
Definition: random.h:388
ket randket(idx D)
Generates a random normalized ket (pure state vector)
Definition: random.h:439
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:56
Quantum++ main namespace.
Definition: codes.h:30
double norm(const Eigen::MatrixBase< Derived > &A)
Frobenius norm.
Definition: functions.h:252
std::vector< idx > randperm(idx n)
Generates a random uniformly distributed permutation.
Definition: random.h:488
dyn_mat< typename Derived::Scalar > adjoint(const Eigen::MatrixBase< Derived > &A)
Adjoint.
Definition: functions.h:84
double sigma(const std::vector< double > &prob, const Container &X)
Standard deviation.
Definition: statistics.h:201
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:51
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:219
cmat randU(idx D)
Generates a random unitary matrix.
Definition: random.h:334
Derived::Scalar trace(const Eigen::MatrixBase< Derived > &A)
Trace.
Definition: functions.h:127
constexpr double pi
Definition: constants.h:85
cmat randH(idx D)
Generates a random Hermitian matrix.
Definition: random.h:420
double rand(double a=0, double b=1)
Generates a random real number uniformly distributed in the interval [a, b)
Definition: random.h:43
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:66
cmat randrho(idx D)
Generates a random density matrix.
Definition: random.h:465
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:367