ProjectQプラットフォームに基づくdeutschアルゴリズムdemo


推奨量子計算科学普及:http://tech.sina.com.cn/d/2017-08-31/doc-ifykpysa2199081.shtml
01基本情報及びローカル文書の生成
ProjectQはpythonに基づくオープンソース量子計算フレームワークである.使いやすさがよい.ソースアドレス:https://github.com/ProjectQ-Framework/ProjectQ.git公式サイトはpython 2をサポートすると宣言した.7/3.4/3.5/3.6.
基本的なインストール方法:(途中でエラーが発生した場合、ダウンロードがタイムアウトしたため、コマンドを複数回実行すればよい可能性があります)
pip install projectq

gitがインストールされている場合は、ダウンロード方法:
git clone https://github.com/ProjectQ-Framework/ProjectQ.git

ダウンロードしたソースコードには、以下のローカルコンパイルが必要なドキュメントの説明ProjectQ\docsが豊富に含まれています.具体的な方法については、以下を参照してください.https://github.com/ProjectQ-Framework/ProjectQ/blob/develop/docs/README.rst次のコマンドを実行してprojectqローカルドキュメントをインストールし、生成します.比較的詳細です.複数のフォーマットをサポートします.
python -m pip install sphinx
python -m pip install sphinx_rtd_theme
:: docs        html  
make html
::          singlehtml   
:: ProjectQ\docs\_build\html\index.html
:: 
make singlehtml
:: ProjectQ\docs\_build\singlehtml\index.html
::          epub     
:: 
make epub
:: ProjectQ\docs\_build\epub\ProjectQ.epub

生成されたローカルドキュメントは図:基于ProjectQ平台的deutsch算法demo_第1张图片pdflatex pythonスクリプトに基づいて回路図を生成するには、latexがpdfまたは他のフォーマットを回転するツールが必要です.projectqが推奨するツールはpdflatexです.WindowsにはMiKTexをインストールする必要があります.ダウンロード先:https://miktex.org/download .私が適用するのはbasic-miktex-2.9です.7031-x64.exe回路図を生成するdemoについては、ProjectQexamplesgate_を参照してください.zoo.py.(pdflatexをインストールする必要があることに注意してください.そうしないとlatexファイルしか生成できません.)先にgate_を実行zoo.pyファイル、現在のディレクトリの下でzooを生成します.tex. さらにcmdウィンドウでpdflatex zoo.texを実行すると、pdflatexの依存性をインストールするよう求められます.一度にインストールすればよい(ダウンロード/インストールに依存して失敗した場合は、インストールをずっと提示し、何度もインストールをクリックすればよい).その後、現在のディレクトリの下でzooを生成することができる.pdf. 基于ProjectQ平台的deutsch算法demo_第2张图片
02ベースとProjectQのDeutschアルゴリズム実装
このdemoコードは『量子アルゴリズムとプログラミング入門』4.1から来た.3節ProjectQコードはpython部分の修正を少し行い,量子アルゴリズムに関する内容は修正しなかった.deutsch.py
import sys
import math
import projectq.setups.ibm
from projectq.backends import IBMBackend, CircuitDrawer
from projectq import MainEngine
from projectq.ops import *

def Deutsch(eng, t):
    qs = eng.allocate_qureg(2)
    X | qs[1]
    All(H) | qs
    CNOT | (qs[0], qs[1])
    H | qs[0]
    All(Measure) | qs
    eng.flush()
    if t == 'ibm':
        results = eng.backend.get_probabilities(qs)
    elif t == 'simulator':
        print("  Deutsch  ,        :" + str(int(qs[0])))
    elif t == 'drawer':
        print(drawing_engine.get_latex())
        s = drawing_engine.get_latex()
        prefix = 'deutsch'
        with open('{}.tex'.format(prefix), 'w') as f:
            f.write(s)
    else:
        pass

#     
# D:/git/Quantum/ProjectQ/examples/deutsch.py drawer
#          ,          deutsch.tex  
#    pdflatex deutsch.tex       pdf  deutsch.pdf
# pdflatex deutsch.tex
# D:/git/Quantum/ProjectQ/examples/deutsch.py ibm
# D:/git/Quantum/ProjectQ/examples/deutsch.py simulator
# D:/git/Quantum/ProjectQ/examples/deutsch.py
if __name__ == "__main__":
    if len(sys.argv) == 1:
        types = 'simulator'
    else:
        types = sys.argv[1]

    if types == 'ibm':
        # ibm backend
        eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=True, device="ibmqx4"))
    elif types == 'simulator':
        eng = MainEngine()
        types = 'simulator'
    elif types == 'drawer':
        drawing_engine = CircuitDrawer()
        eng = MainEngine(drawing_engine)
    else:
        eng = MainEngine()
        types = 'simulator'

    Deutsch(eng, types)

ibm物理チップを実行するには、アカウントを設定する必要があります.ここでは,ローカルシミュレータ生成回路図方式をテストする.運転結果は以下の通り:D:/git/Quantum/ProjectQ/examples/deutsch.py drawer
C:\Python36\python.exe D:/git/Quantum/ProjectQ/examples/deutsch.py drawer
\documentclass{standalone}
\usepackage[margin=1in]{geometry}
\usepackage[hang,small,bf]{caption}
\usepackage{tikz}
\usepackage{braket}
\usetikzlibrary{backgrounds,shadows.blur,fit,decorations.pathreplacing,shapes}

