8

I have seen many examples of classical circuit with tikz but none of quantum logical gates. Here are some of the quantum logical gates

 SWAP gate Hadamard gate controlled NOT gate controlled-U gate Toffoli gate Fredkin gate

A whole circuit maybe look like quantum logical circuit

  • A circuitikz multinode element should have anchor names for each of the connection points. You place the elements on a grid and draw lines between the anchors. Bipole elements are generally placed between circuit nodes using "to". – John Kormylo Sep 5 '14 at 14:20
  • BTW, is there a standard nomenclature for the contact points? In and out are standard for bipoles and pin numbers for ICs. – John Kormylo Sep 6 '14 at 3:28
  • @JohnKormylo Wikipedia lists the name of each gate above. Maybe there is no nomenclature for the contact points, but we can give one similar to the ones in Circuitikz. – Eden Harder Sep 6 '14 at 5:20
  • From the article, especially the table for the Fredkin gate, in (or in1, in2, ...) out and control seem to be standard. Not sure what the 6 lead U gate is about. – John Kormylo Sep 6 '14 at 14:13
  • @JohnKormylo Do you mean the controlled-U gate? – user25607 Sep 7 '14 at 10:14
9

Inspired by the code given by @qubyte HERE and after adding some new quantum logic gates and patience, drawing the plot is possible.

Information: more inform

enter image description here

Code

\begin{document}
    \begin{tikzpicture}[thick]
% `operator' will only be used by Hadamard (H) gates here.
% `operator2' is for large U gates
% `phase' is used for controlled phase gates (dots).
% `surround' is used for the background box.
% `crossx' is used for the cross.
% `circlewc' is used for the circle with cross box.
\tikzset{
operator/.style = {draw,fill=white,minimum size=1.5em},
operator2/.style = {draw,fill=white,minimum height=3cm},
phase/.style = {draw,fill,shape=circle,minimum size=5pt,inner sep=0pt},
surround/.style = {fill=blue!10,thick,draw=black,rounded corners=2mm},
cross/.style={path picture={ 
\draw[thick,black](path picture bounding box.north) -- (path picture bounding box.south) (path picture bounding box.west) -- (path picture bounding box.east);
}},
crossx/.style={path picture={ 
\draw[thick,black,inner sep=0pt]
(path picture bounding box.south east) -- (path picture bounding box.north west) (path picture bounding box.south west) -- (path picture bounding box.north east);
}},
circlewc/.style={draw,circle,cross,minimum width=0.3 cm},
}
    %
\matrix[row sep=0.4cm, column sep=0.8cm] (circuit) { % 9 columns
    % First row.
\node (q1) {\ket{\psi}};  
& [-0.5cm] 
& 
&%\node[operator](U11){U};                                     
& 
&  
&  
&[-0.3cm]
&
\coordinate (end1); \\
    % Second row.
\node (q2) {\ket{0}};    
&                                     
&\node[operator] (H21) {H}; 
&\node[](U21){};                                      
&\node[phase] (P21) {};
&\node[operator] (H22) {H};
&\node[phase] (P22) {};   
& \node[crossx] (c21){};
&\coordinate (end2);\\
    % Third row.
\node (q3) {\ket{0}};    
&                                      
&\node[operator] (H31) {H}; 
&%\node[](U31){};                                      
&\node[circlewc] (P31) {};   
& 
&\node[circlewc] (P32) {};                                               
& \node[crossx] (c31){};
&\coordinate (end3);\\
};

% Draw bracket on right with resultant state.
\draw[decorate,decoration={brace},thick]
        ($(end1)+(2pt,0)$)
        to node[midway,right] (bracket) {$\ket{\phi^+}$}
        ($(end2)+(2pt,0)$);
\node at ($(end3)+(10pt,0)$){$\ket{\psi}$};
\begin{pgfonlayer}{background}
\draw[thick] (q1) -- (end1)  
(q2) -- (end2) 
(q3) -- (end3) 
(P21) -- (P31)  (P22) -- (P32);
\draw[thick,shorten >=-4pt,shorten <=-4pt](c21)--(c31);
\foreach \i in {-3,-0.4,4}{
\draw[dashed,thick,red] ([xshift=\i cm]circuit.north) -- ([xshift=\i cm]circuit.south); 
\node[operator2] at (U21){U};  %<-- for large U
}
\end{pgfonlayer}
    %
