安裝ProjectQ並生成量子電路的latex代碼

在寫論文對的時候,大多都要用矢量的量子電路圖。最近發現ProjectQ這個編程工具可以直接生成電路的LaTeX代碼,在overleaf上運行後可以得到矢量電路圖(下圖是Bell State電路,作爲本文的示例):

具體的過程如下:

首先安裝projectQ。在anaconda裏面新建一個環境後,用如下語句完成安裝:

python -m pip install --user projectq

安裝後打開jupyter notebook,首先定義量子電路, 這裏大家可以根據自己設計的電路進行搭建:

def create_bell_pair(eng):
    b1 = eng.allocate_qubit()
    b2 = eng.allocate_qubit()

    H | b1
    CNOT | (b1, b2)

    return b1, b2

然後調包,編譯,打出LaTeX代碼:

from projectq.ops import All, CNOT, H, Measure, Rz, X, Z
from projectq import MainEngine
from projectq.backends import CircuitDrawer

# create a main compiler engine
drawing_engine = CircuitDrawer()
eng = MainEngine(drawing_engine)

create_bell_pair(eng)

eng.flush()
print(drawing_engine.get_latex())

然後就可以打印出這段代碼:

\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]

\node[none] (line0_gate0) at (0.1,-0) {$\Ket{0}$};
\node[none] (line0_gate1) at (0.5,-0) {};
\node[none,minimum height=0.5cm,outer sep=0] (line0_gate2) at (0.75,-0) {};
\node[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);
\node[none] (line1_gate0) at (0.1,-1) {$\Ket{0}$};
\node[xstyle] (line1_gate1) at (1.4000000000000001,-1) {};
\draw[edgestyle] (line1_gate1.north)--(line1_gate1.south);
\draw[edgestyle] (line1_gate1.west)--(line1_gate1.east);
\node[phase] (line0_gate4) at (1.4000000000000001,-0) {};
\draw (line0_gate4) edge[edgestyle] (line1_gate1);
\draw (line1_gate0) edge[edgestyle] (line1_gate1);
\draw (line0_gate3) edge[edgestyle] (line0_gate4);
\node[none] (line0_gate5) at (1.9500000000000002,-0) {};
\draw ([yshift=0.15cm]line0_gate5.center) edge [edgestyle] ([yshift=-0.15cm]line0_gate5.center);
\draw (line0_gate4) edge[edgestyle] (line0_gate5);
\node[none] (line1_gate2) at (1.9500000000000002,-1) {};
\draw ([yshift=0.15cm]line1_gate2.center) edge [edgestyle] ([yshift=-0.15cm]line1_gate2.center);
\draw (line1_gate1) edge[edgestyle] (line1_gate2);

\end{tikzpicture}
\end{document}

將上面的代碼粘貼到overleaf裏,就可以得到文章開始展示的電路圖啦。


Reference:

ProjectQ的documentation:https://projectq.readthedocs.io/en/latest/index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章