The quantum Fourier transform is an implementation of the discrete Fourier transform on a quantum circuit. I will leave the detailed explanation to reference books and other sites, and here I would like to implement it with Qiskit. It's completely memorial, so don't worry about the details. In the future, I would like to implement other algorithms etc. using this code.
code
python
# coding: utf-8
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
from math import log2, pi
import numpy as np
class QFT:
def __init__(self, numQubit):
self.numQubit = numQubit
def construct_circuit(self, circuit=None, inverse=False):
if circuit is None:
circuit = QuantumCircuit(range(self.numQubit))
if not inverse:
self._input_initial_state(circuit)
self._qft(circuit)
self._swap_registers(circuit)
return circuit
elif inverse:
self._swap_registers(circuit)
self._qft(circuit, inverse=True)
self._input_initial_state(circuit, inverse=True)
return circuit
def _input_initial_state(self, circuit, inverse=False):
if not inverse:
for j in range(self.numQubit):
circuit.h(j)
circuit.u1(-pi / float(2 ** j), j)
elif inverse:
for j in range(self.numQubit):
circuit.u1(-pi / float(2 ** j), j)
circuit.h(j)
def _qft(self, circuit, inverse=False):
if not inverse:
for j in range(self.numQubit):
circuit.h(j)
for k in range(j + 1, self.numQubit):
circuit.cu1(pi / float(2 ** (k - j)), k, j)
elif inverse:
for j in range(self.numQubit).__reversed__():
for k in range(j + 1, self.numQubit).__reversed__():
circuit.cu1(pi / float(2 ** (k - j)), k, j)
circuit.h(j)
circuit.barrier()
def _swap_registers(self, circuit):
for j in range(int(np.floor(self.numQubit / 2.))):
circuit.swap(j, self.numQubit - j - 1)
Recommended Posts