Implementation of controlled quantum_gate1

René Schubotz schubotz at gmx.de
Tue Mar 27 04:23:17 CEST 2007


Hi!

I implemented an interface for arbitrary controlled 2-qubit operations:

1 0 0 0
0 1 0 0
0 0 a b
0 0 c d

It works fine for me. For those interested, here are the sources:

== snip ====================================================
void quantum_gate2(int control, int target, quantum_matrix m, 
quantum_reg *reg)
{
   int i, j, k, iset, tset;
   int addsize=0, decsize=0;
   COMPLEX_FLOAT t, tnot=0;
   float limit;
   char *done;

   if ((m.cols != 2) || (m.rows != 2))
   {
       printf("Matrix is not a 2x2 matrix!\n");
       exit(1);
   }

   /* calculate the number of basis states to be added */
   for(i=0; i<reg->size; i++)
   {
     if (reg->node[i].state & ((MAX_UNSIGNED) 1 << control))
     {
       addsize++;
     }
   }

   /* allocate memory for the new basis states */
   reg->node = realloc(reg->node, ((reg->size)+addsize) * 
sizeof(quantum_reg_node));
   if(!reg->node)
   {
     printf("Not enough memory for %i-sized qubit!\n", reg->size + addsize);
     exit(1);
   }
   quantum_memman(addsize*sizeof(quantum_reg_node));

   /* inite new basis states */
   for(i=0; i<addsize; i++)
   {
     reg->node[i+reg->size].state = 0;
     reg->node[i+reg->size].amplitude = 0;
   }
   k = reg->size;

   /* perform the actual matrix multiplication */
   for(i=0; i<reg->size; i++)
   {
     /* determine if the control of the basis state is set */
     if(reg->node[i].state & ((MAX_UNSIGNED) 1 << control))
     {
       /* determine if the target of the basis state is set */
       tset = reg->node[i].state & ((MAX_UNSIGNED) 1 << target);

       /* get amplitude of basis state */
       t = reg->node[i].amplitude;

       if (tset)
       {
         reg->node[i].amplitude = m.t[3] * t;
         reg->node[k].amplitude = m.t[1] * t;
         reg->node[k].state = reg->node[i].state ^ ((MAX_UNSIGNED) 1 << 
target);
       }
       else if (!tset)
       {
         reg->node[i].amplitude = m.t[0] * t;
         reg->node[k].amplitude = m.t[2] * t;
         reg->node[k].state = reg->node[i].state ^ ((MAX_UNSIGNED) 1 << 
target);
       }
       else
       {
         printf("ERROR\n");
         abort();
       }
       k++;
	}
   }

   /* now increase reg->size */
   reg->size += addsize;

   /* remove basis states with extremely small amplitude */
   limit = (1.0 / ((MAX_UNSIGNED) 1 << reg->width)) / 1000000;

   for(i=0, j=0; i<reg->size; i++)
   {
     if(quantum_prob_inline(reg->node[i].amplitude) < limit)
     {
       j++;
       decsize++;
     }
     else if(j)
     {
       reg->node[i-j].state = reg->node[i].state;
       reg->node[i-j].amplitude = reg->node[i].amplitude;
     }
   }

   if(decsize)
   {
       reg->size -= decsize;
       reg->node = realloc(reg->node, reg->size * sizeof(quantum_reg_node));
       if(!reg->node)
	{
	  printf("!!Not enough memory for %i-sized qubit!\n",
		 reg->size + addsize);
	  exit(1);
	}
     quantum_memman(-decsize * sizeof(quantum_reg_node));
   }

   /* rebuild hash table */
   for(i=0; i<(1 << reg->hashw); i++)
   {
     reg->hash[i] = 0;
   }

   for(i=0; i<reg->size; i++)
   {
     quantum_add_hash(reg->node[i].state, i, reg);
   }

   /* decohere */
   quantum_decohere(reg);
}
== snap ====================================================

Regards, René



More information about the libquantum-users mailing list