Quantum++  v0.8.8
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 #ifdef _WITH_OPENMP_
406 #pragma omp parallel for collapse(3)
407 #endif
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);
412 
413  return result;
414 }
415 
422 inline cmat randH(idx D)
423 {
424  // EXCEPTION CHECKS
425 
426  if ( D == 0 )
427  throw Exception("qpp::randH()", Exception::Type::DIMS_INVALID);
428  // END EXCEPTION CHECKS
429 
430  cmat H = 2 * rand<cmat>(D, D) - (1. + 1_i) * cmat::Ones(D, D);
431 
432  return H + adjoint(H);
433 }
434 
441 inline ket randket(idx D)
442 {
443  // EXCEPTION CHECKS
444 
445  if ( D == 0 )
446  throw Exception("qpp::randket()", Exception::Type::DIMS_INVALID);
447  // END EXCEPTION CHECKS
448 
449  /* slow
450  ket kt = ket::Ones(D);
451  ket result = static_cast<ket>(randU(D) * kt);
452 
453  return result;
454  */
455 
456  ket kt = randn<cmat>(D, 1);
457 
458  return kt / norm(kt);
459 }
460 
467 inline cmat randrho(idx D)
468 {
469  // EXCEPTION CHECKS
470 
471  if ( D == 0 )
472  throw Exception("qpp::randrho()", Exception::Type::DIMS_INVALID);
473  // END EXCEPTION CHECKS
474 
475  cmat result = 10 * randH(D);
476  result = result * adjoint(result);
477 
478  return result / trace(result);
479 }
480 
490 inline std::vector<idx> randperm(idx n)
491 {
492  // EXCEPTION CHECKS
493 
494  if ( n == 0 )
495  throw Exception("qpp::randperm()", Exception::Type::PERM_INVALID);
496  // END EXCEPTION CHECKS
497 
498  std::vector<idx> result(n);
499 
500  // fill in increasing order
501  std::iota(std::begin(result), std::end(result), 0);
502  // shuffle
503 #ifdef _NO_THREAD_LOCAL_
504  std::shuffle(std::begin(result), std::end(result),
506 #else
507  std::shuffle(std::begin(result), std::end(result),
509 
510 #endif // _NO_THREAD_LOCAL_
511 
512  return result;
513 }
514 
515 } /* namespace qpp */
516 
517 #endif /* RANDOM_H_ */
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:441
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
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
std::vector< idx > randperm(idx n)
Generates a random uniformly distributed permutation.
Definition: random.h:490
dyn_mat< typename Derived::Scalar > adjoint(const Eigen::MatrixBase< Derived > &A)
Adjoint.
Definition: functions.h:84
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: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:79
cmat randH(idx D)
Generates a random Hermitian matrix.
Definition: random.h:422
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:467
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