1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Qiskit: 量子フーリエ変換

Posted at

概要

量子フーリエ変換とは,離散フーリエ変換を量子回路上に実装したものです.
詳しい解説は参考書や他のサイトに任せて,ここではQiskitでの実装を行っていきたいと思います.
なお,完全に備忘録的な感じなので細かいところは気にしないでください.
今後はこのcodeを用いて他のアルゴリズム等を実装していきたいですね.

code

# 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)

参考文献

[1]Qiskit textbook

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?