\end{tikzpicture}
\end{document}
  • Missing code \documentclass[]{standalone} \usepackage{tikz} \usetikzlibrary{backgrounds,decorations.pathreplacing,calc} \newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}} – Jesse Jan 22 '15 at 2:51
  • Can I use your code as a part of a possible package for drawing quantum circuits using TikZ? I may release it under the GNU public license if I can do it. If you can participate on this package development, that will also be great. I will also contact the linked original author... Thanks. – Xiaodong Qi Feb 25 '16 at 5:19
  • @XiaodongQi: Yes, please feel free to use it, if it gives you some idea to move on, leading to completion of your package. It would be my honer, really. – Jesse Feb 25 '16 at 6:36
2

I realise this is an old question, and I used some of the excellent answers already present in what I've done, but I have recently produced a tikz library called quantikz that helps to typeset quantum circuits. There's a full tutorial available here. (It's also a route to downloading the package, by looking at the source code, but hopefully in a day or two, it'll be available through ctan.)

I reproduced the target circuit: enter image description here with the following code (and loading the quantikz library in the document preamble):

\begin{tikzcd}
\lstick{\ket{\psi}}\slice{} & \qw & \gate[wires=3]{U}\slice{} & \qw & \qw & \qw\slice{} & \rstick[wires=2]{\ket{\phi^+}}\qw \\
\lstick{\ket{0}} & \gate{H} & & \ctrl{1} & \gate{H} & \ctrl{1} & \qw \\
\lstick{\ket{0}} & \gate{H} & \phantom{wide} & \targ{} & \qw & \targ{} & \rstick{\ket{\psi}}\qw
\end{tikzcd}
  • Have you put the code somewhere else? It would be great to contribute to the code in order to make small improvements. Btw, this is the greatest tool I've been using lately for quantum circuit design and the switch from qcircuit is really easy. At the moment, after a few days of usage, the only thing I really miss is the nghost and cghost macros, but the pros of this package really outweigh the cons. – tigerjack89 Mar 14 at 15:08
  • 1
    I haven't put it anywhere like github so that people can directly suggest changes. Please feel free to email me. There are already several user-requested features that I've added and am yet to upload, along with some improvement in spacing. nghost and cghost hadn't previously impinged upon my conciousness. I'll have a look at adding an equivalent. – DaftWullie Mar 15 at 10:13
1

The swap gate is implemented using two bipoles. The disadvantage is that you have to add a line between the two centers. The advantage is that you don't have to worry about how far apart they are.

circuit elements

I moved the default label location to the bottom so as to avoid the control anchor.

\documentclass{standalone}
\usepackage{circuitikz}

\newlength{\ResRight}
\newlength{\ResUp}

\makeatletter
\def\TikzBipolePath#1#2{\pgf@circ@bipole@path{#1}{#2}}
\def\CircDirection{\pgf@circ@direction}
\makeatother

% h-gate

\ctikzset{bipoles/hgate/width/.initial=.65}
\ctikzset{bipoles/hgate/height/.initial=.65}
\ctikzset{bipoles/hgate/symbol/.initial=\textit{\Large H}}

\pgfcircdeclarebipole{}
  {\ctikzvalof{bipoles/hgate/height}}
  {hgate}
  {\ctikzvalof{bipoles/hgate/height}}
  {\ctikzvalof{bipoles/hgate/width}}
  {
    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
    \pgfpathrectanglecorners{\southwest}{\northeast}
    \pgfusepath{draw}
    \pgftext[rotate=-\CircDirection]{\ctikzvalof{bipoles/hgate/symbol}}
  }

