Ejemplo n.º 1
0
int main ()
{
  quantum_reg reg;
  int result;

  srand(time(0));

  reg = quantum_new_qureg(0, 1);

  quantum_hadamard(0, &reg);

  result = quantum_bmeasure(0, &reg);

  printf("The Quantum RNG returned %i!\n", result);

  return 0;
}
int main(int argc, char **argv) {

  quantum_reg qr;
  int i;
  int width, swidth;
  int x = 0;
  int N;
  int c,q,a,b, factor;

#if defined(SPEC_CPU)
	spec_srand(26);			
#else
	srandom(time(0));
#endif /* SPEC_CPU */

  if(argc == 1)
    {
      printf("Usage: shor [number]\n\n");
      return 3;
    }

  N=atoi(argv[1]);

  if(N<15)
    {
      printf("Invalid number\n\n");
      return 3;
    }

  width=quantum_getwidth(N*N);
  swidth=quantum_getwidth(N);

  printf("N = %i, %i qubits required\n", N, width+3*swidth+2);

  if(argc >= 3)
    {
      x = atoi(argv[2]);
    }
  while((quantum_gcd(N, x) > 1) || (x < 2))
    {
 #if defined(SPEC_CPU)
	x = (long)(spec_rand() * 2147483647L) % N;        
 #else
	x = random() % N;
 #endif /* SPEC_CPU */
    } 

  printf("Random seed: %i\n", x);

  qr=quantum_new_qureg(0, width);

  for(i=0;i<width;i++)
    quantum_hadamard(i, &qr);

  quantum_addscratch(3*swidth+2, &qr);

  quantum_exp_mod_n(N, x, width, swidth, &qr);

  for(i=0;i<3*swidth+2;i++)
    {
      quantum_bmeasure(0, &qr);
    }

  quantum_qft(width, &qr); 
  
  for(i=0; i<width/2; i++)
    {
      quantum_cnot(i, width-i-1, &qr);
      quantum_cnot(width-i-1, i, &qr);
      quantum_cnot(i, width-i-1, &qr);
    }
  
  c=quantum_measure(qr);

  if(c==-1)
    {
      printf("Impossible Measurement!\n");
      exit(1);
    }

  if(c==0)
    {
      printf("Measured zero, try again.\n");
      exit(2);
    }

  q = 1<<(width);

  printf("Measured %i (%f), ", c, (float)c/q);

  quantum_frac_approx(&c, &q, width);

  printf("fractional approximation is %i/%i.\n", c, q);

  if((q % 2 == 1) && (2*q<(1<<width)))
    {
      printf("Odd denominator, trying to expand by 2.\n");
      q *= 2;
    }
    
  if(q % 2 == 1)
    {
      printf("Odd period, try again.\n");
      exit(2);
    }

  printf("Possible period is %i.\n", q);
  
  a = quantum_ipow(x, q/2) + 1 % N;
  b = quantum_ipow(x, q/2) - 1 % N;
  
  a = quantum_gcd(N, a);
  b = quantum_gcd(N, b);
  
  if(a>b)
    factor=a;
  else
    factor=b;

  if((factor < N) && (factor > 1))
    {
      printf("%i = %i * %i\n", N, factor, N/factor);
    }
  else
    {
      printf("Unable to determine factors, try again.\n");
#if defined(SPEC_CPU)
	exit(0);
#else
	exit(2);
#endif /* SPEC_CPU */
    }
    
  quantum_delete_qureg(&qr);

  /*  printf("Memory leak: %i bytes\n", (int) quantum_memman(0)); */

  return 0;
}
Ejemplo n.º 3
0
void
quantum_objcode_run(char *file, quantum_reg *reg)
{
  int i, j, k, l;
  FILE *fhd;
  unsigned char operation;
  unsigned char buf[OBJBUF_SIZE];
  MAX_UNSIGNED mu;
  double d;

  fhd = fopen(file, "r");

  if(!fhd)
    {
      fprintf(stderr, "quantum_objcode_run: Could not open %s: ", file);
      perror(0);
      return;
    }

  for(i=0; !feof(fhd); i++)
    {
      for(j=0; j<OBJBUF_SIZE; j++)
	buf[j] = 0;
      
      operation = fgetc(fhd);
      switch(operation)
	{
	case INIT:
	  if(!fread(buf, sizeof(MAX_UNSIGNED), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  mu = quantum_char2mu(buf);
	  *reg = quantum_new_qureg(mu, 12);

	  break;

	case CNOT:
	case COND_PHASE:
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  j = quantum_char2int(buf);
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  
	  k = quantum_char2int(buf);
	  switch(operation)
	    {
	    case CNOT: quantum_cnot(j, k, reg);
	      break;
	    case COND_PHASE: quantum_cond_phase(j, k, reg);
	      break;
	    }
	  break;

	case TOFFOLI:
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  j = quantum_char2int(buf);
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  k = quantum_char2int(buf);
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  l = quantum_char2int(buf);
	  quantum_toffoli(j, k, l, reg);
	  break;

	case SIGMA_X:
	case SIGMA_Y:
	case SIGMA_Z:
	case HADAMARD:
	case BMEASURE:
	case BMEASURE_P:
	case SWAPLEADS:
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  j = quantum_char2int(buf);
	  switch(operation)
	    {
	    case SIGMA_X: quantum_sigma_x(j, reg);
	      break;
	    case SIGMA_Y: quantum_sigma_y(j, reg);
	      break;
	    case SIGMA_Z: quantum_sigma_z(j, reg);
	      break;
	    case HADAMARD: quantum_hadamard(j, reg);
	      break;
	    case BMEASURE: quantum_bmeasure(j, reg);
	      break;
	    case BMEASURE_P: quantum_bmeasure_bitpreserve(j, reg);
	      break;
	    case SWAPLEADS: quantum_swaptheleads(j, reg);
	      break;
	    }
	  break;

	case ROT_X:
	case ROT_Y:
	case ROT_Z:
	case PHASE_KICK:
	case PHASE_SCALE:
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  j = quantum_char2int(buf);
	  if(!fread(buf, sizeof(double), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  d = quantum_char2double(buf);
	  switch(operation)
	    {
	    case ROT_X: quantum_r_x(j, d, reg);
	      break;
	    case ROT_Y: quantum_r_y(j, d, reg);
	      break;
	    case ROT_Z: quantum_r_z(j, d, reg);
	      break;
	    case PHASE_KICK: quantum_phase_kick(j, d, reg);
	      break;
	    case PHASE_SCALE: quantum_phase_scale(j, d, reg);
	      break;
	    }
	  break;

	case CPHASE_KICK:
	  if(!fread(buf, sizeof(int), 1, fhd))	
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  j = quantum_char2int(buf);
	  if(!fread(buf, sizeof(int), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  k = quantum_char2int(buf);
	  if(!fread(buf, sizeof(double), 1, fhd))
	    {
	      quantum_error(QUANTUM_FAILURE);
	      break;
	    }
	  d = quantum_char2double(buf);
	  quantum_cond_phase_kick(j, k, d, reg);
	  break;
	  
	case MEASURE: quantum_measure(*reg);
	  break;

	case NOP:
	  break;

	default:
	  fprintf(stderr, "%i: Unknown opcode 0x(%X)!\n", i, operation);
	  return;
	}

    }

  fclose(fhd);

}