Repository: Qiskit/qiskit

Issue:
Shor.construct_circuit() takes a long time
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: Tried on both 0.17.0 & Master branch
- **Python version**: 3.7.7
- **Operating system**: OSX

### What is the current behavior?

```
from qiskit.algorithms import Shor
shor = Shor()
qc = shor.construct_circuit(15, a=13)
```
Takes a _very_ long time for a short circuit (I haven't actually seen it complete yet). When I interrupt, it is always stuck on the same line:
```
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-18e3f4454be5> in <module>
      1 from qiskit.algorithms import Shor
      2 shor = Shor()
----> 3 qc = shor.construct_circuit(15, a=13)

/usr/local/anaconda3/lib/python3.7/site-packages/qiskit/algorithms/factorizers/shor.py in construct_circuit(self, N, a, measurement)
    242         # the report in order to create the exponentiation
    243         for i, ctl_up in enumerate(self._up_qreg):  # type: ignore
--> 244             a = int(pow(a, pow(2, i)))
    245             controlled_multiple_mod_N = self._controlled_multiple_mod_N(
    246                 len(self._down_qreg) + len(self._aux_qreg) + 1, N, a,

KeyboardInterrupt:
```


### Steps to reproduce the problem

```
from qiskit.algorithms import Shor
shor = Shor()
qc = shor.construct_circuit(15, a=13)
```

### What is the expected behavior?

Method completes in a reasonable amount of time.


### Suggested solutions

The loop with this line in it seems to grow much larger than exponentially. I added a couple of lines in the method to print the log of `a` with each iteration:

Out:
```
2
5
20
164
2626
84048
5379088
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-e9ec41b9fa14> in <module>
      1 from qiskit.algorithms import Shor
      2 shor = Shor()
----> 3 shor.construct_circuit(15, 13)

~/workspace/terra-fork/qiskit/algorithms/factorizers/shor.py in construct_circuit(self, N, a, measurement)
    243         from math import log
    244         for i, ctl_up in enumerate(self._up_qreg):  # type: ignore
--> 245             a = int(pow(a, pow(2, i)))
    246             print(int(log(a)))
    247             controlled_multiple_mod_N = self._controlled_multiple_mod_N(

KeyboardInterrupt: 
```
So I think this is probably the root of the problem.


Discussion:

Resolving patch:
diff --git a/qiskit/algorithms/factorizers/shor.py b/qiskit/algorithms/factorizers/shor.py
--- a/qiskit/algorithms/factorizers/shor.py
+++ b/qiskit/algorithms/factorizers/shor.py
@@ -241,9 +241,9 @@ def construct_circuit(self,
         # Apply the multiplication gates as showed in
         # the report in order to create the exponentiation
         for i, ctl_up in enumerate(self._up_qreg):  # type: ignore
-            a = int(pow(a, pow(2, i)))
+            a_aux = int(pow(a, pow(2, i)))
             controlled_multiple_mod_N = self._controlled_multiple_mod_N(
-                len(self._down_qreg) + len(self._aux_qreg) + 1, N, a,
+                len(self._down_qreg) + len(self._aux_qreg) + 1, N, a_aux,
             )
             circuit.append(
                 controlled_multiple_mod_N, [ctl_up, *self._down_qreg, *self._aux_qreg]


Test patch: