2
1

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.

Groverのアルゴリズム

Last updated at Posted at 2021-10-10

#はじめに
GroverのアルゴリズムをQiskitで実装しました.
n量子ビットの場合も実装しています.
なぐり書きなので今後修正していきます.
参考になれば嬉しいです.

Groverのアルゴリズム

グローバーのアルゴリズムは,整列化されていないデータベースから特定のデータを探索するための量子アルゴリズムです.

Qiskitによる実装

全体のPythonコードは以下です.

2量子ビットの場合

import qiskit
from qiskit import QuantumCircuit, Aer, QuantumRegister, IBMQ
from qiskit.providers.aer import noise
from qiskit.tools.visualization import plot_histogram

from qiskit.ignis.mitigation.measurement import complete_meas_cal, CompleteMeasFitter
from qiskit import Aer, execute

#状態重ね合わせ
def superposition(nqubits):
    for qubit in range(nqubits):
        qc.h(qubit)

    
#オラクル    
def oracle(nqubits):
    #qc.cz(0, 1)
    qc.x(0)
    qc.x(1)
    # マルチ制御Zゲートをかけます
    qc.h(nqubits-1)
    qc.mct(list(range(nqubits-1)), nqubits-1)  # マルチ制御トフォリ
    qc.h(nqubits-1)
    qc.x(0)
    qc.x(1)

    
def diffuser(nqubits):
    for qubit in range(nqubits):
        qc.h(qubit)
        qc.x(qubit)
        
    # マルチ制御Zゲートをかけます
    qc.h(nqubits-1)
    qc.mct(list(range(nqubits-1)), nqubits-1)  # マルチ制御トフォリ
    qc.h(nqubits-1)
    
    for qubit in range(nqubits):
        qc.x(qubit)
        qc.h(qubit)

        
#測定
def measure(nqubits):
    for qubit in range(nqubits):
        qc.measure(qubit, qubit)
    
    
nqubits = 2
qc = QuantumCircuit(nqubits, nqubits)

superposition(nqubits)
qc.barrier(list(range(nqubits)))

for num in range(1):
    oracle(nqubits)
    qc.barrier(list(range(nqubits)))
    diffuser(nqubits)
    qc.barrier(list(range(nqubits)))

measure(nqubits)
qc.barrier(list(range(nqubits)))

    
    
qc.draw('mpl')

download.png

backend = Aer.get_backend('qasm_simulator')
results = execute(qc, backend=backend, shots=1024).result()
answer = results.get_counts()
plot_histogram(answer)

download-1.png

n量子ビットに拡張

from qiskit import Aer, execute

#状態重ね合わせ
def superposition(nqubits):
    for qubit in range(nqubits):
        qc.h(qubit)

    
#オラクル    
def oracle(nqubits):
    #qc.cz(0, 1)
    qc.x(2)
    qc.x(0)
    # マルチ制御Zゲートをかけます
    qc.h(nqubits-1)
    qc.mct(list(range(nqubits-1)), nqubits-1)  # マルチ制御トフォリ
    qc.h(nqubits-1)
    qc.x(2)
    qc.x(0)

    
def diffuser(nqubits):
    for qubit in range(nqubits):
        qc.h(qubit)
        qc.x(qubit)
        
    # マルチ制御Zゲートをかけます
    qc.h(nqubits-1)
    qc.mct(list(range(nqubits-1)), nqubits-1)  # マルチ制御トフォリ
    qc.h(nqubits-1)
    
    for qubit in range(nqubits):
        qc.x(qubit)
        qc.h(qubit)

        
#測定
def measure(nqubits):
    for qubit in range(nqubits):
        qc.measure(qubit, qubit)
    
    
nqubits = 4
qc = QuantumCircuit(nqubits, nqubits)

superposition(nqubits)
qc.barrier(list(range(nqubits)))

for num in range(2):
    oracle(nqubits)
    qc.barrier(list(range(nqubits)))
    diffuser(nqubits)
    qc.barrier(list(range(nqubits)))

measure(nqubits)

    
qc.draw('mpl')

download-2.png

download-3.png

#参考文献
L. K. Grover, “A fast quantum mechanical algorithm for database search," arXiv, 9605043, Nov. 1996.

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?