// quantum computing emulation: addition // // q = (a,b) -> (a,(a+b)%N) // // q represents qn qubits: a = top n bits, b = bottom m bits, qn = n + m, N = 2**m // // Usage: add [qn [n]] // // R. Perry, Jan. 2018 // #include #include // atoi() #include #include "qce.h" // classical add // unsigned long f_add( unsigned long a, unsigned long b, unsigned long N) { return (a + b) % N; } //--------------------------------------------------------------------- int main( int argc, char *argv[]) { unsigned int qn = 2; if( argc > 1) qn = atoi(argv[1]); // number of qubits in q if( qn == 0) error( "add: bad qn arg"); int n = 0; if( argc > 2) n = atoi(argv[2]); // number of bits in a if( n <= 0) n = qn >> 1; unsigned long N = 1LU << (qn - n); // modulus for the addition printf( "add: qn = %u, n = %i, N = %lu\n", qn, n, N); State q = Q(qn); // set q.a[i] = i so we can easily see where the values end up // for( unsigned long i = 0; i < q.N; ++i) q.a[i] = i; print( "q", q, 1); operate( q, n, f_add, N); print( "add", q, 1); return 0; }