Skip to content

Commit 60fe50e

Browse files
authored
Make circuit latex code accessible (#108)
- QubitCircuit.latex_code() now returns the full latex code, which can be saved as a tex file and compiled externally. - The algorithm will print the latex code if pdflatex failed. This simplifies the debugging process.
1 parent d319fb4 commit 60fe50e

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/qutip_qip/circuit.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ def latex_code(self):
20412041
code += r" & %s" % rows[m][n]
20422042
code += r" & \qw \\ " + "\n"
20432043

2044-
return code
2044+
return _latex_template % code
20452045

20462046
# This slightly convoluted dance with the conversion formats is because
20472047
# image conversion has optional dependencies. We always want the `png` and
@@ -2103,6 +2103,17 @@ def _to_qasm(self, qasm_out):
21032103
op._to_qasm(qasm_out)
21042104

21052105

2106+
_latex_template = r"""
2107+
\documentclass{standalone}
2108+
\usepackage[braket]{qcircuit}
2109+
\renewcommand{\qswap}{*=<0em>{\times}}
2110+
\begin{document}
2111+
\Qcircuit @C=1cm @R=1cm {
2112+
%s}
2113+
\end{document}
2114+
"""
2115+
2116+
21062117
class CircuitResult:
21072118
"""
21082119
Result of a quantum circuit simulation.

src/qutip_qip/circuit_latex.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77
import tempfile
88
import warnings
99

10-
_latex_template = r"""
11-
\documentclass{standalone}
12-
\usepackage[braket]{qcircuit}
13-
\renewcommand{\qswap}{*=<0em>{\times}}
14-
\begin{document}
15-
\Qcircuit @C=1cm @R=1cm {
16-
%s}
17-
\end{document}
18-
"""
19-
2010

2111
def _run_command(command, *args, **kwargs):
2212
"""
@@ -215,7 +205,7 @@ def image_from_latex(code, file_type="png"):
215205
try:
216206
os.chdir(temporary_dir)
217207
with open(filename + ".tex", "w") as file:
218-
file.write(_latex_template % code)
208+
file.write(code)
219209
try:
220210
_run_command(
221211
(_pdflatex, "-interaction", "batchmode", filename)
@@ -226,6 +216,11 @@ def image_from_latex(code, file_type="png"):
226216
" Perhaps you do not have it installed, or you are"
227217
" missing the LaTeX package 'qcircuit'."
228218
)
219+
message += (
220+
"The latex code is printed below. "
221+
"Please try to compile locally using pdflatex:\n"
222+
+ code
223+
)
229224
raise RuntimeError(message) from e
230225
_crop_pdf(filename + ".pdf")
231226
if file_type in _MISSING_CONVERTERS:

tests/test_circuit.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,20 @@ def test_wstate(self):
626626
np.testing.assert_allclose(probs_initial, probs_final)
627627
assert sum(result_cbits[i]) == 1
628628

629+
_latex_template = r"""
630+
\documentclass{standalone}
631+
\usepackage[braket]{qcircuit}
632+
\renewcommand{\qswap}{*=<0em>{\times}}
633+
\begin{document}
634+
\Qcircuit @C=1cm @R=1cm {
635+
%s}
636+
\end{document}
637+
"""
638+
629639
def test_latex_code_teleportation_circuit(self):
630640
qc = _teleportation_circuit()
631641
latex = qc.latex_code()
632-
assert latex == "\n".join([
642+
assert latex == self._latex_template % "\n".join([
633643
r" & \lstick{c1} & \qw & \qw & \qw & \qw"
634644
r" & \qw \cwx[4] & \qw & \qw & \ctrl{2} & \qw \\ ",
635645
r" & \lstick{c0} & \qw & \qw & \qw & \qw"

0 commit comments

Comments
 (0)