fastflair / Tutorials

Tutorials regarding technologies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: `Shor` algo: missing insertion of the inverse qft

MattePalte opened this issue · comments

First of all thanks for the cool material you shared! I might have found a problem in the implementation of the shor algorithm.

Environment

  • qiskit.version: 0.19.2
  • numpy.version: 1.19
  • Python version: 3.8.0
  • Operating system: Ubuntu 18.04

What is happening?

In the file Shor.py, in the function construct_circuit, the line circuit.compose(iqft, qubits=self._up_qreg) doesn't effectively add the inverse of the quantum fourier transform to the circuit.

How can we reproduce the issue?

I inserted two prints in the around this function call:

class Shor():

    def construct_circuit(self):
        ...

        # BEFORE COMPOSE
        before_circuit = circuit.qasm()

        # Apply inverse QFT
        iqft = QFT(len(self._up_qreg), inverse=True)
        circuit.compose(iqft, qubits=self._up_qreg)

        # AFTER COMPOSE
        after_circuit = circuit.qasm()

        # THE CIRCUIT SHOULD HAVE CHANGED:
        assert before_diagram != after_diagram, 'failed... they are the same'

        ...

Then I run it via:

Shor().construct_circuit()

added at the end of the class.

But I get:

  File "Shor.py", line 330, in construct_circuit
    assert before_diagram != after_diagram, 'failed... they are the same'
AssertionError: failed... they are the same

And the two qasm representations are identical, thus the circuit hasn't changed.

What should happen?

I would have expected the circuit to include the inverse quantum fourier transform.

Any suggestions?

The problem is in the way compose() is used. See here.
The correct way to use it is:

circuit = circuit.compose(iqft, qubits=self._up_qreg)
# or
circuit.compose(iqft, qubits=self._up_qreg, inplace=True)