// quantum computing emulation library // // R. Perry, Jan. 2018 // #ifndef _QCE_H_ #define _QCE_H_ // macros: // // DRAND 0.0 <= d < 1.0 // IRAND 0 <= i < n // #define DRAND (rand()/(RAND_MAX+1.0)) #define IRAND(n) ((unsigned long)(DRAND*(n))) // // print options: // #define PRINT_TANGLE -1 // entanglement measure #define PRINT_LABELS 1 // decimal state labels #define PRINT_BITS 2 // binary state indices and probabilities // constants: // extern const double pi; // 4*atan(1) extern const double s2; // sqrt(2)/2 // functions for real or complex states (see template.h and template.c): // // Q allocate a new state vector, uninitialized // zero set state vector to all zeros // init initialize state vector // swap swap two states // product tensor product // tangle trace distance as a measure of entanglement // H Hadamard transformation on one qubit // X NOT transformation on one qubit // Z phase-flip on one qubit // CX controlled NOT on one qubit // M measurement, uses DRAND // reduce reduce state based on a measurement // collapse collapse state based on a measurement of one qubit // operate q = (a,b) -> (a,f(a,b,z)) // print display message and qubit state or entanglement measure // abs2 magnitude squared // // functions for complex only (see below): // // Hra Hadamard with non-resonant pulse error // Hrp Hadamard with phase shift error // qfft quantum FFT // fft classical FFT // // general functions (see below): // // SRAND set srand() using time(0) or SRAND environment variable // printb print in binary // error print error message and exit // emalloc malloc with exit on error // // typedefs: // // Func function type for operate() // qtype complex or real amplitude // State qubit register // typedef unsigned long (*Func) (unsigned long, unsigned long, unsigned long); // typedef double qtype_Real; typedef double _Complex qtype_Complex; // // concatenation for name construction in template.h and template.c: // #undef _namex_ #undef _name_ #define _namex_(x,y) x ## _ ## y #define _name_(x,y) _namex_(x,y) // x,y will be expanded in _namex_ if they are macros // // complex functions: // #undef QTYPE_COMPLEX #define QTYPE_COMPLEX #undef QTYPE #undef qtype #undef State #define QTYPE Complex #define qtype qtype_Complex #define State State_Complex #include "template.h" // // real functions: // #undef QTYPE_COMPLEX #undef QTYPE #undef qtype #undef State #define QTYPE Real #define qtype qtype_Real #define State State_Real #include "template.h" // // type generic functions: // #undef QTYPE #undef qtype #undef State #ifndef _QCE_C_ // not compiling qce.c #ifdef REAL #include "Real.h" // Real.h and Complex.h are temporary files #else // generated by the Makefile #include "Complex.h" #endif #endif // // functions for complex only: // void Hra( State_Complex q, unsigned int k, double ra); void Hrp( State_Complex q, unsigned int k, double rp); void qfft( State_Complex q, int inv); void fft( double _Complex *X, unsigned long N, int inv); // // general functions: // unsigned int SRAND( void); void printb( unsigned long x, unsigned int n); void error( const char *msg); void *emalloc( unsigned long size); #endif // _QCE_H_