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

Bugfix: use layout when placing tikz operations for nq gates #754

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
27 changes: 18 additions & 9 deletions src/tools/circuit-diagram.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,24 @@ The convention is that the source operation takes two arguments: the qubit index
:for offset := (- (first target-qubits) q)
:do (append-to-diagram diagram q (tikz-control q offset)))
;; per quantikz: we put the gate on first target line, with correct size
(append-to-diagram diagram
(first target-qubits)
(tikz-gate name
:size (length target-qubits)
:params (quil:application-parameters instr)
:dagger dagger))
;; and then NOP on the rest
(loop :for q :in (rest target-qubits)
:do (append-to-diagram diagram q (tikz-nop))))))))))
(let ((actual-target
(resolve-gate-target target-qubits (diagram-layout diagram))))
(append-to-diagram diagram
actual-target
(tikz-gate name
:size (length target-qubits)
:params (quil:application-parameters instr)
:dagger dagger))
;; and then NOP on the rest
(loop :for q :in target-qubits
:unless (= q actual-target)
:do (append-to-diagram diagram q (tikz-nop)))))))))))


(defun resolve-gate-target (qubits layout)
(flet ((qubit-line (q)
(gethash q layout)))
(a:extremum qubits #'< :key #'qubit-line)))


(defun qubits-in-applications-or-measurements (instrs)
Expand Down
27 changes: 27 additions & 0 deletions tests/tools/circuit-diagram-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@
(cl-quil.tools:plot-circuit pp
:backend :latex
:right-align-measurements t)))))

(deftest test-plot-circuit-nq-gate-layout ()
(let ((pp (quil:parse-quil "
DEFGATE FOO p q r AS PAULI-SUM:
X(pi) p
Y(pi) q
Z(pi) r

FOO 3 2 1"))
(expected
"\\documentclass[convert={density=300,outext=.png}]{standalone}
\\usepackage[margin=1in]{geometry}
\\usepackage{tikz}
\\usepackage{quantikz}

\\begin{document}
\\begin{tikzcd}
\\lstick{\\ket{q_1}} & \\gate[wires=3]{FOO} & \\qw \\\\
\\lstick{\\ket{q_2}} & \\qw & \\qw \\\\
\\lstick{\\ket{q_3}} & \\qw & \\qw \\\\
\\end{tikzcd}
\\end{document}
"))
(is (string= expected
(cl-quil.tools:plot-circuit pp
:backend :latex
:layout-strategy ':increasing)))))