// quantum computing emulation library // // R. Perry, Jan. 2018 // // 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 // typedefs: // // State qubit register // Func function type for operate() // typedef struct State { unsigned int n; // number of bits unsigned long N; // number of states, N = 2**n double _Complex *a; // N amplitudes } State; // typedef unsigned long (*Func) (unsigned long, unsigned long, unsigned long); // functions: // // SRAND set srand() using time(0) or SRAND environment variable // // 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 // Hra Hadamard with non-resonant pulse error // Hrp Hadamard with phase shift error // 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)) // qfft quantum FFT // fft classical FFT // // printb print in binary // print display message and qubit state or entanglement measure // error print error message and exit // // abs2 complex magnitude squared // emalloc malloc with exit on error // unsigned int SRAND( void); State Q( int n); void zero( State q); void init( State q, unsigned long k, int rphase); void swap( State q, unsigned long i, unsigned long j); State product( State r, State s); double tangle( State q, unsigned int k); void H( State q, unsigned int k); void Hra( State q, unsigned int k, double ra); void Hrp( State q, unsigned int k, double rp); void X( State q, unsigned int k); void Z( State q, unsigned int k); void CX( State q, unsigned int c, unsigned int k); unsigned long M( State q); State reduce( State q, unsigned int n, unsigned long m); void collapse( State q, unsigned int k, unsigned int m); void operate( State q, unsigned int n, Func f, unsigned long z); void qfft( State q, int inv); void fft( double _Complex *X, unsigned long N, int inv); // void printb( unsigned long x, unsigned int n); void print( const char *msg, State q, int option); void error( const char *msg); // double abs2( double _Complex x); void *emalloc( unsigned long size);