4

I'm trying to typeset some quantum circuits. Here, they provide an example:

\documentclass[10pt]{standalone}
\usepackage{tikz}

% TikZ libraries `calc` needed now to tweak bracket.
\usetikzlibrary{backgrounds,fit,decorations.pathreplacing,calc}
% Dirac Kets
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{document}
    \begin{tikzpicture}[thick]
    % `operator' will only be used by Hadamard (H) gates here.
    % `phase' is used for controlled phase gates (dots).
    % `surround' is used for the background box.
    \tikzstyle{operator} = [draw,fill=white,minimum size=1.5em] 
    \tikzstyle{phase} = [draw,fill,shape=circle,minimum size=5pt,inner sep=0pt]
    \tikzstyle{surround} = [fill=blue!10,thick,draw=black,rounded corners=2mm]
    %
    \matrix[row sep=0.4cm, column sep=0.8cm] (circuit) {
    % First row.
    \node (q1) {\ket{0}}; &[-0.5cm] 
    \node[operator] (H11) {H}; &
    \node[phase] (P12) {}; &
    \node[phase] (P13) {}; &
    &[-0.3cm]
    \coordinate (end1); \\
    % Second row.
    \node (q2) {\ket{0}}; &
    \node[operator] (H21) {H}; &
    \node[phase] (P22) {}; &
    &
    \node[operator] (H24) {H}; &
    \coordinate (end2);\\
    % Third row.
    \node (q3) {\ket{0}}; &
    \node[operator] (H31) {H}; &
    &
    \node[phase] (P33) {}; &
    \node[operator] (H34) {H}; &
    \coordinate (end3); \\
    };
    % Draw bracket on right with resultant state.
    \draw[decorate,decoration={brace},thick]
        ($(circuit.north east)-(0cm,0.3cm)$)
        to node[midway,right] (bracket) {$\displaystyle\frac{\ket{000}+\ket{111}}{\sqrt{2}}$}
        ($(circuit.south east)+(0cm,0.3cm)$);
    \begin{pgfonlayer}{background}
        % Draw background box.
        \node[surround] (background) [fit = (q1) (H31) (bracket)] {};
        % Draw lines.
        \draw[thick] (q1) -- (end1)  (q2) -- (end2) (q3) -- (end3) (P12) -- (P22) (P13) -- (P33);
    \end{pgfonlayer}
    %
    \end{tikzpicture}
\end{document}

Whose output is:

enter image description here

This is very nice, but now I'd like to add a kind of big box that fit several wires like this:

enter image description here

Do you have any idea how to do that properly ? I tought that I could use some empty nodes, and then put a fit on top of it, but it's not very practical. A syntax like the one of qcircuit would be very nice.

Thank you!

2

This is a possible solution using your own code. We keep phase nodes (except P33) and use them as reference for a new fit node which will cover them:

\node[fit=(H11.north-|P13.east) (H21.south-|P22.west), 
       operator, inner sep=0pt, label=center:F]{};

It's an operator kind of node and its contents is added with label=center:... option. Node corners are defined to align with north and south borders of neighbour operators.

Aside note: Please, read Should \tikzset or \tikzstyle be used to define TikZ styles?

\documentclass[10pt]{standalone}
\usepackage{tikz}

% TikZ libraries `calc` needed now to tweak bracket.
\usetikzlibrary{backgrounds,fit,decorations.pathreplacing,calc}
% Dirac Kets
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{document}
    \begin{tikzpicture}[thick]
    % `operator' will only be used by Hadamard (H) gates here.
    % `phase' is used for controlled phase gates (dots).
    % `surround' is used for the background box.
    \tikzstyle{operator} = [draw,fill=white,minimum size=1.5em] 
    \tikzstyle{phase} = [draw,fill,shape=circle,minimum size=5pt,inner sep=0pt]
    \tikzstyle{surround} = [fill=blue!10,thick,draw=black,rounded corners=2mm]
    %
    \matrix[row sep=0.4cm, column sep=0.8cm] (circuit) {
    % First row.
    \node (q1) {\ket{0}}; &[-0.5cm] 
    \node[operator] (H11) {H}; &
    \node[phase] (P12) {}; &
    \node[phase] (P13) {}; &
    &[-0.3cm]
    \coordinate (end1); \\
    % Second row.
    \node (q2) {\ket{0}}; &
    \node[operator] (H21) {H}; &
    \node[phase] (P22) {}; &
    &
    \node[operator] (H24) {H}; &
    \coordinate (end2);\\
    % Third row.
    \node (q3) {\ket{0}}; &
    \node[operator] (H31) {H}; &
    &
%    \node[phase] (P33) {}; 
    &
    \node[operator] (H34) {H}; &
    \coordinate (end3); \\
    };
    % Draw `big box`
    \node[fit=(H11.north-|P13.east) (H21.south-|P22.west), operator, inner sep=0pt, label=center:F]{};
    % Draw bracket on right with resultant state.
    \draw[decorate,decoration={brace},thick]
        ($(circuit.north east)-(0cm,0.3cm)$)
        to node[midway,right] (bracket) {$\displaystyle\frac{\ket{000}+\ket{111}}{\sqrt{2}}$}
        ($(circuit.south east)+(0cm,0.3cm)$);
    \begin{pgfonlayer}{background}
        % Draw background box.
        \node[surround] (background) [fit = (q1) (H31) (bracket)] {};
        % Draw lines.
        \draw[thick] (q1) -- (end1)  
            (q2) -- (end2) 
            (q3) -- (end3) 
            (P12) -- (P22) 