\def\hgatepath#1{\TikzBipolePath{hgate}{#1}}
\tikzset{hgate/.style = {\circuitikzbasekey, /tikz/to path=\hgatepath, l_=#1}}

% u-gate

\ctikzset{bipoles/ugate/width/.initial=.65}
\ctikzset{bipoles/ugate/height/.initial=.65}
\ctikzset{bipoles/ugate/symbol/.initial=\textit{\Large U}}

\pgfcircdeclarebipole
  {
  \anchor{control}{
    \pgfextracty{\ResUp}{\northeast}
    \pgfpoint{0}{\ResUp}
  }}
  {\ctikzvalof{bipoles/ugate/height}}
  {ugate}
  {\ctikzvalof{bipoles/ugate/height}}
  {\ctikzvalof{bipoles/ugate/width}}
  {
    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
    \pgfpathrectanglecorners{\southwest}{\northeast}
    \pgfusepath{draw}
    \pgftext[rotate=-\CircDirection]{\ctikzvalof{bipoles/ugate/symbol}}
  }

\def\ugatepath#1{\TikzBipolePath{ugate}{#1}}
\tikzset{ugate/.style = {\circuitikzbasekey, /tikz/to path=\ugatepath, l_=#1}}

% cnot gate

\ctikzset{bipoles/cnot/width/.initial=.4}
\ctikzset{bipoles/cnot/height/.initial=.4}

\pgfcircdeclarebipole
  {
    \anchor{control}{
    \pgfextracty{\ResUp}{\northeast}
    \pgfpoint{0cm}{\ResUp}
  }}
  {\ctikzvalof{bipoles/cnot/height}}
  {cnot}
  {\ctikzvalof{bipoles/cnot/height}}
  {\ctikzvalof{bipoles/cnot/width}}
  {
    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
    \pgfextractx{\ResRight}{\northeast}
    \pgfextracty{\ResUp}{\northeast}
    \pgfpathellipse{\pgfpointorigin}{\pgfpoint{\ResRight}{0cm}}{\pgfpoint{0cm}{\ResUp}}
    \pgfpathmoveto{\pgfpoint{0cm}{\ResUp}}
    \pgfpathlineto{\pgfpoint{0cm}{-\ResUp}}
    \pgfpathmoveto{\pgfpoint{\ResRight}{0cm}}
    \pgfpathlineto{\pgfpoint{-\ResRight}{0cm}}
    \pgfusepath{draw}
  }

\def\cnotpath#1{\TikzBipolePath{cnot}{#1}}
\tikzset{cnot/.style = {\circuitikzbasekey, /tikz/to path=\cnotpath, l_=#1}}

% swap gate (half)

\ctikzset{bipoles/swap/width/.initial=.4}
\ctikzset{bipoles/swap/height/.initial=.4}

\pgfcircdeclarebipole
  {\anchor{control}{\pgfpointorigin}}% equivalent to center
  {\ctikzvalof{bipoles/swap/height}}
  {swap}
  {\ctikzvalof{bipoles/swap/height}}
  {\ctikzvalof{bipoles/swap/width}}
  {
    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
    \pgfextractx{\ResRight}{\northeast}
    \pgfextracty{\ResUp}{\northeast}
    \pgfpathmoveto{\pgfpoint{.5\ResRight}{.5\ResUp}}
    \pgfpathlineto{\pgfpoint{-.5\ResRight}{-.5\ResUp}}
    \pgfpathmoveto{\pgfpoint{.5\ResRight}{-.5\ResUp}}
    \pgfpathlineto{\pgfpoint{-.5\ResRight}{.5\ResUp}}
    \pgfusepath{draw}
    \pgfsetlinewidth{\pgfstartlinewidth}
    \pgfpathmoveto{\pgfpoint{-\ResRight}{0cm}}
    \pgfpathlineto{\pgfpoint{\ResRight}{0cm}}
    \pgfusepath{draw}
  }

\def\swappath#1{\TikzBipolePath{swap}{#1}}
\tikzset{swap/.style = {\circuitikzbasekey, /tikz/to path=\swappath, l_=#1}}

\begin{document}
\begin{circuitikz}

\draw (0,0) to[hgate=H1] (2,0) to[ugate=U1, n=U1] (4,0) to[cnot=CNOT, n=N1] (5,0) to[swap=SWAP, n=Bottom] (7,0);
\draw (0,1) -- (5,1) to[swap, n=Top] (7,1);
\draw (U1.control) to[short, -*] (3,1);
\draw (N1.control) to[short, -*] (4.5,1);
\draw (Bottom.center) -- (Top.center);

\end{circuitikz}
\end{document}
  • One could implement the remaining gates using multiple bipole gates with extra anchors. – John Kormylo Sep 8 '14 at 21:31
  • Thanks! It will be perfect if you implement the remaining gates in the same tex file. I do not know how to implement them. – Eden Harder Sep 9 '14 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.