| 237 | // ---------------------------------------------------------------------------------------- |
| 238 | |
| 239 | int main() |
| 240 | { |
| 241 | // seed the random number generator |
| 242 | rnd.set_seed(cast_to_string(time(0))); |
| 243 | |
| 244 | // Pick out some of the gates we will be using below |
| 245 | using namespace dlib::quantum_gates; |
| 246 | const gate<1> h = quantum_gates::hadamard(); |
| 247 | const gate<1> z = quantum_gates::z(); |
| 248 | const gate<1> x = quantum_gates::x(); |
| 249 | const gate<1> i = quantum_gates::noop(); |
| 250 | |
| 251 | quantum_register reg; |
| 252 | |
| 253 | // We will be doing the 12 qubit version of Grover's search algorithm. |
| 254 | const int bits=12; |
| 255 | reg.set_num_bits(bits); |
| 256 | |
| 257 | |
| 258 | // set the quantum register to its initial state |
| 259 | (i,i, i,i,i,i,i, i,i,i,i,x).apply_gate_to(reg); |
| 260 | |
| 261 | // Print out the starting bits |
| 262 | cout << "starting bits: "; |
| 263 | for (int i = reg.num_bits()-1; i >= 0; --i) |
| 264 | cout << reg.probability_of_bit(i); |
| 265 | cout << endl; |
| 266 | |
| 267 | |
| 268 | // Now apply the Hadamard gate to all the input bits |
| 269 | (h,h, h,h,h,h,h, h,h,h,h,h).apply_gate_to(reg); |
| 270 | |
| 271 | // Here we do the grover iteration |
| 272 | for (int j = 0; j < 35; ++j) |
| 273 | { |
| 274 | (uf_gate<bits>()).apply_gate_to(reg); |
| 275 | (w_gate<bits-1>(),i).apply_gate_to(reg); |
| 276 | |
| 277 | |
| 278 | cout << j << " probability: bit 1 = " << reg.probability_of_bit(1) << ", bit 9 = " << reg.probability_of_bit(9) << endl; |
| 279 | } |
| 280 | |
| 281 | cout << endl; |
| 282 | |
| 283 | // Print out the final probability of measuring a 1 for each of the bits |
| 284 | for (int i = reg.num_bits()-1; i >= 1; --i) |
| 285 | cout << "probability for bit " << i << " = " << reg.probability_of_bit(i) << endl; |
| 286 | cout << endl; |
| 287 | |
| 288 | cout << "The value we want grover's search to find is 257 which means we should measure a bit pattern of 00100000001" << endl; |
| 289 | cout << "Measured bits: "; |
| 290 | // finally, measure all the bits and print out what they are. |
| 291 | for (int i = reg.num_bits()-1; i >= 1; --i) |
| 292 | cout << reg.measure_bit(i,rnd); |
| 293 | cout << endl; |
| 294 | |
| 295 | |
| 296 |
nothing calls this directly
no test coverage detected