%           (P13) -- (P33)
            ;
    \end{pgfonlayer}
    %
    \end{tikzpicture}
\end{document}

enter image description here

0
\documentclass[10pt]{standalone}
\usepackage{tikz}

% TikZ libraries `calc` needed now to tweak bracket.
\usetikzlibrary{backgrounds,fit,decorations.pathreplacing,calc}
% Dirac Kets
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{document}
    \begin{tikzpicture}[thick]
    % `operator' will only be used by Hadamard (H) gates here.
    % `phase' is used for controlled phase gates (dots).
    % `surround' is used for the background box.
    \tikzstyle{operator} = [draw,fill=white,minimum size=1.5em] 
    \tikzstyle{phase} = [draw,fill,shape=circle,minimum size=5pt,inner sep=0pt]
    \tikzstyle{surround} = [fill=blue!10,thick,draw=black,rounded corners=2mm]
    %
    \matrix[row sep=0.4cm, column sep=0.8cm] (circuit) {
        % First row.
        \node (q1) {\ket{0}}; &[-0.5cm] 
        \node[operator] (H11) {H}; &
        \node[] (P12) {}; &
        \node[] (P13) {}; &
        &[-0.3cm]
        \coordinate (end1); \\
        % Second row.
        \node (q2) {\ket{0}}; &
        \node[operator] (H21) {H}; &
        \node[] (P22) {}; &
        &
        \node[operator] (H24) {H}; &
        \coordinate (end2);\\
        % Third row.
        \node (q3) {\ket{0}}; &
        \node[operator] (H31) {H}; &
        &
        \node[] (P33) {}; &
        \node[operator] (H34) {H}; &
        \coordinate (end3); \\
    };
    % Draw bracket on right with resultant state.
    \draw[decorate,decoration={brace},thick]
    ($(circuit.north east)-(0cm,0.3cm)$)
    to node[midway,right] (bracket) {$\displaystyle\frac{\ket{000}+\ket{111}}{\sqrt{2}}$}
    ($(circuit.south east)+(0cm,0.3cm)$);

    % to draw the big rectangle 
    \node[fill=white,draw,minimum size=4.3em]at ($(P13)!.5!(P22)$){F};

    \begin{pgfonlayer}{background}
    % Draw background box.
    \node[surround] (background) [fit = (q1) (H31) (bracket)] {};
    % Draw lines.
    \draw[thick] (q1) -- (end1)  (q2) -- (end2) (q3) -- (end3) ;
    \end{pgfonlayer}
    %
    \end{tikzpicture}
\end{document}
0

I have recently produced a tikz library called quantikz that can produce the sort of diagrams you're after. I started from the qcircuit syntax, although it has evolved a little since then. I produced the two circuits described in the question: enter image description here using the code

\begin{center}
\begin{tikzcd}
\lstick{\ket{0}} & \gate{H} & \ctrl{1} & \ctrl{2} & \qw &\rstick[wires=3]{$\displaystyle\frac{\ket{000}+\ket{111}}{\sqrt{2}}$}\qw \\
\lstick{\ket{0}} & \gate{H} & \control{} & \qw & \gate{H} & \qw \\
\lstick{\ket{0}} & \gate{H} & \qw & \control{} & \gate{H} & \qw
\end{tikzcd}
\end{center}


\begin{center}
\begin{tikzcd}
\lstick{\ket{0}} & \gate{H} & \gate[wires=2]{F}  & \qw &\rstick[wires=3]{$\displaystyle\frac{\ket{000}+\ket{111}}{\sqrt{2}}$}\qw \\
\lstick{\ket{0}} & \gate{H} & \phantom{wider} & \gate{H} & \qw \\
\lstick{\ket{0}} & \gate{H} & \qw & \gate{H} & \qw
\end{tikzcd}
\end{center}

Of course, you have to have the package installed, and declared in the document preamble. For more details, see the tutorial on the arXiv. The source code (including the package) can be downloaded by following the "Other formats" link although hopefully, in a day or two, it will be available through ctan.

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.