Enter matrices by columns rather than rows in TikZ

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:

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?

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

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}
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{document}

\begin{figure}[ht]
\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}

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