\begin{document}
\begin{tikzpicture}[scale=0.8, transform shape]

\tikzstyle{basicshadow}=[blur shadow={shadow blur steps=8, shadow xshift=0.7pt, shadow yshift=-0.7pt, shadow scale=1.02}]\tikzstyle{basic}=[draw,fill=white,basicshadow]
\tikzstyle{operator}=[basic,minimum size=1.5em]
\tikzstyle{phase}=[fill=black,shape=circle,minimum size=0.1cm,inner sep=0pt,outer sep=0pt,draw=black]
\tikzstyle{none}=[inner sep=0pt,outer sep=-.5pt,minimum height=0.5cm+1pt]
\tikzstyle{measure}=[operator,inner sep=0pt,minimum height=0.5cm, minimum width=0.75cm]
\tikzstyle{xstyle}=[circle,basic,minimum height=0.35cm,minimum width=0.35cm,inner sep=-1pt,very thin]
\tikzset{
shadowed/.style={preaction={transform canvas={shift={(0.5pt,-0.5pt)}}, draw=gray, opacity=0.4}},
}
\tikzstyle{swapstyle}=[inner sep=-1pt, outer sep=-1pt, minimum width=0pt]
\tikzstyle{edgestyle}=[very thin]


ode[none] (line0_gate0) at (0.1,-0) {$\Ket{0}$};
ode[none] (line0_gate1) at (0.5,-0) {};
ode[none,minimum height=0.5cm,outer sep=0] (line0_gate2) at (0.75,-0) {};
ode[none] (line0_gate3) at (1.0,-0) {}; \draw[operator,edgestyle,outer sep=0.5cm] ([yshift=0.25cm]line0_gate1) rectangle ([yshift=-0.25cm]line0_gate3) node[pos=.5] {H}; \draw (line0_gate0) edge[edgestyle] (line0_gate1);
ode[none] (line1_gate0) at (0.1,-1) {$\Ket{0}$};
ode[xstyle] (line1_gate1) at (0.5,-1) {}; \draw[edgestyle] (line1_gate1.north)--(line1_gate1.south); \draw[edgestyle] (line1_gate1.west)--(line1_gate1.east); \draw (line1_gate0) edge[edgestyle] (line1_gate1);
ode[none] (line1_gate2) at (1.05,-1) {};
ode[none,minimum height=0.5cm,outer sep=0] (line1_gate3) at (1.3,-1) {};
ode[none] (line1_gate4) at (1.55,-1) {}; \draw[operator,edgestyle,outer sep=0.5cm] ([yshift=0.25cm]line1_gate2) rectangle ([yshift=-0.25cm]line1_gate4) node[pos=.5] {H}; \draw (line1_gate1) edge[edgestyle] (line1_gate2);
ode[xstyle] (line1_gate5) at (1.9500000000000002,-1) {}; \draw[edgestyle] (line1_gate5.north)--(line1_gate5.south); \draw[edgestyle] (line1_gate5.west)--(line1_gate5.east);
ode[phase] (line0_gate4) at (1.9500000000000002,-0) {}; \draw (line0_gate4) edge[edgestyle] (line1_gate5); \draw (line1_gate4) edge[edgestyle] (line1_gate5); \draw (line0_gate3) edge[edgestyle] (line0_gate4);
ode[none] (line0_gate5) at (2.5000000000000004,-0) {};
ode[none,minimum height=0.5cm,outer sep=0] (line0_gate6) at (2.7500000000000004,-0) {};
ode[none] (line0_gate7) at (3.0000000000000004,-0) {}; \draw[operator,edgestyle,outer sep=0.5cm] ([yshift=0.25cm]line0_gate5) rectangle ([yshift=-0.25cm]line0_gate7) node[pos=.5] {H}; \draw (line0_gate4) edge[edgestyle] (line0_gate5);
ode[measure,edgestyle] (line0_gate8) at (3.5000000000000004,-0) {}; \draw[edgestyle] ([yshift=-0.18cm,xshift=0.07500000000000001cm]line0_gate8.west) to [out=60,in=180] ([yshift=0.035cm]line0_gate8.center) to [out=0, in=120] ([yshift=-0.18cm,xshift=-0.07500000000000001cm]line0_gate8.east); \draw[edgestyle] ([yshift=-0.18cm]line0_gate8.center) to ([yshift=-0.07500000000000001cm,xshift=-0.18cm]line0_gate8.north east); \draw (line0_gate7) edge[edgestyle] (line0_gate8);
ode[measure,edgestyle] (line1_gate6) at (2.6000000000000005,-1) {}; \draw[edgestyle] ([yshift=-0.18cm,xshift=0.07500000000000001cm]line1_gate6.west) to [out=60,in=180] ([yshift=0.035cm]line1_gate6.center) to [out=0, in=120] ([yshift=-0.18cm,xshift=-0.07500000000000001cm]line1_gate6.east); \draw[edgestyle] ([yshift=-0.18cm]line1_gate6.center) to ([yshift=-0.07500000000000001cm,xshift=-0.18cm]line1_gate6.north east); \draw (line1_gate5) edge[edgestyle] (line1_gate6); \end{tikzpicture} \end{document} Process finished with exit code 0
D:\git\Quantum\ProjectQ\examples>pdflatex deutsch.tex 基于ProjectQ平台的deutsch算法demo_第3张图片
誤りがあれば,批判と指摘を歓迎する.QQ群:579809480.