From 26305f321fc79f5432134bf8314fa2de4c972386 Mon Sep 17 00:00:00 2001 From: Erik Davis Date: Tue, 19 Oct 2021 15:05:27 -0700 Subject: [PATCH] bugfix: use layout when placing tikz operations for nq gates --- src/tools/circuit-diagram.lisp | 27 +++++++++++++++++--------- tests/tools/circuit-diagram-tests.lisp | 27 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/tools/circuit-diagram.lisp b/src/tools/circuit-diagram.lisp index a0eaa5917..6b3045d18 100644 --- a/src/tools/circuit-diagram.lisp +++ b/src/tools/circuit-diagram.lisp @@ -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) diff --git a/tests/tools/circuit-diagram-tests.lisp b/tests/tools/circuit-diagram-tests.lisp index 16f9afd46..f7f168d8a 100644 --- a/tests/tools/circuit-diagram-tests.lisp +++ b/tests/tools/circuit-diagram-tests.lisp @@ -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)))))