// quantum computing emulation library // // R. Perry, January 2022 // #include #include // rand(), srand(), RAND_MAX, atoi(), getenv(), malloc(), exit() #include // time() #include "lib.h" namespace qce { // non-template functions: // set srand() using time(0) or SRAND environment variable unsigned int SRAND() { char *env = getenv( "SRAND"); unsigned int seed = 0; if( env) seed = atoi(env); if( seed == 0) seed = time(0); srand( seed); return seed; } // 0.0 <= drand < 1.0 double drand() { return rand()/(RAND_MAX+1.0); } // 0 <= irand < n unsigned long irand( unsigned long n) { return n*drand(); } // print binary, n bits void printb( unsigned long x, unsigned int n) { unsigned long mask = 1LU << (n-1); while( mask) { std::cout << ((x & mask) ? '1' : '0'); mask >>= 1; } } // bit reverse, used by qfft() unsigned long bitrev( unsigned long x, unsigned int nbits) { unsigned long r = 0; for( unsigned int i = 0; i < nbits; ++i) { r <<= 1; r |= x & 1; x >>= 1; } return r; } // print error message and exit void error( const char *msg) { std::cerr << msg << std::endl; exit(1); } // malloc with exit on error void *emalloc( unsigned long size) { void *r = malloc(size); if( !r) { error( "qce: malloc() failed"); } return r; } } // namespace qce