Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I find myself drawing a lot of quantum circuits these days, and I do them in TikZ because it produces very nice diagrams.

A quantum circuit is a lot like staff (as in music). You go from left to right, and those things that are vertically aligned are coincident. Therefore it makes more sense to write the operations in time order, by entering them a column at a time rather than by row. This enables easy tweaking, whereas by row you need to move up and down a lot to pick out the same positions.

At the moment, I am forced to write them in row order since that's the way matrices in TikZ, and LaTeX in general, work. Here's a sample:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,matrix}
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{tikzpicture}[thick,scale=0.75]
\tikzstyle{operator} = [shape=rectangle,draw,fill=white,minimum size=1.7em,inner sep=2]
\tikzstyle{phase} = [draw,fill,shape=circle,minimum size=5pt,inner sep=0pt]
%
\matrix[row sep=2mm, column sep=2mm] {
% First row.
\node (q1) {\ket{\psi}}; &
&
\node[operator] (CR1) {A1}; &
\node[operator] {A2}; &
\node[operator] (CR3) {A3}; &
\node[operator] {A4}; &
\node[operator] (CR5) {A5}; &
&
&
&
\node (end1) {\ket{\phi}};
\\
\node (q2) {\ket{0}}; &
\node[operator] (H1) {B1}; &
\node[phase] (CR2) {}; &
\node[operator] (H2) {B2}; &
\node[phase] (CR4) {}; &
\node[operator] (H2) {B3}; &
\node[phase] (CR6) {}; &
\node[operator] (H2) {B4}; &
\node[operator] (M1) {B5}; &
&
\node (end2) {\ket{0}};
\\
% Second row.
};
% Lines
\draw[thick] (CR1) -- (CR2);
\draw[thick] (CR3) -- (CR4);
\draw[thick] (CR5) -- (CR6);
% Behind
\begin{pgfonlayer}{background}
\draw[thick] (q1) -- (end1);
\draw[thick] (q2) -- (end2);
\end{pgfonlayer}
%
\end{tikzpicture}

\end{document}

As you can see, it's a mess of ampersands to count through, and this is only a simple circuit. The output generated is this:

A simple quantum circuit

My question is; is there a way to enter TikZ matrices by column rather than row, or a simple set of macros that'll do the same job?

share|improve this question
    
I'm not sure I understand your question correctly: If you would enter the matrix by column rather than by row, wouldn't you just end up typing a lot of \\ and less &? –  Jake Nov 4 '11 at 5:59
1  
Sure, my comment on ampersands is maybe what threw you off there. You would trade one for the other. My bad. The reason I want to do this is that entering in column order will keep those quantum operations that happen at the same time in one place. As it is, I have to look up and down a lot and there is a lot of counting of ampersands. –  qubyte Nov 4 '11 at 6:04
1  
The matrix mechanism in TikZ uses halign internally to lay out the contents. As far as I know, TeX doesn't have a natural vertical equivalent. So what you'd have to do is to gather in all the cells and then re-lay them out in the correct fashion. –  Loop Space Nov 4 '11 at 8:00
1  
If you happen to be using Emacs you could use this to transpose the table just before generating the document: gist.github.com/1157646. I haven't found anything for other editors though. –  Roelof Spijker Nov 4 '11 at 8:55
    
Just short of a transpose (the rows would be upside down) I guess I could implement it as a rotation... –  qubyte Nov 4 '11 at 9:23
show 4 more comments

1 Answer 1

up vote 4 down vote accepted

Ok. I came up with a rather inelegant solution. I used a mixture of adjustbox and node rotations and reflections to get the effect I wanted.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,matrix}
\usepackage{adjustbox}
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{document}

\begin{figure}[ht]
\begin{adjust box}{reflect,angle=90}
\begin{tikzpicture}[thick]
\tikzstyle{operator} = [xscale=-1,rotate=-90,shape=rectangle,draw,fill=white,minimum size=1.7em,inner sep=2]
\tikzstyle{phase} = [draw,fill,shape=circle,minimum size=5pt,inner sep=0pt]
\tikzstyle{state} = [xscale=-1,rotate=-90]
%
\matrix[row sep=2mm, column sep=2mm] {
\node[state] (q1) {\ket{\psi}};   & \node[state] (q2) {\ket{0}}; \\
                                  & \node[operator] (H1) {B1}; \\
\node[operator] (CR1) {A1};       & \node[phase] (CR2) {}; \\
\node[operator] {A2};             & \node[operator] (H2) {B2}; \\
\node[operator] (CR3) {A3};       & \node[phase] (CR4) {}; \\
\node[operator] {A4};             & \node[operator] (H2) {B3}; \\
\node[operator] (CR5) {A5};       & \node[phase] (CR6) {}; \\
                                  & \node[operator] (H2) {B4}; \\
                                  & \node[operator] (M1) {B5}; \\
\node[state] (end1) {\ket{\phi}}; & \node[state] (end2) {\ket{0}}; \\
};
% Lines
\draw[thick] (CR1) -- (CR2);
\draw[thick] (CR3) -- (CR4);
\draw[thick] (CR5) -- (CR6);
% Behind
\begin{pgfonlayer}{background}
\draw[thick] (q1) -- (end1);
\draw[thick] (q2) -- (end2);
\end{pgfonlayer}
%
\end{tikzpicture}
\end{adjustbox}
\end{figure}

\end{document}

The output is the same as the question, and the entries are column first. Can anyone improve on this?

share|improve this answer
    
You should either use a % after the { of \adjustbox to avoid a space there or use the adjustbox environment. You might also be interested in the reflect key, which reflects the content. –  Martin Scharrer Nov 7 '11 at 0:00
    
@MartinScharrer: Thanks. I moved to the environment version. However, the reflect key appears not to be recognised by adjustbox. Any idea what's going on? –  qubyte Nov 7 '11 at 0:50
    
Ah, wait, I had to update my packages. It works now but raises a few other issues. –  qubyte Nov 7 '11 at 1:04
    
This works, but I'd rather not have to add the adjustbox package. Is there any way to do this with just TikZ? It seems that the TikZ matrix really limits it. Perhaps I should do this without it by using chains. –  qubyte Nov 8 '11 at 2:42
    
Oh well. I'll accept this for now, but if anyone does come up with a better solution I will of course accept that. –  qubyte Nov 10 '11 at 7:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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