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.

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);
}
}
};
if #sum>1 {                           // set carry if available
CNot(sum[1],b & sum[0]);
}
}

// 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
}
}

// 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/