| 98 | |
| 99 | template <typename rand_type> |
| 100 | bool measure_bit ( |
| 101 | int bit, |
| 102 | rand_type& rnd |
| 103 | ) |
| 104 | { |
| 105 | // make sure requires clause is not broken |
| 106 | DLIB_CASSERT(0 <= bit && bit < num_bits(), |
| 107 | "\tbool quantum_register::measure_bit()" |
| 108 | << "\n\tinvalid arguments to this function" |
| 109 | << "\n\tbit: " << bit |
| 110 | << "\n\tnum_bits(): " << num_bits() |
| 111 | << "\n\tthis: " << this |
| 112 | ); |
| 113 | |
| 114 | const bool value = (rnd.get_random_double() < probability_of_bit(bit)); |
| 115 | |
| 116 | // Next we set all the states where this bit doesn't have the given value to 0 |
| 117 | |
| 118 | // But first make a mask that selects our bit |
| 119 | unsigned long mask = 1; |
| 120 | for (int i = 0; i < bit; ++i) |
| 121 | mask <<= 1; |
| 122 | |
| 123 | // loop over all the elements in the state vector and zero out those that |
| 124 | // conflict with the measurement we just made. |
| 125 | for (long r = 0; r < state.nr(); ++r) |
| 126 | { |
| 127 | const unsigned long field = r; |
| 128 | // if this state indicates that the bit should be set and it isn't |
| 129 | if ((field & mask) && !value) |
| 130 | { |
| 131 | state(r) = 0; |
| 132 | } |
| 133 | // else if this state indicates that the bit should not be set and it is |
| 134 | else if (!(field & mask) && value) |
| 135 | { |
| 136 | state(r) = 0; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // normalize the state |
| 141 | state = state/(std::sqrt(sum(norm(state)))); |
| 142 | |
| 143 | return value; |
| 144 | } |
| 145 | |
| 146 | template <typename rand_type> |
| 147 | bool measure_and_remove_bit ( |