Skip to content

Commit 4436b85

Browse files
authored
Test qcircuit pdf generation without extra prepare_only argument (#7227)
Let us avoid test-only argument in `circuit_to_pdf_using_qcircuit_via_tex`. Mock `Document.generate_pdf` so we can test without the `latexmk` program. Related to #7220
1 parent 9cb81ad commit 4436b85

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

cirq-core/cirq/contrib/qcircuit/qcircuit_pdf.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def circuit_to_pdf_using_qcircuit_via_tex(
2929
qcircuit_kwargs=None,
3030
clean_ext=('dvi', 'ps'),
3131
documentclass='article',
32-
prepare_only=False,
3332
):
3433
"""Compiles the QCircuit-based latex diagram of the given circuit.
3534
@@ -43,8 +42,6 @@ def circuit_to_pdf_using_qcircuit_via_tex(
4342
default, latexmk is used with the '-pdfps' flag, which produces
4443
intermediary dvi and ps files.
4544
documentclass: The documentclass of the latex file.
46-
prepare_only: If True, only prepare the document, do not generate PDF.
47-
Used only for testing.
4845
4946
Raises:
5047
OSError, IOError: If cleanup fails.
@@ -61,11 +58,10 @@ def circuit_to_pdf_using_qcircuit_via_tex(
6158
doc.packages.append(Package('qcircuit'))
6259
doc.preamble.append(Package('inputenc', options=['utf8']))
6360
doc.append(NoEscape(tex))
64-
if not prepare_only: # pragma: nocover
65-
doc.generate_pdf(filepath, **pdf_kwargs)
66-
for ext in clean_ext:
67-
try:
68-
os.remove(filepath + '.' + ext)
69-
except (OSError, IOError) as e:
70-
if e.errno != errno.ENOENT:
71-
raise
61+
doc.generate_pdf(filepath, **pdf_kwargs)
62+
for ext in clean_ext:
63+
try:
64+
os.remove(filepath + '.' + ext)
65+
except (OSError, IOError) as e:
66+
if e.errno != errno.ENOENT:
67+
raise # pragma: nocover

cirq-core/cirq/contrib/qcircuit/qcircuit_pdf_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from unittest import mock
16+
17+
import pylatex
18+
1519
import cirq
1620
import cirq.contrib.qcircuit.qcircuit_pdf as qcircuit_pdf
1721

1822

19-
def test_qcircuit_pdf_prepare_only():
23+
@mock.patch.object(pylatex.Document, "generate_pdf")
24+
def test_qcircuit_pdf(mock_generate_pdf):
2025
circuit = cirq.Circuit(cirq.X(cirq.q(0)), cirq.CZ(cirq.q(0), cirq.q(1)))
21-
qcircuit_pdf.circuit_to_pdf_using_qcircuit_via_tex(circuit, "/tmp/test_file", prepare_only=True)
26+
qcircuit_pdf.circuit_to_pdf_using_qcircuit_via_tex(circuit, "/tmp/test_file")
27+
mock_generate_pdf.assert_called_once_with(
28+
"/tmp/test_file", compiler="latexmk", compiler_args=["-pdfps"]
29+
)

0 commit comments

Comments
 (0)