// single-qubit state and operations // #include #include // sqrt() #include // rand(), RAND_MAX #include "Q1.h" using namespace std; Q1::Q1() { a0 = 1; a1 = 0; } void Q1::print(const char msg[]) const { cout << msg << ":" << endl; cout << " 0 " << a0*a0 << " (" << a0 << ")" << endl; cout << " 1 " << a1*a1 << " (" << a1 << ")" << endl; } void Q1::X() { double t0 = a0, t1 = a1; a0 = t1; a1 = t0; } // NOT int Q1::M() // measure { double v, P = rand()/(RAND_MAX+1.0), R = a0*a0; if( P < R) { v = 0; a0 = 1; a1 = 0; } else { v = 1; a0 = 0; a1 = 1; } return v; } void Q1::Z() { a1 = -a1; } // phase-flip void Q1::H() // Hadamard { double s2 = sqrt(2)/2, t0 = a0, t1 = a1; a0 = s2*(t0+t1); a1 = s2*(t0-t1); }