5

I want to draw quantum circuits and commutative diagrams, so I want to use the two packages quantikz and tikz-cd.

But somehow quantikz overwrites tikz-cd. Take a look at this code:

\documentclass[11pt,a4paper]{report}
\usepackage{tikz-cd}
\usepackage{quantikz}

\begin{document}
    
\begin{tikzcd}
    & B \\
    A \arrow[ru] &  
\end{tikzcd}

\end{document}

It results in:

enter image description here

Commenting out quantikz, I get what I want:

enter image description here

How can I use both usepackages and draw nice commutative diagrams?

2 Answers 2

8

The quantikz manual says:

enter image description here

I'm not an expert of quantikz, but setting \setwiretype{n} at every row or wire types={n,n} in the optional parameter it works.

By the way, you don't need to load tikz-cd because it's already loaded by quantikz.

Please note that you can use both quantikz and tikzcd environments.

You can also create a style to set the proper spacing for your CDs.

\documentclass[11pt,a4paper]{report}
\usepackage{quantikz}
\tikzset{
  mystyle/.style={
    nodes={inner sep=4pt},
    row sep=1.8em, 
    column sep=2.4em
    }
  }
  
\begin{document}

\begin{quantikz}[wire types={n,n}, mystyle]
    & B \\
    A \arrow[ru] & 
\end{quantikz}
\begin{quantikz}[mystyle]
    \setwiretype{n} & B \\
    A \setwiretype{n}\ar[ru] &
\end{quantikz}

\begin{tikzcd}[wire types={n,n}, mystyle]
    & B \\
    A \arrow[ru] &  
\end{tikzcd}
\begin{tikzcd}[nodes={inner sep=2pt}, mystyle]
    \setwiretype{n}& B \\
    A \ar[ru]\setwiretype{n} &  
\end{tikzcd}

% The following code is taken from the quantikz manual
\begin{quantikz}
\lstick{\ket{0}\\initial state} & & \push{X} & &
\meter{0/1} \arrow[r] &
\rstick{\ket{1}}\setwiretype{n}
\end{quantikz}

\end{document}

enter image description here

3
  • Thanks! However, the arrows are much smaller than in the regular tikz-cd. Do you know how to change the default length of an arrow in quantikz? :)
    – Blau
    Apr 17 at 9:09
  • Just use \arrow[rr] instead of \arrow[r], etc.
    – Blau
    Apr 17 at 9:24
  • 1
    @Blau Instead of using \arrow[rr], set the desired spacing (inner sep, column sep, row sep). I created a style (mystyle) to avoid repeating the settings in every CD.
    – CarLaTeX
    Apr 17 at 12:29
5

I have seen very well the documentation of quantikz remembering that of tikz-cd. Reading carefully the manual of both packages we observe that they use the command \arrow[r].

enter image description here

I think that quantikz see the row of each array as a wire in tikz-cd. If you switch using xy package you would have no problem. The syntax of xy is very complicated and difficult to digest initially; you would be able to create switch graphs almost identical to tikz-cd. In many aspects xy package is similar, for simple commutative graphs, to tikz-cd. See example below.

\documentclass[11pt,a4paper]{report}
\usepackage{quantikz}
\usepackage[all,cmtip]{xy}% to have the tips of tikz-cd
\begin{document}
\begin{xymatrix}{
    & B \\
    A \ar[ru] &}  
\end{xymatrix}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .