From my understanding Shor's algorithm can be summarised as follows...
Task: given a positive integer $N$ find another positive integer $s$ such that $s$ divides $N$.
Steps (informal)
- Choose an integer $a$ uniformly at random from the interval $[1, N]$
- Compute $b = \gcd(a, N)$. If $b > 1$, then $b$ divides $N$. Otherwise continue to 3.
- Determine the period of the function $f(x) = a^x \,\text{mod} \,N$. In other words, find the least positive integer $r$ such that $f(x+r)=f(x)$. Note that this is the part that uses the quantum computer. This is typically done with quantum phase estimation.
- Now if $a^{r/2}$ is an integer evaluate $s=\gcd(a^{r/2}-1, N)$. If $a^{r/2}$ is not an integer our algorithm fails. Likewise if $s=1$, our algorithm hasn't worked and we try need to try again. If $s \neq 1$, then $s$ is a factor of $N$ and our algorithm has worked.
Note that the steps above give you a single non-trivial factor of $N$. In the textbook version of Shor's algorithm, we usually have to repeat the above to get all the factors of $N$.
My question is mostly about step 1 where we choose $a$.
A key part of the algorithm is this difference of two squares factorisation.
$$ a^r -1 = (a^{r/2} -1)(a^{r/2} + 1) = 0 \,\, \text{mod} \, N $$
Now if $a^{r/2}$ is not an integer, then we're in trouble as $\gcd(a^{r/2}-1, N)$ is not defined. This is usually the case when our quantum computer gives us an odd number for the period $r$. However in the edge case where $r$ is odd but $a$ is a perfect square the algorithm can still succeed. This is becuase if $a=j^2$ for some positive integer $j$, then $a^{r/2}=(j^2)^{r/2} =j^r$.
I'm asking the following... What is the reason for choosing $a$ uniformly at random from the positive integers less than $N$? Have people considered other distributions from which to sample $a$?
A trivial way to choose $a$ would be to sample from the set of all of the perfect squares less than $N$. This would mean that $a^{r/2}$ is always an integer regardless of the value of the period $r$. Surely it cannot be this simple and I must have misunderstood something or missed some other requirements for the algorithm to succeed. Perhaps sampling from contrived distributions makes it less likely to find non-trivial factors of $N$? I'm not sure.
Example
Consider the basic example of factoring $N=21$. Suppose our random choice in step 1. gives us $a=4$.
Now we have to determine the period of the function $f(x) = 4^x \, \text{mod} \, 21$. We can determine that the period is $r=3$ by evaluating $f(x)$ on a range of inputs.
Now normally the fact that $r$ is odd would cause problems. However in this case $a^{r/2}=4^{3/2}=8$ (an integer as $4$ is a perfect square).
Now we see that $\gcd(a^{r/2}-1,\, N)=\gcd(7,\, 21)=7$. Clearly $7$ divides $21$. So we have found that $21 = 7 \times 3$.