在 TikZ 中按列而不是行输入矩阵

在 TikZ 中按列而不是行输入矩阵

我发现自己最近画了很多量子电路,我用 TikZ 来画,因为它可以生成非常漂亮的图表。

量子电路很像五线谱(就像音乐中的五线谱)。从左到右,垂直对齐的东西是重合的。因此,按时间顺序编写操作更有意义,一次输入一列而不是按行。这使得调整变得容易,而按行编写则需要上下移动很多次才能选出相同的位置。

目前,我不得不按行顺序编写它们,因为这是 TikZ 和 LaTeX 中矩阵的工作方式。以下是示例:

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

如您所见,这是一个需要计数的混乱的“与”符号,这只是一个简单的电路。生成的输出如下:

简单的量子电路

我的问题是;有没有办法按列而不是行输入 TikZ 矩阵,或者一组简单的宏可以完成同样的工作?

答案1

好的。我想出了一个不太优雅的解决方案。我混合使用了调整框和节点旋转和反射来获得我想要的效果。

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

输出与问题相同,条目以列优先。有人能改进这一点吗?

相关内容