Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latex-source (and latex) circuit drawer support for standalone (and cleaner output) #6483

Merged
merged 16 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 25 additions & 37 deletions qiskit/visualization/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
self.ops = ops

# image scaling
self.scale = 0.7 if scale is None else scale
self.scale = 1.0 if scale is None else scale

# Map of qregs to sizes
self.qregs = {}
Expand Down Expand Up @@ -157,31 +157,24 @@ def latex(self):

self._initialize_latex_array()
self._build_latex_array()
header_1 = r"""% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
"""
beamer_line = "\\usepackage[size=custom,height=%d,width=%d,scale=%.1f]{beamerposter}\n"
JoshDumo marked this conversation as resolved.
Show resolved Hide resolved
header_2 = r"""% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
header_1 = r"""\documentclass[border=2px]{standalone}
"""

header_2 = r"""
\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}
\usepackage{graphicx}

\begin{document}
"""
header_scale = "\\scalebox{{{}}}".format(self.scale) + "{"

qcircuit_line = r"""
\begin{equation*}
\Qcircuit @C=%.1fem @R=%.1fem @!R {
\Qcircuit @C=%.1fem @R=%.1fem @!R { \\
"""
output = io.StringIO()
output.write(header_1)
output.write("%% img_width = %d, img_depth = %d\n" % (self.img_width, self.img_depth))
output.write(beamer_line % self._get_beamer_page())
output.write(header_2)
output.write(header_scale)
if self.global_phase:
output.write(
r"""{$\mathrm{%s} \mathrm{%s}$}"""
Expand All @@ -195,9 +188,8 @@ def latex(self):
if j != self.img_depth:
output.write(" & ")
else:
output.write(r"\\" + "\n")
output.write("\t }\n")
output.write("\\end{equation*}\n\n")
output.write(r"\\ " + "\n")
output.write(r"\\ " + "}}\n")
output.write("\\end{document}")
contents = output.getvalue()
output.close()
Expand Down Expand Up @@ -228,24 +220,20 @@ def _initialize_latex_array(self):
if isinstance(self.ordered_bits[i], Clbit):
if self.cregbundle:
reg = self.bit_locations[self.ordered_bits[i + offset]]["register"]
self._latex[i][0] = "\\lstick{" + reg.name + ":"
label = reg.name + ":"
clbitsize = self.cregs[reg]
self._latex[i][1] = "\\lstick{/_{_{" + str(clbitsize) + "}}} \\cw"
offset += clbitsize - 1
else:
self._latex[i][0] = (
"\\lstick{"
+ self.bit_locations[self.ordered_bits[i]]["register"].name
+ "_{"
+ str(self.bit_locations[self.ordered_bits[i]]["index"])
+ "}:"
)
label = self.bit_locations[self.ordered_bits[i]]["register"].name + "_{"
label += str(self.bit_locations[self.ordered_bits[i]]["index"]) + "}:"
if self.initial_state:
self._latex[i][0] += "0"
self._latex[i][0] += "}"
label += "0"
label += "}"
self._latex[i][0] = "\\nghost{" + label + " & " + "\\lstick{" + label
else:
if self.layout is None:
label = "\\lstick{{ {{{}}}_{{{}}} : ".format(
label = " {{{}}}_{{{}}} : ".format(
self.bit_locations[self.ordered_bits[i]]["register"].name,
self.bit_locations[self.ordered_bits[i]]["index"],
)
Expand All @@ -257,17 +245,17 @@ def _initialize_latex_array(self):
virt_reg = next(
reg for reg in self.layout.get_registers() if virt_bit in reg
)
label = "\\lstick{{ {{{}}}_{{{}}}\\mapsto{{{}}} : ".format(
label = " {{{}}}_{{{}}}\\mapsto{{{}}} : ".format(
virt_reg.name, virt_reg[:].index(virt_bit), bit_location["index"]
)
except StopIteration:
label = "\\lstick{{ {{{}}} : ".format(bit_location["index"])
label = " {{{}}} : ".format(bit_location["index"])
else:
label = "\\lstick{{ {{{}}} : ".format(bit_location["index"])
label = " {{{}}} : ".format(bit_location["index"])
if self.initial_state:
label += "\\ket{{0}}"
label += " }"
self._latex[i][0] = label
self._latex[i][0] = "\\nghost{" + label + " & " + "\\lstick{" + label

def _get_image_depth(self):
"""Get depth information for the circuit."""
Expand Down
34 changes: 11 additions & 23 deletions test/python/visualization/references/test_latex_4597.tex
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 4, img_depth = 4
\usepackage[size=custom,height=10,width=10,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=0.2em @!R {
\lstick{ {q}_{0} : } & \qw & \qw & \qw & \qw\\
\lstick{ {q}_{1} : } & \qw & \qw & \qw & \qw\\
\lstick{ {q}_{2} : } & \qw & \gate{\mathrm{X}} & \qw & \qw\\
\lstick{c:} & \lstick{/_{_{3}}} \cw & \dstick{_{_{=2}}} \cw \cwx[-1] & \cw & \cw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=0.2em @!R { \\
\nghost{ {q}_{0} : } & \lstick{ {q}_{0} : } & \qw & \qw & \qw & \qw\\
\nghost{ {q}_{1} : } & \lstick{ {q}_{1} : } & \qw & \qw & \qw & \qw\\
\nghost{ {q}_{2} : } & \lstick{ {q}_{2} : } & \qw & \gate{\mathrm{X}} & \qw & \qw\\
\nghost{c:} & \lstick{c:} & \lstick{/_{_{3}}} \cw & \dstick{_{_{=2}}} \cw \cwx[-1] & \cw & \cw\\
\\ }}
\end{document}
38 changes: 13 additions & 25 deletions test/python/visualization/references/test_latex_big_gates.tex
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 6, img_depth = 4
\usepackage[size=custom,height=10,width=40,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=1.0em @!R {
\lstick{ {q}_{0} : } & \multigate{2}{\mathrm{iqp:[[6\,5\,3];\,[5\,4\,5];\,[3\,5\,1]]}}_<<<{0} & \gate{\mathrm{Unitary}} & \qw & \qw\\
\lstick{ {q}_{1} : } & \ghost{\mathrm{iqp:[[6\,5\,3];\,[5\,4\,5];\,[3\,5\,1]]}}_<<<{1} & \multigate{1}{\mathrm{Hamiltonian}}_<<<{0} & \qw & \qw\\
\lstick{ {q}_{2} : } & \ghost{\mathrm{iqp:[[6\,5\,3];\,[5\,4\,5];\,[3\,5\,1]]}}_<<<{2} & \ghost{\mathrm{Hamiltonian}}_<<<{1} & \qw & \qw\\
\lstick{ {q}_{3} : } & \multigate{2}{|\psi\rangle\,\mathrm{(}\mathrm{0.25\jmath},\mathrm{0.3536},\mathrm{0.25+0.25\jmath},\mathrm{0},...\mathrm{)}}_<<<{0} & \multigate{1}{\mathrm{Isometry}}_<<<{0} & \qw & \qw\\
\lstick{ {q}_{4} : } & \ghost{|\psi\rangle\,\mathrm{(}\mathrm{0.25\jmath},\mathrm{0.3536},\mathrm{0.25+0.25\jmath},\mathrm{0},...\mathrm{)}}_<<<{1} & \ghost{\mathrm{Isometry}}_<<<{1} & \qw & \qw\\
\lstick{ {q}_{5} : } & \ghost{|\psi\rangle\,\mathrm{(}\mathrm{0.25\jmath},\mathrm{0.3536},\mathrm{0.25+0.25\jmath},\mathrm{0},...\mathrm{)}}_<<<{2} & \qw & \qw & \qw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=1.0em @!R { \\
\nghost{ {q}_{0} : } & \lstick{ {q}_{0} : } & \multigate{2}{\mathrm{iqp:[[6\,5\,3];\,[5\,4\,5];\,[3\,5\,1]]}}_<<<{0} & \gate{\mathrm{Unitary}} & \qw & \qw\\
\nghost{ {q}_{1} : } & \lstick{ {q}_{1} : } & \ghost{\mathrm{iqp:[[6\,5\,3];\,[5\,4\,5];\,[3\,5\,1]]}}_<<<{1} & \multigate{1}{\mathrm{Hamiltonian}}_<<<{0} & \qw & \qw\\
\nghost{ {q}_{2} : } & \lstick{ {q}_{2} : } & \ghost{\mathrm{iqp:[[6\,5\,3];\,[5\,4\,5];\,[3\,5\,1]]}}_<<<{2} & \ghost{\mathrm{Hamiltonian}}_<<<{1} & \qw & \qw\\
\nghost{ {q}_{3} : } & \lstick{ {q}_{3} : } & \multigate{2}{|\psi\rangle\,\mathrm{(}\mathrm{0.25\jmath},\mathrm{0.3536},\mathrm{0.25+0.25\jmath},\mathrm{0},...\mathrm{)}}_<<<{0} & \multigate{1}{\mathrm{Isometry}}_<<<{0} & \qw & \qw\\
\nghost{ {q}_{4} : } & \lstick{ {q}_{4} : } & \ghost{|\psi\rangle\,\mathrm{(}\mathrm{0.25\jmath},\mathrm{0.3536},\mathrm{0.25+0.25\jmath},\mathrm{0},...\mathrm{)}}_<<<{1} & \ghost{\mathrm{Isometry}}_<<<{1} & \qw & \qw\\
\nghost{ {q}_{5} : } & \lstick{ {q}_{5} : } & \ghost{|\psi\rangle\,\mathrm{(}\mathrm{0.25\jmath},\mathrm{0.3536},\mathrm{0.25+0.25\jmath},\mathrm{0},...\mathrm{)}}_<<<{2} & \qw & \qw & \qw\\
\\ }}
\end{document}
36 changes: 12 additions & 24 deletions test/python/visualization/references/test_latex_cnot.tex
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 5, img_depth = 7
\usepackage[size=custom,height=10,width=16,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=0.2em @!R {
\lstick{ {q}_{0} : } & \gate{\mathrm{X}} & \ctrl{1} & \ctrl{1} & \ctrlo{1} & \ctrl{1} & \qw & \qw\\
\lstick{ {q}_{1} : } & \qw & \targ & \ctrl{1} & \targ & \ctrlo{1} & \qw & \qw\\
\lstick{ {q}_{2} : } & \qw & \qw & \targ & \ctrlo{-1} & \ctrl{2} & \qw & \qw\\
\lstick{ {q}_{3} : } & \qw & \qw & \qw & \ctrl{-1} & \qw & \qw & \qw\\
\lstick{ {q}_{4} : } & \qw & \qw & \qw & \qw & \targ & \qw & \qw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=0.2em @!R { \\
\nghost{ {q}_{0} : } & \lstick{ {q}_{0} : } & \gate{\mathrm{X}} & \ctrl{1} & \ctrl{1} & \ctrlo{1} & \ctrl{1} & \qw & \qw\\
\nghost{ {q}_{1} : } & \lstick{ {q}_{1} : } & \qw & \targ & \ctrl{1} & \targ & \ctrlo{1} & \qw & \qw\\
\nghost{ {q}_{2} : } & \lstick{ {q}_{2} : } & \qw & \qw & \targ & \ctrlo{-1} & \ctrl{2} & \qw & \qw\\
\nghost{ {q}_{3} : } & \lstick{ {q}_{3} : } & \qw & \qw & \qw & \ctrl{-1} & \qw & \qw & \qw\\
\nghost{ {q}_{4} : } & \lstick{ {q}_{4} : } & \qw & \qw & \qw & \qw & \targ & \qw & \qw\\
\\ }}
\end{document}
32 changes: 10 additions & 22 deletions test/python/visualization/references/test_latex_conditional.tex
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 3, img_depth = 6
\usepackage[size=custom,height=10,width=15,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=0.2em @!R {
\lstick{ {q}_{0} : } & \gate{\mathrm{H}} & \meter & \qw & \gate{\mathrm{H}} & \qw & \qw\\
\lstick{ {q}_{1} : } & \gate{\mathrm{H}} & \qw & \meter & \qw & \qw & \qw\\
\lstick{c:} & \lstick{/_{_{2}}} \cw & \dstick{_{_{0}}} \cw \cwx[-2] & \dstick{_{_{1}}} \cw \cwx[-1] & \dstick{_{_{=2}}} \cw \cwx[-2] & \cw & \cw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=0.2em @!R { \\
\nghost{ {q}_{0} : } & \lstick{ {q}_{0} : } & \gate{\mathrm{H}} & \meter & \qw & \gate{\mathrm{H}} & \qw & \qw\\
\nghost{ {q}_{1} : } & \lstick{ {q}_{1} : } & \gate{\mathrm{H}} & \qw & \meter & \qw & \qw & \qw\\
\nghost{c:} & \lstick{c:} & \lstick{/_{_{2}}} \cw & \dstick{_{_{0}}} \cw \cwx[-2] & \dstick{_{_{1}}} \cw \cwx[-1] & \dstick{_{_{=2}}} \cw \cwx[-2] & \cw & \cw\\
\\ }}
\end{document}
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 4, img_depth = 4
\usepackage[size=custom,height=10,width=12,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=0.2em @!R {
\lstick{ {q}_{0} : } & \gate{\mathrm{X}} & \gate{\mathrm{H}} & \qw & \qw\\
\lstick{ {q}_{1} : } & \gate{\mathrm{X}} & \qw & \qw & \qw\\
\lstick{c_{0}:} & \cw & \cw & \cw & \cw\\
\lstick{c_{1}:} & \cw & \cw & \cw & \cw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=0.2em @!R { \\
\nghost{ {q}_{0} : } & \lstick{ {q}_{0} : } & \gate{\mathrm{X}} & \gate{\mathrm{H}} & \qw & \qw\\
\nghost{ {q}_{1} : } & \lstick{ {q}_{1} : } & \gate{\mathrm{X}} & \qw & \qw & \qw\\
\nghost{c_{0}:} & \lstick{c_{0}:} & \cw & \cw & \cw & \cw\\
\nghost{c_{1}:} & \lstick{c_{1}:} & \cw & \cw & \cw & \cw\\
\\ }}
\end{document}
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 3, img_depth = 4
\usepackage[size=custom,height=10,width=12,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=0.2em @!R {
\lstick{ {q}_{0} : \ket{{0}} } & \gate{\mathrm{X}} & \gate{\mathrm{H}} & \qw & \qw\\
\lstick{ {q}_{1} : \ket{{0}} } & \gate{\mathrm{X}} & \qw & \qw & \qw\\
\lstick{c:0} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=0.2em @!R { \\
\nghost{ {q}_{0} : \ket{{0}} } & \lstick{ {q}_{0} : \ket{{0}} } & \gate{\mathrm{X}} & \gate{\mathrm{H}} & \qw & \qw\\
\nghost{ {q}_{1} : \ket{{0}} } & \lstick{ {q}_{1} : \ket{{0}} } & \gate{\mathrm{X}} & \qw & \qw & \qw\\
\nghost{c:0} & \lstick{c:0} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw\\
\\ }}
\end{document}
36 changes: 12 additions & 24 deletions test/python/visualization/references/test_latex_cswap_rzz.tex
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
% \documentclass[preview]{standalone}
% If the image is too large to fit on this documentclass use
\documentclass[draft]{beamer}
% img_width = 5, img_depth = 8
\usepackage[size=custom,height=10,width=16,scale=0.7]{beamerposter}
% instead and customize the height and width (in cm) to fit.
% Large images may run out of memory quickly.
% To fix this use the LuaLaTeX compiler, which dynamically
% allocates memory.
\documentclass[border=2px]{standalone}

\usepackage[braket, qm]{qcircuit}
\usepackage{amsmath}
\pdfmapfile{+sansmathaccent.map}
% \usepackage[landscape]{geometry}
% Comment out the above line if using the beamer documentclass.
\begin{document}

\begin{equation*}
\Qcircuit @C=1.0em @R=0.2em @!R {
\lstick{ {q}_{0} : } & \gate{\mathrm{X}} & \ctrl{1} & \control \qw & \qw & \qw & \qw & \qw & \qw\\
\lstick{ {q}_{1} : } & \gate{\mathrm{X}} & \qswap & \ctrl{-1} & \qw & \qw & \qw & \qw & \qw\\
\lstick{ {q}_{2} : } & \qw & \qswap \qwx[-1] & \ctrlo{-1} & \qw & \qw & \qw & \qw & \qw\\
\lstick{ {q}_{3} : } & \qw & \qw & \ctrl{-3} & \dstick{\hspace{2.0em}\mathrm{ZZ}\,\mathrm{(}\mathrm{\frac{3\pi}{4}}\mathrm{)}} \qw & \qw & \qw & \qw & \qw\\
\lstick{ {q}_{4} : } & \qw & \qw & \ctrlo{-1} & \qw & \qw & \qw & \qw & \qw\\
}
\end{equation*}
\usepackage{graphicx}

\begin{document}
\scalebox{1.0}{
\Qcircuit @C=1.0em @R=0.2em @!R { \\
\nghost{ {q}_{0} : } & \lstick{ {q}_{0} : } & \gate{\mathrm{X}} & \ctrl{1} & \control \qw & \qw & \qw & \qw & \qw & \qw\\
\nghost{ {q}_{1} : } & \lstick{ {q}_{1} : } & \gate{\mathrm{X}} & \qswap & \ctrl{-1} & \qw & \qw & \qw & \qw & \qw\\
\nghost{ {q}_{2} : } & \lstick{ {q}_{2} : } & \qw & \qswap \qwx[-1] & \ctrlo{-1} & \qw & \qw & \qw & \qw & \qw\\
\nghost{ {q}_{3} : } & \lstick{ {q}_{3} : } & \qw & \qw & \ctrl{-3} & \dstick{\hspace{2.0em}\mathrm{ZZ}\,\mathrm{(}\mathrm{\frac{3\pi}{4}}\mathrm{)}} \qw & \qw & \qw & \qw & \qw\\
\nghost{ {q}_{4} : } & \lstick{ {q}_{4} : } & \qw & \qw & \ctrlo{-1} & \qw & \qw & \qw & \qw & \qw\\
\\ }}
\end{document}
Loading