Searching With Quantum Computers 
by Lov K. Grover

 
Example 1:

int random();  /* random(N) returns random number between 0 and (N-1) */ 
int f();   /* f(r) is an arbitrary binary function that returns 1 only  
           for a single value of r, for all other values of r it returns 0 */ 
main() 
{ 
   int i, r, answer = -1; 
   r = random(N); 
   for(i = 0; i < N; i++) 
   { 
       if (f(r) == 1)    
           answer = r; 
       r = random(N); 
    } 
    print(answer); 
} 


Example 2:

int qrandom(); 
/* qrandom(N,r) returns a random number between 0 and (N-1) */ 
int f(); 
/* f(r) is an arbitrary binary function that returns 1 only  
     for a single value of r, for all other values of r it returns 0 */ 
quantum_main() 
{ 
   int i, r; 
   r = qrandom(N,0); 
   for(i = 0; i < eta; i++)  /* eta is a number  */ 
   { 
      if (f(r) == 1)  
         invert_phase(r);  /* f(r) is evaluated within the program itself  
                           this does not need an external observation  
                           (any observation would perturb the system) */ 
      r = qrandom(N,r); 
      invert_phase(0); 
      r = qrandom(N,r); 
   } 
   print(r); 
} 

Example 3: 

r = qrandom(4,0); 
if (f(r) == 1)  invert_phase(r); 
r = qrandom(4,r); 
invert_phase(0); 
r = qrandom(4,r); 
print(r); 






1

