1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/* shor.c: Implementation of Shor's factoring algorithm
Copyright 2003 Bjoern Butscher, Hendrik Weimer
This file is part of libquantum
libquantum is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
libquantum is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with libquantum; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <quantum.h>
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;
srand(time(0));
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))
{
x = rand() % N;
}
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");
return 1;
}
if(c==0)
{
printf("Measured zero, try again.\n");
return 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");
return 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");
return 2;
}
quantum_delete_qureg(&qr);
/* printf("Memory leak: %i bytes\n", (int) quantum_memman(0)); */
return 0;
}
|