ProjectQ-Framework / ProjectQ

ProjectQ: An open source software framework for quantum computing
https://projectq.ch
Apache License 2.0
876 stars 271 forks source link

code of QFT gate #371

Closed quantum-rookie closed 4 years ago

quantum-rookie commented 4 years ago

hello, I wonder the function code for QFT and not the direct QFT gate. I tried to get code on my own, but i doesn't right. image

dbretaud commented 4 years ago

Hi, first of all I think that you may be able to deduce n from qubits at the beginning of the function, otherwise it may be possible to pick a bigger n and crash the function.

Otherwise the code seems to run fine for me with the following example:

import cmath
import math

from projectq import MainEngine  # import the main compiler engine
from projectq.ops import H,R, Measure, All  # import the operations we want to perform
from projectq.meta import Control

def QFT(eng,qubits):
    n=len(qubits)
    for i in range(n):
        H | qubits[i]
        for k in range(n-i-1):
            theta = 2*cmath.pi/math.pow(2,k+2)
            with Control(eng,qubits[i+k+1]):
                R(theta) | qubits[i]

eng = MainEngine()  # create a default compiler (the back-end is a simulator)
qubits = eng.allocate_qureg(4)  # allocate 4 qubits for this example
QFT(eng,qubits)
All(Measure) | qubits
eng.flush()  # flush all gates (and execute measurements)
for qubit in qubits:
    print("Measured {}".format(int(qubit)))  # output measurement result

Could you include the rest of your file, as well as the error message you received?

quantum-rookie commented 4 years ago

ok thank you, All the problems have been solved.