Skip to content

Commit

Permalink
bugfix: use layout when placing tikz operations for nq gates
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Davis authored and stylewarning committed Oct 25, 2021
1 parent 0adb180 commit 66e7130
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
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)))))

0 comments on commit 66e7130

Please sign in to comment.