Skip to content

Commit

Permalink
Inverse QFT - Implementation and Test (#15)
Browse files Browse the repository at this point in the history
Implement and test inverse QFT
  • Loading branch information
vontell authored and willzeng committed Jan 30, 2017
1 parent 3d2c75a commit a85f2e1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 15 deletions.
56 changes: 41 additions & 15 deletions grove/qft/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ def bit_reversal(qubits):
p.inst(SWAP(qubits[i], qubits[-i - 1]))
return p

def _core_qft(qubits, coeff):
"""
Generates the core program to perform the quantum Fourier transform
:param qubits: A list of qubit indexes.
:param coeff: A modifier for the angle used in rotations (-1 for inverse
QFT, 1 for QFT)
:return: A Quil program to compute the core (inverse) QFT of the qubits.
"""

q = qubits[0]
qs = qubits[1:]
if 1 == len(qubits):
return [H(q)]
else:
n = 1 + len(qs)
cR = []
for idx, i in enumerate(xrange(n - 1, 0, -1)):
q_idx = qs[idx]
angle = math.pi / 2 ** (n - i)
cR.append(CPHASE(coeff * angle)(q, q_idx))
return _core_qft(qs, coeff) + list(reversed(cR)) + [H(q)]

def qft(qubits):
"""
Expand All @@ -42,23 +64,27 @@ def qft(qubits):
:return: A Quil program to compute the Fourier transform of the qubits.
"""

def core_qft(qubits):
q = qubits[0]
qs = qubits[1:]
if 1 == len(qubits):
return [H(q)]
else:
n = 1 + len(qs)
cR = []
for idx, i in enumerate(xrange(n - 1, 0, -1)):
q_idx = qs[idx]
angle = math.pi / 2 ** (n - i)
cR.append(CPHASE(angle)(q, q_idx))
return core_qft(qs) + list(reversed(cR)) + [H(q)]

p = pq.Program().inst(core_qft(qubits))
p = pq.Program().inst(_core_qft(qubits, 1))
return p + bit_reversal(qubits)


def inverse_qft(qubits):
"""
Generate a program to compute the inverse quantum Fourier transform on
a set of qubits.
:param qubits: A list of qubit indexes.
:return: A Quil program to compute the inverse Fourier transform of the
qubits.
"""

qft_result = pq.Program().inst(_core_qft(qubits, -1))
qft_result += bit_reversal(qubits)
inverse_qft = pq.Program()
while len(qft_result.actions) > 0:
new_inst = qft_result.actions.pop()[1]
inverse_qft.inst(new_inst)
return inverse_qft

if __name__ == '__main__':
print qft([0, 1, 2, 3])
46 changes: 46 additions & 0 deletions grove/qft/tests/test_qft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
##############################################################################
# Copyright 2016-2017 Rigetti Computing
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################

from pyquil.gates import *
from grove.qft.fourier import inverse_qft
import pyquil.quil as pq
from grove.pyqaoa.utils import compare_progs # This would be nice to have in
# general purpose Util module

def test_simple_inverse_qft():

trial_prog = pq.Program()
trial_prog.inst(X(0))
trial_prog = trial_prog + inverse_qft([0])

result_prog = pq.Program().inst([X(0), H(0)])

compare_progs(trial_prog, result_prog)

def test_multi_qubit_qft():

trial_prog = pq.Program()
trial_prog.inst(X(0), X(1), X(2))
trial_prog = trial_prog + inverse_qft([0, 1, 2])

result_prog = pq.Program().inst([X(0), X(1), X(2),
SWAP(0, 2), H(0),
CPHASE(-1.5707963267948966, 0, 1),
CPHASE(-0.7853981633974483, 0, 2),
H(1), CPHASE(-1.5707963267948966, 1, 2),
H(2)])

compare_progs(trial_prog, result_prog)

0 comments on commit a85f2e1

Please sign in to comment.