Ejemplo n.º 1
0
quantum_reg quantum_new_qureg( unsigned long long initval, int width )
{
  quantum_reg reg;
  char *c;
  reg.width = width;
  reg.size = 1;
  reg.hashw = width + 2;
  reg.node = calloc( 1, sizeof( quantum_reg_node ) );
  if ( reg.node == 0 )
    quantum_error( 2 );
  quantum_memman( 16 );
  reg.hash = calloc( 1 << reg.hashw, sizeof( int ) );
  if ( reg.hash == 0 )
    quantum_error( 2 );
  quantum_memman( 4 << reg.hashw );
  reg.node->state = initval;
  reg.node->amplitude = 1.000000000000;
  c = getenv( "QUOBFILE" );
  if ( c != 0 )
  {
    quantum_objcode_start( );
    quantum_objcode_file( c );
    atexit( &quantum_objcode_exit );
  }
  quantum_objcode_put( 0, initval );
  return reg;
}
Ejemplo n.º 2
0
quantum_reg quantum_kronecker( quantum_reg *reg1, quantum_reg *reg2 )
{
  int i, j;
  quantum_reg reg;
  reg.width = reg2->width + reg1->width;
  reg.size = reg2->size * reg1->size;
  reg.hashw = ( reg2->size * reg1->size ) + 2;
  reg.node = calloc( reg.size, sizeof( quantum_reg_node ) );
  if ( reg.node == 0 )
    quantum_error( 2 );
  quantum_memman( reg.size << 4 );
  reg.hash = calloc( 1 << reg.hashw, sizeof( int ) );
  if ( reg.hash == 0 )
    quantum_error( 2 );
  quantum_memman( 4 << reg.hashw );
  i = 0;
  for ( ; i < reg1->size; i++ )
  {
    j = 0;
    for ( ; j < reg2->size; j++ )
    {
      reg.node[ j + ( reg2->size * i ) ].state = reg2->node[ j ].state | ( reg1->node[ i ].state << reg2->width );
      reg.node[ j + ( reg2->size * i ) ].amplitude = reg2->node[ j ].amplitude * reg1->node[ i ].amplitude;
      //j++;
    }
    //i++;
  }
  return reg;
}
Ejemplo n.º 3
0
void quantum_decohere( quantum_reg *reg )
{
  float u, v, s, x;
  float *nrands;
  float angle;
  int i, j;
  quantum_gate_counter( 1 );
  if ( quantum_status != 0 )
  {
    nrands = calloc( reg->width, sizeof( float ) );
    if ( nrands == 0 )
      quantum_error( 2 );
    quantum_memman( reg->width << 2 );
    i = 0;
    for ( ; i < reg->width; i++ )
    {
      do
      {
        u = ( quantum_frand( ) * (double)( 2 ) ) - 1.000000000000;
        v = ( quantum_frand( ) * (double)( 2 ) ) - 1.000000000000;
        s = ( u * u ) + ( v * v );
      }
      while ( (bit)( 0 ) );
      if ( (bit)( 0 ) )
      {
      }
      x = sqrt( ( log( s ) * -2.000000000000 ) / s ) * u;
      x *= sqrt( quantum_lambda + quantum_lambda );
      nrands[ i ] = x / 2.000000000000;
      //i++;
    }
    i = 0;
    for ( ; i < reg->size; i++ )
    {
      angle = 0.0;
      j = 0;
      for ( ; j < reg->width; j++ )
      {
        if ( reg->node[ i ].state & ( (long long)1 << j ) )
          angle += nrands[ j ];
        else
          angle -= nrands[ j ];
        //j++;
      }
      reg->node[ i ].amplitude *= quantum_cexp( angle );
      //i++;
    }
    free( nrands );
    quantum_memman( reg->width * -4 );
  }
  return;
}
Ejemplo n.º 4
0
void quantum_delete_qureg_hashpreserve( quantum_reg *reg )
{
  free( reg->node );
  quantum_memman( reg->size * -16 );
  reg->node = 0;
  return;
}
Ejemplo n.º 5
0
void quantum_destroy_hash( quantum_reg *reg )
{
  free( reg->hash );
  quantum_memman( -4 << reg->hashw );
  reg->hash = 0;
  return;
}
Ejemplo n.º 6
0
void quantum_delete_matrix( quantum_matrix *m )
{
  free( m->t );
  quantum_memman( m->cols * m->rows * -8 );
  m->t = 0;
  return;
}
Ejemplo n.º 7
0
void
quantum_objcode_stop()
{
  opstatus = 0;
  free(objcode);
  objcode = 0;
  quantum_memman(- allocated * OBJCODE_PAGE * sizeof(char));
  allocated = 0;
}
Ejemplo n.º 8
0
void quantum_delete_qureg( quantum_reg *reg )
{
  if ( reg->hashw != 0 && reg->hash != 0 )
    quantum_destroy_hash( reg );
  free( reg->node );
  quantum_memman( reg->size * -16 );
  reg->node = 0;
  return;
}
Ejemplo n.º 9
0
quantum_reg quantum_state_collapse( int pos, int value, quantum_reg reg )
{
  int i, j, k;
  int size = 0;
  double d = 0.000000000000;
  unsigned long long lpat = 0, rpat = 0, pos2 = (long long)1 << pos;
  quantum_reg out;
  i = 0;
  for ( ; i < reg.size; i++ )
  {
    if ( ( ( reg.node[ i ].state & pos2 ) != 0 && value != 0 ) || ( ( reg.node[ i ].state & pos2 ) == 0 && value == 0 ) )
    {
      d += quantum_prob_inline( reg.node[ i ].amplitude );
      size++;
    }
    //i++;
  }
  out.width = reg.width - 1;
  out.size = size;
  out.node = calloc( size, sizeof( quantum_reg_node ) );
  if ( out.node == 0 )
    quantum_error( 2 );
  quantum_memman( size << 4 );
  out.hashw = reg.hashw;
  out.hash = reg.hash;
  i = 0;
  j = 0;
  for ( ; i < reg.size; i++ )
  {
    if ( ( ( reg.node[ i ].state & pos2 ) != 0 && value != 0 ) || ( ( reg.node[ i ].state & pos2 ) == 0 && value == 0 ) )
    {
      k = 0;
      rpat = 0;
      for ( ; k < pos; k++ )
      {
        rpat += (long long)1 << k;
        //k++;
      }
      rpat &= reg.node[ i ].state;
      k = 63;
      lpat = 0;
      for ( ; pos < k; k-- )
      {
        lpat += (long long)1 << k;
        //k--;
      }
      lpat &= reg.node[ i ].state;
      out.node[ j ].state = rpat | ( lpat >> 1 );
      if ( (bit)( 0 ) )
      {
      }
      out.node[ j ].amplitude = reg.node[ i ].amplitude / sqrt( d );
      j++;
    }
    //i++;
  }
Ejemplo n.º 10
0
quantum_matrix quantum_new_matrix( int cols, int rows )
{
  quantum_matrix m;
  m.rows = rows;
  m.cols = cols;
  m.t = calloc( rows * cols, sizeof( float _Complex ) );
  if ( m.t == 0 )
    quantum_error( 2 );
  quantum_memman( ( rows * cols ) << 3 );
  return m;
}
Ejemplo n.º 11
0
void quantum_copy_qureg( quantum_reg *src, quantum_reg *dst )
{
  dst->width = src->width;
  dst->size = src->size;
  dst->hashw = src->hashw;
  dst->node = src->node;
  dst->hash = src->hash;
  dst->node = calloc( dst->size, sizeof( quantum_reg_node ) );
  if ( dst->node == 0 )
    quantum_error( 2 );
  quantum_memman( dst->size << 4 );
  if ( dst->hashw != 0 )
  {
    dst->hash = calloc( 1 << dst->hashw, sizeof( int ) );
    if ( dst->hash == 0 )
      quantum_error( 2 );
    quantum_memman( 4 << dst->hashw );
  }
  memcpy( dst->node, src->node, src->size << 4 );
  return;
}
Ejemplo n.º 12
0
void
quantum_objcode_start()
{
  opstatus = 1;
  allocated = 1;
  objcode = malloc(OBJCODE_PAGE * sizeof(char));

  if(!objcode)
    quantum_error(QUANTUM_ENOMEM);

  quantum_memman(OBJCODE_PAGE * sizeof(char));
}
Ejemplo n.º 13
0
quantum_reg quantum_new_qureg_size( int n, int width )
{
  quantum_reg reg;
  reg.width = width;
  reg.size = n;
  reg.hashw = 0;
  reg.hash = 0;
  reg.node = calloc( n, sizeof( quantum_reg_node ) );
  if ( reg.node == 0 )
    quantum_error( 2 );
  quantum_memman( n << 4 );
  return reg;
}
Ejemplo n.º 14
0
quantum_reg quantum_matrix2qureg( quantum_matrix *m, int width )
{
  quantum_reg reg;
  int i, j, size = 0;
  if ( m->cols != 1 )
    quantum_error( 65536 );
  reg.width = width;
  i = 0;
  for ( ; i < m->rows; i++ )
  {
    if ( 0 != 1 || 1 == 0 )
      size++;
    //i++;
  }
  reg.size = size;
  reg.hashw = width + 2;
  reg.node = calloc( size, sizeof( quantum_reg_node ) );
  if ( reg.node == 0 )
    quantum_error( 2 );
  quantum_memman( size << 4 );
  reg.hash = calloc( 1 << reg.hashw, sizeof( int ) );
  if ( reg.hash == 0 )
    quantum_error( 2 );
  quantum_memman( 4 << reg.hashw );
  i = 0;
  j = 0;
  for ( ; i < m->rows; i++ )
  {
    if ( 0 != 1 || 1 == 0 )
    {
      reg.node[ j ].state = i;
      reg.node[ j ].amplitude = m->t[ i ];
      j++;
    }
    //i++;
  }
  return reg;
}
Ejemplo n.º 15
0
void quantum_delete_density_op( quantum_density_op *rho )
{
  int i;
  quantum_destroy_hash( rho->reg );
  i = 0;
  for ( ; i < rho->num; i++ )
  {
    quantum_delete_qureg_hashpreserve( &rho->reg[ i ] );
    //i++;
  }
  free( rho->prob );
  free( rho->reg );
  quantum_memman( rho->num * -24 );
  rho->prob = 0;
  rho->reg = 0;
  return;
}
Ejemplo n.º 16
0
quantum_density_op quantum_new_density_op( int num, float *prob, quantum_reg *reg )
{
  int i;
  quantum_density_op rho;
  int *phash;
  int hashw;
  rho.num = num;
  rho.prob = calloc( num, sizeof( float ) );
  if ( rho.prob == 0 )
    quantum_error( 2 );
  rho.reg = calloc( num, sizeof( quantum_reg ) );
  if ( rho.reg == 0 )
    quantum_error( 2 );
  quantum_memman( num * 24 );
  rho.prob[0] = prob[0];
  phash = reg->hash;
  hashw = reg->hashw;
  rho.reg->width = reg->width;
  rho.reg->size = reg->size;
  rho.reg->hashw = reg->hashw;
  rho.reg->node = reg->node;
  rho.reg->hash = reg->hash;
  reg->size = 0;
  reg->width = 0;
  reg->node = 0;
  reg->hash = 0;
  i = 1;
  for ( ; i < num; i++ )
  {
    rho.prob[ i ] = prob[ i ];
    rho.reg[ i ].width = reg[ i ].width;
    rho.reg[ i ].size = reg[ i ].size;
    rho.reg[ i ].hashw = reg[ i ].hashw;
    rho.reg[ i ].node = reg[ i ].node;
    rho.reg[ i ].hash = reg[ i ].hash;
    rho.reg[ i ].hash = phash;
    rho.reg[ i ].hashw = hashw;
    reg[ i ].size = 0;
    reg[ i ].width = 0;
    reg[ i ].node = 0;
    reg[ i ].hash = 0;
    //i++;
  }
  return rho;
}
Ejemplo n.º 17
0
void quantum_reduced_density_op( int pos, quantum_density_op *rho )
{
  int i, j;
  double p0 = 0.000000000000, ptmp;
  unsigned long long pos2;
  quantum_reg rtmp;
  rho->prob = realloc( rho->prob, rho->num << 3 );
  if ( rho->prob == 0 )
    quantum_error( 2 );
  rho->reg = realloc( rho->reg, rho->num * 40 );
  if ( rho->reg == 0 )
    quantum_error( 2 );
  quantum_memman( rho->num * 24 );
  pos2 = (long long)1 << pos;
  i = 0;
  for ( ; i < rho->num; i++ )
  {
    ptmp = rho->prob[ i ];
    rtmp.width = rho->reg[ i ].width;
    rtmp.size = rho->reg[ i ].size;
    rtmp.hashw = rho->reg[ i ].hashw;
    rtmp.node = rho->reg[ i ].node;
    rtmp.hash = rho->reg[ i ].hash;
    p0 = 0.000000000000;
    j = 0;
    for ( ; j < rho->reg[ i ].size; j++ )
    {
      if ( ( rho->reg[ i ].node[ j ].state & pos2 ) == 0 )
      {
        p0 += quantum_prob_inline( rho->reg[ i ].node[ j ].amplitude );
      }
      //j++;
    }
    rho->prob[ i ] = ptmp * p0;
    rho->prob[ rho->num + i ] = ptmp * ( 1.000000000000 - p0 );
    rho->reg[ i ] = quantum_state_collapse( pos, 0, rtmp );
    rho->reg[ rho->num + i ] = quantum_state_collapse( pos, 1, rtmp );
    quantum_delete_qureg_hashpreserve( &rtmp );
    //i++;
  }
  rho->num <<= 1;
  return;
}
Ejemplo n.º 18
0
int
quantum_objcode_put(unsigned char operation, ...)
{
  int i, size = 0;
  va_list args;
  unsigned char buf[80];
  double d;
  MAX_UNSIGNED mu;

  if(!opstatus)
    return 0;

  va_start(args, operation);
  
  buf[0] = operation;
  
  switch(operation)
    {
    case INIT:
      mu = va_arg(args, MAX_UNSIGNED);
      quantum_mu2char(mu, &buf[1]);
      size = sizeof(MAX_UNSIGNED) + 1;
      break;
    case CNOT:
    case COND_PHASE:
      i = va_arg(args, int);
      quantum_int2char(i, &buf[1]);
      i = va_arg(args, int);
      quantum_int2char(i, &buf[sizeof(int)+1]);
      size = 2 * sizeof(int) + 1;
      break;
    case TOFFOLI:
      i = va_arg(args, int);
      quantum_int2char(i, &buf[1]);
      i = va_arg(args, int);
      quantum_int2char(i, &buf[sizeof(int)+1]);
      i = va_arg(args, int);
      quantum_int2char(i, &buf[2*sizeof(int)+1]);
      size = 3 * sizeof(int) + 1;
      break;
    case SIGMA_X:
    case SIGMA_Y:
    case SIGMA_Z:
    case HADAMARD:
    case BMEASURE:
    case BMEASURE_P:
    case SWAPLEADS:
      i = va_arg(args, int);
      quantum_int2char(i, &buf[1]);
      size = sizeof(int) + 1;
      break;
    case ROT_X:
    case ROT_Y:
    case ROT_Z:
    case PHASE_KICK:
    case PHASE_SCALE:
      i = va_arg(args, int);
      d = va_arg(args, double);
      quantum_int2char(i, &buf[1]);
      quantum_double2char(d, &buf[sizeof(int)+1]);
      size = sizeof(int) + sizeof(double) + 1;
      break;
    case CPHASE_KICK:
      i = va_arg(args, int);
      quantum_int2char(i, &buf[1]);
      i = va_arg(args, int);
      quantum_int2char(i, &buf[sizeof(int)+1]);
      d = va_arg(args, double);
      quantum_double2char(d, &buf[2*sizeof(int)+1]);
      size = 2 * sizeof(int) + sizeof(double) + 1;
      break;
    case MEASURE:
    case NOP:
      size = 1;
      break;
    default:
      quantum_error(QUANTUM_EOPCODE);
    }
  
  if((position+size) / OBJCODE_PAGE > position / OBJCODE_PAGE)
    {
      allocated++;
      objcode = realloc(objcode, allocated * OBJCODE_PAGE);

      if(!objcode)
	quantum_error(QUANTUM_ENOMEM);

      quantum_memman(OBJCODE_PAGE * sizeof(char));
    }

  for(i=0; i<size; i++)
    {
      objcode[position] = buf[i];
      position++;
    }

  return 1;
}
int
quantum_bmeasure_bitpreserve(int pos, quantum_reg *reg)
{
  int i, j;
  int size=0, result=0;
  double d=0, pa=0, r;
  MAX_UNSIGNED pos2;
  quantum_reg out;

  if(quantum_objcode_put(BMEASURE_P, pos))
     return 0;

  pos2 = (MAX_UNSIGNED) 1 << pos;

  /* Sum up the probability for 0 being the result */

  for(i=0; i<reg->size; i++)
    {
      if(!(reg->node[i].state & pos2))
	pa += quantum_prob_inline(reg->node[i].amplitude);
    }

  /* Compare the probability for 0 with a random number and determine
     the result of the measurement */

  r = quantum_frand();
  
  if (r > pa)
    result = 1;

  /* Eradicate all amplitudes of base states which have been ruled out
     by the measurement and get the absolute of the new register */

  for(i=0;i<reg->size;i++)
    {
      if(reg->node[i].state & pos2)
	{
	  if(!result)
	    reg->node[i].amplitude = 0;
	  else
	    {
	      d += quantum_prob_inline(reg->node[i].amplitude);
	      size++;
	    }
	}
      else
	{
	  if(result)
	    reg->node[i].amplitude = 0;
	  else
	    {
	      d += quantum_prob_inline(reg->node[i].amplitude);
	      size++;
	    }
	}
    }

  /* Build the new quantum register */

  out.size = size;
  out.node = calloc(size, sizeof(quantum_reg_node));
  if(!out.node)
    {
      printf("Not enough memory for %i-sized qubit!\n", size);
      exit(1);
    }
  quantum_memman(size * sizeof(quantum_reg_node));
  out.hashw = reg->hashw;
  out.hash = reg->hash;
  out.width = reg->width;

  /* Determine the numbers of the new base states and norm the quantum
     register */
  
  for(i=0, j=0; i<reg->size; i++)
    {
      if(reg->node[i].amplitude)
	{
	  out.node[j].state = reg->node[i].state;
	  out.node[j].amplitude = reg->node[i].amplitude * 1 / (float) sqrt(d);
	
	  j++;
	}
    }

  quantum_delete_qureg_hashpreserve(reg);
  *reg = out;
  return result;
}