// two-bit state and operations // // #ifndef B2_H_ // #define B2_H_ class B2 { private: int b1, b0; public: B2(); void print(const char msg[]) const; void X(); int M(); // NOT, measure - bit 1 void CX(); // controlled-NOT on bit 0 controlled by bit 1 }; // #endif // B2_H_ // two-bit state and operations // #include // #include "B2.h" using namespace std; B2::B2() { b1 = b0 = 0; } void B2::print(const char msg[]) const { cout << msg << ": " << b1 << " " << b0 << endl; } void B2::X() { b1 = !b1; } // NOT, bit 1 void B2::CX() { if( b1) b0 = !b0; } // controlled-NOT on bit 0 controlled by bit 1 int B2::M() { return b1; } // measure, bit 1 #include // #include "B2.h" using namespace std; int main() { B2 b; b.print("b"); b.CX(); b.print("CX"); b.X(); b.print("X"); b.CX(); b.print("CX"); cout << "m1 = " << b.M() << endl; b.print("b"); }