next up previous contents
Next: dft.qcl Up: The Shor Algorithm in Previous: functions.qcl   Contents


qufunct.qcl

set allow-redefines 1;

// pseudo classic operator to swap bit order

qufunct flip(qureg q) { 
  int i;                // declare loop counter
  for i=0 to #q/2-1 {   // swap 2 symmetric bits
    Swap(q[i],q[#q-i-1]);
  }
}  

// Conditional Xor

qufunct cxor(quconst a,qureg b,quconst e) {
  int i;
  for i=0 to #a-1 {
    CNot(b[i],a[i] & e);
  }
}

// Conditional multiplexed binary adder for one of 2 classical 
// bits and 1 qubit.
// Full adder if #sum=2, half adder if #sum=1.

qufunct muxaddbit(boolean a0,boolean a1,quconst sel,
                  quconst b,qureg sum,quconst e) {
  qureg s=sel;                          // redeclare sel as qureg

  if (a0 xor a1) {                      // a0 and a1 differ?
    if a0  { Not(s); }                  // write a into sect qubit
    if #sum>1 {                         // set carry if available
      CNot(sum[1],sum[0] & s & e);
    }  
    CNot(sum[0],s & e);                 // add a
    if a0  { Not(s); }                  // restore sect qubit
  } else {
    if a0 and a1 { 
      if #sum>1 {                       // set carry if available
        CNot(sum[1],sum[0] & e);   
      } 
      CNot(sum[0],e);                   // add a
    }
  };
                                        // Add qubit b  
  if #sum>1 {                           // set carry if available
    CNot(sum[1],b & sum[0]); 
  }
  CNot(sum[0],b);                       // add b
}
    
// conditional multiplexed binary adder for one of 2 integers 
// and 1 qureg. No output carry.

qufunct muxadd(int a0,int a1,qureg sel,quconst b,quvoid sum,quconst e) { 
  int i;
  for i=0 to #b-2 {                     // fulladd first #b-1 bits
    muxaddbit(bit(a0,i),bit(a1,i),sel,b[i],sum[i:i+1],e);
  }
                                        // half add last bit
  muxaddbit(bit(a0,#b-1),bit(a1,#b-1),sel,b[#b-1],sum[#b-1],e);
}

// Comparison operator. flag is toggled if b<a. 
// b gets overwritten. Needs a #b-1 qubit junk register j 
// as argument which is left dirty.

qufunct lt(int a,qureg b,qureg flag,quvoid j) {
  int i;
  if bit(a,#b-1) {               // disable further comparison
    CNot(j[#b-2],b[#b-1]);       // and set result flag if
    Not(b[#b-1]);                // MSB(a)>MSB(b)
    CNot(flag,b[#b-1]);
  } else {
    Not(b[#b-1]);                // disable further comparison
    CNot(j[#b-2],b[#b-1]);       // if MSB(a)<MSB(b)
  }
  for i=#b-2 to 1 step -1 {      // continue for lower bits
    if bit(a,i) {                // set new junk bit if undecided
      CNot(j[i-1],j[i] & b[i]);
      Not(b[i]);                 // honor last junk bit and
      CNot(flag,j[i] & b[i]);    // set result flag if a[i]>b[i]
    } else {
      Not(b[i]);
      CNot(j[i-1],j[i] & b[i]);
    }
  }
  if bit(a,0) {
    Not(b[0]);                   // if still undecided (j[0]=1)
    CNot(flag,j[0] & b[0]);      // result is LSB(a)>LSB(b)
  }
}

set allow-redefines 0;




(c) Bernhard Ömer - oemer@tph.tuwien.ac.at - http://tph.tuwien.ac.at/~oemer/