-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support common notations in circuit_to_latex_using_qcircuit - PR for Issue #4685 #7151
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
base: main
Are you sure you want to change the base?
Changes from all commits
31ffe34
e1c21bb
0586420
a1484fd
0cb9db2
23269f6
1780947
3898f9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
\documentclass{article}% | ||
\usepackage[T1]{fontenc}% | ||
\usepackage[utf8]{inputenc}% | ||
\usepackage{lmodern}% | ||
\usepackage{textcomp}% | ||
\usepackage{lastpage}% | ||
\usepackage{amsmath}% | ||
\usepackage{qcircuit}% | ||
% | ||
\usepackage[utf8]{inputenc}% | ||
% | ||
\begin{document}% | ||
\normalsize% | ||
\Qcircuit @R=1em @C=0.75em { | ||
\\ | ||
&\lstick{\text{q(0)}}& \qw&\ctrlo{} \qw &\control \qw &\targ \qw &\qswap \qw& \qw&\qw\\ | ||
&\lstick{\text{q(1)}}& \qw&\control \qw\qwx&\targ \qw\qwx&\ctrlo{} \qw\qwx&\qswap\qwx \qw&\qswap \qw&\qw\\ | ||
&\lstick{\text{q(2)}}& \qw&\targ \qw\qwx&\ctrlo{} \qw\qwx&\control \qw\qwx& \qw&\qswap\qwx \qw&\qw\\ | ||
\\ | ||
}% | ||
\end{document} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
import cirq.contrib.qcircuit as ccq | ||
import cirq.testing as ct | ||
|
||
# import cirq.contrib.qcircuit.qcircuit_pdf as pdf | ||
|
||
|
||
def assert_has_qcircuit_diagram(actual: cirq.Circuit, desired: str, **kwargs) -> None: | ||
"""Determines if a given circuit has the desired qcircuit diagram. | ||
|
@@ -165,3 +167,66 @@ def test_sqrt_iswap_diagram(): | |
\\ | ||
}""".strip() | ||
assert_has_qcircuit_diagram(circuit, expected_diagram) | ||
|
||
|
||
def test_latex_formatting(): | ||
# test for proper rendering of failing latex formats | ||
q0, q1, q2 = cirq.LineQubit.range(3) | ||
|
||
# custom gate with a control for zero and one | ||
custom_gate = cirq.X.controlled(2, control_values=(0, 1)) | ||
|
||
circuit = cirq.Circuit( | ||
custom_gate(q0, q1, q2), | ||
custom_gate(q2, q0, q1), | ||
custom_gate(q1, q2, q0), | ||
cirq.SWAP(q0, q1), | ||
cirq.SWAP(q1, q2), | ||
) | ||
|
||
expected_diagram = r""" | ||
\Qcircuit @R=1em @C=0.75em { | ||
\\ | ||
&\lstick{\text{q(0)}}& \qw&\ctrlo{} \qw &\control \qw &\targ \qw &\qswap \qw& \qw&\qw\\ | ||
&\lstick{\text{q(1)}}& \qw&\control \qw\qwx&\targ \qw\qwx&\ctrlo{} \qw\qwx&\qswap\qwx \qw&\qswap \qw&\qw\\ | ||
&\lstick{\text{q(2)}}& \qw&\targ \qw\qwx&\ctrlo{} \qw\qwx&\control \qw\qwx& \qw&\qswap\qwx \qw&\qw\\ | ||
\\ | ||
}""".strip() | ||
|
||
assert_has_qcircuit_diagram(circuit, expected_diagram) | ||
|
||
|
||
# def test_pdf_custom_gates(): | ||
# # test for proper rendering of failing latex formats in a pdf | ||
# q0, q1, q2 = cirq.LineQubit.range(3) | ||
|
||
# # custom gate with a control for zero and one | ||
# custom_gate = cirq.X.controlled(2, control_values=(0, 1)) | ||
|
||
# circuit = cirq.Circuit( | ||
# custom_gate(q0, q1, q2), | ||
# custom_gate(q2, q0, q1), | ||
# custom_gate(q1, q2, q0), | ||
# cirq.SWAP(q0, q1), | ||
# cirq.SWAP(q1, q2), | ||
# ) | ||
|
||
# pdf_kwargs = { | ||
# 'compiler': 'latexmk', | ||
# 'compiler_args': ['-pdf', '-f', '-pdflatex=xelatex', '-quiet'], | ||
# } | ||
|
||
# remove = ['aux', 'fdb_latexmk', 'fls', 'log'] | ||
|
||
# # This test was facing errors due to technical issues in qcuircuit_pdf.py | ||
# # that are out of scope of my issue (4685) | ||
# # It will remain commented out until a resolution is found, | ||
# # or until it is realized that this test is not necessary | ||
|
||
# pdf.circuit_to_pdf_using_qcircuit_via_tex( | ||
# circuit, | ||
# "./cirq-core/cirq/contrib/qcircuit/pdf_test_files/test", | ||
# pdf_kwargs, | ||
# None, | ||
# remove | ||
# ) | ||
Comment on lines
+199
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why this commented-out code is in the file. We don't typically want to leave commented-out code in the final versions of things, so it would be best to remove this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I will remove this - I was trying to test PDF output, but the required packages aren't included in the virtual environment set up, so the tests would not be able to run on everyone's machine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this!
It's really great to provide an example file and rendered output. Would you be open to adding a couple more test cases? The issue #4685 only shows a particular example (namely, the particular case the OP was concerned about), but IMHO the issue is a little broader. This PR is an opportunity to cover a support slightly more operations. Especially welcome would be:
Examples of these gates can be seen in the Qcircuit tutorial and in the Cirq docs about basic circuits & gates. The gates in particular have some similarities, which suggests that the implementation in this PR could be done by looking for commonalities and writing generalized code, rather than very specific code for each individual case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can add more test cases and cover these additional gates. I'll get working on that soon!