Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 7 additions & 11 deletions cirq-core/cirq/contrib/qcircuit/qcircuit_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def circuit_to_pdf_using_qcircuit_via_tex(
qcircuit_kwargs=None,
clean_ext=('dvi', 'ps'),
documentclass='article',
prepare_only=False,
):
"""Compiles the QCircuit-based latex diagram of the given circuit.
Expand All @@ -43,8 +42,6 @@ def circuit_to_pdf_using_qcircuit_via_tex(
default, latexmk is used with the '-pdfps' flag, which produces
intermediary dvi and ps files.
documentclass: The documentclass of the latex file.
prepare_only: If True, only prepare the document, do not generate PDF.
Used only for testing.
Raises:
OSError, IOError: If cleanup fails.
Expand All @@ -61,11 +58,10 @@ def circuit_to_pdf_using_qcircuit_via_tex(
doc.packages.append(Package('qcircuit'))
doc.preamble.append(Package('inputenc', options=['utf8']))
doc.append(NoEscape(tex))
if not prepare_only: # pragma: nocover
doc.generate_pdf(filepath, **pdf_kwargs)
for ext in clean_ext:
try:
os.remove(filepath + '.' + ext)
except (OSError, IOError) as e:
if e.errno != errno.ENOENT:
raise
doc.generate_pdf(filepath, **pdf_kwargs)
for ext in clean_ext:
try:
os.remove(filepath + '.' + ext)
except (OSError, IOError) as e:
if e.errno != errno.ENOENT:
raise # pragma: nocover
12 changes: 10 additions & 2 deletions cirq-core/cirq/contrib/qcircuit/qcircuit_pdf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest import mock

import pylatex

import cirq
import cirq.contrib.qcircuit.qcircuit_pdf as qcircuit_pdf


def test_qcircuit_pdf_prepare_only():
@mock.patch.object(pylatex.Document, "generate_pdf")
def test_qcircuit_pdf(mock_generate_pdf):
circuit = cirq.Circuit(cirq.X(cirq.q(0)), cirq.CZ(cirq.q(0), cirq.q(1)))
qcircuit_pdf.circuit_to_pdf_using_qcircuit_via_tex(circuit, "/tmp/test_file", prepare_only=True)
qcircuit_pdf.circuit_to_pdf_using_qcircuit_via_tex(circuit, "/tmp/test_file")
mock_generate_pdf.assert_called_once_with(
"/tmp/test_file", compiler="latexmk", compiler_args=["-pdfps"]
)
Loading