texlive[55537] Master/texmf-dist: yquant (13jun20)

commits+karl at tug.org commits+karl at tug.org
Sat Jun 13 22:58:17 CEST 2020


Revision: 55537
          http://tug.org/svn/texlive?view=revision&revision=55537
Author:   karl
Date:     2020-06-13 22:58:17 +0200 (Sat, 13 Jun 2020)
Log Message:
-----------
yquant (13jun20)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/latex/yquant/README.md
    trunk/Master/texmf-dist/doc/latex/yquant/yquant-doc.pdf
    trunk/Master/texmf-dist/doc/latex/yquant/yquant-doc.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-circuit.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-config.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-draw.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-env.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-lang.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-langhelper.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-shapes.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant-tools.tex
    trunk/Master/texmf-dist/tex/latex/yquant/yquant.sty

Added Paths:
-----------
    trunk/Master/texmf-dist/doc/latex/yquant/test1.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test10.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test11.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test12.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test13.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test14.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test15.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test16.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test17.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test18.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test2.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test3.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test4.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test5.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test6.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test7.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test8.qasm
    trunk/Master/texmf-dist/doc/latex/yquant/test9.qasm
    trunk/Master/texmf-dist/tex/latex/yquant/yquantlanguage-qasm.sty

Modified: trunk/Master/texmf-dist/doc/latex/yquant/README.md
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/README.md	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/doc/latex/yquant/README.md	2020-06-13 20:58:17 UTC (rev 55537)
@@ -7,8 +7,10 @@
 
 A detailed reference with lots of examples is provided in the PDF version of this Readme. We will sketch some basic usage.
 
+Now yquant also understands circuits written in the qasm language!
+
 ## License
-This material is subject to the LaTeX Project Public License 1.3.
+This material is subject to the LaTeX Project Public License 1.3c.
 
 ## Examples
 Many more examples and explanations can be found in the [PDF version](https://github.com/projekter/yquant/raw/master/doc/latex/yquant/yquant-doc.pdf) of this Readme.

Added: trunk/Master/texmf-dist/doc/latex/yquant/test1.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test1.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test1.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,12 @@
+# 
+# File:   test1.qasm
+# Date:   22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - EPR creation
+#
+        qubit 	q0
+        qubit 	q1
+
+	h	q0	# create EPR pair
+	cnot	q0,q1

Added: trunk/Master/texmf-dist/doc/latex/yquant/test10.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test10.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test10.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,16 @@
+# 
+# File:	  test10.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - multi-qubit gates
+# also demonstrates use of classical bits 
+
+	qubit	q0	
+	cbit	c1
+	qubit	q2
+	
+	h	q0
+	Utwo	q0,c1
+	S	q2
+	Utwo	c1,q2

Added: trunk/Master/texmf-dist/doc/latex/yquant/test11.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test11.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test11.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,20 @@
+# 
+# File:	  test11.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - user-defined 
+# multi-qubit ops
+
+	defbox	fx,2,0,'U_{f(x)}'
+	defbox	fxy,3,0,'U_{f(x,y)}'
+  
+	qubit	q0
+	qubit	q1
+	qubit	q2
+
+	h	q0
+	fx	q0,q1
+	h	q1
+	fxy	q0,q1,q2
+

Added: trunk/Master/texmf-dist/doc/latex/yquant/test12.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test12.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test12.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,20 @@
+# 
+# File:	  test12.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - multi-qubit controlled
+# multi-qubit operations
+
+	defbox	CU2,3,1,'U'
+	defbox	CV2,3,1,'V'
+
+	qubit	q0
+	qubit	q1
+	qubit	q2
+	
+	h	q0
+	CU2	q0,q1,q2
+	h	q0
+	CV2	q2,q0,q1
+	
\ No newline at end of file

Added: trunk/Master/texmf-dist/doc/latex/yquant/test13.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test13.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test13.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,42 @@
+# 
+# File:	  test13.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - three-qubit phase
+# estimation circuit with QFT and controlled-U
+
+	defbox	CU,3,1,'U'
+	defbox	CU2,3,1,'U^2'
+	defbox	CU4,3,1,'U^4'
+	def	c-S,1,'S'
+	def	c-T,1,'T'
+
+	qubit	j0,0	# QFT qubits
+	qubit	j1,0
+	qubit	j2,0
+	qubit	s0	# U qubits
+	qubit	s1
+
+	h	j0	# equal superposition
+	h	j1
+	h	j2
+
+	CU4	j0,s0,s1	# controlled-U
+	CU2	j1,s0,s1
+	CU	j2,s0,s1
+
+	h	j0	# QFT
+	c-S	j0,j1
+	h	j1
+	nop	j0
+	c-T	j0,j2
+	c-S	j1,j2
+	h	j2
+	nop	j0
+	nop	j0
+	nop	j1
+	
+	measure	j0	# final measurement
+	measure	j1	
+	measure	j2	

Added: trunk/Master/texmf-dist/doc/latex/yquant/test14.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test14.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test14.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,49 @@
+# 
+# File:	  test14.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - three-qubit FT QEC
+# circuit with syndrome measurement
+
+	defbox	synd,4,0,'\txt{Process\\Syndrome}'
+	defbox	rop,7,4,'{\symcal R}'
+
+	qubit	q0	# code data qubits
+	qubit	q1
+	qubit	q2
+	qubit	s0,0	# syndrome measurement qubits
+	qubit	s1,0
+	cbit	c0,0	# classical bits to store syndromes
+	cbit	c1,0
+
+	h	s0	# create EPR pair for FT meas
+	cnot	s0,s1
+	cnot	q0,s0	# measure parity of q0,q1
+	nop	s1	# prevent cnot's from colliding
+	cnot	q1,s1
+	cnot	s0,s1	# uncreate EPR
+	h	s0
+	measure	s0	# measure syndrome qubits
+	nop	s1
+	measure s1
+	cnot	s0,c0	# copy to classical bits
+	nop	s1
+	cnot	s1,c1
+	space	s0
+
+	zero	s0
+	zero	s1
+	h	s0	# create EPR pair for FT meas
+	cnot	s0,s1
+	cnot	q1,s0	# measure parity of q1,q2
+	nop	s1	# prevent cnot's from colliding
+	cnot	q2,s1
+	cnot	s0,s1	# uncreate EPR
+	h	s0
+	measure	s0	# measure syndrome qubits
+	nop	s1
+	measure s1
+
+	synd	s0,s1,c0,c1
+	rop	s0,s1,c0,c1,q0,q1,q2

Added: trunk/Master/texmf-dist/doc/latex/yquant/test15.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test15.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test15.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,17 @@
+# 
+# File:	  test15.qasm
+# Date:	  24-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - "D-type" measurement
+# requested by Nielsen
+
+	def	MeasZ,0,'\dmeterwide{HZ_\theta}{18pt}'
+
+	qubit	q0,\psi
+	qubit	q1,+
+
+	nop	q0
+	ZZ	q0,q1
+	nop	q0
+	MeasZ	q0

Added: trunk/Master/texmf-dist/doc/latex/yquant/test16.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test16.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test16.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,22 @@
+# 
+# File:	  test16.qasm
+# Date:	  24-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - example from Nielsen
+# paper on cluster states
+
+	qubit	q0,\psi
+	qubit	q1,\psi
+	qubit	q2,\phi
+	qubit	q3,0
+
+	nop	q0
+	nop	q0
+	slash	q0
+	nop	q1
+	ZZ	q1,q2
+	cnot	q2,q3
+	nop	q2
+	discard	q2
+	dmeter	q3

Added: trunk/Master/texmf-dist/doc/latex/yquant/test17.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test17.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test17.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,22 @@
+# 
+# File:	  test17.qasm
+# Date:	  24-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - example from Nielsen
+# paper on cluster states
+
+	def	MeasH,0,'\dmeter{H}'
+
+	qubit	q0,\psi
+	qubit	q1,+
+	qubit	q2,+
+	qubit	q3,\phi
+
+	nop	q0
+	nop	q2
+	ZZ	q0,q1
+	ZZ	q2,q3
+	ZZ	q1,q2
+	MeasH	q1
+	MeasH	q2

Added: trunk/Master/texmf-dist/doc/latex/yquant/test18.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test18.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test18.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,20 @@
+# 
+# File:	  test18.qasm
+# Date:	  25-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - multiple-control bullet op
+
+	def	MeasH,0,'\dmeter{H}'
+	def	Z4,3,'bullet'	# handled specially
+
+	qubit	q0,\psi
+	qubit	q1,+
+	qubit	q2,+
+	qubit	q3,\phi
+
+	nop	q0
+	nop	q2
+	Z4	q0,q1,q2,q3
+	MeasH	q1
+	MeasH	q2

Added: trunk/Master/texmf-dist/doc/latex/yquant/test2.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test2.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test2.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,20 @@
+# 
+# File:   test2.qasm
+# Date:   29-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - simple teleportation circuit
+#
+        qubit 	q0
+        qubit 	q1
+	qubit 	q2
+
+	h	q1	# create EPR pair
+	cnot	q1,q2
+	cnot	q0,q1	# Bell basis measurement
+	h	q0
+	nop	q1
+	measure	q0	
+	measure	q1
+	c-x	q1,q2	# correction step
+	c-z	q0,q2

Added: trunk/Master/texmf-dist/doc/latex/yquant/test3.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test3.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test3.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,13 @@
+# 
+# File:   test3.qasm
+# Date:   22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - swap circuit
+#
+        qubit 	q0
+        qubit 	q1
+
+	cnot	q0,q1
+	cnot	q1,q0
+	cnot	q0,q1

Added: trunk/Master/texmf-dist/doc/latex/yquant/test4.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test4.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test4.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,23 @@
+# 
+# File:	  test4.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - quantum
+# fourier transform on three qubits
+
+	def	c-S,1,'S'
+	def	c-T,1,'T'
+
+	qubit	j0
+	qubit	j1
+	qubit	j2
+
+	h	j0	
+	c-S	j1,j0
+	c-T	j2,j0
+	nop	j1
+	h	j1
+	c-S	j2,j1
+	h	j2
+	swap	j0,j2

Added: trunk/Master/texmf-dist/doc/latex/yquant/test5.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test5.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test5.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,15 @@
+# 
+# File:	  test5.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - demonstrate arbitray qubit matrix ops
+
+	def	c-P,1,'\m{e^{i\alpha} & 0 \cr 0 & e^{-i\alpha}}'
+	def	Ryt,0,'\m{\cos{\theta}&-\sin{\theta}\cr\sin{\theta}&\cos{\theta}}'
+
+	qubit	j0
+	qubit	j1
+
+	c-P	j0,j1
+	Ryt	j0
\ No newline at end of file

Added: trunk/Master/texmf-dist/doc/latex/yquant/test6.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test6.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test6.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,20 @@
+# 
+# File:	  test6.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - demonstrate
+# multiple-qubit controlled single-q-gates
+
+	def	c-U,3,'U'
+
+	qubit	j0
+	qubit	j1
+	qubit	j2
+	qubit	j3
+
+	toffoli	j0,j1,j2
+	X	j0
+	c-U	j2,j3,j0,j1
+	H	j2
+	measure	j3

Added: trunk/Master/texmf-dist/doc/latex/yquant/test7.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test7.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test7.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,21 @@
+# 
+# File:	  test7.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - measurement
+# of operator with correction
+
+	def	c-U,1,'U'
+	def	c-V,1,'V'
+
+	qubit	q0
+	qubit	q1
+
+	H	q0
+	c-U	q0,q1
+	H	q0
+	measure	q0
+	c-V	q0,q1
+	nop	q0
+	nop	q1

Added: trunk/Master/texmf-dist/doc/latex/yquant/test8.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test8.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test8.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,23 @@
+# 
+# File:	  test8.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - stage in
+# simplification of quantum teleportation
+
+	def	c-Z,1,'Z'
+
+	qubit	q0,\psi
+	qubit	q1,0
+	qubit	q2,0
+
+	H	q1
+	cnot	q0,q1
+	cnot	q1,q2
+	cnot	q0,q1
+	cnot	q1,q2
+	H	q0
+	c-Z	q2,q0
+	H	q0
+	H	q0

Added: trunk/Master/texmf-dist/doc/latex/yquant/test9.qasm
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/test9.qasm	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/latex/yquant/test9.qasm	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,20 @@
+# 
+# File:	  test9.qasm
+# Date:	  22-Mar-04
+# Author: I. Chuang <ichuang at mit.edu>
+#
+# Sample qasm input file - two-qubit gate circuit
+# implementation of Toffoli 
+
+	def	c-X,1,'\sqrt{X}'
+	def	c-Xd,1,'{\sqrt{X}}^\dagger'
+
+	qubit	q0
+	qubit	q1
+	qubit	q2
+
+	c-X	q1,q2
+	cnot	q0,q1
+	c-Xd	q1,q2
+	cnot	q0,q1
+	c-X	q0,q2

Modified: trunk/Master/texmf-dist/doc/latex/yquant/yquant-doc.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/doc/latex/yquant/yquant-doc.tex
===================================================================
--- trunk/Master/texmf-dist/doc/latex/yquant/yquant-doc.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/doc/latex/yquant/yquant-doc.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -14,8 +14,9 @@
 \definecolor{darkblue}{RGB}{5, 10, 122}
 \usepackage[english]{babel}
 \usepackage[colorlinks, linkcolor=darkblue, bookmarksdepth=paragraph]{hyperref}
-\usepackage{yquant,braket,hyphenat,microtype,hologo,minted}
+\usepackage{yquant,braket,hyphenat,microtype,hologo,minted,import}
 \usetikzlibrary{quotes,fit,shapes.symbols,backgrounds,quantikz}
+\useyquantlanguage{qasm}
 \usepackage{amsmath,adjustbox,cleveref}
 \usepackage[framemethod=tikz]{mdframed}
 % END_FOLD
@@ -52,16 +53,17 @@
    \endgroup\vskip-\parskip%
    \noindent\ignorespaces%
 }
-\newenvironment{option}[1]{\vskip5mm%
+\newenvironment{option*}[2]{\vskip5mm%
    \noindent\begin{minipage}[t]{\linewidth}%
       \begingroup%
          \let\minted at inline@iii=\patched at minted@inline at iii%
          \raggedleft%
          \hspace*{-1cm}%
-         \phantomsection\label{style:/yquant/#1}%
-         \texttt{\textcolor{gray}{/yquant/}\textcolor{mintgreen}{#1}}\hfill default: %
+         \phantomsection\label{style:/#1/#2}%
+         \texttt{\textcolor{gray}{/#1/}\textcolor{mintgreen}{#2}}\hfill default: %
          \tex}{%
    \end{minipage}}
+\newenvironment{option}{\csname option*\endcsname{yquant}}{\csname endoption*\endcsname}
 
 \newsavebox\codeexamplebox
 \newenvironment{codeexample}{%
@@ -196,8 +198,8 @@
    \section{Introduction}
       This document outlines the scope and usage of the \Yquant{} package.
       It contains both a reference and a huge number of examples.
-      \Yquant{} is a package that makes typesetting quantum circuits easy; the package is not yet available on CTAN.
-      This alpha version~0.1.2 \emph{should} be stable and interfaces are not very likely to change in an incompatible way in the future.
+      \Yquant{} is a package that makes typesetting quantum circuits easy; the package is available on CTAN.
+      This alpha version~0.3 \emph{should} be stable and interfaces are not very likely to change in an incompatible way in the future.
       Please do report all issues and desirable additions.
       
       \subsection{How to read the manual}
@@ -209,7 +211,9 @@
          Then, have a look at \cref{sec:tikz} (or \cref{sec:alt}).
       
       \subsection{Installation}
-         At the moment, clone this repository or download a copy and extract the files to a path visible to your \TeX{} compiler.
+         The recommended way of installation is through CTAN.
+         A direct installation from this Git repository to obtain the latest additions and features is be possible by just cloning it to a path visible to your \TeX{} compiler.
+         While the repository may contain new additions, they are not thoroughly tested until they end up on CTAN; features that are not documented in this manual are entirely unreliable.
          For example, you may put them in the same directory as your document (if you just want to give a try), or you may extract them to \texttt{tex/latex/yquant} in your local \texttt{texmf} (followed by an update of the file name database).
       
       \subsection{Purpose of \Yquant, alternatives}\label{sec:alt}
@@ -229,6 +233,8 @@
                      Changes to the output, while possible, will be overwritten if \texttt{qasm2circ} is run again.
                      \pkg{qasm} output often looks sub\hyp optimal do to the fact that, e.g., rectangles are made up of four lines that do not properly connect and give a crumbly general feeling.
                      
+                     Note that since version~0.3, \Yquant{} understands \pkg{qasm} syntax, see \cref{sec:foreign:qasm}.
+                     
                      Maintenance status: last update of \pkg{qasm} in 2005. Also, \pkg{xy} was last updated in 2013, and the script is not compatible out\hyp of\hyp the\hyp box with Python~3, though an automatic conversion should work.
                   \item \pkg{qcircuit} is probably the most\hyp widely used package.
                      It provides commands that make it much easier to create quantum circuits using the \pkg{xy} package.
@@ -243,7 +249,7 @@
                      It also reduces burdens of the \pkg{xy} syntax.
                      However, the disadvantages of the grid\hyp based syntax still remain.
                      
-                     Maintenance status: last update in 2019; the underlying \TikZ{} is actively maintained again by now.
+                     Maintenance status: last update in 2020; the underlying \TikZ{} is actively maintained again by now.
                   \item \pkg{qpic} follows the approach of \pkg{qasm}: It makes use of an external Python program that reads the quantum circuits in an own language and converts them into \TikZ{} commands.
                      The language \pkg{qpic} follows is much more powerful than \pkg{qasm}'s.
                      The disadvantage that modifications in the output code will not remain after running the Python script again is mitigated by the possibility to define own \TeX{} macros.
@@ -292,7 +298,7 @@
             The optional arguments for the \tex!yquant! environment have to appear \emph{on the same line} as the environment itself.
             If you want to put the arguments into a new line, it is crucial to mask the line break by putting a comment symbol after the environment: \tex!\begin{yquant}%!.
             Without this comment, \Yquant{} will detect your line break (this is one of the few places in \TeX{} where line breaks and spaces are different) and assume that the expression in square brackets instead provides arguments for the following operation! \\
-            Finally note that in (non\hyp fragile) \texttt{beamer} frames, this discrimination between spaces and new lines does not work; the optional arguments will always be counted for the environment, not for the gate.
+            Finally note that in (non\hyp fragile) \pkg{beamer} frames, this discrimination between spaces and new lines does not work; the optional arguments will always be counted for the environment, not for the gate.
             In this case, you can either declare the frame as fragile or (recommended) introduce a blank line between the environment and the options for the first gate.
          \end{warning}
       
@@ -302,6 +308,7 @@
          
          The starred form additionally supports the use of undeclared registers: it basically declares a registers upon its first usage.
          This will always be a \texttt{qubit} register; but if you use the corresponding option and the first usage is an \gate{init} command, you may overwrite this.
+         Subcircuits always use the unstarred form.
          
          Additionally, if you refer to the index $i$ of a vector register of length $L < i$, this register will automatically be enlarged to $i \coloneq L$.
          It is also possible to convert a scalar register into a vector register in this manner.
@@ -435,6 +442,8 @@
          It will draw a \emph{main} part of the gate at the first contiguous slice of registers in the target list---you may select another register for this part by preceding the name or index with a star (which, contrary to the simplified grammar, may only occur \emph{once} in a target specification).
          All other contiguous slices of target registers will be drawn in a \emph{subordinate style} for this gate.
          Finally, all slices will be connected by a single vertical line with the style \style{/yquant/every multi line}.
+         Subcircuits, supported as of version~0.2, will always span the full region from the first to the last register specified in a multi\hyp qubit gate.
+         This is due to the fact that they may contain arbitrary ancilla registers which may be positioned somewhere in between the parts that actually constitute the subcircuit---so this whole region must not have wires of other registers crossing.
          \begin{warning}[Discontiguous targets and control lines]
             A control line extends from the very first to the very last affected register in an operation.
             A sub\hyp gate line that is used for discontiguous registers will only span the range of a multi\hyp register.
@@ -477,7 +486,98 @@
          If there are no positive controls, the list may be empty or, together with the pipe, omitted.
          Preceded by a tilde (``\yquant!~!''), the list of negative controls then follows; this mimics the syntax of many programming languages that denote logical negation by a tilde.
          If there are no negative controls, the list may be empty or, together with the pipe, omitted.
+         
+      \clearpage
+      \subsection{Importing circuits from files}\label{sec:import}
+         Since version~0.2, \Yquant{} provides a simple way to import circuits that are stored in external files.
+         The macro \tex!\yquantimport! can be used in three different contexts:
+         \begin{itemize}
+            \item Outside of a \TikZ{} picture environment. \\
+               In this case, \tex!\yquantimport[<options>]{<filename>}! will be equivalent to
+               \begin{minted}{tex}
+\begin{tikzpicture}
+   \begin{yquant}[<options>]
+      % the content of <filename> goes here
+   \end{yquant}
+\end{tikzpicture}
+               \end{minted}
+               The starred form, \tex!\yquantimport*[<options>]{<filename>}!, instead inserts the starred \Yquant{} environment.
+               Note that the options are always \Yquant{} options; if you want to pass \TikZ{} options, you will have to create the picture environment by yourself or change the option path to the correct one (\texttt{/tikz/.cd}).
+            \item Inside a \TikZ{} picture environment, but outside of a \Yquant{} environment. \\
+               This is the same as before, just that no extra picture environment will be added.
+            \item Inside both a \TikZ{} picture environment and a \Yquant{} environment. \\
+               The file will be inserted directly into the environment.
+               \Yquant's parser is automatically restarted after this.
+               The content will always be put in a \TeX{} group; if additional options are provided, \Yquant{} also inserts a \TikZ{} \tex!scope! and executes \tex!\yquantset{<options>}! directly after the scope.
+               If \tex!\yquantimport! is used, the content will be read as if the containing environment was an unstarred one; if \tex!\yquantimport*! is used, the content will be read as if the containing environment was a starred one.
+         \end{itemize}
+         Note that \Yquant{} internally uses plain \TeX's \tex!\import! command (i.e., \tex!\@@import! in \LaTeX).
+         However, when the \pkg{import} package is loaded, it uses \\ \tex!\subimport{\yquantimportpath}{<filename>}!, where \tex!\yquantimportpath! defaults to \texttt{./}---so by changing this, files from other folders may be imported which by themselves again include other files, and the relative path resolution will work.
+         
+         Note that you may in particular import the content of a \gate{subcircuit}.
       
+      \subsection{Defining own gates}
+         \begin{warning}[Scope]
+            All gate declarations are always global.
+         \end{warning}
+         
+         Since version~0.2.1, if you want to define a gate that corresponds to a single \gate{box} gate with a certain pre\hyp defined content, you may use the macro\\
+         \tex!\yquantdefinebox{<name>}[<style>]{<content>}!, which is far more efficient than the much more general \tex!\yquantdefinegate! introduced below.
+         It works in the following way:
+         \begin{itemize}
+            \item It creates a new gate with name \texttt{<name>} that can be accessed as all the other build\hyp in gates.
+               Note that \texttt{<name>} is case\hyp insensitive and may not contain spaces.
+               Special characters are allowed if \TeX{} can cope with them (i.e., no comment signs, no unbalanced braces, no backslashes...).
+            \item It creates a style \style{/yquant/operators/every <name>} and assigns the optional \texttt{<style>} to it.
+               If no style is provided, the default style will inherit from \style{/yquant/operators/every box}.
+            \item It defines \texttt{<content>} to be the value that is written into the box.
+               This \texttt{<content>} is expanded in a protected manner at the time of gate declaration.
+               You may need to prefix fragile macros by \tex!\protect!.
+         \end{itemize}
+         
+         Sometimes, you may wish to define gates that are more than just a single box---perhaps a succession of multiple gates or even multi\hyp register gates with individual operations on the input registers. \\
+         Since version~0.2, \Yquant{} provides a simple macro that allows this.
+         The macro \tex!\yquantdefinegate{<name>}[<style>]{<content>}! works in the following way:
+         \begin{itemize}
+            \item It creates a new gate with name \texttt{<name>} that can be accessed as all the other built\hyp in gates.
+               Note that \texttt{<name>} is case\hyp insensitive and may not contain spaces.
+               Special characters are allowed if \TeX{} can cope with them (i.e., no comment signs, no unbalanced braces, no backslashes...).
+            \item It creates a style \style{/yquant/operators/every <name>} and assigns the optional \texttt{<style>} to it.
+               If no style is provided, the default style will inherit from \style{/yquant/operators/every custom gate}.
+               This will make the gate ``seamless,'' i.e., avoid highlighting the fact that this is a custom gate.
+            \item It defines a macro that contains \texttt{<content>} (expanded in a protected manner) and that will be inserted as a subcircuit whenever this gate is invoked.
+               This in particular means that if you use \hyperref[sec:import]{\tex!\yquantimport!} within the gate, the file will only be loaded once at the time of declaration.
+         \end{itemize}
+         When the gate is later drawn, the styles are invoked in the following order---remember custom gates are implemented by means of subcircuits---:
+         \begin{enumerate}
+            \item \style{/yquant/every operator}
+            \item \style{/yquant/operators/every <name>}
+            \item \style{/yquant/operators/every subcircuit box}
+            \item \style{/yquant/this operator}
+            \item \style{/yquant/operators/this subcircuit box}
+         \end{enumerate}
+         Gates defined in this way can only make use of the default gates or other custom gates.
+         They do not accept custom arguments, and it is not possible to declare own, custom shapes in this way (though other predefined shapes may be used).
+         If they are used in a multi\hyp qubit manner, they will never be split into contiguous slices (but their content will be, so if you use the default style that turns off the box, the only way to notice this is that intermediate unaffected gates will not be allowed to place gates within the custom gate).
+         
+         \begin{warning}[Redefining existing gates]
+            The above macros will issue an error if the gate already exists.
+            You can use \tex!\yquantredefinebox! or \tex!\yquantredefinegate! to overwrite existing gate definitions.
+            Note that this will overwrite \emph{any} gate, even the built\hyp in ones.
+            
+            Generally, it is discouraged to make use of this possibility.
+            For custom gates, if you redefine a gate as a box which was previously a general subcircuit\hyp based gate, the macro that contains the subcircuit will still be held in memory.
+            Overwriting built\hyp in gates will not clear the attributes associated to this gate (though required attributes will no longer be required afterwards).
+            Again, this is not a problem but prevents \Yquant{} from issuing potentially helpful error message if such a---now meaningless---attribute is used.
+            
+            Finally, once a built\hyp in gate is overwritten, it cannot be restored.
+            In particular, the register creation pseudo\hyp gates \gate{qubit}, \gate{cbit}, \gate{qubits}, and \gate{nobit} perform some magic that cannot be mimicked with custom gates.
+         \end{warning}
+         
+         More advanced declaration of custom gates requires the use of backend macros.
+         Refer to \texttt{yquant-lang.tex} for this.
+         For the declaration of custom shapes, see \texttt{yquant-shapes.tex} for examples.
+      
    \section{Configuration}\label{sec:config}
       \Yquant{} uses \pkg{pgfkeys} to control its options, which are located in the path \texttt{/yquant}.
       The following list contains all options and styles that are recognized, apart from gate arguments.
@@ -560,6 +660,10 @@
             The decoration \texttt{gapped brace} allows to additionally specify the regions in which a line should be drawn by using the \texttt{/tikz/decoration/from to} key, which expects a comma\hyp separated list of dimension ranges, and which is automatically populated by \Yquant.
             Since it may happen that the arch of the brace needs to be shifted from the value specified in \texttt{/tikz/decoration/aspect} (else, it would be drawn into a gap), the special key \texttt{/yquant/gapped brace/apply shift} is installed, which transforms the \texttt{pos=-1} specification into a position that corresponds to the actual \texttt{aspect} value.
          \end{option}
+         
+         \begin{option}{every input label}!!
+            This style is installed for every register name label in a \gate{subcircuit} when the register is an input (or input and output) register.
+         \end{option}
       
       \subsection{Register outputs}\unskipOpt
          \begin{option}{every output}!shape=yquant-text, anchor=west, align=left!
@@ -671,6 +775,12 @@
             This style is installed for every \gate{box} operator.
          \end{option}
          
+         \begin{option}{operators/every custom gate}!/yquant/operators/this subcircuit box/.append style={draw=none, inner sep=0pt}, /yquant/register/default name=!
+            This style is by default installed for every user\hyp defined gate (since version~0.2).
+            User\hyp defined gates are implemented via subcircuits; this style suppresses the box that surrounds the subcircuit and by default suppresses all register names.
+            This allows a seamless integration of the gate/subcircuit into the main circuit, without putting particular emphasis to the fact that what was defined as the custom gate indeed belongs together.
+         \end{option}
+         
          \begin{option}{operators/every dmeter}!shape=yquant-dmeter, x radius=2mm, y radius=2mm, fill=white, draw!
             This style is installed for every \gate{dmeter} gate.
             The \texttt{yquant-dmeter} shape consists of a rectangle whose right side is replaced by a circle, resembling the letter ``D.''
@@ -708,6 +818,28 @@
             This style is installed for every \gate{slash} pseudo\hyp gate, i.e., the one that is used to indicate that a single register line actually denotes multiple registers.
          \end{option}
          
+         \begin{option}{operators/every subcircuit}!!
+            This style is installed for every \gate{subcircuit}.
+         \end{option}
+         
+         \begin{option}{operators/every subcircuit box}!/yquant/operators/every box!
+            This style is installed for every \gate{subcircuit}.
+            Note that in contrast to all other styles such as \style{/yquant/operators/every subcircuit} or \style{/yquant/this operator}, this style is only applied to the ``container'' node of the subcircuit, but not to the elements in the subcircuit themselves.
+         \end{option}
+         
+         \begin{option}{subcircuit box style}!/yquant/operators/every subcircuit box/.append style={#1}!
+            This is a shorthand to apply styles to the subcircuit box only.
+         \end{option}
+         
+         \begin{option}{operators/this subcircuit box}!!
+            This style is appended to the current style installed for the \gate{subcircuit}, but will not apply to its contents.
+            Additionally, this style will be reset to an empty style at the beginning of each subcircuit, so that it really only applies to exactly the subcircuit box it is explicitly specified on, not to nested subcircuit boxes.
+         \end{option}
+         
+         \begin{option}{this subcircuit box style}!/yquant/operators/this subcircuit box/.append style={#1}!
+            This is a shorthand to apply styles to the current subcircuit box only.
+         \end{option}
+         
          \begin{option}{operators/every swap}!shape=yquant-swap, radius=.75mm, draw!
             This style is installed for every \gate{swap} gate that interchanges two qubits.
             The \texttt{yquant-swap} shape consists of a single cross.
@@ -755,6 +887,9 @@
       Multiple slices in a discontiguous multi\hyp register are additionally suffixed by \texttt{-s\textit{<slice index>}}.
       All controls are also named, suffixed by \tex!-p\idx! or \tex!-n\idx! for positive and negative controls (i.e., the topmost positive control of the previous operator will be available as \texttt{op-p0}).
       Counters for target registers, positive, and negative controls are all independent.
+      Finally, you can even access names within a subcircuit, provided you give a name to the subcircuit.
+      All nodes in the subcircuit will then have the name \texttt{\textit{<subcircuit name>}-\textit{<name specified in the subcircuits>}}.
+      For nested subcircuits, you will get multiple prefixes.
       
       All \Yquant{} shapes have the anchors available you would typically expect from a \TikZ{} shape of the given outline.
       Before version~0.1.2, \Yquant{} shapes needed to provide a \texttt{circuit} anchor and projection anchors.
@@ -786,9 +921,6 @@
                \item Remove the clipping paths stored so far on this register, apart from the clipping on the last gate (which will be needed again if this was not the end of the circuit).
             \end{itemize}
       \end{itemize}
-      
-%      The current version of \Yquant{} does not implement subcircuits yet.
-%      However, this is planned in a future version; by naming a subcircuit, all named operations inside should then also become available (prefixed by the subcircuit's name).
    
    \section{Reference: Gates and operations}\label{sec:gates}
       This section lists all operations \Yquant{} currently understands.
@@ -943,7 +1075,12 @@
          Names are case\hyp insensitive.
          The register can be made into a vector register by specifying \texttt{<len>} (default \tex!1!).
          
-         \emph{Possible attributes:} none
+         \clearpage
+         \emph{Possible attributes:}
+         \begin{itemize}
+            \item \yquant![out]! or \yquant![ancilla]! (required in subcircuits) \\
+               \emph{see \gate{qubit}}
+         \end{itemize}
       
       \subsection{\texorpdfstring{\yquant!not!}{not}}\label{gate:not}
          Syntax: \yquant!not <target> | <pcontrol> ~ <ncontrol>;! \\
@@ -994,6 +1131,21 @@
          \begin{itemize}
             \item \yquant![after=<regname>]! \\
                If given, the register will start not at the left of the circuit but instead at the position at which the last gate in the register \texttt{<regname>} ended.
+               
+               This attribute may not be given in combination with \yquant![in]! or \yquant![inout]!.
+            \item \yquant![in]!, \yquant![out]!, \yquant![inout]!, or \yquant![ancilla]! \\
+               Default: \yquant![ancilla]! for top\hyp level circuits (do not change there); \yquant![inout]! for subcircuits.
+               
+               Determines how a subcircuit interacts with its parent circuit.
+               
+               Registers declared with the \yquant![ancilla]! attribute are available only to the subcircuit; they cannot be connected to an outside wire.
+               
+               Registers declared with the \yquant![in]! or \yquant![inout]! attribute will expect an outer wire of the same type to be present and will then be identical with this outer wire.
+               Any changes applied to the wire within the subcircuit automatically also happen on the associated outer wire.
+               If the attribute is \yquant![in]!, the wire will automatically be discarded at the end of the subcircuit (and hence also in the outer circuit, where it may be re\hyp initialized).
+               This is different from applying the \gate{discard} gate in that the wire will still extend until the end of the subcircuit and may thus receive proper \gate{output}s.
+               
+               Registers declared with the \yquant![out]! attribute will expect a discarded outer wire to be present, which will be initialized to a \texttt{qubit} at the beginning of the subcircuit, and from then on be identical with the outer wire.
             \item \yquant![value=<value>]! \\
                Denotes the label that is printed to the left of the wire.
                If the value is omitted, the default is used (\style{/yquant/register/default name}, preinitialized to \tex!\regidx!).
@@ -1044,6 +1196,36 @@
          
          \emph{Possible attributes:} none
       
+      \subsection{\texorpdfstring{\yquant!subcircuit!}{subcircuit}}\label{gate:subcircuit}
+         Syntax: \yquant!subcircuit <target>;! \\
+         This is a subcircuit gate which inserts independent quantum circuits at the current position within the circuit.
+         It may span multiple registers, but is never split into contiguous slices.
+         It allows for controls and may change the type of any target involved, depending on the particular subcircuit.
+         The style \style{/yquant/operators/every subcircuit} is installed.
+         
+         \emph{Possible attributes:}
+         \begin{itemize}
+            \item \yquant!value=<subcircuit>! (required) \\
+               Denotes the content of the subcircuit.
+               It is specified in the usual syntax of \Yquant.
+               Note that, regardless of the outer environment, a subcircuit always implicitly uses the unstarred form, i.e., you must declare every register explicitly before its first usage.
+               This is to make sure that the interface of the circuit, i.e., which registers are taken as input and/or output parameters and in which order, is not accidentally mistaken.
+               
+               The mapping between input and output registers is trivial for single\hyp qubit uses.
+               For multi\hyp qubit uses, it works in the following way---in short, it matches in visual order.
+               You declare input and output registers by using the appropriate attributes on the \gate{qubit}, \gate{cbit}, \gate{qubits} (or even \gate{nobit}) gates.
+               The list of all non\hyp ancillas, from the topmost to the bottom\hyp most, forms the list of parameter registers of the subcircuit.
+               This is exactly the number of registers that must be supplied within one multi\hyp qubit target.
+               Also within the multi\hyp qubit target, we sort all registers from the topmost to the bottom\hyp most (in the order as they visually appear, not the order in which they are entered).
+               Those two lists of equal length are then mapped $1:1$ to each other.
+               Intermixing with ancillas is possible at every position and will lead to a vertical shift of the wires, until all registers, inner and outer, can be displayed flawlessly.
+               
+               As subcircuits follow the same rules as ordinary circuits, it is possible to mix them with arbitrary \TeX{} code, and also to access named gates within the subcircuit---but note that named gates in the outer circuit cannot be accessed (at least unless you play with the \texttt{name prefix} key in \TikZ).
+               In order to access inner nodes from the outer circuit, the subcircuit itself must be named; the inner nodes are then prefixed by the name of the subcircuit and a dash.
+               
+               It is possible to nest subcircuits arbitrarily.
+         \end{itemize}
+      
       \subsection{\texorpdfstring{\yquant!swap!}{swap}}\label{gate:swap}
          Syntax: \yquant!swap <targets> | <pcontrol> ~ <ncontrol>;! \\
          This is the two\hyp qubit \textsc{swap} gate $\ketbra{00}{00} + \ketbra{01}{10} + \ketbra{10}{01} + \ketbra{11}{11}$ that exchanges two qubits.
@@ -1120,6 +1302,8 @@
          \end{minted}
          in the preamble, as is done here.
          
+         Note that since version~0.3, \Yquant{} supports the \pkg{qasm} syntax, see \cref{sec:foreign:qasm}
+         
          \begin{example}[test1 (create an EPR pair)]
             \begin{codeexample}
 \begin{tikzpicture}
@@ -2201,7 +2385,7 @@
    \node[inner, anchor=east] at (a-3 -| u.east) {\smash{$y \oplus f(x)$}};
 \end{tikzpicture}
                \end{codeexample*}
-               There is no simple way to draw \emph{within} a gate, though this is probably something that will be easier using subcircuits (planned feature).
+               There is no simple way to draw \emph{within} a gate, unless this gate is a proper quantum circuit itself.
                Instead, here the intricate parts were reproduced using \TikZ: first, we make sure we assign a name to every relevant coordinate.
                Then we use some \TikZ{} styles to draw the braces and nodes at the intersection of these coordinates.
                Here, we also make use of the \texttt{moveto} decoration transformation that comes with \Yquant{} and that allows to enlarge the braces slightly for a good overall appearance.
@@ -2481,7 +2665,7 @@
    \end{yquant}
 \end{tikzpicture}
                      \end{codeexample*}
-                     While here, we opted for the most logical choice to name the declaration of the register, a name put to any operation on the desired register would also serve the purpose.
+                     While here, we opted for the most logical choice to name the declaration of the register, a name put to any operation on the desired register would also serve the purpose (as long as this operation is vertically symmetrical, which subcircuits may not be).
                   \end{example}
                   
                   \clearpage
@@ -2646,32 +2830,51 @@
          draw, inner sep=6pt, "reversed c-\textsc{not}"] {};
 \end{tikzpicture}
                   \end{codeexample*}
+                  In this case, since version~0.2, a subcircuit may provide a similar experience:
+                  \begin{codeexample*}
+% \usetikzlibrary{quotes}
+\begin{tikzpicture}
+   \begin{yquant*}
+      h a;
+      cnot b | a;
+      [this subcircuit box style={inner sep=6pt, "reversed c-\textsc{not}"}]
+      subcircuit {
+         qubit {} x;
+         qubit {} y;
+         h -;
+         cnot y | x;
+         h -;
+      } (-);
+      cnot b | a;
+      h b;
+   \end{yquant*}
+\end{tikzpicture}
+                  \end{codeexample*}
+                  Here, we used the key \style{/yquant/this subcircuit box style} to influence only the style of the subcircuit box itself instead of providing global options that apply to every object in the subcircuit (you wouldn't want the label be assigned to every single gate).
                \end{example}
                
                \clearpage
                \begin{example}
                   \begin{codeexample*}
-% \usetikzlibrary{quotes, backgrounds}
+% \usetikzlibrary{quotes}
 \begin{tikzpicture}
    \begin{yquant*}
       h a;
-      [name=left]
-      cnot b | a;
-      cnot a | b;
-      [name=right]
-      cnot b | a;
+      [this subcircuit box style={draw, dashed, rounded corners, fill=blue!20, inner xsep=6pt, inner ysep=10pt, "\textsc{swap}" below}, register/default name=]
+      subcircuit {
+         qubit a;
+         qubit b;
+         cnot b | a;
+         cnot a | b;
+         cnot b | a;
+      } (a-b);
       h b;
    \end{yquant*}
-   \scoped[on background layer]
-      \node[fit=(left-0) (left-p0) (right-0) (right-p0),
-            draw, dashed, rounded corners, fill=blue!20,
-            inner xsep=6pt, inner ysep=10pt,
-            "\textsc{swap}" below] {};
 \end{tikzpicture}
                   \end{codeexample*}
-                  In this example, we need to refer to names, but want to fill the background before those nodes are actually available.
-                  Hence, we use the layering mechanism of \TikZ{} and put the node on the background layer.
-                  Alternatively, we could have drawn on top and used opacity to still make visible what is behind; but in general, whenever you can avoid to use opacities, do avoid it; it adds overhead at the renderer and may give sub\hyp optimal result when printing since the viewer has to reduce all elements to non\hyp overlapping parts.
+                  Since version~0.2, fully enclosing a bunch of operations (with no controls extending to some inner component) is possible by means of \gate{subcircuit}s.
+                  Before, this had to be done using named operations and layers.
+                  Note that here we used the style \style{/yquant/this subcircuit box style} to assign a styling that only applies to the box containing the subcircuit, but not to the inner gates---which would have happened had we just given the arguments to the subcircuit directly.
                \end{example}
                
                \Yquant{} does not support the fancy nearest\hyp neighbor swap gate that \pkg{quantikz} has.
@@ -2726,30 +2929,97 @@
             \end{example}
       \endgroup
    
+   \section{Foreign language support}
+      \Yquant{} is built in various modules, so that it is not hard to use the quantum circuit rendering backend, but expose a different language frontend.
+      Since version~0.3, \Yquant{} not only understands its own language, but also others.
+      
+      \subsection{qasm}
+         By saying \tex!\useyquantlanguage{qasm}! in the preamble after loading \Yquant{} itself, the parser for \pkg{qasm} (not OpenQASM) is loaded.
+         It provides the environment \tex!qasm! as well as the macro \tex!\qasmimport!, which works similarly to \tex!\yquantimport! (but does not accept additional options).
+         
+         \subsubsection{Language specification}
+            The \pkg{qasm} language is not formally defined, but an overview is provided at \href{https://web.archive.org/web/20050410022847/https://www.media.mit.edu/quanta/qasm2circ/#spec}{the archived website of \texttt{qasm2circ}}.
+            The \Yquant{} implementation is designed to be compatible with the original parser, with the following exceptions:
+            \begin{itemize}
+               \item In \pkg{qasm}, lines could begin in an arbitrary manner; the first whitespace followed by the first valid command were then the instruction.
+                  Contrary to this, \Yquant's parser always expects a line to start with a valid gate (preceded by arbitrary whitespaces), a comment, or to be empty.
+               \item In \pkg{qasm}, user\hyp defined gates will be drawn in a box unless they contain the text \texttt{\textbackslash dmeter}, and they will be recognized as measurement gates if they contain \texttt{\textbackslash meter} or \texttt{\textbackslash dmeter}.
+                  Contrary to this, \Yquant's parser expect the gates to \emph{start} with one of the \emph{macros} \tex!\meter!, \tex!\dmeter!, or \tex!\dmeterwide!.
+                  Using these macros \emph{within} the content of a gate does not make sense from the point of view that in \Yquant, gates are nodes with shapes, so either the full gate has a particular shape or it does not, but not only parts of it.
+               \item The \texttt{space} gate is supposed to produce a horizontal whitespace without a gate.
+                  In \Yquant's implementation, you have to discard the wire if you want to reproduce this behavior; \texttt{space} and \texttt{nop} are equivalent.
+            \end{itemize}
+            
+            The default \pkg{qasm} style defines several macros that can be used in gates.
+            \Yquant{} makes \tex!\m! (matrix; requires \pkg{amsmath}) and \tex!\txt! (switch to text mode) available within the \pkg{qasm} environment.
+            
+            Do not expect \Yquant's output to match the one of \pkg{qasm} exactly.
+            \Yquant{} is not grid based, so that commands such as \texttt{nop} don't even make sense.
+            They are implemented for compatibility reasons and will produce a fixed horizontal space of the operator minimum width plus one separation, which might or might not be accurate.
+            
+            Note that whatever you write between \tex!\begin{qasm}! and \tex!\end{qasm}! is essentially treated as verbatim; only where the specification says so (in the definition of a new gate and in the optional third command to the register definition), it is interpreted as \TeX{} markup.
+            Consequently, in \pkg{beamer}, any frame containing these environments must be given the \texttt{verbatim} option.
+         
+         \subsubsection{Configuration}
+            Loading the \pkg{qasm} language interpreter will define several new configuration keys.
+            For all the gates, it will use the keys defined in \cref{sec:config}, and it additionally provides the following:
+            
+            \begin{option}{operators/every s}!/yquant/operators/every box!
+               This style is installed for every \texttt{s} operator.
+            \end{option}
+            
+            \begin{option}{operators/every t}!/yquant/operators/every box!
+               This style is installed for every \texttt{t} operator.
+            \end{option}
+            
+            \begin{option}{operators/every utwo}!/yquant/operators/every box!
+               This style is installed for every \texttt{Utwo} operator.
+            \end{option}
+            
+            \begin{option*}{qasm}{zero}!\qasm at ket0!
+               The content of this macro is used as the initialization content whenever the \texttt{zero} gate is invoked.
+            \end{option*}
+            
+            \begin{option*}{qasm}{register/default qubit name}!\qasm at ket{#1}!
+               This macro is invoked with a single parameter (the name of a qubit register) and gives back what is printed as the name of the register (will be in math mode automatically).
+            \end{option*}
+            
+            \begin{option*}{qasm}{register/default qubit name value}!\qasm at ket{#1} = \qasm at ket{#2}!
+               This macro is invoked with two parameters (the name of a qubit register and its initial value) and gives back what is printed as the name of the register (will be in math mode automatically).
+            \end{option*}
+         
+         \subsubsection{Examples}
+            The unaltered\footnote{Only in \texttt{test14.qasm}, \tex!\cal! was replaced by \tex!\symcal!---and this is only necessary as this manual was set with \pkg{unicode-math}. In traditional mode, even this would not be necessary.} \texttt{.qasm} files provided from \href{https://www.media.mit.edu/quanta/qasm2circ/}{the \texttt{qasm2circ} page} were stored in the subfolder \texttt{qasm} relative to this manual's \TeX{} file.
+            The following command is then used to print all of them:
+            \begin{minted}{tex}
+% preamble:
+% \usepackage{yquant,import}
+% \useyquantlanguage{qasm}
+\def\yquantimportpath{qasm/}
+\foreach \circuitno in {1, ..., 18} {
+   \paragraph{Circuit \#\circuitno}
+      \begin{center}
+         \qasmimport{test\circuitno.qasm}
+      \end{center}
+}
+            \end{minted}
+            
+            \def\yquantimportpath{qasm/}
+            \foreach \circuitno in {1, ..., 18} {
+               \paragraph{Circuit \#\circuitno}\leavevmode\nopagebreak\par\nopagebreak
+                  \begin{adjustbox}{center}
+                     \qasmimport{test\circuitno.qasm}
+                  \end{adjustbox}
+            }
+      
    \section{Wishlist}
       This section contains some thoughts on future improvements and features.
       \begin{itemize}
-         \item Subcircuit support. \\
-            A subcircuit is a quantum circuit on its own that is put into a box within other circuits.
-            It has input, output, and also internal wires.
-            Subcircuits may be declared on\hyp the\hyp fly if they are used only once, but there should also be the option to globally declare subcircuits and use them at any time.
-            As with ordinary quantum circuits, everything in a subcircuit should be allowed to have a name.
-            If the subcircuit itself is then also named, those inner names should be made available (prefixed with the subcircuit's name), to the outer circuit.
-            Subcircuits may also contain subcircuits.
-            While the number of input registers should match, a subcircuit may have more, less or different output registers.
-            The language needs to be extended to somehow allow for this.
-            Subcircuits will typically be multi\hyp qubit elements that, at least if internal wires are used, may significantly increase the required height for an individual register.
-            Hence, the internal height calculations must be adapted.
-            This will be particularly problematic if the subcircuit targets non\hyp adjacent wires.
-         \item Support for other languages. \\
-            It would be particularly nice to introduce a language mode.
-            While the \Yquant{} language will always provide the set of everything \Yquant{} can do at the moment, it would be nice if \Yquant{} can automatically detect \texttt{OpenQASM} and parse its content correctly.
-            \texttt{OpenQASM} is much more limited than \Yquant{} and, being a language designed for actual execution of the circuits, does not provide means to change visual appearance.
-            Probably some \Yquant{} additions to \texttt{OpenQASM} would be ok, as long as they only complement the original language?
-            Also, \texttt{OpenQASM} support would probably require subcircuits.
-            
-            Another nice feature would be to support \pkg{qasm}.
-            Also here, the feature set is much more limited and it would probably be hard to implement an automatic detection, the user would have to specify the language by hand.
+         \item Support for more other languages. \\
+            Since version~0.3, \Yquant{} understands \pkg{qasm}.
+            It would be nice if \Yquant{} could also understand \texttt{OpenQASM} correctly.
+            The way \texttt{OpenQASM} treats operations is a bit different from \Yquant{} and also \pkg{qasm}.
+            Some machinery would be required to automatically carry out transversal gates; apart from that everything should be available in \Yquant.
          \item Vertical layout. \\
             Sometimes, long quantum circuits on a portrait page can be better represented in a vertical layout.
             Also if lots of explanations are to be added, this becomes problematic in the horizontal version.
@@ -2773,5 +3043,15 @@
          \Yquant{} now also properly draws non\hyp contiguous multi\hyp qubit operations. \\
          New gate: \gate{correlate}.
          Various bug fixes.
+      
+      \subsection{2020-06-02: Version 0.2}
+         Introduce \gate{subcircuit}; required rewriting how \Yquant{} internally positions vertically.
+         Provide simple macros to load circuits (or parts) from a file and to declare own custom gates.
+      
+      \subsection{2020-06-07: Version 0.2.1}
+         Introduce a macro to declare a lightweight custom gate, which is only a single box with custom content.
+         
+      \subsection{2020-06-13: Version 0.3}
+         Introduce support for the \pkg{qasm} language.
 %END_FOLD
 \end{document}
\ No newline at end of file

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-circuit.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-circuit.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-circuit.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -1,19 +1,13 @@
 % BEGIN_FOLD Drawing wires
-% a bit faster than nested \@firstoftwo/\@secondoftwo
-% note \@thirdofthree is defined in the latex kernel already.
-\long\def\@firstofthree#1#2#3{#1}%
-\long\def\@secondofthree#1#2#3{#2}%
-\long\def\@firstoffour#1#2#3#4{#1}%
-\long\def\@secondoffour#1#2#3#4{#2}%
-\long\def\@thirdoffour#1#2#3#4{#3}%
-\long\def\@fourthoffour#1#2#3#4{#4}%
-\long\def\@thirdandfourthoffour#1#2#3#4{#3#4}%
-
 % extends the wire of register #1. Assumes a node called yquantbox is set up, and the \pgfshapeclippathhorzresult was set up appropriately for this node.
-\protected\def\yquant at circuit@extendwire#1{%
+\protected\def\yquant at circuit@extendwire#1#2{%
    \begingroup%
-      \pgfpointanchor{yquantbox}{center}%
-      \edef\wirexpos{\the\pgf at x}%
+      \ifx*#2%
+         \edef\wirexpos{\the\dimen0}%
+      \else%
+         \pgfpointanchor{yquantbox}{#2}%
+         \edef\wirexpos{\the\pgf at x}%
+      \fi%
       \yquant at register@get at typeywire{#1}\wiretype\wireypos\wirelast%
       \edef\wirexprevpos{\expandafter\@firstoffour\wirelast}%
       \ifnum\wiretype=\yquant at register@type at none%
@@ -29,7 +23,7 @@
             {\unexpanded\expandafter\expandafter\expandafter{%
                 \expandafter\@thirdandfourthoffour\wirelast%
              }%
-            }
+            }%
             {\unexpanded\expandafter{\pgfshapeclippathhorzresult}}%
          }%
       \fi%
@@ -36,16 +30,39 @@
    \endgroup%
 }
 
-% finishes the wire of registers 1 to #1 until x position \yquant at env@end at xpos
+% finishes the wires of all registers until x position #1
 \protected\def\yquant at circuit@endwires#1{%
-   \yquant at for \yquant at circuit@endwires at i := 1 to #1 {
-      \yquant at draw@wire\yquant at circuit@endwires at i\yquant at env@end at xpos%
+   \ifcsname\yquant at prefix xshift\endcsname%
+      \dimdef\yquant at env@end at xpos{#1+\csname\yquant at prefix xshift\endcsname}%
+   \else%
+      \def\yquant at env@end at xpos{#1}%
+   \fi%
+   \yquant at for \yquant at circuit@endwires at i := 1 to \csname\yquant at prefix registers\endcsname {%
+      % we only extend the wire if it does not come from the outer circuit - this one would be responsible for the extension.
+      \ifcsname\yquant at prefix registermap@\yquant at circuit@endwires at i\endcsname%
+         \xifinlistcs\yquant at circuit@endwires at i{\yquant at prefix inonly}{%
+            % however, the wire is to be discarded after this circuit
+            \edef\storedleft{\the\pgf at picminx}%
+            \yquant at draw@wire\yquant at circuit@endwires at i1%
+            \global\pgf at picminx=\storedleft%
+            \yquant at register@set at type\yquant at circuit@endwires at i\yquant at register@type at none%
+         }\relax%
+      \else%
+         \yquant at draw@wire\yquant at circuit@endwires at i1%
+      \fi%
    }%
 }
 
 % outputs the wire according to its previous instructions and prepares for a change in the wire style
 \protected\def\yquant at circuit@flushwire#1{%
-   \yquant at draw@wire{#1}{}%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+     % make sure drawing the wire does not affect our left bounding box position
+      \edef\storedleft{\the\pgf at picminx}%
+      \yquant at draw@wire{#1}0%
+      \global\pgf at picminx=\storedleft%
+   \else%
+      \yquant at draw@wire{#1}0%
+   \fi%
    \begingroup%
       \yquant at register@get at lastwire{#1}\wirelast%
       \yquant at register@set at lastwire{#1}{%
@@ -156,6 +173,8 @@
    \let\yquant at circuit@operator at mintarget=\yquant at register@get at ids@min%
    \let\yquant at circuit@operator at maxtarget=\yquant at register@get at ids@max%
    \let\yquant at circuit@operator at numtarget=\yquant at register@get at ids@count%
+   % make sure to reset this for subcircuits
+   \yquant at circuit@operator at hasControlsfalse%
    % For the targets, multi-qubit registers might have been allowed, but certainly not for
    % the controls!
    \yquant at register@get at allowmultifalse%
@@ -198,93 +217,65 @@
    }%
 }
 
-% BEGIN_FOLD Subcircuit-related
-% Does all necessary calculations for inserting a sub-circuit
-% #1: subcircuit code (should start with \begin{yquant})
-%\newbox\yquant at circuit@subcircuit at box
-%\protected\long\def\yquant at circuit@subcircuit#1{%
-%   % We need to place the inner circuit at the correct position; but for this, we need
-%   % its extent. For this, we first place it within a box. But this box is then
-%   % integrated seamlessly, in particular, named nodes must be re-placed. We use a lot of
-%   % inspiration from pgf's matrix capabilities.
-%   % First, we anticipate the macro that is used by our subcircuit to store the node
-%   % names.
-%   \edef\yquant at circuit@subcircuit at nodelist{yquant at env\the\numexpr\yquant at env+1\relax @circuit at subcircuit@nodelist}%
-%   \global\cslet\yquant at circuit@subcircuit at nodelist\empty%
-%   \pgfinterruptboundingbox%
-%      % we make sure there are no conflicts by prefixing any named nodes in any case.
-%      \ifx\yquant at lang@attr at name\empty%
-%         \pgfkeys{/tikz/name prefix/.expanded={sub\yquant at prefix-}}%
-%      \else%
-%         \pgfkeys{/tikz/name prefix/.expanded={\pgfkeysvalueof{/tikz/name prefix}\yquant at lang@attr at name-}}%
-%      \fi%
-%      \let\pgf at nodecallback=\yquant at circuit@subcircuit at nodecallback%
-%      \pgftransformreset%
-%      \global\setbox\yquant at circuit@subcircuit at box=\hbox{{%
-%         % bypass 'overlay' option
-%         \pgf at relevantforpicturesizetrue%
-%         \pgfsys at beginpicture%
-%         #1%
-%         \pgfsys at endpicture%
-%         \ifdim\pgf at picmaxx=-16000pt %
-%            \global\pgf at picmaxx=0pt %
-%            \global\pgf at picminx=0pt %
-%            \global\pgf at picmaxy=0pt %
-%            \global\pgf at picminy=0pt %
-%         \fi%
-%      }}%
-%      \wd\yquant at circuit@subcircuit at box=0pt %
-%      \global\setbox\yquant at circuit@subcircuit at box=\hbox{{%
-%         \hskip-\pgf at picminx%
-%         \unhbox\yquant at circuit@subcircuit at box%
-%         \hskip\pgf at picmaxx%
-%      }}%
-%      \ht\yquant at circuit@subcircuit at box=\pgf at picmaxy%
-%      \dp\yquant at circuit@subcircuit at box=-\pgf at picminy%
-%      % We need to remember the offset for the coordinate shifts
-%      \dimgdef\yquant at circuit@subcircuit at shiftx{-\pgf at picminx}%
-%   \endpgfinterruptboundingbox%
-%   \ifx\yquant at lang@attr at name\empty%
-%      % However, if the outer node was not named, no access to the inner nodes is desired,
-%      % so we delete all nodes again.
-%      \def\do##1{%
-%         \csgundef{pgf at sh@ns@##1}%
-%         \csgundef{pgf at sh@np@##1}%
-%         \csgundef{pgf at sh@nt@##1}%
-%         \csgundef{pgf at sh@pi@##1}%
-%         \csgundef{pgf at sh@ma@##1}%
-%      }%
-%      \dolistcsloop\yquant at circuit@subcircuit at nodelist%
-%      \global\cslet\yquant at circuit@subcircuit at nodelist\empty%
-%   \else%
-%      % But now access is desired, and probably we are deeply nested. Append the sublist to ours, so that our parent can re-map them again.
-%      \ifx\pgf at nodecallback\yquant at circuit@subcircuit at nodecallback%
-%         \csxappto{\yquant at prefix circuit at subcircuit@nodelist}%
-%                  {\csname\yquant at circuit@subcircuit at nodelist\endcsname}%
-%      \fi%
-%   \fi%
-%}
-%
-%\def\yquant at circuit@subcircuit at nodecallback#1{%
-%   \listcsxadd{\yquant at prefix circuit at subcircuit@nodelist}{#1}%
-%}
+% creates a subcircuit. Parameter registers must be filled in \yquant at circuit@subcircuit at params.
+\protected\def\yquant at circuit@subcircuit#1{%
+   \numdef\yquant at circuit@subcircuit at id{\yquant at env+1}%
+   \listcsxadd{\yquant at prefix subcircuits}{\yquant at circuit@subcircuit at id}%
+   \begingroup%
+      \tikzset{/yquant/every operator, #1,%
+               /yquant/this operator, /yquant/operators/this subcircuit box/.style={}}%
+      % execute the subcircuit
+      \expandafter%
+      \yquant at env@begin at noarg%
+         \yquant at lang@attr at value% this contains the subcircuit's content
+         \expandafter\unless\expandafter\ifx\csname\yquant at prefix parameters\endcsname\empty%
+            \PackageError{yquant.sty}{Invalid subcircuit parameters count}%
+                         {Too many parameters given.}%
+         \fi%
+         \global\cslet{\yquant at prefix parameters}\yquant at circuit@subcircuit at param%
+         \dimen0=\yquant at config@register at sep%
+         % the first input must count all its height plus everything that is above it in the subcircuit as the height of the outer wire it is attached to
+         \edef\firstinput{%
+            \expandafter\expandafter\expandafter%
+               \@firstoftwo\csname\yquant at prefix firstinput\endcsname%
+         }%
+         \edef\outerfirst{%
+            \expandafter\expandafter\expandafter%
+               \@secondoftwo\csname\yquant at prefix firstinput\endcsname%
+         }%
+         \dimen2=\yquant at register@get at height\firstinput\relax%
+         \ifnum\firstinput>1 %
+            \yquant at fordown \i := \the\numexpr\firstinput-1\relax downto 1 {%
+               \advance\dimen2 by \dimexpr\yquant at register@get at height\i+%
+                                          \yquant at register@get at depth\i+\dimen0\relax%
+            }%
+         \fi%
+         % the last input must count all its depth plus everything that is below it in the subcircuit as the depth of the outer wire it is attached to
+         \edef\lastinput{%
+            \expandafter\expandafter\expandafter%
+               \@firstoftwo\csname\yquant at prefix lastinput\endcsname%
+         }%
+         \edef\outerlast{%
+            \expandafter\expandafter\expandafter%
+               \@secondoftwo\csname\yquant at prefix lastinput\endcsname%
+         }%
+         \dimen4=\yquant at register@get at depth\lastinput\relax%
+         \ifnum\lastinput<\csname\yquant at prefix registers\endcsname%
+            \yquant at for \i := \the\numexpr\lastinput+1\relax to \csname\yquant at prefix registers\endcsname {%
+               \advance\dimen4 by \dimexpr\yquant at register@get at height\i+%
+                                          \yquant at register@get at depth\i+\dimen0\relax%
+            }%
+         \fi%
+         \begingroup%
+            \let\yquant at prefix=\yquant at parent%
+            \yquant at register@update at height\outerfirst{\the\dimen2}%
+            \yquant at register@update at depth\outerlast{\the\dimen4}%
+         \endgroup%
+         % we cannot make more precise statements about the inputs in between - for example, if input 1 is mapped to outer wire 1 and input 2 to outer wire 5, how to split the heights/depth appropriately between the outer wires 1-5?
+      \yquant at env@end%
+   \endgroup%
+}
 
-%\protected\def\yquant at circuit@subcircuit at shiftnodes#1{%
-%   \expandafter\unless\expandafter\ifx\csname\yquant at circuit@subcircuit at nodelist\endcsname\empty{%
-%      \pgftransformreset%
-%      \pgf at process{\pgfpointanchor{#1}{text}}%
-%      \edef\yquant at circuit@subcircuit at offset{%
-%         \noexpand\pgfqpoint{\the\dimexpr\pgf at x+\yquant at circuit@subcircuit at shiftx\relax}%
-%                            {\the\dimexpr\pgf at y\relax}%
-%      }%
-%      \def\do##1{%
-%         \pgf at shift@node{##1}\yquant at circuit@subcircuit at offset%
-%      }%
-%      \dolistcsloop\yquant at circuit@subcircuit at nodelist%
-%   }\fi%
-%}
-% END_FOLD
-
 % BEGIN_FOLD Helpers for operator callbacks
 % turn a wire into a different type
 \def\yquant at circuit@settype#1{%
@@ -358,67 +349,42 @@
 }
 
 \protected\long\def\yquant at circuit@output at do#1#2#3{%
-   % this must only be called at the end of an environment, where \yquant at env@end at xpos is
-   % set up properly!
+   % this must only be called at the end of an environment, where \yquant at env@end at xpos is set up properly! It not only has to provide the appropriate drawing commands for later, but also has to measure the actual width of the outputs, which is required for proper subcircuit positioning.
    \def\do##1{%
-      \ifyquant at firsttoken\yquant at register@multi{##1}{%
-         \csxappto{\yquant at prefix draw}%
-                  {\noexpand\yquant at circuit@output at do@multi%
-                   \yquant at circuit@output at do@multi@@extract##1{\unexpanded{#3}}}%
-      }{%
-         \csgappto{\yquant at prefix draw}%
-                  {\yquant at circuit@output at do@single{##1}{#3}}%
+      \pgfinterruptboundingbox%
+         \yquant at env@virtualize at path%
+         \ifyquant at firsttoken\yquant at register@multi{##1}{%
+            \csxappto{\yquant at prefix draw}%
+                     {\yquant at draw@output at multi%
+                      \yquant at circuit@output at do@multi@@extract##1{\unexpanded{#3}}}%
+            \yquant at draw@@output at multi{#3}%
+         }{%
+            \csgappto{\yquant at prefix draw}%
+                     {\yquant at draw@output at single{##1}{#3}}%
+            \yquant at draw@@output at single{##1}{#3}%
+            \yquant at register@update at height{##1}{\the\pgf at picmaxy}%
+            \yquant at register@update at depth{##1}{\the\dimexpr-\pgf at picminy\relax}%
+         }%
+         \ifdim\pgf at picmaxx>\csname\yquant at prefix xmax\endcsname%
+            \csxdef{\yquant at prefix xmax}{\the\pgf at picmaxx}%
+         \fi%
+      \endpgfinterruptboundingbox%
+   }%
+   \yquant at draw@output at group{#2}%
+      \csgappto{\yquant at prefix draw}{%
+         \yquant at draw@output at group{#2}%
       }%
-   }%
-   \csgappto{\yquant at prefix draw}{%
-      \yquant at circuit@output at group{#2}%
-   }
-   % \dolistloop will carry out one \expandafter on the argument; but this expansion step
-   % is already done. If #1 starts with \yquant at register@multi, this will be expanded once
-   % despite \protected, so insert some expand-to-nothing token first.
-   \dolistloop{\empty#1}%
-   \csgappto{\yquant at prefix draw}{%
-      \yquant at circuit@output at endgroup%
-   }%
+      % \dolistloop will carry out one \expandafter on the argument; but this expansion step
+      % is already done. If #1 starts with \yquant at register@multi, this will be expanded once
+      % despite \protected, so insert some expand-to-nothing token first.
+      \dolistloop{\empty#1}%
+      \csgappto{\yquant at prefix draw}{%
+         \yquant at draw@output at endgroup%
+      }%
+   \yquant at draw@output at endgroup%
 }
 
 \def\yquant at circuit@output at do@multi@@extract#1#2#3#4#5{%
    {#2}{#3}{#5}%
 }
-
-\protected\long\def\yquant at circuit@output at group#1{%
-   \begingroup%
-      \def\idx{0}%
-      \yquant at set{#1}%
-}
-
-\let\yquant at circuit@output at endgroup=\endgroup%
-
-\long\def\yquant at circuit@output at do@single#1#2{%
-   \path
-      (\yquant at env@end at xpos, \yquant at register@get at y{#1})
-      node[/yquant/every output,
-           /yquant/every \ifcase\yquant at register@get at type{#1} nobit\or qubit\or cbit\or qubits\fi\space output] {#2};
-   \numdef\idx{\idx+1}%
-}
-
-\long\def\yquant at circuit@output at do@multi#1#2#3#4{%
-   % extremely similar to \yquant at draw@multiinit
-   \@tempdima=-.5\dimexpr\yquant at config@register at sep\relax%
-   \dimdef\yquant at draw@multiinit@@min{\yquant at register@get at y{#1}-\@tempdima}%
-   \dimdef\yquant at draw@multiinit@@max{\yquant at register@get at y{#2}+\@tempdima}%
-   \dimdef\yquant at draw@multiinit@@total{%
-      \yquant at draw@multiinit@@max-\yquant at draw@multiinit@@min%
-   }%
-   \def\pgfdecorationsegmentaspect{0}%
-   \let\yquant at register@multi at contiguous=\yquant at draw@multiinit at contiguous%
-   \let\pgfdecorationsegmentfromto=\empty%
-   #3%
-   \edef\pgfdecorationsegmentfromto{\expandafter\@gobble\pgfdecorationsegmentfromto}%
-   \path[/yquant/every multi output]
-      (\yquant at env@end at xpos, \yquant at draw@multiinit@@min) --
-      (\yquant at env@end at xpos, \yquant at draw@multiinit@@max)
-      node {#4};%
-   \numdef\idx{\idx+1}%
-}
 % END_FOLD
\ No newline at end of file

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-config.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-config.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-config.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -1,145 +1,151 @@
-\pgfdeclaremetadecoration{tikz at internal}{pre}{
-   \state{pre}[width=\pgfkeysvalueof{/pgf/decoration/pre length}+\pgfkeysvalueof{/pgf/decoration/post length}, next state=main]{
-      \appto\tikz at dec@shift{\pgftransformxshift{-\pgfkeysvalueof{/pgf/decoration/post length}}}
-      \tikz at dec@trans
-      \decoration{\pgfkeysvalueof{/pgf/decoration/pre}}
-   }
-   \state{main}[width=\pgfmetadecoratedremainingdistance, next state=final]{
-      \tikz at dec@trans
-      \decoration{\tikz at decoration@name}
-   }
-   \state{final}{
-      \tikz at dec@trans
-      \decoration{\pgfkeysvalueof{/pgf/decoration/post}}
-   }
+\pgfdeclaremetadecoration{tikz at internal}{pre}{%
+   \state{pre}[width=\pgfkeysvalueof{/pgf/decoration/pre length}+\pgfkeysvalueof{/pgf/decoration/post length}, next state=main]{%
+      \appto\tikz at dec@shift{\pgftransformxshift{-\pgfkeysvalueof{/pgf/decoration/post length}}}%
+      \tikz at dec@trans%
+      \decoration{\pgfkeysvalueof{/pgf/decoration/pre}}%
+   }%
+   \state{main}[width=\pgfmetadecoratedremainingdistance, next state=final]{%
+      \tikz at dec@trans%
+      \decoration{\tikz at decoration@name}%
+   }%
+   \state{final}{%
+      \tikz at dec@trans%
+      \decoration{\pgfkeysvalueof{/pgf/decoration/post}}%
+   }%
 }
-\pgfqkeys{/yquant}{
+\pgfqkeys{/yquant}{%
    every circuit/.style={%
       every node/.prefix style={transform shape},%
       every label/.prefix style={transform shape=false}% TODO: no, we don't really want this, but pgf bug #843 requires this if we still want to have `label position` available
-   },
+   },%
    % register settings
    register/default name/.store in=%
-      \yquant at config@register at default@name,
+      \yquant at config@register at default@name,%
    register/minimum height/.code=%
-      {\dimdef\yquant at config@register at minimum@height{#1}},
+      {\dimdef\yquant at config@register at minimum@height{#1}},%
    register/separation/.code=%
-      {\dimdef\yquant at config@register at sep{#1}},
+      {\dimdef\yquant at config@register at sep{#1}},%
    % register label style
    every label/.style=%
-      {shape=yquant-text, anchor=center, align=right},
+      {shape=yquant-text, anchor=center, align=right},%
    every initial label/.style=%
-      {anchor=east},
+      {anchor=east},%
    every qubit label/.style=%
-      {},
+      {},%
    every cbit label/.style=%
-      {},
+      {},%
    every qubits label/.style=%
-      {},
+      {},%
    every multi label/.style=%
-      {shift={(-.075, 0)}, draw, decoration={gapped brace, mirror}, decorate, /yquant/gapped brace/apply shift,
-       every node/.append style={shape=yquant-text, anchor=east, align=right, shift={(-.05, 0)}, pos=-1}},
+      {shift={(-.075, 0)}, draw, decoration={gapped brace, mirror}, decorate, /yquant/gapped brace/apply shift,%
+       every node/.append style={shape=yquant-text, anchor=east, align=right, shift={(-.05, 0)}, pos=-1}},%
+   every input label/.style=%
+      {},%
    % output label styles
    every output/.style=%
-      {shape=yquant-text, anchor=west, align=left},
+      {shape=yquant-text, anchor=west, align=left},%
    every qubit output/.style=%
-      {},
+      {},%
    every cbit output/.style=%
-      {},
+      {},%
    every qubits output/.style=%
-      {},
+      {},%
    every multi output/.style=%
-      {shift={(.075, 0)}, draw, decoration={gapped brace}, decorate, /yquant/gapped brace/apply shift,
-       every node/.append style={shape=yquant-text, anchor=west, align=left, shift={(.05, 0)}, pos=-1}},
+      {shift={(.075, 0)}, draw, decoration={gapped brace}, decorate, /yquant/gapped brace/apply shift,%
+       every node/.append style={shape=yquant-text, anchor=west, align=left, shift={(.05, 0)}, pos=-1}},%
    % wire style
    every wire/.style=%
-      {draw},
+      {draw},%
    every qubit wire/.style=%
-      {},
+      {},%
    every cbit wire/.style=%
-      {},
+      {},%
    every qubits wire/.style=%
-      {},
+      {},%
    % operator settings
    operator/separation/.code=%
-      {\dimdef\yquant at config@operator at sep{#1}},
+      {\dimdef\yquant at config@operator at sep{#1}},%
    operator/minimum width/.code=%
-      {\dimdef\yquant at config@operator at minimum@width{#1}},
+      {\dimdef\yquant at config@operator at minimum@width{#1}},%
    operator/multi warning/.is if=%
-      yquant at config@operator at multi@warn,
+      yquant at config@operator at multi@warn,%
    % operator style: control
    every control line/.style=%
-      {draw},
+      {draw},%
    every control/.style=%
-      {shape=yquant-circle, anchor=center, radius=.5mm},
+      {shape=yquant-circle, anchor=center, radius=.5mm},%
    every positive control/.style=%
-      {fill=black},
+      {fill=black},%
    every negative control/.style=%
-      {draw},
+      {draw},%
    % operator style: main part
    every operator/.style=%
-      {anchor=center},
+      {anchor=center},%
    operator/multi main/.is if=%
-      yquant at config@operator at multi@main,
+      yquant at config@operator at multi@main,%
    operator/multi as single/.style=%
-      {/yquant/every multi line/.style=/yquant/every control line},
+      {/yquant/every multi line/.style=/yquant/every control line},%
    every multi line/.style=%
-      {draw, decoration={snake, amplitude=.25mm, segment length=5pt}, decorate},
+      {draw, decoration={snake, amplitude=.25mm, segment length=5pt}, decorate},%
    % overwriting all styles
    this operator/.style=%
-      {},
+      {},%
    this control/.style=%
-      {},
+      {},%
    operator style/.style=%
-      {/yquant/this operator/.append style={#1}},
+      {/yquant/this operator/.append style={#1}},%
    control style/.style=%
-      {/yquant/every control line/.append style={#1},
-       /yquant/this control/.append style={#1}},
+      {/yquant/every control line/.append style={#1},%
+       /yquant/this control/.append style={#1}},%
    style/.style=%
-      {/yquant/this operator/.append style={#1},
-       /yquant/every control line/.append style={#1},
-       /yquant/this control/.append style={#1}},
+      {/yquant/this operator/.append style={#1},%
+       /yquant/every control line/.append style={#1},%
+       /yquant/this control/.append style={#1}},%
    % different operator appearances
    operators/every box/.style=%
-      {shape=yquant-rectangle, draw, align=center, inner xsep=1mm, x radius=2mm, y radius=2.47mm},
-   operators/every h/.style=%
-      {/yquant/operators/every box},
+      {shape=yquant-rectangle, draw, align=center, inner xsep=1mm, x radius=2mm, y radius=2.47mm},%
+   operators/every subcircuit/.style=%
+      {},%
+   operators/every subcircuit box/.style=%
+      {/yquant/operators/every box},%
+   subcircuit box style/.style=%
+      {/yquant/operators/every subcircuit box/.append style={#1}},%
+   operators/this subcircuit box/.style=%
+      {},%
+   this subcircuit box style/.style=%
+      {/yquant/operators/this subcircuit box/.append style={#1}},%
+   operators/every custom gate/.style=%
+      {/yquant/operators/this subcircuit box/.append style={draw=none, inner sep=0pt},%
+       /yquant/register/default name=},
+   % every h, every x, every y, every z are implicitly defined during gate declaration
    operators/every pauli/.style=%
-      {/yquant/operators/every box},
-   operators/every x/.style=%
-      {/yquant/operators/every pauli},
-   operators/every y/.style=%
-      {/yquant/operators/every pauli},
-   operators/every z/.style=%
-      {/yquant/operators/every pauli},
-%   operators/every subcircuit/.style=%
-%      {/yquant/operators/every box, inner ysep=0pt},
+      {/yquant/operators/every box},%
    operators/every phase/.style=%
-      {shape=yquant-circle, radius=.5mm, fill},
+      {shape=yquant-circle, radius=.5mm, fill},%
    operators/every zz/.style=%
-      {shape=yquant-circle, radius=.5mm, fill},
+      {shape=yquant-circle, radius=.5mm, fill},%
    operators/every xx/.style=%
-      {shape=yquant-rectangle, radius=.75mm, draw},
+      {shape=yquant-rectangle, radius=.75mm, draw},%
    operators/every slash/.style=%
-      {shape=yquant-slash, x radius=.5mm, y radius=.7mm, draw},
+      {shape=yquant-slash, x radius=.5mm, y radius=.7mm, draw},%
    operators/every swap/.style=%
-      {shape=yquant-swap, radius=.75mm, draw},
+      {shape=yquant-swap, radius=.75mm, draw},%
    operators/every not/.style=%
-      {shape=yquant-oplus, radius=1.3mm, draw},
+      {shape=yquant-oplus, radius=1.3mm, draw},%
    operators/every measure/.style=%
-      {shape=yquant-measure, x radius=4mm, y radius=2.5mm, draw},
+      {shape=yquant-measure, x radius=4mm, y radius=2.5mm, draw},%
    operators/every measure meter/.style=%
-      {draw, -{Latex[length=2.5pt]}},
+      {draw, -{Latex[length=2.5pt]}},%
    operators/every dmeter/.style=%
-      {shape=yquant-dmeter, x radius=2mm, y radius=2mm, draw},
+      {shape=yquant-dmeter, x radius=2mm, y radius=2mm, draw},%
    operators/every barrier/.style=%
-      {shape=yquant-line, dashed, draw},
+      {shape=yquant-line, dashed, draw},%
    operators/every wave/.style=%
-      {shape=yquant-circle, radius=.5mm, fill},
-   /pgf/decoration/from to/.store in=\pgfdecorationsegmentfromto,
+      {shape=yquant-circle, radius=.5mm, fill},%
+   /pgf/decoration/from to/.store in=\pgfdecorationsegmentfromto,%
    gapped brace/apply shift/.code={%
       \let\tikz at timer@line=\yquant at gappedbrace@timer%
-   },
+   },%
 }
 
 \def\yquant at config@register at default@name{\regidx}
@@ -162,7 +168,7 @@
    \ifdim\tikz at time pt=-1pt %
       % first set \pgfdecoratedremainingdistance appropriately
       \pgfpointdiff\tikz at timer@start\tikz at timer@end%
-      \pgfmathsqrt@{\dimexpr\pgf at x*\pgf at x/65536+\pgf at y*\pgf at y/65536\relax\@gobbletwo}
+      \pgfmathsqrt@{\dimexpr\pgf at x*\pgf at x/65536+\pgf at y*\pgf at y/65536\relax\@gobbletwo}%
       \pgfdecoratedremainingdistance=\pgfmathresult pt %
       % now perform the transformation
       \pgf at xc=\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance%

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-draw.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-draw.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-draw.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -3,7 +3,11 @@
 
 \protected\def\yquant at draw@group#1#2#3#4#5{%
    \begingroup%
-      \def\yquant at draw@@x{#1}%
+      \ifcsname\yquant at prefix xshift\endcsname%
+         \dimdef\yquant at draw@@x{#1+\csname\yquant at prefix xshift\endcsname}%
+      \else%
+         \def\yquant at draw@@x{#1}%
+      \fi%
       \ifx F#2%
          \yquant at draw@@currentcontroltype=0 %
       \else%
@@ -65,7 +69,7 @@
                         /yquant/this operator}%
    }%
    \cmd%
-   \yquant at circuit@extendwire{#1}%
+   \yquant at circuit@extendwire{#1}{center}%
    \expandafter\yquant at circuit@extendcontrolline\expandafter%
       {\the\yquant at draw@@currentcontroltype}\yquant at draw@@x%
    % check for empty name parameter
@@ -106,7 +110,7 @@
               y radius/.expanded=\the\dimexpr.5\dimexpr\yquant at register@get at ydist{#1}{#2}\relax\relax+%
                      .5*\noexpand\pgfkeysvalueof{/tikz/y radius}\fi,%
               name prefix=, name suffix=, name=yquantbox]%
-            {\unexpanded\expandafter{\yquant at draw@@content}};
+            {\unexpanded\expandafter{\yquant at draw@@content}};%
       \pgfshapeclippath{yquantbox}%
                        {/yquant/every operator, \yquant at draw@@style,%
                         /yquant/this operator}%
@@ -113,7 +117,7 @@
    }%
    \cmd%
    \yquant at for \i := #1 to #2 {%
-      \yquant at circuit@extendwire\i%
+      \yquant at circuit@extendwire\i{center}%
    }%
    \yquant at circuit@extendmultiline\yquant at draw@@x%
    \expandafter\yquant at circuit@extendcontrolline\expandafter%
@@ -148,7 +152,7 @@
          (\yquant at draw@@x, \yquant at draw@multiinit@@min) --%
          (\yquant at draw@@x, \yquant at draw@multiinit@@max)%
          node[name prefix=, name suffix=, name=yquantbox]%
-            {\unexpanded\expandafter{\yquant at draw@@content}};
+            {\unexpanded\expandafter{\yquant at draw@@content}};%
    }%
    \cmd%
    % no wire extension (we are still at the initial position), no control line (init doesn't allow for those, so just save the no-op), no multi line
@@ -197,6 +201,162 @@
    \fi%
 }
 
+\newbox\yquant at draw@subcircuit at box
+
+\protected\def\yquant at draw@subcircuit at nodecallback#1{%
+   \ifstrequal{#1}{yquantbox}\relax{%
+      \listcsxadd{\yquant at prefix draw at subcircuit@nodelist}{#1}%
+   }%
+}
+
+\protected\long\def\yquant at draw@subcircuit at prepare#1#2{%
+   \let\idx=\yquant at draw@@idx at content%
+   % In order to wrap the inner circuit in a proper box operator and clip outer paths appropriately (which was not possible yet, as we didn't know the exact vertical positions), we first place it within a box. During the setup time, we assumed that the subcircuit be placed at position #3; however, now, this has changed due to the additional box.
+   % First, we anticipate the macro that is used by our subcircuit to store the node
+   % names.
+   \edef\yquant at draw@subcircuit at nodelist{yquant at env#1 at draw@subcircuit at nodelist}%
+   \global\cslet\yquant at draw@subcircuit at nodelist\empty%
+   \pgfinterruptboundingbox%
+      \let\yquant at parent=\yquant at prefix%
+      \def\yquant at prefix{yquant at env#1@}%
+      \ifstrempty{#2}{%
+         % we make sure there are no conflicts by prefixing any named nodes in any case.
+         \pgfkeys{/tikz/name prefix/.expanded={sub\yquant at prefix-}}%
+         \let\pgf at nodecallback=\yquant at draw@subcircuit at nodecallback%
+      }{%
+         \pgfkeys{/tikz/name prefix/.expanded={\pgfkeysvalueof{/tikz/name prefix}#2-}}%
+      }%
+      \letcs\xmin{\yquant at prefix xmin}%
+      \letcs\xmax{\yquant at prefix xmax}%
+      \global\setbox\yquant at draw@subcircuit at box=\hbox to 0pt {{%
+         % bypass 'overlay' option
+         \pgf at relevantforpicturesizetrue%
+         \pgfsys at beginpicture%
+            % reset all styles to the expected defaults (similar, but extended to \pgfpicture, see pgf issue #870)
+            \pgfsetcolor{.}%
+            \pgfsetlinewidth{0.4pt}%
+            \pgfsetbuttcap%
+            \pgfsetmiterjoin%
+            \pgfsetmiterlimit{10}%
+            \pgfsetdash{}{0pt}%
+            % The left outer position of our box will be \yquant at draw@@x-.5(xmax-xmin).
+            % To compensate for, we perform a left shift of all commands that take explicit coordinates from the subcircuit.
+            % The y positions, on the other hand, are exactly the ones as they should be integrated in the picture.
+            \csdimdef{\yquant at prefix xshift}{\yquant at draw@@x-.5\dimexpr\xmax+\xmin\relax}%
+            \csname\yquant at prefix draw\endcsname%
+            \ifdim\pgf at picmaxx=-16000pt %
+               \global\pgf at picmaxx=0pt %
+               \global\pgf at picminx=0pt %
+               \global\pgf at picmaxy=0pt %
+               \global\pgf at picminy=0pt %
+            \fi%
+            \ifyquantdebug%
+               \pgf at relevantforpicturesizefalse%
+               \draw[green] (current bounding box.north east) rectangle (current bounding box.south west);%
+            \fi%
+         \pgfsys at endpicture%
+      }}%
+      \global\setbox\yquant at draw@subcircuit at box=\hbox to \dimexpr\xmax-\xmin\relax {%
+         \hskip-\dimexpr\yquant at draw@@x-.5\dimexpr\xmax-\xmin\relax\relax%
+         \lower\pgf at picmaxy%
+         \box\yquant at draw@subcircuit at box%
+      }%
+      \ht\yquant at draw@subcircuit at box=0pt%
+      \dp\yquant at draw@subcircuit at box=\dimexpr\pgf at picmaxy-\pgf at picminy\relax%
+      \expandafter%
+   \endpgfinterruptboundingbox%
+   \expandafter\edef\expandafter\yquant at draw@subcircuit at y\expandafter{%
+      \the\dimexpr.5\pgf at picminy+.5\pgf at picmaxy\relax%
+   }%
+   \ifstrempty{#2}{%
+      % However, if the outer node was not named, no access to the inner nodes is desired, so we delete all nodes again.
+      \def\do##1{%
+         \csgundef{pgf at sh@ns@##1}%
+         \csgundef{pgf at sh@np@##1}%
+         \csgundef{pgf at sh@nt@##1}%
+         \csgundef{pgf at sh@pi@##1}%
+         \csgundef{pgf at sh@ma@##1}%
+      }%
+      \dolistcsloop{\yquant at draw@subcircuit at nodelist}%
+      \csgundef\yquant at draw@subcircuit at nodelist%
+   }{%
+      \ifcsname\yquant at prefix draw at subcircuit@nodelist\endcsname%
+         \csxappto{\yquant at prefix draw at subcircuit@nodelist}%
+                  {\csname\yquant at draw@subcircuit at nodelist\endcsname}%
+      \fi%
+   }%
+}
+
+\protected\long\def\yquant at draw@subcircuit at single#1#2#3{%
+   \yquant at draw@subcircuit at prepare{#2}{#3}%
+   \edef\cmd{%
+      \noexpand\path (\yquant at draw@@x, \yquant at draw@subcircuit at y)%
+         node[/yquant/every operator, \yquant at draw@@style,%
+              /yquant/operators/every subcircuit box, /yquant/this operator,%
+              /yquant/operators/this subcircuit box,%
+              name prefix=, name suffix=, name=yquantbox]%
+         {\box\yquant at draw@subcircuit at box};%
+      \pgfshapeclippath{yquantbox}%
+                       {/yquant/every operator, \yquant at draw@@style,%
+                        /yquant/operators/every subcircuit box, /yquant/this operator,%
+                        /yquant/operators/this subcircuit box}%
+   }%
+   \cmd%
+   % see comment in draw at subcircuit@multi
+   \yquant at softpath@extractmaxxat\pgfshapeclippathhorzresult{\yquant at register@get at y{#1}}%
+   \let\pgfshapeclippathhorzresult=\empty%
+   \yquant at circuit@extendwire{#1}{*}%
+   \expandafter\yquant at circuit@extendcontrolline\expandafter%
+      {\the\yquant at draw@@currentcontroltype}\yquant at draw@@x%
+   % check for empty name parameter
+   \ifstrempty{#3}\relax{%
+      \pgfnodealias{\tikz at pp@name{#3}}{yquantbox}%
+   }%
+   \numdef\yquant at draw@@idx at content{\yquant at draw@@idx at content+1}%
+}
+
+\protected\long\def\yquant at draw@subcircuit at multi#1#2#3#4#5#6{%
+   % there is no contiguous slicing for subcircuits, as they may have all kinds of wire operations that can extend beyond the individual slices, let alone ancillas
+   \yquant at draw@subcircuit at prepare{#5}{#6}%
+   % We need to somehow extract the y radius
+   \edef\cmd{%
+      \noexpand\path (\yquant at draw@@x, \yquant at draw@subcircuit at y)%
+         node[/yquant/every operator, \yquant at draw@@style,%
+              /yquant/operators/every subcircuit box, /yquant/this operator,%
+              /yquant/operators/this subcircuit box,
+              /yquant/operator/multi main=true,%
+              name prefix=, name suffix=, name=yquantbox]%
+            {\box\yquant at draw@subcircuit at box};%
+      \pgfshapeclippath{yquantbox}%
+                       {/yquant/every operator, \yquant at draw@@style,%
+                        /yquant/operators/every subcircuit box, /yquant/this operator,%
+                        /yquant/operators/this subcircuit box,%
+                        /yquant/operator/multi main=true}%
+   }%
+   \cmd%
+   % install the clippings - but only on wires that are visually between the first and list while not being part of the circuit.
+   \let\nonaffectedpgfshapeclippathhorzresult=\pgfshapeclippathhorzresult%
+   \yquant at for \i := #1 to #2 {%
+      \xifinlist{\i}{#4}{%
+         % Usually, we always begin with a wire from the center of the operator shape and clip the inner parts away. This can't be done here, as the wire needs to be drawn _inside_ of the outer box operator here. Instead of clipping against the clip path, we extract its maximum x position at the position of the wire (which is an overkill for simple shapes, but the allows to specify even more complicated ones) and place the wire at this position without clipping.
+         % Note: this works very well for lines joining at perpendicular angles; but if the shape of the box is more fancy, while the position will be calculated correctly, the wire has a rectangular (or rounded, depending on the line cap) shape that is drawn on top of thw operator. While \yquant at softpath@extractmaxxat could without much effort determine exactly the segment of the path that corresponds to the rightmost line, we would then have to convert this single line into a closed path that fills the linewidth and clip against it to get proper joiners. Since most likely, no-one will ever need this, we don't do it. But file a feature request if desired.
+         \yquant at softpath@extractmaxxat\nonaffectedpgfshapeclippathhorzresult%
+                                       {\yquant at register@get at y\i}%
+         \let\pgfshapeclippathhorzresult=\empty%
+         \yquant at circuit@extendwire\i{*}%
+      }{%
+         \let\pgfshapeclippathhorzresult=\nonaffectedpgfshapeclippathhorzresult%
+         \yquant at circuit@extendwire\i{center}%
+      }%
+   }%
+   \expandafter\yquant at circuit@extendcontrolline\expandafter%
+      {\the\yquant at draw@@currentcontroltype}\yquant at draw@@x%
+   \ifstrempty{#6}\relax{%
+      \pgfnodealias{\tikz at pp@name{#6}}{yquantbox}%
+   }%
+   \numdef\yquant at draw@@idx at content{\yquant at draw@@idx at content+1}%
+}
+
 \protected\def\yquant at draw@control#1#2#3{%
    \edef\cmd{%
       \noexpand\path (\yquant at draw@@x, \yquant at register@get at y{#2})%
@@ -208,7 +368,7 @@
                         /yquant/this control}%
    }%
    \cmd%
-   \yquant at circuit@extendwire{#2}%
+   \yquant at circuit@extendwire{#2}{center}%
    \yquant at draw@@currentcontroltype=\yquant at register@get at type{#2}\relax%
    \expandafter\yquant at circuit@extendcontrolline\expandafter%
       {\yquant at draw@@currentcontroltype}\yquant at draw@@x%
@@ -251,7 +411,7 @@
       \fi%
       \edef\cmd{%
          \noexpand\path[/yquant/every control line]%
-            \yquant at circuit@extendcontrolline at cmd;
+            \yquant at circuit@extendcontrolline at cmd;%
       }%
       \cmd%
    \endpgfscope%
@@ -285,7 +445,7 @@
             \pgfusepathqclip%
          \endgroup%
       \fi%
-      \path[/yquant/every multi line] #2;
+      \path[/yquant/every multi line] #2;%
    \endpgfscope%
 }
 
@@ -300,18 +460,18 @@
 \protected\def\yquant at draw@wire#1#2{%
    \begingroup%
       \yquant at register@get at typeywire{#1}\wiretype\wireypos\wirelast%
-      \edef\wirexprevpos{\expandafter\@firstoffour\wirelast}%
-      \ifx\yquant at env@end at xpos#2\relax%
-         \let\wirexpos=\yquant at env@end at xpos%
-      \else%
-         \edef\wirexpos{\expandafter\@secondoffour\wirelast}%
-      \fi%
       \unless\ifnum\wiretype=\yquant at register@type at none%
+         \edef\wirexprevpos{\expandafter\@firstoffour\wirelast}%
+         \ifx0#2%
+            \edef\wirexpos{\expandafter\@secondoffour\wirelast}%
+         \else%
+            \let\wirexpos=\yquant at env@end at xpos%
+         \fi%
          \ifdim\wirexpos>\wirexprevpos %
             \edef\wirestyle{\noexpand\tikzset{%
                /yquant/this wire/.style={%
                   /yquant/every wire,%
-                  /yquant/every \ifcase\wiretype\relax nobit\or qubit \or cbit \or qubits \fi wire,%
+                  /yquant/every \yquant at register@type at tostring\wiretype\space wire,%
                      \yquant at register@get at style{#1}%
                }, /yquant/this wire%
             }}%
@@ -334,13 +494,13 @@
                   % We need to access the current bounding box as well as other positions in the local coordinate frame. For this, transform the bounding box to the current frame (though this is expensive). Does this capture rotations correctly?
                   \begingroup%
                      \pgftransforminvert%
-                     \pgfpointtransformednonlinear{\pgfqpoint{\pgf at picminx}{\pgf at picminy}}
+                     \pgfpointtransformednonlinear{\pgfqpoint{\pgf at picminx}{\pgf at picminy}}%
                      \global\@tempdima=\pgf at y%
                      \pgfpointtransformednonlinear{\pgfqpoint{\pgf at picmaxx}{\pgf at picmaxy}}%
                      \global\@tempdimb=\pgf at y%
                   \endgroup%
                   % To avoid rendering artifacts at all zoom levels with all renderers, we need to make the clipping region large. Let's try the current bounding box first.
-                  % This may be insufficient if there no or a tiny wire label and only registers of a small height. In this case, take at ten times the line width or at least 1cm, but don't let it affect the bounding box.
+                  % This may be insufficient if there no or a tiny wire label and only registers of a small height. In this case, take ten times the line width or at least 1cm, but don't let it affect the bounding box.
                   \ifdim\dimexpr\@tempdimb-\@tempdima\relax<10\pgflinewidth %
                      \@tempdima=\dimexpr\wireypos-5\pgflinewidth\relax%
                      \@tempdimb=\dimexpr\wireypos+5\pgflinewidth\relax%
@@ -370,7 +530,7 @@
 % quantum wire
 \protected\csdef{yquant at draw@wire@\yquant at register@type at q}#1{%
    \edef\cmd{%
-      \noexpand\path [/yquant/this wire]
+      \noexpand\path [/yquant/this wire]%
          (\wirexprevpos,\wireypos) -- (\wirexpos,\wireypos);%
    }%
    \cmd%
@@ -379,7 +539,7 @@
 % classical wire
 \protected\csdef{yquant at draw@wire@\yquant at register@type at c}#1{%
    \edef\cmd{%
-      \noexpand\path [/yquant/this wire]
+      \noexpand\path [/yquant/this wire]%
          (\wirexprevpos,\wireypos+2\pgflinewidth)--(\wirexpos,\wireypos+2\pgflinewidth)%
          (\wirexprevpos,\wireypos-2\pgflinewidth)--(\wirexpos,\wireypos-2\pgflinewidth);%
    }%
@@ -389,7 +549,7 @@
 % quantum-bundle
 \protected\csdef{yquant at draw@wire@\yquant at register@type at qs}#1{%
    \edef\cmd{%
-      \noexpand\path [/yquant/this wire]
+      \noexpand\path [/yquant/this wire]%
          (\wirexprevpos,\wireypos+2\pgflinewidth)--(\wirexpos,\wireypos+2\pgflinewidth)%
          (\wirexprevpos,\wireypos)--(\wirexpos,\wireypos)%
          (\wirexprevpos,\wireypos-2\pgflinewidth)--(\wirexpos,\wireypos-2\pgflinewidth);%
@@ -396,6 +556,42 @@
    }%
    \cmd%
 }
+
+\protected\long\def\yquant at draw@output at group#1{%
+   \begingroup%
+      \def\idx{0}%
+      \yquant at set{#1}%
+}
+
+\let\yquant at draw@output at endgroup=\endgroup%
+
+\protected\long\def\yquant at draw@output at single#1#2{%
+   \path%
+      (\yquant at env@end at xpos, \yquant at register@get at y{#1})%
+      node[/yquant/every output,%
+           /yquant/every \yquant at register@type at tostring{\yquant at register@get at type{#1}} output] {#2};
+   \numdef\idx{\idx+1}%
+}
+
+\protected\long\def\yquant at draw@output at multi#1#2#3#4{%
+   % extremely similar to \yquant at draw@multiinit
+   \@tempdima=-.5\dimexpr\yquant at config@register at sep\relax%
+   \dimdef\yquant at draw@multiinit@@min{\yquant at register@get at y{#1}-\@tempdima}%
+   \dimdef\yquant at draw@multiinit@@max{\yquant at register@get at y{#2}+\@tempdima}%
+   \dimdef\yquant at draw@multiinit@@total{%
+      \yquant at draw@multiinit@@max-\yquant at draw@multiinit@@min%
+   }%
+   \def\pgfdecorationsegmentaspect{0}%
+   \let\yquant at register@multi at contiguous=\yquant at draw@multiinit at contiguous%
+   \let\pgfdecorationsegmentfromto=\empty%
+   #3%
+   \edef\pgfdecorationsegmentfromto{\expandafter\@gobble\pgfdecorationsegmentfromto}%
+   \path[/yquant/every multi output]%
+      (\yquant at env@end at xpos, \yquant at draw@multiinit@@min) --%
+      (\yquant at env@end at xpos, \yquant at draw@multiinit@@max)%
+      node {#4};%
+   \numdef\idx{\idx+1}%
+}
 % END_FOLD
 
 % BEGIN_FOLD Preparation of drawing a generic shape
@@ -443,6 +639,12 @@
       }{%
          \yquant at draw@@single{#1}{#2}{##1}%
       }%
+         \ifx\yquant at draw@@multi\yquant at draw@@multiinit%
+            % if we draw an initialization (whether multi or single), this possibly affects the minimal x position. All other gates will be shifted so that they cannot extend beyond the minimal position.
+            \ifdim\pgf at picminx<\csname\yquant at prefix xmin\endcsname%
+               \csxdef{\yquant at prefix xmin}{\the\pgf at picminx}%
+            \fi%
+         \fi%
          \expandafter%
       \endpgfinterruptboundingbox%
       \expandafter\dimen\expandafter0\expandafter=%
@@ -451,41 +653,44 @@
          \dimen2=\dimen0 %
       \fi%
       \numdef\idx{\idx+1}%
-      % TODO
-%      \yquant at draw@callback at box\nodename%
    }%
    \dolistloop\yquant at circuit@operator at targets%
    % END_FOLD
-   % BEGIN_FOLD controls
+   \yquant at draw@@controls%
+   \yquant at draw@@finalize{#1}{#2}%
+}
+
+\protected\def\yquant at draw@@controls at loop#1#2{%
+   \ifx\yquant at lang@attr at name\empty%
+      \let\nodename=\empty%
+   \else%
+      \edef\nodename{\yquant at lang@attr at name-#1\idx}%
+   \fi%
+   \yquant at sort@eadd{%
+      \expandafter\noexpand\csname yquant at draw@#1control\endcsname%
+         {#2}% register index
+         {\nodename}%
+   }%
+   \unless\ifdefined\yquant at draw@controltype%
+      \edef\yquant at draw@controltype{#2}%
+   \fi%
+   \numdef\idx{\idx+1}%
+}
+
+\protected\def\yquant at draw@@controls{%
    \ifyquant at circuit@operator at hasControls%
-      \def\do##1{%
-         \ifx\yquant at lang@attr at name\empty%
-            \let\nodename=\empty%
-         \else%
-            \edef\nodename{\yquant at lang@attr at name-\yquant at draw@controlprefix\idx}%
-         \fi%
-         \yquant at sort@eadd{%
-            \expandafter\noexpand\csname yquant at draw@\yquant at draw@controlprefix control\endcsname%
-               {##1}% register index
-               {\nodename}%
-         }%
-         \unless\ifdefined\yquant at draw@controltype%
-            \edef\yquant at draw@controltype{##1}%
-         \fi%
-         \numdef\idx{\idx+1}%
-      }
-      \def\yquant at draw@controlprefix{p}%
       \def\idx{0}%
-      \dolistloop\yquant at circuit@operator at pctrls%
-      \def\yquant at draw@controlprefix{n}%
+      \forlistloop{\yquant at draw@@controls at loop p}\yquant at circuit@operator at pctrls%
       \def\idx{0}%
-      \dolistloop\yquant at circuit@operator at nctrls%
+      \forlistloop{\yquant at draw@@controls at loop n}\yquant at circuit@operator at nctrls%
       \unless\ifdefined\yquant at draw@controltype%
          \PackageError{yquant.sty}{Assertion failure}%
                       {Internal inconsistency in yquant: Controlled action detected, but no controls were found.}%
       \fi%
    \fi%
-   % END_FOLD
+}
+
+\protected\long\def\yquant at draw@@finalize#1#2{%
    % We now know the dimensions of all the registers (though we didn't bother with the height of the control knobs [if present], we just assume they are too small to change this).
    \protected\def\idx{}%
    \protected at edef\yquant at draw@append{%
@@ -502,9 +707,8 @@
    }%
    \yquant at sort\yquant at draw@sort%
    \advance \dimen2 by \yquant at circuit@operator at x\relax%
-   % BEGIN_FOLD shipout
    \ifyquant at circuit@operator at hasControls%
-      % If we draw a control line, all intermediate registers are affected in their position so that the line is never crossed. If the vertical line is instead caused by a multi register, \yquant at draw@finalize at ctrl will be responsible for advancing only the affected positions.
+      % If we draw a control line, all intermediate registers are affected in their position so that the line is never crossed. If the vertical line is instead caused by a multi register, \yquant at draw@@finalize at ctrl will be responsible for advancing only the affected positions.
       \yquant at for \yquant at i := \yquant at circuit@operator at minctrl to \yquant at circuit@operator at maxctrl {%
          \yquant at register@set at x\yquant at i{\the\dimen2}%
       }%
@@ -511,7 +715,7 @@
    \fi%
    \def\do##1{%
       \appto\yquant at draw@append{##1}%
-      \yquant at draw@finalize at ctrl##1%
+      \yquant at draw@@finalize at ctrl##1%
    }%
    \yquant at sort@dolistloop%
    \csxappto{\yquant at prefix draw}{%
@@ -557,7 +761,6 @@
             \fi%
          \fi%
    }%
-   % END_FOLD
 }
 
 \protected\def\yquant at draw@@single#1#2#3{%
@@ -570,12 +773,11 @@
    \pgfinterruptboundingbox%
       \yquant at env@virtualize at path%
       \path%
-         (0pt, 0pt)
+         (0pt, 0pt)%
          node[/yquant/every operator, #2, /yquant/this operator,%
               name prefix=, name suffix=, name=] {#1};%
-      \yquant at register@update at height{#3}{%
-         \the\dimexpr\pgf at picmaxy-\pgf at picminy\relax%
-      }%
+      \yquant at register@update at height{#3}{\the\pgf at picmaxy}%
+      \yquant at register@update at depth{#3}{\the\dimexpr-\pgf at picminy\relax}%
 }
 
 \protected\def\yquant at draw@@multi#1#2#3{%
@@ -594,9 +796,9 @@
       \ifdim\pgf at picmaxy=-16000pt %
          % if there was no single contiguous part before, determine the width now
          \path%
-            (0pt, 0pt)
+            (0pt, 0pt)%
             node[/yquant/every operator, #2, /yquant/this operator,%
-                 name prefix=, name suffix=, name=] {#1};
+                 name prefix=, name suffix=, name=] {#1};%
       \fi%
 }
 
@@ -608,14 +810,13 @@
       \edef\cmd{%
          \noexpand\path (0pt, 0pt)%
             node[/yquant/every operator, \yquant at draw@@style, /yquant/this operator,%
-                 /yquant/operator/multi main=\ifnum#3=1 true\else false\fi,
+                 /yquant/operator/multi main=\ifnum#3=1 true\else false\fi,%
                  name prefix=, name suffix=, name=]%
-               {\unexpanded\expandafter{\yquant at draw@@content}};
+               {\unexpanded\expandafter{\yquant at draw@@content}};%
       }%
       \cmd%
-      \yquant at register@update at height{#1}{%
-         \the\dimexpr\pgf at picmaxy-\pgf at picminy\relax%
-      }%
+      \yquant at register@update at height{#1}{\the\pgf at picmaxy}%
+      \yquant at register@update at depth{#1}{\the\dimexpr-\pgf at picminy\relax}%
    \fi%
 }
 
@@ -629,24 +830,116 @@
    \pgfinterruptboundingbox%
       \yquant at env@virtualize at path%
       \path%
-         (0pt, 0pt)
+         (0pt, 0pt)%
          node[/yquant/every operator, #2, /yquant/every multi label,%
-              name prefix=, name suffix=, name=] {#1};
+              name prefix=, name suffix=, name=] {#1};%
 }
 
-\def\yquant at draw@finalize at ctrl#1{%
+\protected\long\def\yquant at draw@@subcircuit#1{%
+   \yquant at sort@clear%
+   \def\idx{0}%
+   \dimen2=\yquant at config@operator at minimum@width%
+   % BEGIN_FOLD register
+   \def\do##1{%
+      \ifx\yquant at lang@attr at name\empty%
+         \let\nodename=\empty%
+      \else%
+         \edef\nodename{\yquant at lang@attr at name-\idx}%
+      \fi%
+      \ifyquant at firsttoken\yquant at register@multi{##1}{%
+         \yquant at draw@@subcircuit at multi{#1}{##1}%
+      }{%
+         \yquant at draw@@subcircuit at single{#1}{##1}%
+      }%
+      \dimen0=\dimexpr%
+         \csname yquant at env\yquant at circuit@subcircuit at id @xmax\endcsname-%
+         \csname yquant at env\yquant at circuit@subcircuit at id @xmin\endcsname%
+      \relax\relax%
+      \pgfinterruptboundingbox%
+         % There will still be some extra width taken by separations or other things related to the box itself.
+         \yquant at env@virtualize at path%
+         \path%
+            (0pt, 0pt)%
+            node[/yquant/every operator, #1,%
+                 /yquant/operators/every subcircuit box, /yquant/this operator,%
+                 /yquant/operators/this subcircuit box,%
+                 name prefix=, name suffix=, name=]%
+            {\hbox to \dimen0{}};%
+         % We also don't update the height here, as the height of the subcircuit is undetermined as long as we did not match inner and outer wires.
+         \expandafter%
+      \endpgfinterruptboundingbox%
+      \expandafter\dimen\expandafter0\expandafter=%
+         \the\dimexpr\pgf at picmaxx-\pgf at picminx\relax\relax%
+      \ifdim\dimen0>\dimen2 %
+         \dimen2=\dimen0 %
+      \fi%
+      \numdef\idx{\idx+1}%
+   }%
+   \dolistloop\yquant at circuit@operator at targets%
+   % END_FOLD
+   \yquant at draw@@controls%
+   \yquant at draw@@finalize{}{#1}%
+}
+
+\protected\def\yquant at draw@@subcircuit at single#1#2{%
+   \edef\yquant at circuit@subcircuit at param{#2\yquant at list@delim}%
+   \yquant at circuit@subcircuit{#1}%
+   \yquant at sort@eadd{%
+      \yquant at draw@subcircuit at single%
+         {#2}% register index
+         {\yquant at circuit@subcircuit at id}%
+         {\nodename}%
+   }%
+}
+
+\protected\def\yquant at draw@@subcircuit at multi#1#2{%
+   \edef\yquant at circuit@subcircuit at param{\@fifthoffive#2}%
+   \yquant at sort@list\yquant at circuit@subcircuit at param\yquant at sort@ascending%
+   \yquant at circuit@subcircuit{#1}%
+   \yquant at sort@eadd{%
+      \yquant at draw@subcircuit at multi%
+         #2%
+         {\yquant at circuit@subcircuit at id}%
+         {\nodename}%
+   }
+}
+
+\protected\long\def\yquant at draw@@output at single#1#2{%
+   \path
+      (\yquant at env@end at xpos, 0pt)%
+      node[/yquant/every output,%
+           /yquant/every \yquant at register@type at tostring{\yquant at register@get at type{#1}} output] {#2};%
+   \numdef\idx{\idx+1}%
+}
+
+\protected\long\def\yquant at draw@@output at multi#1{%
+   \path[/yquant/every multi output]%
+      (\yquant at env@end at xpos, 0pt) -- (\yquant at env@end at xpos, 1cm)%
+      node {#1};%
+   \numdef\idx{\idx+1}%
+}
+
+\def\yquant at draw@@finalize at ctrl#1{%
+   \let\next=\yquant at draw@@finalize at ctrl@single%
    \ifx\yquant at draw@multi#1%
-      \expandafter\yquant at draw@finalize at ctrl@multi%
+      \let\next=\yquant at draw@@finalize at ctrl@multi%
    \else%
       \ifx\yquant at draw@multiinit#1%
-         \expandafter\expandafter\expandafter\yquant at draw@finalize at ctrl@multi%
+         \let\next=\yquant at draw@@finalize at ctrl@multi%
       \else%
-         \expandafter\expandafter\expandafter\yquant at draw@finalize at ctrl@single%
+         \ifx\yquant at draw@subcircuit at multi#1%
+            \let\next=\yquant at draw@@finalize at ctrl@subcircuit at multi%
+         \else%
+            \ifx\yquant at draw@subcircuit at single#1%
+               \let\next=\yquant at draw@@finalize at ctrl@subcircuit at single%
+            \fi%
+         \fi%
       \fi%
    \fi%
+   \next%
 }
 
-\protected\def\yquant at draw@finalize at ctrl@single#1#2{%
+\protected\def\yquant at draw@@finalize at ctrl@single#1#2{%
    \unless\ifyquant at circuit@operator at hasControls%
       \yquant at register@set at x#1{\the\dimen2}%
    \fi%
@@ -655,13 +948,17 @@
    }%
 }
 
-\protected\def\yquant at draw@finalize at ctrl@singleinit#1#2{%
+\def\yquant at draw@@finalize at ctrl@subcircuit at single#1#2#3{%
+   \yquant at draw@@finalize at ctrl@single{#1}{#3}%
+}
+
+\protected\def\yquant at draw@@finalize at ctrl@singleinit#1#2{%
    \eappto\yquant at draw@append{%
       \yquant at draw@callback at wire{#1}%
    }%
 }
 
-\protected\def\yquant at draw@finalize at ctrl@multi#1#2#3#4#5{%
+\protected\def\yquant at draw@@finalize at ctrl@multi#1#2#3#4#5{%
    \unless\ifyquant at circuit@operator at hasControls{%
       % \yquant at for uses \loop...\repeat and hence redefines \body, which would destroy an outer loop.
       % if we did not draw a control line, the x position has not yet been set. A multi-qubit register might visually extend over multiple registers that are not even part, hence we update them all.
@@ -669,16 +966,15 @@
          \yquant at register@set at x\yquant at i{\the\dimen2}%
       }%
    }\fi%
-   % this is called from a do loop itself, so preserve \do (but do not enter grouping)
-   \let\yquant at register@multi at contiguous=\yquant at draw@finalize at ctrl@multi at contiguous%
+   \let\yquant at register@multi at contiguous=\yquant at draw@@finalize at ctrl@multi at contiguous%
    \ifyquant at circuit@operator at hasControls%
       \ifyquant at config@operator at multi@warn%
-         \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{0}%
+         \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{0}%
       \else%
-         \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{2}%
+         \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{2}%
       \fi%
    \else%
-      \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{2}%
+      \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{2}%
    \fi%
    \cslet{\yquant at prefix finalize at ctrl@draw at appto}\empty%
    #4%
@@ -686,17 +982,37 @@
    \csgundef{\yquant at prefix finalize at ctrl@draw at appto}%
 }
 
-\protected\def\yquant at draw@finalize at ctrl@multiinit#1#2#3#4#5{%
+\protected\def\yquant at draw@@finalize at ctrl@subcircuit at multi@loop#1{%
+   \begingroup%
+      \edef\tmp{\yquant at draw@callback at wire{#1}}%
+      \expandafter%
+   \endgroup%
+   \expandafter\appto\expandafter\yquant at draw@append\expandafter{\tmp}%
+}
+
+\protected\def\yquant at draw@@finalize at ctrl@subcircuit at multi#1#2#3#4#5#6{%
+   % there are no contiguous parts here, don't call the normal @multi
+   \unless\ifyquant at circuit@operator at hasControls{%
+      % \yquant at for uses \loop...\repeat and hence redefines \body, which would destroy an outer loop.
+      % if we did not draw a control line, the x position has not yet been set. A multi-qubit register might visually extend over multiple registers that are not even part, hence we update them all.
+      \yquant at for \yquant at i := #1 to #2 {%
+         \yquant at register@set at x\yquant at i{\the\dimen2}%
+      }%
+   }\fi%
+   \forlistloop\yquant at draw@@finalize at ctrl@subcircuit at multi@loop{#4}%
+}
+
+\protected\def\yquant at draw@@finalize at ctrl@multiinit#1#2#3#4#5{%
    % this is called from a do loop itself, so preserve \do (but do not enter grouping)
-   \let\yquant at register@multi at contiguous=\yquant at draw@finalize at ctrl@multi at contiguous%
+   \let\yquant at register@multi at contiguous=\yquant at draw@@finalize at ctrl@multi at contiguous%
    \ifyquant at circuit@operator at hasControls%
       \ifyquant at config@operator at multi@warn%
-         \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{0}%
+         \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{0}%
       \else%
-         \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{2}%
+         \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{2}%
       \fi%
    \else%
-      \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{2}%
+      \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{2}%
    \fi%
    \cslet{\yquant at prefix finalize at ctrl@draw at appto}\empty%
    #4%
@@ -704,15 +1020,15 @@
    \csgundef{\yquant at prefix finalize at ctrl@draw at appto}%
 }
 
-\protected\def\yquant at draw@finalize at ctrl@multi at contiguous#1#2#3{%
-   \ifnum\yquant at draw@finalize at ctrl@multi at contiguous@warn=1 %
+\protected\def\yquant at draw@@finalize at ctrl@multi at contiguous#1#2#3{%
+   \ifnum\yquant at draw@@finalize at ctrl@multi at contiguous@warn=1 %
       \PackageWarning{yquant.sty}{Ambiguous operation: multiple discontiguous multi-register operations in combination with controls make it hard to visually determine on which registers the gates act on.}%
       % switch the warning off for this group (which is a single operation)
       \yquant at config@operator at multi@warnfalse%
-      \def\yquant at draw@finalize at ctrl@multi at contiguous@warn{2}%
+      \def\yquant at draw@@finalize at ctrl@multi at contiguous@warn{2}%
    \else%
-      \numdef\yquant at draw@finalize at ctrl@multi at contiguous@warn{%
-         \yquant at draw@finalize at ctrl@multi at contiguous@warn+1%
+      \numdef\yquant at draw@@finalize at ctrl@multi at contiguous@warn{%
+         \yquant at draw@@finalize at ctrl@multi at contiguous@warn+1%
       }%
    \fi%
    {% save \body

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-env.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-env.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-env.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -42,71 +42,235 @@
    \yquant at env@begin at arg[]%
 }
 
-\long\protected\def\yquant at env@begin at arg[#1]{%
+\def\yquant at env@begin at arg{%
+   \let\yquant at env@check=\yquant at env@check@%
+   \yquant at env@begin at generic\yquant%
+}
+
+\long\protected\def\yquant at env@begin at generic#1[#2]{
    \begingroup%
-      \advance\yquant at env by 1 %
+      \let\yquant at parent=\yquant at prefix%
+      \global\advance\yquant at env by 1 %
       \edef\yquant at prefix{yquant at env\the\yquant at env @}%
-      \let\yquant=\yquant at env@scan%
-      \yquant at lang@reset at attrs%
+      \ifnum\yquant at env=1 %
+         \yquant at env@substikz#1%
+         \def\yquant at env@create at x{0pt}%
+         \global\cslet{\yquant at prefix parameters}\empty%
+      \else%
+         \let\yquant at lang@reset at attrs@inputoutput=\yquant at lang@reset at attrs@inputoutput at subcircuit%
+         \let\yquant at env@create at x=\yquant at circuit@operator at x%
+         \global\cslet{\yquant at prefix parameters}\yquant at circuit@subcircuit at param%
+         \yquant at env@lazyfalse% forbid lazy register creation in subcircuits. We need a proper and in-order declaration of the subcircuit's interface.
+      \fi%
+      \let#1=\yquant at env@scan%
+      \yquant at env@reset at commands%
       \csgdef{\yquant at prefix registers}{0}%
       \global\cslet{\yquant at prefix draw}\relax%
       \global\cslet{\yquant at prefix outputs}\relax%
+      \csdimgdef{\yquant at prefix xmin}{\yquant at env@create at x+\yquant at config@operator at sep}%
+      \global\cslet{\yquant at prefix subcircuits}\empty%
+      \global\cslet{\yquant at prefix inonly}\empty%
       \csxdef{\yquant at prefix cleanup}{%
          \expandafter\noexpand\csname\yquant at prefix registers\endcsname%
          \expandafter\noexpand\csname\yquant at prefix draw\endcsname%
          \expandafter\noexpand\csname\yquant at prefix outputs\endcsname%
+         \expandafter\noexpand\csname\yquant at prefix parameters\endcsname%
+         \expandafter\noexpand\csname\yquant at prefix xmin\endcsname%
+         \expandafter\noexpand\csname\yquant at prefix subcircuits\endcsname%
+         \expandafter\noexpand\csname\yquant at prefix inonly\endcsname%
          \expandafter\noexpand\csname\yquant at prefix cleanup\endcsname%
       }%
-      \ifnum\yquant at env=1 %
-         \yquant at env@substikz%
-      \fi%
-      \scope[{/yquant/.cd, #1, /tikz/.cd, /yquant/every circuit}]%
+      \scope[{/yquant/.cd, #2, /tikz/.cd, /yquant/every circuit}]%
 }
-\newif\ifyquantdebug
+
 \protected\def\yquant at env@end{%
          \letcs\yquant at env@end at registers{\yquant at prefix registers}%
          \ifnum\yquant at env@end at registers>0 %
             % draw all wires
             \yquant at register@get at maxxrange\yquant at env@end at xpos{1}{\yquant at env@end at registers}%
-            % to have a symmetric situation, we extend again one separation at the end
+            \ifdim\csname\yquant at prefix xmin\endcsname<%
+                  \dimexpr\yquant at env@create at x+\yquant at config@operator at sep\relax%
+               % to have a symmetric situation, we extend again one separation at the end
+               \dimdef\yquant at env@end at xpos{\yquant at env@end at xpos+\yquant at config@operator at sep}%
+               \global\cslet{\yquant at prefix xmax}\yquant at env@end at xpos%
+            \else%
+               % while the outputs need this, the subcircuit doesn't if there are no outputs.
+               \global\cslet{\yquant at prefix xmax}\yquant at env@end at xpos%
+               \dimdef\yquant at env@end at xpos{\yquant at env@end at xpos+\yquant at config@operator at sep}%
+            \fi%
+            \yquant at cleanup@csadd{xmax}%
             \csxappto{\yquant at prefix draw}{%
-               \def\noexpand\yquant at env@end at xpos{%
-                  \the\dimexpr\yquant at env@end at xpos+\yquant at config@operator at sep\relax%
-               }%
-               \yquant at circuit@endwires{\yquant at env@end at registers}%
-            }
-            % also calculate the true y positions
-            \dimen0=0pt %
-            \dimen2=0pt %
-            \dimen4=\yquant at config@register at sep %
-            \yquant at for \i := 1 to \yquant at env@end at registers {%
-               % we do not care if the wire is present for the y position
-               \dimen2=.5\dimexpr\yquant at register@get at height\i\relax%
-               \advance\dimen0 by \dimen2\relax%
-               % top-to-bottom means negative coordinates in pgf
-               \yquant at register@set at y\i{-\the\dimen0}%
-               \advance\dimen0 by \dimen2\relax%
-               \advance\dimen0 by \dimen4\relax%
+               \yquant at circuit@endwires{\yquant at env@end at xpos}%
             }%
             \csname\yquant at prefix outputs\endcsname%
+            % also calculate the true y positions
+            \unless\ifdefined\yquant at parent%
+               \yquant at env@end at calcypositions%
+            \fi%
+         \else%
+            \ifdefined\yquant at parent%
+               \PackageError{yquant.sty}{Empty subcircuit}%
+                            {Subcircuits must contain registers.}%
+            \else%
+               \PackageWarning{yquant.sty}{Empty quantum circuit}%
+            \fi%
          \fi%
-      \endscope%
-      % Now we actually carry out the tikz commands which were previously stored in the draw command. But before this, we get rid of all \yquant at env@scan calls and also restore the scope command, since this would add itself once again. And get at y needs to expand.
-      \let\path=\tikz at command@path%
-      \let\tikz at lib@scope at check=\yquant at env@substikz at scopecheck%
-      \let\tikz at scope@opt=\yquant at env@substikz at scope%
-      \let\endtikz at scope@env=\yquant at env@substikz at endscope%
-      \let\endscope=\endtikz at scope@env%
-      \let\stopscope=\endscope%
-      \yquant at register@get at y@@expandable%
-      \ifyquantdebug%
-         \csshow{\yquant at prefix draw}%
+         \global\let\yquant at env@scan=\relax% don't rescan after the scope
+      \endscope% this will exit five to seven groups (depending on the transparency group state, and it might even be driver-dependent, though it is not at the moment), so better make the relax global
+      \global\let\yquant at env@scan=\yquant at env@scan@%
+      \ifdefined\yquant at parent%
+         % We are in a subcircuit. The drawing and cleanup is delayed until the end.
+         \csxappto{\yquant at parent cleanup}%
+            {\unexpanded\expandafter\expandafter\expandafter{%
+               \csname\yquant at prefix cleanup\endcsname%
+            }}%
+         \ifyquantdebug%
+            \csshow{\yquant at prefix draw}%
+         \fi%
+      \else%
+         % Now we actually carry out the tikz commands which were previously stored in the draw command. But before this, we get rid of all \yquant at env@scan calls and also restore the scope command, since this would add itself once again. And get at y needs to expand.
+         \let\path=\tikz at command@path%
+         \let\tikz at lib@scope at check=\yquant at env@substikz at scopecheck%
+         \let\tikz at scope@opt=\yquant at env@substikz at scope%
+         \let\endtikz at scope@env=\yquant at env@substikz at endscope%
+         \let\endscope=\endtikz at scope@env%
+         \let\stopscope=\endscope%
+         \yquant at register@get at y@@expandable%
+         \ifyquantdebug%
+            \csshow{\yquant at prefix draw}%
+         \fi%
+         \csname\yquant at prefix draw\endcsname%
+         \expandafter\expandafter\expandafter\yquant at cleanup\csname\yquant at prefix cleanup\endcsname|%
+         \global\yquant at env=0 %
       \fi%
-      \csname\yquant at prefix draw\endcsname%
-      \expandafter\expandafter\expandafter\yquant at cleanup\csname\yquant at prefix cleanup\endcsname|%
    \endgroup%
 }
 
+\protected\def\yquant at env@end at calcypositions{%
+   \begingroup%
+      \dimen4=\yquant at config@register at sep %
+      % We know the heights of most gates, but subcircuits are tricky. Since the position of their inner wires depends on the heights of the outer wires, we could not make any assumption about their heights yet. So we now need multiple iterations: First, we fix the positions of our own wires preliminarily. Then, we iterate through all subcircuits at the first level, place their wires appropriately and try to align input and output. In doing so, we recognize when there's not enough space at an outer wire. If this is the case, we enlarge the outer wire's height appropriately and restart alignment from the outmost level. If everything is proper, we start aligning at the next level.
+      \yquant at env@end at calcypositions@toplevel1%
+      % Turn the preliminary positions into true ones at every level.
+      \yquant at env@end at setypositions1%
+   \endgroup%
+}
+
+\protected\def\yquant at env@end at calcypositions@loop#1{%
+   % \yquant at parent has to be set already
+   \def\yquant at prefix{yquant at env#1@}%
+   % #1 now holds the id of the subcircuit. Find the first input.
+   \edef\firstinput{%
+      \expandafter\expandafter\expandafter%
+         \@firstoftwo\csname\yquant at prefix firstinput\endcsname%
+   }%
+   \edef\lastinput{%
+      \expandafter\expandafter\expandafter%
+         \@firstoftwo\csname\yquant at prefix lastinput\endcsname%
+   }%
+   % we already made sure during in \yquant at circuit@subcircuit that there is enough space above and below the subcircuit. Still, the wires need to be positioned. However, we will first deal with all intermediate wires, which need to be placed by means of shifts - there is no direct correspondence to heights or depths of parent wires any more.
+   \ifnum\firstinput<\numexpr\lastinput-1\relax%
+      \dimen0=\dimexpr%
+         \csname y@\csname\yquant at prefix registermap@\firstinput\endcsname\endcsname+%
+         \yquant at register@get at depth\firstinput+\dimen4%
+      \relax%
+      \yquant at for \i := \numexpr\firstinput+1\relax to \lastinput {%
+         \advance\dimen0 by \yquant at register@get at height\i\relax%
+         \ifcsname\yquant at prefix registermap@\i\endcsname%
+            % this is an input: do we have enough space by using the outer position?
+            \letcs\outername{\yquant at prefix registermap@\i}%
+            \letcs\outery{y@\outername}%
+            \ifdim\dimen0>\outery\relax%
+               % re-aligning the outer positions is required
+               \ifcsname y+@\outername\endcsname%
+                  \csdimdef{y+@\outername}%
+                           {\csname y+@\outername\endcsname+\dimen0-\outery}%
+               \else%
+                  \csdimdef{y+@\outername}{\dimen0-\outery}%
+               \fi%
+               \let\yquant at env@end at calcypositions@redo=\relax%
+            \fi%
+         \else%
+            \csedef{y@\yquant at prefix register@\i}{\the\dimen0}%
+         \fi%
+         \ifdefined\yquant at env@end at calcypositions@redo%
+            \expandafter\yquant at for@break%
+         \else%
+            \advance\dimen0 by \dimexpr\yquant at register@get at depth\i+\dimen4\relax%
+         \fi%
+      }%
+   \fi%
+   \unless\ifdefined\yquant at env@end at calcypositions@redo%
+      % sounds good, no readjustments necessary. Now transfer the wires above and below in the appropriate macros.
+      \ifnum\firstinput>1 %
+         \dimen0=\dimexpr%
+            \csname y@\csname\yquant at prefix registermap@\firstinput\endcsname\endcsname-%
+            \yquant at register@get at height\firstinput-\dimen4%
+         \relax%
+         \yquant at fordown \i := \numexpr\firstinput-1\relax downto 1 {%
+            \advance\dimen0 by -\yquant at register@get at depth\i\relax%
+            \csedef{y@\yquant at prefix register@\i}{\the\dimen0}%
+            \advance\dimen0 by -\dimexpr\yquant at register@get at height\i+\dimen4\relax%
+         }%
+      \fi%
+      \ifnum\lastinput<\csname\yquant at prefix registers\endcsname%
+         \dimen0=\dimexpr%
+            \csname y@\csname\yquant at prefix registermap@\lastinput\endcsname\endcsname+%
+            \yquant at register@get at depth\lastinput+\dimen4%
+         \relax%
+         \yquant at for \i := \numexpr\lastinput+1\relax to \csname\yquant at prefix registers\endcsname {%
+            \advance\dimen0 by \yquant at register@get at height\i\relax%
+            \csedef{y@\yquant at prefix register@\i}{\the\dimen0}%
+            \advance\dimen0 by \dimexpr\yquant at register@get at depth\i+\dimen4\relax%
+         }%
+      \fi%
+   \fi%
+   \ifdefined\yquant at env@end at calcypositions@redo%
+      % forget every plan about going deeper, restart.
+      \expandafter\listbreak%
+   \else%
+      \expandafter\yquant at env@end at calcypositions@subcircuits%
+   \fi%
+}
+
+\protected\def\yquant at env@end at calcypositions@subcircuits{%
+   \let\yquant at parent=\yquant at prefix%
+   \forlistcsloop\yquant at env@end at calcypositions@loop{\yquant at prefix subcircuits}%
+   \ifdefined\yquant at env@end at calcypositions@redo%
+      \expandafter\listbreak%
+   \fi%
+}
+
+\protected\def\yquant at env@end at calcypositions@toplevel{%
+   \def\yquant at prefix{yquant at env1@}%
+   \dimen0=0pt %
+   \yquant at for \i := 1 to \yquant at env@end at registers {%
+      % we do not care if the wire is present for the y position
+      \advance\dimen0 by \yquant at register@get at height\i\relax%
+      \ifcsname y+@\yquant at prefix register@\i\endcsname%
+         \advance\dimen0 by \csname y+@\yquant at prefix register@\i\endcsname\relax%
+      \fi%
+      \csedef{y@\yquant at prefix register@\i}{\the\dimen0}%
+      \advance\dimen0 by \dimexpr\yquant at register@get at depth\i+\dimen4\relax%
+   }%
+   \undef\yquant at env@end at calcypositions@redo%
+   \let\yquant at parent=\yquant at prefix%
+   \forlistcsloop\yquant at env@end at calcypositions@loop{\yquant at prefix subcircuits}%
+   \ifdefined\yquant at env@end at calcypositions@redo%
+      \expandafter\yquant at env@end at calcypositions@toplevel%
+   \fi%
+}
+
+\protected\def\yquant at env@end at setypositions#1{%
+   \def\yquant at prefix{yquant at env#1@}%
+   \yquant at for \i := 1 to \csname\yquant at prefix registers\endcsname {%
+      \unless\ifcsname\yquant at prefix registermap@\i\endcsname%
+         \yquant at register@set at y\i{-\csname y@\yquant at prefix register@\i\endcsname}%
+      \fi%
+   }%
+   \forlistcsloop\yquant at env@end at setypositions{\yquant at prefix subcircuits}%
+}
+
 \tikzaddtikzonlycommandshortcutlet\yquant\yquant at envunstar
 \expandafter\tikzaddtikzonlycommandshortcutlet\csname yquant*\endcsname\yquant at envstar
 \tikzaddtikzonlycommandshortcutlet\endyquant\yquant at env@end
@@ -117,11 +281,16 @@
 }
 \let\yquant at set=\yquantset%
 
-% private yquant-language related helpers
 % In order to allow nested environments and also grouping (without the need to smuggle definitions out of the groups whenever necessary), we count the number of nested environments.
 \newcount\yquant at env
 
-\def\yquant at env@substikz at finish{%
+% Some commands may be overwritten while a subcircuit is processed, in particular attributes, but also some others. However, within the subcircuit, they need to have their original (un)definition.
+\protected\def\yquant at env@reset at commands{%
+   \yquant at lang@reset at attrs%
+   \yquant at register@reset at multi%
+}
+
+\protected\def\yquant at env@substikz at finish{%
    % Rendering pipeline
    \endgroup%
    \global\pgflinewidth=\tikzscope at linewidth\relax%
@@ -137,17 +306,17 @@
 }
 
 % substitute the tikz commands (defined in \tikz at installcommands) so that they can be arbitrarily interleaved with yquant code. We patch \path, \scope, \endscope, \stopscope, their internal complements, and also patch \yquantset.
-\protected\def\yquant at env@substikz{%
+\protected\def\yquant at env@substikz#1{%
    % \tikz at path@do at at@end is called after a path. Hence, it is an ideal candidate to re-invoke \yquant at env@scan. However, it is by default defined to be \tikz at lib@scope at check, and we need this definition for the scopes library to work correctly. But since \tikz at lib@scope at check is also called after a scope and the end of a scope, this is even better. Yet, we need to check whether the scopes library is present or not.
    \let\yquant at env@substikz at scopecheck=\tikz at lib@scope at check%
    \ifx\tikz at lib@scope at check\pgfutil at empty%
       % no, it is not. This is simple.
-      \let\tikz at lib@scope at check=\yquant at env@scan%
+      \def\tikz at lib@scope at check{\yquant at env@scan}%
    \else%
       % yes, it is. Call it after the special behavior is done.
       \patchcmd\tikz at lib@scope at check{{}}{\yquant at env@scan}\relax{%
          \PackageWarning{yquant.sty}{Patching \string\tikz at lib@scope at check\space failed; you must invoke \string\yquant\space manually after every tikz command to switch back to yquant code.}%
-      }
+      }%
    \fi%
    % We not only need to re-invoke yquant after the tikz command, but we must also make sure that the tikz command is not actually drawn now, but at the end. However, what happens if some macros are used within the command? Here, we choose to expand the macros to the values they have _now_, but protected (which should prevent bad things for formatting commands). If you find this to be an issue, please report.
    \def\path##1;{%
@@ -154,7 +323,7 @@
       \protected at csxappto{\yquant at prefix draw}{%
          \noexpand\path##1;%
       }%
-      \yquant%
+      #1%
    }%
    % no need for \scoped, because it internally calls \scope.
    % We need to hack into \scope, but this is a bit tricky due to its argument handling. In order to get all optional arguments, including the possible animations library, correct, we change \tikz at scope@opt.
@@ -189,6 +358,7 @@
    \begingroup%
       \yquant at env@contscan%
 }
+\let\yquant at env@scan@=\yquant at env@scan
 
 \protected\def\yquant at env@contscan{%
    \yquant at futurenonspacelet\yquant at env@nextchar\yquant at env@check%
@@ -199,7 +369,7 @@
    \yquant at env@scan%
 }
 
-\protected\def\yquant at env@check{%
+\protected\def\yquant at env@check@{%
    \let\next=\relax%
    % Here we assume standard catcodes for A and [, but our language specification also requires this implicitly.
    \ifx\yquant at env@nextchar[%
@@ -254,4 +424,59 @@
 \def\yquant at env@gobblepar{%
    \afterassignment\yquant at env@contscan%
    \let\@eattoken= %
+}
+
+\def\yquantimport at now#1{%
+   \expandafter\yquant\@@input #1\relax%
+}
+
+\def\yquantimportcommand{\yquantimport at now}%
+
+\AtBeginDocument{%
+   % do we have the import package?
+   \ifdefined\@doimport%
+      \providecommand\yquantimportpath{./}%
+      \def\yquantimportcommand{\@doimport\yquantimport at now{\yquantimportpath}}%
+   \fi%
+}
+
+\def\yquantimport{%
+   \@ifstar{\yquantimport at i{*}}{\yquantimport at i{}}%
+}
+
+\def\yquantimport at i#1{%
+   \@ifnextchar[{\yquantimport at ii{#1}}{\yquantimport at ii{#1}[]}%
+}%
+
+\def\yquantimport at ii#1[#2]#3{%
+   \ifpgfpicture%
+      \ifnum\yquant at env>0 %
+         \begingroup%
+            \ifstrequal{#1}{*}{%
+               \yquant at env@lazytrue%
+            }{%
+               \yquant at env@lazyfalse%
+            }%
+            \ifstrempty{#2}{%
+               \yquantimportcommand{#3}%
+            }{%
+               \scope%
+                  \yquantset{#2}%
+                  \yquantimportcommand{#3}%
+               \endscope%
+            }%
+         \endgroup%
+         \expandafter\expandafter\expandafter\yquant%
+      \else%
+         \begin{yquant#1}[{#2}]
+            \yquantimportcommand{#3}%
+         \end{yquant#1}%
+      \fi%
+   \else%
+      \begin{tikzpicture}%
+         \begin{yquant#1}[{#2}]
+            \yquantimportcommand{#3}%
+         \end{yquant#1}%
+      \end{tikzpicture}
+   \fi%
 }
\ No newline at end of file

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-lang.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-lang.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-lang.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -1,14 +1,33 @@
 % BEGIN_FOLD Attributes
-\yquant at langhelper@declare at attr{
-   value/.store in=\yquant at lang@attr at value,
+\yquant at langhelper@declare at attr{%
+   value/.store in=\yquant at lang@attr at value,%
    after/.code={%
       \yquant at register@get at ids{#1}%
       \let\yquant at lang@attr at after=\yquant at register@get at ids@list%
-   },
-   type/.store in=\yquant at lang@attr at type
+   },%
+   type/.store in=\yquant at lang@attr at type,%
+   ancilla/.code={%
+      \undef\yquant at lang@attr at input%
+      \undef\yquant at lang@attr at output%
+   },%
+   in/.code={%
+      \let\yquant at lang@attr at input=\relax%
+      \undef\yquant at lang@attr at output%
+   },%
+   in/.default=\regidx,%
+   out/.code={%
+      \undef\yquant at lang@attr at input%
+      \let\yquant at lang@attr at output=\relax%
+   },%
+   out/.default=\regidx,%
+   inout/.code={%
+      \let\yquant at lang@attr at input=\relax%
+      \let\yquant at lang@attr at output=\relax%
+   }%
+   inout/.default=\regidx,%
 }
-\yquant at langhelper@declare at attr@global{
-   name/.store in=\yquant at lang@attr at name
+\yquant at langhelper@declare at attr@global{%
+   name/.store in=\yquant at lang@attr at name%
 }
 
 \protected\def\yquant at lang@reset at attrs{%
@@ -15,13 +34,24 @@
    \undef\yquant at lang@attr at value%
    \undef\yquant at lang@attr at after%
    \undef\yquant at lang@attr at type%
+   \yquant at lang@reset at attrs@inputoutput%
    \let\yquant at lang@attr at name=\empty%
 }
+
+\protected\def\yquant at lang@reset at attrs@inputoutput{%
+   \undef\yquant at lang@attr at input%
+   \undef\yquant at lang@attr at output%
+}
+
+\protected\def\yquant at lang@reset at attrs@inputoutput at subcircuit{%
+   \let\yquant at lang@attr at input=\relax%
+   \let\yquant at lang@attr at output=\relax%
+}
 % END_FOLD
 
 % BEGIN_FOLD Declaration of registers
 \yquant at langhelper@declare at command@uncontrolled{nobit}\yquant at lang@@nobit
-\yquant at langhelper@setup at attrs{nobit}{}{}
+\yquant at langhelper@setup at attrs{nobit}{}{ancilla,out}
 \def\yquant at lang@@nobit#1{%
    \let\yquant at lang@create at type=\yquant at register@type at none%
    \def\yquant at lang@create at style{initial}% there is no separate style, just duplicate
@@ -28,14 +58,14 @@
    \ifdefined\yquant at lang@attr at value%
       \PackageError{yquant.sty}{Placeholder initialization must not have value}%
                    {You must not provide a description for an invisible register.}
-   \else
+   \else%
       \let\yquant at lang@attr at value=\empty%
    \fi%
-   \yquant at lang@create at parse@name#1[;
+   \yquant at lang@create at parse@name#1[;%
 }
 
 \yquant at langhelper@declare at command@uncontrolled{qubit}\yquant at lang@@qubit
-\yquant at langhelper@setup at attrs{qubit}{}{after,value}
+\yquant at langhelper@setup at attrs{qubit}{}{after,value,ancilla,in,out,inout}
 \def\yquant at lang@@qubit#1{%
    \let\yquant at lang@create at type=\yquant at register@type at q%
    \def\yquant at lang@create at style{qubit}%
@@ -42,11 +72,11 @@
    \unless\ifdefined\yquant at lang@attr at value%
       \let\yquant at lang@attr at value=\yquant at config@register at default@name%
    \fi%
-   \yquant at lang@create at parse@name#1[;
+   \yquant at lang@create at parse@name#1[;%
 }
 
 \yquant at langhelper@declare at command@uncontrolled{cbit}\yquant at lang@@cbit
-\yquant at langhelper@setup at attrs{qubit}{}{after,value}
+\yquant at langhelper@setup at attrs{cbit}{}{after,value,ancilla,in,out,inout}
 \def\yquant at lang@@cbit#1{%
    \let\yquant at lang@create at type=\yquant at register@type at c%
    \def\yquant at lang@create at style{qubit}%
@@ -53,11 +83,11 @@
    \unless\ifdefined\yquant at lang@attr at value%
       \let\yquant at lang@attr at value=\yquant at config@register at default@name%
    \fi%
-   \yquant at lang@create at parse@name#1[;
+   \yquant at lang@create at parse@name#1[;%
 }
 
 \yquant at langhelper@declare at command@uncontrolled{qubits}\yquant at lang@@qubits
-\yquant at langhelper@setup at attrs{qubit}{}{after,value}
+\yquant at langhelper@setup at attrs{qubits}{}{after,value,ancilla,in,out,inout}
 \def\yquant at lang@@qubits#1{%
    \let\yquant at lang@create at type=\yquant at register@type at qs%
    \def\yquant at lang@create at style{qubits}%
@@ -64,7 +94,7 @@
    \unless\ifdefined\yquant at lang@attr at value%
       \let\yquant at lang@attr at value=\yquant at config@register at default@name%
    \fi%
-   \yquant at lang@create at parse@name#1[;
+   \yquant at lang@create at parse@name#1[;%
 }
 
 \def\yquant at lang@create at parse@name#1[#2;{%
@@ -82,7 +112,7 @@
       \ifnum\len<1 %
          \PackageError{yquant.sty}{Invalid register length}%
                       {Valid register lengths are integers greater or equal to one.}%
-      \fi
+      \fi%
    }{%
       \PackageError{yquant.sty}{Invalid register name}%
          {Register names must not contain `[' apart from register length indication.}%
@@ -109,8 +139,11 @@
    % determine x position
    \ifdefined\yquant at lang@attr at after%
       \yquant at register@get at maxxlist\yquant at lang@create at x\yquant at lang@attr at after%
+      \ifdefined\yquant at lang@attr at input%
+         \PackageError{yquant.sty}{An input register cannot be created with `after` attribute}{}%
+      \fi%
    \else%
-      \def\yquant at lang@create at x{0pt}%
+      \let\yquant at lang@create at x=\yquant at env@create at x%
    \fi%
    \begingroup%
       \ifx\yquant at lang@attr at name\empty%
@@ -120,29 +153,87 @@
       \fi%
       % pre-set y position
       \yquant at for \idx := \idx to \numexpr \len -1\relax {%
-         \yquant at register@define%
-            \yquant at lang@create at type%
-            \yquant at lang@create at x%
-            \reg\idx%
+         \ifdefined\yquant at lang@attr at input%
+            \expandafter\yquant at list@gdequeue\csname \yquant at prefix parameters\endcsname\outermap%
+            \ifx\outermap\empty%
+               \PackageError{yquant.sty}{Invalid subcircuit parameter count}%
+                            {No outer register available for input register `\reg[\idx]`.}%
+            \fi%
+            \csxappto{\yquant at prefix draw}{%
+               \yquant at lang@create at check@input{\outermap}{\yquant at lang@create at type}%
+                                              {\reg[\idx]}%
+            }%
+            \unless\ifdefined\yquant at lang@attr at output%
+               \listcsxadd{\yquant at prefix inonly}%
+                          {\the\numexpr\csname\yquant at prefix registers\endcsname+1\relax}%
+            \fi%
+            \yquant at register@alias%
+               \yquant at lang@create at type%
+               \yquant at lang@create at x%
+               \reg\idx\outermap%
+         \else%
+            \ifdefined\yquant at lang@attr at output%
+               \expandafter\yquant at list@gdequeue\csname\yquant at prefix parameters\endcsname\outermap%
+               \ifx\outermap\empty%
+                  \PackageError{yquant.sty}{Invalid subcircuit parameter count}%
+                               {No outer register available for output register `\reg`.}%
+               \fi%
+               \csxappto{\yquant at prefix draw}{%
+                  \yquant at lang@create at check@output{\outermap}{\reg[\idx]}%
+               }%
+               \yquant at register@alias%
+                  \yquant at lang@create at type%
+                  \yquant at lang@create at x%
+                  \reg\idx\outermap%
+            \else%
+               \yquant at register@define%
+                  \yquant at lang@create at type%
+                  \yquant at lang@create at x%
+                  \reg\idx%
+            \fi%
+         \fi%
          % First determine the actual height by a virtual draw command
-         \pgfinterruptboundingbox%
-            \yquant at env@virtualize at path%
-            \path%
-               (\yquant at lang@create at x, 0pt)
-               node[/yquant/every label, /yquant/every initial label,%
-                    /yquant/every \yquant at lang@create at style\space label]
-               {\trim at spaces{\yquant at lang@attr at value}};%
-            \expandafter\yquant at register@update at height%
-               \csname\yquant at prefix registers\endcsname%
-               {\the\dimexpr\pgf at picmaxy-\pgf at picminy\relax}%
-         \endpgfinterruptboundingbox%
+         \protected at edef\trimmedvalue{\trim at spaces{\yquant at lang@attr at value}}%
+         \unless\ifx\empty\trimmedvalue%
+            \pgfinterruptboundingbox%
+               \yquant at env@virtualize at path%
+               \edef\cmd{%
+                  \noexpand\path%
+                     (\yquant at lang@create at x, 0pt)%
+                     node[/yquant/every label, /yquant/every initial label,%
+                          /yquant/every \yquant at lang@create at style\space label%
+                          \ifdefined\yquant at lang@attr at input, /yquant/every input label\fi]%
+                     {\unexpanded\expandafter{\trimmedvalue}};%
+               }%
+               \cmd%
+               \ifdim\pgf at picminx<\csname\yquant at prefix xmin\endcsname%
+                  \csxdef{\yquant at prefix xmin}{\the\pgf at picminx}%
+               \fi%
+               \expandafter\yquant at register@update at height%
+                  \csname\yquant at prefix registers\endcsname%
+                  {\the\pgf at picmaxy}%
+               \expandafter\yquant at register@update at depth%
+                  \csname\yquant at prefix registers\endcsname%
+                  {\the\dimexpr-\pgf at picminy\relax}%
+            \endpgfinterruptboundingbox%
+         \fi%
          % Prepare to shipout
-         \protected at csxappto{\yquant at prefix draw}{%
+         \csxappto{\yquant at prefix draw}{%
             \yquant at lang@create at draw{\csname\yquant at prefix registers\endcsname}%
                                     {\yquant at lang@create at x}%
                                     {\yquant at lang@create at style}%
-                                    {\yquant at lang@attr at value}%
+                                    \ifdefined\yquant at lang@attr at input1\else0\fi%
+                                    {\unexpanded\expandafter{\trimmedvalue}}%
                                     {\yquant at lang@create at name}%
+            \unless\ifdefined\yquant at lang@attr at input%
+               \ifdefined\yquant at lang@attr at output%
+                  \yquant at circuit@flushwire%
+                     {\csname\yquant at prefix registers\endcsname}%
+                  \yquant at register@set at type%
+                     {\csname\yquant at prefix registers\endcsname}%
+                     \yquant at lang@create at type%
+               \fi%
+            \fi%
          }%
       }%
       \unless\ifx\yquant at lang@attr at name\empty%
@@ -154,32 +245,166 @@
    \endgroup%
 }
 
-\protected\def\yquant at lang@create at draw#1#2#3#4#5{%
+\protected\def\yquant at lang@create at check@input#1#2#3{%
    \begingroup%
+      \let\yquant at prefix=\yquant at parent%
+      \unless\if#2\yquant at register@get at type{#1}%
+         \PackageError{yquant.sty}{Subcircuit expects wire of type `\yquant at register@type at tostring{#2}', but got `\yquant at register@type at tostring{\yquant at register@get at type{#1}}'}%
+                      {Outer and inner wire types must match for input wire `\detokenize{#3}`.}%
+      \fi%
+   \endgroup%
+}
+
+\protected\def\yquant at lang@create at check@output#1#2{%
+   \begingroup%
+      \let\yquant at prefix=\yquant at parent%
+      \unless\if\yquant at register@type at none\yquant at register@get at type{#1}%
+         \PackageError{yquant.sty}%
+                      {Subcircuit expects discarded wire, got `\yquant at register@type at tostring{\yquant at register@get at type{#1}}'}%
+                      {Outer wire must be discarded before being acceptable for output-only register `\detokenize{#2}`.}%
+      \fi%
+   \endgroup%
+}
+
+\protected\def\yquant at lang@create at draw#1#2#3#4#5#6{%
+   \begingroup%
       \dimdef\wireypos{\yquant at register@get at y{#1}}%
-      \ifstrempty{#4}{%
+      \ifcsname\yquant at prefix xshift\endcsname%
+         \dimdef\createxpos{#2+\csname\yquant at prefix xshift\endcsname}%
+      \else%
+         \dimdef\createxpos{#2}%
+      \fi%
+      \ifstrempty{#5}{%
          % For empty labels, we still put the node at the appropriate position as it may needs to be referenced, but we will not let it effect the bounding box (so that the left end is not shifted), and we don't need an inner separation, so that the label is truely just a coordinate.
-         \path[overlay]
-            (#2, \wireypos)%
-            coordinate[name prefix=, name suffix=, name=yquantbox];
+         \path[overlay]%
+            (\createxpos, \wireypos)%
+            coordinate[name prefix=, name suffix=, name=yquantbox];%
+         \let\pgfshapeclippathhorzresult=\empty%
       }{%
-         \path
-            (#2, \wireypos)%
-            node[/yquant/every label, /yquant/every initial label,%
-                 /yquant/every #3 label, name prefix=, name suffix=, name=yquantbox]%
-                {#4};%
+         \edef\cmd{%
+            \noexpand\path%
+               (\createxpos, \wireypos)%
+               node[/yquant/every label, /yquant/every initial label,%
+                    /yquant/every #3 label\ifx1#4, /yquant/every input label\fi,%
+                    name prefix=, name suffix=, name=yquantbox]%
+                   {\unexpanded{#5}};%
+            \ifcsname\yquant at prefix registermap@#1\endcsname%
+               \pgfshapeclippath{yquantbox}%
+                                {/yquant/every label, /yquant/every initial label,%
+                                 /yquant/every #3 label\ifx1#4, /yquant/every input label\fi}%
+            \fi%
+         }%
+         \cmd%
       }%
-      % set the wire style to have the correct \pgflinewidth available (we don't allow individual line widths for different types of wires)
-      \tikzset{/yquant/every wire}%
-      \pgfpointanchor{yquantbox}{east}%
-      \yquant at register@set at lastwire{#1}{{\the\pgf at x}{\the\pgf at x}{}{}}%
-      \ifstrempty{#5}\relax{%
-         \pgfnodealias{\tikz at pp@name{#5}}{yquantbox}%
+      \ifcsname\yquant at prefix registermap@#1\endcsname%
+         % if this is an alias, the creation is just an extension
+         \if\yquant at register@type at none\yquant at register@get at type{#1}%
+            \ifstrequal{#3}{initial}{%
+               \yquant at circuit@extendwire{#1}{east}%
+            }{%
+               % however, our inner wire is present, while the outer wire was discarded.
+               \tikzset{/yquant/every wire}%
+               \pgfpointanchor{yquantbox}{east}%
+               \yquant at register@set at lastwire{#1}{%
+                  {\the\pgf at x}{\the\pgf at x}{}%
+                  {\unexpanded\expandafter{\pgfshapeclippathhorzresult}}%
+               }%
+            }%
+         \else%
+            \yquant at circuit@extendwire{#1}{east}%
+         \fi%
+      \else%
+         % set the wire style to have the correct \pgflinewidth available (we don't allow individual line widths for different types of wires)
+         \tikzset{/yquant/every wire}%
+         \pgfpointanchor{yquantbox}{east}%
+         \yquant at register@set at lastwire{#1}{{\the\pgf at x}{\the\pgf at x}{}{}}%
+      \fi%
+      \ifstrempty{#6}\relax{%
+         \pgfnodealias{\tikz at pp@name{#6}}{yquantbox}%
       }%
    \endgroup%
 }
 % END_FOLD
 
+% BEGIN_FOLD Define own gates
+\def\yquantdefinegate#1{%
+   \ifcsname yquant at lang@#1\endcsname%
+      \PackageError{yquant.sty}{Gate redefined}%
+                   {The gate `#1' already exists. Use \string\yquantredefinegate\space if you really want to redefine it.}%
+   \fi%
+   \@ifnextchar[{\yquantdefinegate at i{#1}}%
+                {\yquantdefinegate at i{#1}[/yquant/operators/every custom gate]}%
+}
+
+% while this command is provided to redefine gates, we do not clear the attributes that were once assigned to the previous one, so gate redefinition is discouraged!
+\def\yquantredefinegate#1{%
+   \unless\ifcsname yquant at lang@#1\endcsname%
+      \PackageError{yquant.sty}{Unknown gate redefined}%
+                   {The gate `#1' is unknown and cannot be redefined. Use \string\yquantdefinegate\space to define it.}%
+   \fi%
+   \@ifnextchar[{\yquantdefinegate at i{#1}}%
+                {\yquantdefinegate at i{#1}[/yquant/operators/every custom gate]}%
+}
+
+\protected\long\def\yquantdefinegate at i#1[#2]#3{%
+   \pgfkeys{/yquant/operators/every #1/.style={#2}}%
+   \protected at edef\yquantdefinegate at do{%
+      \yquant at langhelper@declare at command%
+         {#1}%
+         \expandafter\noexpand\csname yquant at lang@@#1\endcsname%
+      % This does not clear the attributes for redefines, but makes at least sure nothing is marked as required that should not be.
+      \yquant at langhelper@setup at attrs{#1}{}{}%
+      % Now define the gate's content as a macro
+      \def\expandafter\noexpand\csname yquant at lang@@@#1\endcsname{%
+         #3%
+      }%
+      % And provide the implementation
+      \protected\def\expandafter\noexpand\csname yquant at lang@@#1\endcsname####1####2####3{%
+         \yquant at register@get at multiaslist%
+         \yquant at circuit@operator{####1}{####2}{####3}%
+         \let\noexpand\yquant at lang@attr at value=\expandafter\noexpand\csname yquant at lang@@@#1\endcsname%
+         \yquant at draw@@subcircuit{/yquant/operators/every #1}%
+      }%
+   }%
+   \yquantdefinegate at do%
+}
+
+\def\yquantdefinebox#1{%
+   \ifcsname yquant at lang@#1\endcsname%
+      \PackageError{yquant.sty}{Gate redefined}%
+                   {The gate `#1' already exists. Use \string\yquantredefinebox\space if you really want to redefine it.}%
+   \fi%
+   \@ifnextchar[{\yquantdefinebox at i{#1}}%
+                {\yquantdefinebox at i{#1}[/yquant/operators/every box]}%
+}
+
+\def\yquantredefinebox#1{%
+   \unless\ifcsname yquant at lang@#1\endcsname%
+      \PackageError{yquant.sty}{Unknown gate redefined}%
+                   {The gate `#1' is unknown and cannot be redefined. Use \string\yquantdefinebox\space to define it.}%
+   \fi%
+   \yquantdefinebox at i{#1}%
+   \@ifnextchar[{\yquantdefinebox at i{#1}}%
+                {\yquantdefinebox at i{#1}[/yquant/operators/every box]}%
+}
+
+\protected\long\def\yquantdefinebox at i#1[#2]#3{%
+   \pgfkeys{/yquant/operators/every #1/.style={#2}}%
+   \protected at edef\yquantdefinebox at do{%
+      \yquant at langhelper@declare at command%
+         {#1}%
+         \expandafter\noexpand\csname yquant at lang@@#1\endcsname%
+      \yquant at langhelper@setup at attrs{#1}{}{}%
+      \def\expandafter\noexpand\csname yquant at lang@@#1\endcsname{%
+         \yquant at draw%
+            {#3}%
+            {/yquant/operators/every #1}%
+      }%
+   }%
+   \yquantdefinebox at do%
+}
+% END_FOLD
+
 % BEGIN_FOLD Box registers
 % all-purpose box with arbitrary text
 \yquant at langhelper@declare at command{box}\yquant at lang@@box
@@ -187,68 +412,30 @@
 \def\yquant at lang@@box{%
    \yquant at register@get at allowmultitrue%
    \expandafter\yquant at draw%
-      \expandafter{\yquant at lang@attr at value}
+      \expandafter{\yquant at lang@attr at value}%
       {/yquant/operators/every box}%
 }
 
 % Hadamard
-\yquant at langhelper@declare at command{h}\yquant at lang@@h
-\yquant at langhelper@setup at attrs{h}{}{}
-\def\yquant at lang@@h{%
-   \yquant at draw%
-      {$H$}%
-      {/yquant/operators/every h}%
-}
+\yquantdefinebox{h}{$H$}
 
 % Pauli X (or NOT)
-\yquant at langhelper@declare at command{x}\yquant at lang@@x
-\yquant at langhelper@setup at attrs{x}{}{}
-\def\yquant at lang@@x{%
-   \yquant at draw%
-      {$X$}%
-      {/yquant/operators/every x}%
-}
+\yquantdefinebox{x}[/yquant/operators/every pauli]{$X$}
 
 % Pauli Y
-\yquant at langhelper@declare at command{y}\yquant at lang@@y
-\yquant at langhelper@setup at attrs{y}{}{}
-\def\yquant at lang@@y{%
-   \yquant at draw%
-      {$Y$}%
-      {/yquant/operators/every y}%
-}
+\yquantdefinebox{y}[/yquant/operators/every pauli]{$Y$}
 
 % Pauli Z
-\yquant at langhelper@declare at command{z}\yquant at lang@@z
-\yquant at langhelper@setup at attrs{z}{}{}
-\def\yquant at lang@@z{%
-   \yquant at draw%
-      {$Z$}%
-      {/yquant/operators/every z}%
-}
+\yquantdefinebox{z}[/yquant/operators/every pauli]{$Z$}
 
 % sub-circuit: This is a nested circuit.
-%\yquant at langhelper@declare at command{subcircuit}\yquant at lang@@subcircuit
-%\yquant at langhelper@setup at attrs{subcircuit}{}{shift}
-%\protected\def\yquant at lang@@subcircuit#1#2#3{%
-%   % First we restore access to the yquant environment (which was a shorthand to
-%   % re-starting the parser within the environment, conveniently forbidding nesting).
-%   \let\yquant=\yquant at envunstar%
-%   \cslet{yquant*}\yquant at envstar%
-%   % Then determine the actual subcircuit content.
-%   \yquant at circuit@subcircuit\yquant at lang@attr at value%
-%   % Now the subcircuit is stored in \yquant at circuit@subcircuit at box. However, note that
-%   % it may contain named nodes (whose names are stored in
-%   % \csname\yquant at prefix circuit at subcircuit@nodelist\endcsname) that need to be shifted
-%   % whenever we actually position the subcircuit.
-%%   \let\yquant at draw@callback at box=\yquant at circuit@subcircuit at shiftnodes%
-%   \yquant at register@get at allowmultitrue%
-%   \yquant at draw%
-%      {\copy\yquant at circuit@subcircuit at box}%
-%      {/yquant/operators/every subcircuit}
-%      {#1}{#2}{#3}%
-%   \global\setbox\yquant at circuit@subcircuit at box=\box\voidb at x% memory management
-%}
+\yquant at langhelper@declare at command{subcircuit}\yquant at lang@@subcircuit
+\yquant at langhelper@setup at attrs{subcircuit}{value}{}
+\protected\def\yquant at lang@@subcircuit#1#2#3{%
+   \yquant at register@get at multiaslist%
+   \yquant at circuit@operator{#1}{#2}{#3}%
+   \yquant at draw@@subcircuit{/yquant/operators/every subcircuit}%
+}
 % END_FOLD
 
 % BEGIN_FOLD other geometric shapes
@@ -291,8 +478,8 @@
 \protected\def\yquant at lang@@slash#1{%
    % temporarily squeeze most into the separation
    \pgfkeys{%
-      /yquant/operator/minimum width=0pt,
-      /yquant/operator/separation=.5\dimexpr\yquant at config@operator at sep-\pgflinewidth\relax
+      /yquant/operator/minimum width=0pt,%
+      /yquant/operator/separation=.5\dimexpr\yquant at config@operator at sep-\pgflinewidth\relax%
    }%
    \def\yquant at draw@callback at wire##1{%
       \yquant at register@set at x%
@@ -332,20 +519,7 @@
 \yquant at langhelper@setup at attrs{measure}{}{value,type}
 \def\yquant at lang@@measure{%
    \ifdefined\yquant at lang@attr at type%
-      \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{qubit}%
-         {\let\yquant at circuit@settype at to=\yquant at register@type at q}%
-         {%
-            \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{cbit}%
-            {\let\yquant at circuit@settype at to=\yquant at register@type at c}%
-            {%
-               \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{qubits}%
-               {\let\yquant at circuit@settype at to=\yquant at register@type at qs}%
-               {%
-                  \PackageError{yquant.sty}{Invalid bit type: `\yquant at lang@attr at type}%
-                     {Use one of `qubit', `cbit', or `qubits'.}%
-               }%
-            }%
-         }%
+      \yquant at register@type at fromstring\yquant at lang@attr at type\yquant at circuit@settype at to%
    \else%
       \let\yquant at circuit@settype at to=\yquant at register@type at c%
    \fi%
@@ -365,20 +539,7 @@
 \yquant at langhelper@setup at attrs{dmeter}{}{value,type}
 \def\yquant at lang@@dmeter{%
    \ifdefined\yquant at lang@attr at type%
-      \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{qubit}%
-         {\let\yquant at circuit@settype at to=\yquant at register@type at q}%
-         {%
-            \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{cbit}%
-            {\let\yquant at circuit@settype at to=\yquant at register@type at c}%
-            {%
-               \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{qubits}%
-               {\let\yquant at circuit@settype at to=\yquant at register@type at qs}%
-               {%
-                  \PackageError{yquant.sty}{Invalid bit type: `\yquant at lang@attr at type}%
-                     {Use one of `qubit', `cbit', or `qubits'.}%
-               }%
-            }%
-         }%
+      \yquant at register@type at fromstring\yquant at lang@attr at type\yquant at circuit@settype at to%
    \else%
       \let\yquant at circuit@settype at to=\yquant at register@type at c%
    \fi%
@@ -402,7 +563,7 @@
    \yquant at draw%
       {}%
       {/yquant/operators/every barrier}%
-      {}{}
+      {}{}%
 }
 
 \yquant at langhelper@declare at command@uncontrolled{correlate}\yquant at lang@@correlate
@@ -453,25 +614,12 @@
       \fi%
    }%
 }
-\long\def\@fifthoffive#1#2#3#4#5{#5}
+
 \protected\def\yquant at lang@@init#1{%
    \yquant at register@get at allowmultitrue%
    \yquant at register@get at ids{#1}%
    \ifdefined\yquant at lang@attr at type%
-      \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{qubit}%
-         {\let\yquant at circuit@settype at to=\yquant at register@type at q}%
-         {%
-            \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{cbit}%
-            {\let\yquant at circuit@settype at to=\yquant at register@type at c}%
-            {%
-               \expandafter\ifstrequal\expandafter{\yquant at lang@attr at type}{qubits}%
-               {\let\yquant at circuit@settype at to=\yquant at register@type at qs}%
-               {%
-                  \PackageError{yquant.sty}{Invalid bit type: `\yquant at lang@attr at type}%
-                     {Use one of `qubit', `cbit', or `qubits'.}%
-               }%
-            }%
-         }%
+      \yquant at register@type at fromstring\yquant at lang@attr at type\yquant at circuit@settype at to%
    \else%
       % We don't know which type is desired. Scan all target registers and use the first wire that is available as a type.
       \let\yquant at circuit@settype at to=\yquant at register@type at none%
@@ -492,15 +640,9 @@
          % now we don't have a clue; assume it's a qubit
          \let\yquant at circuit@settype at to=\yquant at register@type at q%
       \fi%
-      \ifx\yquant at circuit@settype at to\yquant at register@type at q%
-         \def\yquant at lang@attr at type{qubit}%
-      \else%
-         \ifx\yquant at circuit@settype at to\yquant at register@type at c%
-            \def\yquant at lang@attr at type{cbit}%
-         \else%
-            \def\yquant at lang@attr at type{qubits}%
-         \fi%
-      \fi%
+      \edef\yquant at lang@attr at type{%
+         \yquant at register@type at tostring\yquant at circuit@settype at to%
+      }%
    \fi%
    \let\yquant at draw@callback at wire=\yquant at circuit@settype%
    \let\yquant at draw@@multi=\yquant at draw@@multiinit%
@@ -532,20 +674,7 @@
 \yquant at langhelper@setup at attrs{settype}{value}{}
 \protected\def\yquant at lang@@settype#1{%
    \yquant at register@get at ids{#1}%
-   \expandafter\ifstrequal\expandafter{\yquant at lang@attr at value}{qubit}%
-      {\let\yquant at circuit@settype at to=\yquant at register@type at q}%
-      {%
-         \expandafter\ifstrequal\expandafter{\yquant at lang@attr at value}{cbit}%
-         {\let\yquant at circuit@settype at to=\yquant at register@type at c}%
-         {%
-            \expandafter\ifstrequal\expandafter{\yquant at lang@attr at value}{qubits}%
-            {\let\yquant at circuit@settype at to=\yquant at register@type at qs}%
-            {%
-               \PackageError{yquant.sty}{Invalid bit type: `\yquant at lang@attr at value}%
-                  {Use one of `qubit', `cbit', or `qubits'.}%
-            }%
-         }%
-      }%
+   \yquant at register@type at fromstring\yquant at lang@attr at value\yquant at circuit@settype at to%
    \yquant at circuit@actonwires%
       \yquant at circuit@settype%
       \yquant at register@get at ids@list%

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-langhelper.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-langhelper.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-langhelper.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -45,7 +45,7 @@
             }%
          \fi%
       \endpgfkeysinterruptkeyfilter%
-   },
+   },%
    /pgf/key filters/attr filter/.code={%
       % we need to capture every non-attribute (which means filtering them out).
       \expandafter\ifyquant at beginswith\expandafter{\pgfkeyscurrentkey}{/yquant/attrs/}{%
@@ -84,9 +84,9 @@
             \pgfkeysfiltercontinuefalse%
          }%
       }%
-   },
-   /yquant/attr filter/.style={
-      /pgf/key filters/attr filter/.install key filter,
+   },%
+   /yquant/attr filter/.style={%
+      /pgf/key filters/attr filter/.install key filter,%
       /yquant/attr filter handler/.install key filter handler=\yquant at attrs@remaining%
    }%
 }
@@ -112,7 +112,7 @@
                   \pgfkeysinterruptkeyfilter%
                      \pgfkeys{/yquant/all attrs/##1={####1}}%
                   \endpgfkeysinterruptkeyfilter%
-               }
+               }%
             }%
          }%
          \eappto\keysset{%

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -3,6 +3,38 @@
 \def\yquant at register@type at c{2}
 \def\yquant at register@type at qs{3}
 
+\def\yquant at register@type at tostring#1{%
+   \ifcase#1 %
+      nobit%
+   \or%
+      qubit%
+   \or%
+      cbit%
+   \or%
+      qubits%
+   \else%
+      \PackageError{yquant.sty}{Internal inconsistency detected}%
+                   {Unknown wire type: `#1'.}%
+   \fi%
+}
+
+\protected\def\yquant at register@type at fromstring#1#2{%
+   \expandafter\ifstrequal\expandafter{#1}{qubit}%
+      {\let#2=\yquant at register@type at q}%
+      {%
+         \expandafter\ifstrequal\expandafter{#1}{cbit}%
+         {\let#2=\yquant at register@type at c}%
+         {%
+            \expandafter\ifstrequal\expandafter{#1}{qubits}%
+            {\let#2=\yquant at register@type at qs}%
+            {%
+               \PackageError{yquant.sty}{Invalid bit type: `#1'}%
+                  {Use one of `qubit', `cbit', or `qubits'.}%
+            }%
+         }%
+      }%
+}
+
 % Internally, we store the register information in a single macro. Indeed, this is almost always faster than using multiple macros (at least in the way implemented here), apart from changing a single type of information, which is almost as fast.
 % BEGIN_FOLD Constructor: create single register and cleanup environment
 
@@ -16,18 +48,56 @@
    \csxdef{\yquant at prefix register@\csname\yquant at prefix registers\endcsname}{%
       {#1}% type
       {#2}% x pos
-      {\yquant at config@register at minimum@height}% height; at the end, the y position
-      {{0pt}{0pt}{}{}}% wire start positions and clipping
+      {{\the\dimexpr.5\dimexpr\yquant at config@register at minimum@height\relax\relax}%
+       {\the\dimexpr.5\dimexpr\yquant at config@register at minimum@height\relax\relax}}% height and depth; at the end, the y position
+      {{\yquant at env@create at x}{\yquant at env@create at x}{}{}}% wire start positions and clipping
       {}% wire style
    }%
    \global\csletcs{\yquant at prefix registername@#3[#4]}{\yquant at prefix registers}%
-   \csxdef{\yquant at prefix registerhigh@\reg}{#4}%
+   \csxdef{\yquant at prefix registerhigh@#3}{#4}%
    \yquant at cleanup@csadd{\yquant at prefix register@\csname\yquant at prefix registers\endcsname}%
    \yquant at cleanup@csadd{\yquant at prefix registername@#3[#4]}%
    \ifnum0=#4\relax%
-      \yquant at cleanup@csadd{\yquant at prefix registerhigh@\reg}%
+      \yquant at cleanup@csadd{\yquant at prefix registerhigh@#3}%
    \fi%
 }
+
+% in a subcircuit, create a new register that is only an alias for an existing register in the outer circuit.
+% #5: id in the outer circuit
+\protected\def\yquant at register@alias#1#2#3#4#5{%
+   \csnumgdef{\yquant at prefix registers}%
+             {\csname\yquant at prefix registers\endcsname+1}%
+   \ifcsname\yquant at parent registermap@#5\endcsname%
+      % the parent is already an alias itself, pass this on transparently
+      \global\csletcs%
+         {\yquant at prefix registermap@\csname\yquant at prefix registers\endcsname}%
+         {\yquant at parent registermap@#5}%
+   \else%
+      \csxdef{\yquant at prefix registermap@\csname\yquant at prefix registers\endcsname}%
+             {\yquant at parent register@#5}%
+   \fi%
+   % even though this is an alias, we still need to keep track of its internal dimensions
+   \csgdef{\yquant at prefix registerdim@\csname\yquant at prefix registers\endcsname}%
+          {{0pt}{0pt}}%
+   \yquant at cleanup@csadd{\yquant at prefix registerdim@\csname\yquant at prefix registers\endcsname}%
+   \global\csletcs{\yquant at prefix registername@#3[#4]}{\yquant at prefix registers}%
+   \csxdef{\yquant at prefix registerhigh@#3}{#4}%
+   \yquant at cleanup@csadd{\yquant at prefix registermap@\csname\yquant at prefix registers\endcsname}%
+   \yquant at cleanup@csadd{\yquant at prefix registername@#3[#4]}%
+   \ifnum0=#4\relax%
+      \yquant at cleanup@csadd{\yquant at prefix registerhigh@#3}%
+   \fi%
+   \csxdef{\yquant at prefix lastinput}{{\csname\yquant at prefix registers\endcsname}{#5}}%
+   \unless\ifcsname\yquant at prefix firstinput\endcsname%
+      \csxdef{\yquant at prefix firstinput}{{\csname\yquant at prefix registers\endcsname}{#5}}%
+      \yquant at cleanup@csadd{\yquant at prefix firstinput}%
+      \yquant at cleanup@csadd{\yquant at prefix lastinput}%
+   \fi%
+   \expandafter\yquant at register@set@@aux%
+      \csname\csname\yquant at prefix registermap@\csname\yquant at prefix registers\endcsname\endcsname\endcsname%
+      \yquant at register@set at x@aux%
+      {#2}%
+}
 % END_FOLD
 
 % BEGIN_FOLD Converter: convert a register name to its id
@@ -43,12 +113,10 @@
 
 % same as before, but resolves to the _index_ of the last register
 \protected\def\yquant at register@get at id@high#1#2{%
-   \ifcsname\yquant at prefix registerhigh@#2\endcsname%
-      \letcs#1{\yquant at prefix registerhigh@#2}%
-   \else%
+   \unless\ifcsname\yquant at prefix registerhigh@#2\endcsname%
       \yquant at register@get at id@lazycreate{#2}%
-      \letcs#1{\yquant at prefix registers}%
    \fi%
+   \letcs#1{\yquant at prefix registerhigh@#2}%
 }
 
 % In the lazy mode, we create all unknown registers on-the-fly.
@@ -55,7 +123,7 @@
 \def\yquant at register@get at id@lazycreate#1{%
    \ifyquant at env@lazy%
       % In principle, we could just call \yquant at lang@@qubit. However, if access to a vector register is desired and a _part_ of this does already exist (though not the requested index), we only create the missing registers.
-      \yquant at register@get at id@lazycreate at parse#1[;
+      \yquant at register@get at id@lazycreate at parse#1[;%
    \else%
       \PackageError{yquant.sty}{Register `#1' unknown}%
          {You referred to an unknown register.
@@ -96,13 +164,14 @@
       \yquant at for \idx := \idx to \numexpr \len -1\relax {%
          \yquant at register@define%
             \yquant at register@type at q%
-            {0pt}%
-            \reg\idx
+            {\yquant at env@create at x}%
+            \reg\idx%
          % Prepare to shipout
          \csxappto{\yquant at prefix draw}{%
             \yquant at lang@create at draw{\csname\yquant at prefix registers\endcsname}%
                                     {0pt}%
                                     {qubit}%
+                                    0%
                                     {}%
                                     {}%
          }%
@@ -137,7 +206,10 @@
             \catcode`(=\active%
             \def[##1]{\yquant at register@get at ids@@index{##1}}%
             \def(##1){\yquant at register@get at ids@@multi{##1}}%
-            \edef\yquant at register@get at ids@retokenized{\scantokens{#1\noexpand}}%
+            % this is also invoked by other languages which might have completely redefined all catcodes (qasm redefines the backslash), so don't just add \noexpand at the end of the \scantokens...
+            \everyeof{\noexpand}%
+            \endlinechar=-1 %
+            \edef\yquant at register@get at ids@retokenized{\scantokens{#1}}%
             \expandafter%
          \endgroup%
          \expandafter\docsvlist\expandafter{\yquant at register@get at ids@retokenized}%
@@ -239,7 +311,7 @@
          }%
          \count0=2147483647 %
          \csletcs{ifallowmain}{iffalse}%
-      }{
+      }{%
          \let\yquant at register@get at ids@norange at checkindex@setmain=\relax%
       }%
    \else%
@@ -346,7 +418,7 @@
          \expandafter\yquant at register@get at id\expandafter\idx\expandafter{\idx}%
          \count6=\idx%
          \csletcs{ifallowmain}{iffalse}%
-      }{
+      }{%
          \yquant at register@get at id\idx{\current[#1]}%
       }%
    \else%
@@ -575,37 +647,79 @@
    }%
 }
 
+\protected\def\yquant at register@multi at splitparts@sort{%
+   \yquant at sort@list\yquant at register@get at ids@list\yquant at sort@ascending%
+}
+
 \protected\def\yquant at register@get at multiassingle{%
    \yquant at register@get at allowmultitrue%
    \let\yquant at register@multi at splitparts=\yquant at register@multi at splitparts@sepall%
    \preto\yquant at attrs@remaining{/yquant/operator/multi as single,}%
 }
+
+\protected\def\yquant at register@get at multiaslist{%
+   \yquant at register@get at allowmultitrue%
+   \let\yquant at register@multi at splitparts=\yquant at register@multi at splitparts@sort%
+}
+
+\let\yquant at register@multi at reset@original at splitparts=\yquant at register@multi at splitparts%
+\protected\def\yquant at register@reset at multi{%
+%   \yquant at register@get at allowmultifalse% not needed, as \yquant at circuit@operator automatically resets it
+   \let\yquant at register@multi at splitparts=\yquant at register@multi at reset@original at splitparts%
+}
 % END_FOLD
 
 % BEGIN_FOLD Getters: extract the requested information from the register with given id
 \def\yquant at register@get at type#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant at register@get at type@aux\csname\yquant at prefix register@#1\endcsname%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at type@aux\csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at type@aux\csname\yquant at prefix register@#1\endcsname%
+   \fi%
 }
 
 \def\yquant at register@get at type@aux#1#2#3#4#5{#1}
 
 \def\yquant at register@get at x#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant at register@get at x@aux\csname\yquant at prefix register@#1\endcsname%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at x@aux\csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at x@aux\csname\yquant at prefix register@#1\endcsname%
+   \fi%
 }
 
 \def\yquant at register@get at x@aux#1#2#3#4#5{#2}
 
 \def\yquant at register@get at height#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant at register@get at height@aux\csname\yquant at prefix register@#1\endcsname%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \@firstoftwo\csname\yquant at prefix registerdim@#1\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at height@aux\csname\yquant at prefix register@#1\endcsname%
+   \fi%
 }
 
-\def\yquant at register@get at height@aux#1#2#3#4#5{#3}
+\def\yquant at register@get at height@aux#1#2#3#4#5{\@firstoftwo#3}
 
+\def\yquant at register@get at depth#1{%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \@secondoftwo\csname\yquant at prefix registerdim@#1\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at depth@aux\csname\yquant at prefix register@#1\endcsname%
+   \fi%
+}
+
+\def\yquant at register@get at depth@aux#1#2#3#4#5{\@secondoftwo#3}
+
 % The y parameter get macros exist in two forms: The protected one is used during the storage to the draw macro; it should never be executed. The env environment then maps them to their proper expandable forms.
-\protected\def\yquant at register@get@@protected{%
+\protected\def\yquant at register@get at y{%
    \PackageError%
       {yquant.sty}%
       {Execution of a get-y macro in an illegal context}%
@@ -612,14 +726,21 @@
       {get-y macros may not be called unless the yquant environment ends.}%
 }
 
-\let\yquant at register@get at y=\yquant at register@get@@protected
-\let\yquant at register@y=\yquant at register@get@@protected
 % This plays a similar role: It must never be expanded before the tikz command is invoked (because it does not exist).
-\let\nodepart=\yquant at register@get@@protected
+\protected\def\nodepart{}
 
-% getting the y position is the same as getting the height parameter, since we will reuse it at the end.
-\let\yquant at register@get at y@unprotected=\yquant at register@get at height
+\def\yquant at register@get at y@unprotected#1{%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at y@aux\csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at y@aux\csname\yquant at prefix register@#1\endcsname%
+   \fi%
+}
 
+\def\yquant at register@get at y@aux#1#2#3#4#5{#3}
+
 % y distance between two registers.
 \def\yquant at register@get at ydist#1#2{%
    \expandafter\yquant at abs\expandafter%
@@ -626,17 +747,23 @@
       {\the\dimexpr\yquant at register@get at y{#2}-\yquant at register@get at y{#1}\relax}%
 }
 
-% y position of the current register
-\def\yquant at register@y at unprotected{0pt}%
-
 \protected\def\yquant at register@get at y@@expandable{%
    \let\yquant at register@get at y=\yquant at register@get at y@unprotected%
-   \let\yquant at register@y=\yquant at register@y at unprotected%
 }
 
 \def\yquant at register@get at lastwire#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant at register@get at lastwire@aux\csname\yquant at prefix register@#1\endcsname%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at lastwire@aux\csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at lastwire@aux\csname\yquant at prefix register@#1\endcsname%
+   }%
 }
 
 \protected\def\yquant at register@get at lastwire@aux#1#2#3#4#5#6{%
@@ -644,15 +771,30 @@
 }
 
 \def\yquant at register@get at style#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant at register@get at style@aux\csname\yquant at prefix register@#1\endcsname%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at style@aux\csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at style@aux\csname\yquant at prefix register@#1\endcsname%
+   \fi%
 }
 
 \def\yquant at register@get at style@aux#1#2#3#4#5{#5}
 
 \def\yquant at register@get at typeywire#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant at register@get at typeywire@aux\csname\yquant at prefix register@#1\endcsname%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at typeywire@aux\csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\expandafter\expandafter%
+      \yquant at register@get at typeywire@aux\csname\yquant at prefix register@#1\endcsname%
+   }%
 }
 
 \protected\def\yquant at register@get at typeywire@aux#1#2#3#4#5#6#7#8{%
@@ -695,12 +837,23 @@
 
 % BEGIN_FOLD Setters: change register information
 \protected\long\def\yquant at register@set@@aux#1#2#3{%
-   \long\xdef#1{\expandafter#2#1{#3}}%
+   \xdef#1{\expandafter#2#1{#3}}%
 }
 
 \protected\def\yquant at register@set at type#1{%
-   \expandafter\yquant at register@set@@aux%
-   \csname\yquant at prefix register@#1\endcsname\yquant at register@set at type@aux%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant at register@set@@aux%
+         \csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant at register@set@@aux%
+      \csname\yquant at prefix register@#1\endcsname%
+   }%
+   \yquant at register@set at type@aux%
 }
 
 \long\def\yquant at register@set at type@aux#1#2#3#4#5#6{%
@@ -708,8 +861,19 @@
 }
 
 \protected\def\yquant at register@set at x#1{%
-   \expandafter\yquant at register@set@@aux%
-   \csname\yquant at prefix register@#1\endcsname\yquant at register@set at x@aux%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant at register@set@@aux%
+         \csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant at register@set@@aux%
+      \csname\yquant at prefix register@#1\endcsname%
+   }%
+   \yquant at register@set at x@aux%
 }
 
 \long\def\yquant at register@set at x@aux#1#2#3#4#5#6{%
@@ -717,25 +881,99 @@
 }
 
 \protected\def\yquant at register@update at height#1#2{%
-   \ifdim#2>\yquant at register@get at height{#1} %
-      \expandafter\yquant at register@set@@aux%
-      \csname\yquant at prefix register@#1\endcsname\yquant at register@set at height@aux{#2}%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \@firstoftwo\csname\yquant at prefix registerdim@#1\endcsname%
+         \relax%
+         \csxdef{\yquant at prefix registerdim@#1}{%
+            {#2}%
+            {\expandafter\expandafter\expandafter%
+                \@secondoftwo\csname\yquant at prefix registerdim@#1\endcsname}%
+         }%
+      \fi%
+   \else%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \yquant at register@get at height@aux\csname\yquant at prefix register@#1\endcsname%
+         \relax%
+         \expandafter\yquant at register@set@@aux%
+            \csname\yquant at prefix register@#1\endcsname%
+            \yquant at register@set at height@aux{#2}%
+      \fi%
    \fi%
 }
 
+\long\def\yquant at register@set at height@aux#1#2#3#4#5#6{%
+   \unexpanded{{#1}{#2}}%
+   {{#6}{\unexpanded\expandafter{\@secondoftwo#3}}}%
+   \unexpanded{{#4}{#5}}%
+}
+
+\protected\def\yquant at register@update at depth#1#2{%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \@secondoftwo\csname\yquant at prefix registerdim@#1\endcsname%
+         \relax%
+         \csxdef{\yquant at prefix registerdim@#1}{%
+            {\expandafter\expandafter\expandafter%
+                \@firstoftwo\csname\yquant at prefix registerdim@#1\endcsname}%
+            {#2}%
+         }%
+      \fi%
+   \else%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \yquant at register@get at depth@aux\csname\yquant at prefix register@#1\endcsname%
+         \relax%
+         \expandafter\yquant at register@set@@aux%
+            \csname\yquant at prefix register@#1\endcsname%
+            \yquant at register@set at depth@aux{#2}%
+      \fi%
+   \fi%
+}
+
+\long\def\yquant at register@set at depth@aux#1#2#3#4#5#6{%
+   \unexpanded{{#1}{#2}}%
+   {{\unexpanded\expandafter{\@firstoftwo#3}}{#6}}%
+   \unexpanded{{#4}{#5}}%
+}
+
 \protected\def\yquant at register@set at y#1{%
-   \expandafter\yquant at register@set@@aux%
-   \csname\yquant at prefix register@#1\endcsname\yquant at register@set at height@aux%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant at register@set@@aux%
+         \csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant at register@set@@aux%
+      \csname\yquant at prefix register@#1\endcsname%
+   }%
+   \yquant at register@set at y@aux%
 }
 
-% Set the currently used register
-\long\def\yquant at register@set at height@aux#1#2#3#4#5#6{%
+\long\def\yquant at register@set at y@aux#1#2#3#4#5#6{%
    \unexpanded{{#1}{#2}}{#6}\unexpanded{{#4}{#5}}%
 }
 
 \protected\def\yquant at register@set at lastwire#1{%
-   \expandafter\yquant at register@set@@aux%
-   \csname\yquant at prefix register@#1\endcsname\yquant at register@set at lastwire@aux%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant at register@set@@aux%
+         \csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant at register@set@@aux%
+      \csname\yquant at prefix register@#1\endcsname%
+   }%
+   \yquant at register@set at lastwire@aux%
 }
 
 \long\def\yquant at register@set at lastwire@aux#1#2#3#4#5#6{%
@@ -743,8 +981,19 @@
 }
 
 \protected\def\yquant at register@set at style#1{%
-   \expandafter\yquant at register@set@@aux%
-   \csname\yquant at prefix register@#1\endcsname\yquant at register@set at style@aux%
+   \ifcsname\yquant at prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant at register@set@@aux%
+         \csname\csname\yquant at prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant at register@set@@aux%
+      \csname\yquant at prefix register@#1\endcsname%
+   }%
+   \yquant at register@set at style@aux%
 }
 
 \long\def\yquant at register@set at style@aux#1#2#3#4#5#6{%

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-shapes.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-shapes.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-shapes.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -21,7 +21,7 @@
    {\PackageError{yquant.sty}%
                  {Failed to patch \string\pgfdeclareshape}%
                  {yquant could not provide a necessary extension to pgf.}}%
-% Every shape additionally provides information about how it should clip the wires. The clipping softpath instructions of shape #1 are stored into \pgfshapeclippathresult. The path is drawn with the tikz options #2 in place.
+% Every shape additionally provides information about how it should clip the wires. The clipping softpath instructions of shape #1 are stored into \pgfshapeclippath[horz|vert]result. The path is drawn with the tikz options #2 in place.
 \protected\def\pgfshapeclippath#1#2{%
    % Ok, check whether #1 is known!
    \ifcsname pgf at sh@ns@#1\endcsname%
@@ -56,7 +56,8 @@
       \end{pgfinterruptpath}%
    \else%
       \pgferror{No shape named #1 is known}%
-      \pgfpointorigin%
+      \global\let\pgfshapeclippathhorzresult=\empty%
+      \global\let\pgfshapeclippathvertresult=\empty%
    \fi%
 }
 
@@ -81,7 +82,7 @@
          {\southwest%
           \advance\pgf at x by \@tempdima%
           \advance\pgf at y by \@tempdimb%
-         }
+         }%
          {\northeast%
           \advance\pgf at x by -\@tempdima%
           \advance\pgf at y by -\@tempdimb%
@@ -441,7 +442,7 @@
       % we only draw the meter symbol if this is the main part of a multi-register (or there is no text)
       \ifyquant at config@operator at multi@main%
          % Make sure the meter does not extend beyond the box (we are in a scope here)
-         \path [clip]
+         \path [clip]%
             (-\xradius, \yradius) rectangle (\xradius, -\yradius);%
          % The position of the meter symbol depends on the presence of the text. If there is no text, we just vertically center. If there is some text, we shift the symbol upwards from the text until there is no overlap any more.
          \csname pgf at anchor@yquant-measure at text\endcsname%
@@ -451,10 +452,10 @@
          \else%
             \@tempdima=\dimexpr\pgf at y+2pt\relax%
          \fi%
-         \path [/yquant/operators/every measure meter]
+         \path [/yquant/operators/every measure meter]%
             (-2.25mm, \@tempdima) arc[start angle=160, end angle=20,%
-                                      x radius=2.25mm, y radius=1.4mm]
-            (0, \@tempdima) -- ++(1.6mm, 2.3mm);
+                                      x radius=2.25mm, y radius=1.4mm]%
+            (0, \@tempdima) -- ++(1.6mm, 2.3mm);%
       \fi%
    }%
 }
@@ -553,7 +554,7 @@
       \shorten%
       \@tempdima=\dimexpr\yradius+\pgf at x\relax%
       \@tempdimb=\dimexpr\yradius+\pgf at y\relax%
-      \pgfpathrectanglecorners
+      \pgfpathrectanglecorners%
          {\pgfqpoint{-.5\pgflinewidth}{\@tempdima}}%
          {\pgfqpoint{.5\pgflinewidth}{-\@tempdimb}}%
    }%

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant-tools.tex
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant-tools.tex	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant-tools.tex	2020-06-13 20:58:17 UTC (rev 55537)
@@ -21,11 +21,27 @@
    \let\@eattoken= %
 }
 
+% a bit faster than nested \@firstoftwo/\@secondoftwo
+% note \@thirdofthree is defined in the latex kernel already.
+\long\def\@firstofthree#1#2#3{#1}%
+\long\def\@secondofthree#1#2#3{#2}%
+\long\def\@firstoffour#1#2#3#4{#1}%
+\long\def\@secondoffour#1#2#3#4{#2}%
+\long\def\@thirdoffour#1#2#3#4{#3}%
+\long\def\@fourthoffour#1#2#3#4{#4}%
+\long\def\@thirdandfourthoffour#1#2#3#4{#3#4}%
+\long\def\@fifthoffive#1#2#3#4#5{#5}
+
 % Loop #1 from min(#2, #3) to max(#2, #3), executing #4
 \protected\def\yquant at for #1:=#2to#3#{%
    \yquant at for@aux#1{#2}{#3}%
 }
 
+% Loop #1 from max(#2, #3) down to min(#2, #3), executing #4
+\protected\def\yquant at fordown #1:=#2downto#3#{%
+   \yquant at fordown@aux#1{#2}{#3}%
+}
+
 \long\def\yquant at for@aux#1#2#3#4{%
    \ifnum#2<#3\relax%
       \numdef#1{#2}%
@@ -37,6 +53,17 @@
    \fi%
 }
 
+\long\def\yquant at fordown@aux#1#2#3#4{%
+   \ifnum#2>#3\relax%
+      \numdef#1{#2}%
+      % to allow for things like \yquant at for \i := \i to ..., expand the boundaries
+      \expandafter\yquant at fordown@loop\expandafter#1\expandafter{\the\numexpr#3-1\relax}{#4}%
+   \else%
+      \numdef#1{#3}%
+      \expandafter\yquant at fordown@loop\expandafter#1\expandafter{\the\numexpr#2-1\relax}{#4}%
+   \fi%
+}
+
 \long\def\yquant at for@loop#1#2#3{%
    \loop%
       \ifnum#1<#2\relax%
@@ -45,6 +72,14 @@
    \repeat%
 }
 
+\long\def\yquant at fordown@loop#1#2#3{%
+   \loop%
+      \ifnum#1>#2\relax%
+         #3%
+         \numdef#1{#1-1}%
+   \repeat%
+}
+
 \def\yquant at for@break{%
    \fi%
    \iffalse%
@@ -219,7 +254,7 @@
                \expandafter\iffalse at hidden%
             \fi%
          \repeat%
-         \expandafter
+         \expandafter%
       }%
       \expandafter\count\expandafter2\expandafter=\the\count2\relax%
       \ifnum\count0<\count2 %
@@ -235,15 +270,21 @@
    \cslet{yquant at sort@item#2}\tmp%
 }
 
+% Add an internal etoolbox list to the sorted items
+\def\yquant at sort@addlist#1{%
+   \forlistloop\yquant at sort@addlist at aux#1%
+}
+
+\protected\def\yquant at sort@addlist at aux#1{%
+   \csdef{yquant at sort@item\the\yquant at sort@count}{#1}%
+   \advance\yquant at sort@count by 1 %
+}
+
 % Sorts an internal etoolbox list #1 using macro #2
 \protected\def\yquant at sort@list#1#2{%
    \begingroup%
       \yquant at sort@count=0 %
-      \def\do##1{%
-         \csdef{yquant at sort@item\the\yquant at sort@count}{##1}%
-         \advance\yquant at sort@count by 1 %
-      }%
-      \dolistloop#1%
+      \yquant at sort@addlist#1%
       \yquant at sort#2%
       \let#1=\empty%
       \count0=0 %
@@ -268,7 +309,278 @@
          \expandafter\expandafter\expandafter\do%
          \expandafter\expandafter\expandafter{%
             \csname yquant at sort@item\the\count0\endcsname%
-         }
+         }%
          \advance\count0 by 1 %
    \repeat%
+}
+
+\begingroup
+\catcode`\|=3
+\gdef\yquant at list@delim{|}
+
+\protected\gdef\yquant at list@dequeue#1#2{%
+   \expandafter\ifblank\expandafter{#1}{%
+      \let#2=\empty%
+   }{%
+      \expandafter\yquant at list@dequeue at i#1\etb at lst@q at end{#1}{#2}\def%
+   }%
+}%
+
+\protected\gdef\yquant at list@dequeue at i#1|#2\etb at lst@q at end#3#4#5{%
+   \def#4{#1}%
+   #5#3{#2}%
+}
+
+\protected\gdef\yquant at list@gdequeue#1#2{%
+   \expandafter\ifblank\expandafter{#1}{%
+      \let#2=\empty%
+   }{%
+      \expandafter\yquant at list@dequeue at i#1\etb at lst@q at end{#1}{#2}\gdef%
+   }%
+}
+\endgroup
+
+\def\ifyquant at OR#1#2{%
+   #1%
+      \expandafter\@firstoftwo%
+   \else%
+      #2%
+         \expandafter\expandafter\expandafter\@firstoftwo%
+      \else%
+         \expandafter\expandafter\expandafter\@secondoftwo%
+      \fi%
+   \fi%
+}
+
+% #1 is a pgf soft path. We extract the maximum x position at the y position specified in #2 and assign it to \dimen0.
+\protected\def\yquant at softpath@extractmaxxat#1#2{%
+   \begingroup%
+      \dimen0=-16000pt %
+      \dimen2=#2 %
+      \let\pgfsyssoftpath at movetotoken=\yquant at softpath@extractmaxxat at moveto%
+      \let\pgfsyssoftpath at linetotoken=\yquant at softpath@extractmaxxat at lineto%
+      \let\pgfsyssoftpath at curvetosupportatoken=\yquant at softpath@extractmaxxat at curveto%
+      \let\pgfsyssoftpath at rectcornertoken=\yquant at softpath@extractmaxxat at rectto%
+      \let\pgfsyssoftpath at closepath=\@gobbletwo%
+      % the specialroundtoken (undocumented) is \@gobbletwo by default.
+      #1%
+      \expandafter%
+   \endgroup%
+   \expandafter\dimen\expandafter0\expandafter=\the\dimen0  %
+}
+
+\protected\def\yquant at softpath@extractmaxxat at update#1{%
+   \ifdim\dimen0<#1 %
+      \dimen0=#1 %
+   \fi%
+}
+
+\protected\def\yquant at softpath@extractmaxxat at moveto#1#2{%
+   \dimen4=#1 %
+   \dimen6=#2 %
+}
+
+\protected\def\yquant at softpath@extractmaxxat at lineto#1#2{%
+   \ifyquant at OR{\ifdim\dimen4>\dimen0 }{\ifdim#1>\dimen0 }{%
+      \ifdim\dimen6=\dimen2 %
+         \yquant at softpath@extractmaxxat at update{\dimen4}%
+      \else%
+         \ifdim\dimen6<\dimen2 %
+            \unless\ifdim#2<\dimen2 %
+               \expandafter\yquant at softpath@extractmaxxat at update\expandafter{\the\dimexpr%
+                  \dimen4+% x0
+                  \dimexpr#1-\dimen4\relax*% (x1-x0)
+                  \dimexpr\dimen2-\dimen6\relax/\dimexpr#2-\dimen6\relax% (y-y0)/(y1-y0)
+               \relax}%
+            \fi%
+         \else%
+            \unless\ifdim#2>\dimen2 %
+               \expandafter\yquant at softpath@extractmaxxat at update\expandafter{\the\dimexpr%
+                  \dimen4+% x0
+                  \dimexpr#1-\dimen4\relax*% (x1-x0)
+                  \dimexpr\dimen2-\dimen6\relax/\dimexpr#2-\dimen6\relax% (y-y0)/(y1-y0)
+               \relax}%
+            \fi%
+         \fi%
+      \fi%
+   }\relax%
+   \dimen4=#1 %
+   \dimen6=#2 %
+}
+
+\protected\def\yquant at softpath@extractmaxxat at curveto@checkx#1#2#3{%
+   % \dimen11 holds our only candidate for t. Is it within the curve?
+   \unless\ifdim\dimen11<0pt %
+      \unless\ifdim\dimen11>1pt %
+         % it is. \dimen4: x0, #1: xa, #2: xb, #3: x1
+         \begingroup%
+            \dimen12=\dimexpr1pt-\dimen11\relax% 1 - t
+            \dimen13=\dimexpr\dimen11*\dimen11/65535\relax% t^2
+            \dimen14=\dimexpr\dimen12*\dimen12/65535\relax% (1 - t)^2
+            \dimen255=\dimexpr\dimen13*\dimen11/65535*\dimexpr#3\relax/65535+% t^3 x1
+                              3\dimen13*\dimen12/65535*\dimexpr#2\relax/65535+% t^2(1 - t) xb
+                              \dimen14*\dimen12/65535*\dimen4/65535+% (1 - t)^3 x0
+                              3\dimen11*\dimen14/65535*\dimexpr#1\relax/65535% 3t(1 - t)^2 xa
+                      \relax%
+            \expandafter%
+         \endgroup%
+         \expandafter\yquant at softpath@extractmaxxat at update\expandafter{\the\dimen255}%
+      \fi%
+   \fi%
+}
+
+\protected\def\yquant at softpath@extractmaxxat at curveto#1#2\pgfsyssoftpath at curvetosupportbtoken#3#4\pgfsyssoftpath at curvetotoken#5#6{%
+   % There's really no good way to do this apart from solving the Bézier curve (a third-order polynomial). Let's do it. (Yes, this is inefficient, but if someone substitutes the rectangular box of a subcircuit by a more fancy design, this is not our fault).
+   % Parametrized by t, the x coordinates of the curve are
+   % x0 + 3 (xa - x0) t + 3 (x0 - 2xa + xb) t^2 + (3xa - 3xb + x1 - x0) t^3
+   % where x0 = \dimen4 (the moveto point), xa = #1, xb = #3, x1 = #5.
+   % Likewise for y:
+   %       y0 = \dimen6 (the moveto point), ya = #2, yb = #4, y1 = #6.
+   % We first solve the third-order polynomial for t using the y value, then plug it back into the x value.
+   % TODO: this is accurate to approx. 3 digits. Can this be improved by reformulating Cardanos formula to involve less divisions?
+   \begingroup%
+      % We need so may dimensions that we break with TeX's convention for their use.
+      % for the multiplications with and divisions by dimensions, we exploit that eTeX fuses muldiv to 64 bits. Further note that each dimension has a scaling factor of 65535 for sp<->pt conversion. This is why don't factor out divisions (which would be more efficient, but not give the benefit of 64bit accuracy).
+      % a = 3(ya - yb) + (y1 - y0)
+      \dimen1=\dimexpr3\dimexpr#2-#4\relax+#6-\dimen6\relax%
+      \ifdim\dimen1=0pt %
+         % this is only a quadratic curve!
+         % b = 3(y0 - 2ya + yb)
+         \dimen3=\dimexpr3\dimexpr\dimen6-2\dimexpr#2\relax+#4\relax*65535\relax%
+         % c: 3(ya - y0)
+         \dimen5=\dimexpr3\dimexpr#2-\dimen6\relax*65535\relax%
+         % d: y0 - <desired y>
+         \dimen7=\dimexpr\dimexpr\dimen6-\dimen2\relax*65535\relax%
+         % check the discriminant of the equation
+         \dimen8=\dimexpr\dimen3*\dimen3/65535-4\dimen3*\dimen7/65535\relax%
+         \unless\ifdim\dimen8<0pt%
+            % there are two potential candidates, (-c +- sqrt(c^2 - 4b d))/2b
+            \pgfmathsqrt@{\the\dimen8\@gobbletwo}%
+            \dimen11=\dimexpr\dimexpr-\dimen5+\pgfmathresult pt\relax*65535/%
+                             \dimexpr2\dimen3\relax\relax%
+            \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+            \dimen11=\dimexpr\dimexpr-\dimen5-\pgfmathresult pt\relax*65535/%
+                             \dimexpr2\dimen3\relax\relax%
+            \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+         \fi%
+      \else%
+         % We will simplify by directly dividing all coefficients by a
+         % b = 3(y0 - 2ya + yb)
+         \dimen3=\dimexpr3\dimexpr\dimen6-2\dimexpr#2\relax+#4\relax*65535/\dimen1\relax%
+         % c: 3(ya - y0)
+         \dimen5=\dimexpr3\dimexpr#2-\dimen6\relax*65535/\dimen1\relax%
+         % d: y0 - <desired y>
+         \dimen7=\dimexpr\dimexpr\dimen6-\dimen2\relax*65535/\dimen1\relax%
+         % Note that now our a value (\dimen1) is no longer needed, it is one.
+         % check the discriminant of the equation
+         % Q = (3c - b^2)/9
+         \dimen8=\dimexpr\dimexpr3\dimen5-\dimen3*\dimen3/65535\relax/9\relax%
+         % R = (9bc - 27d - 2b^3)/54 = bc/6 - d/2 - b^3/27
+         \dimen9=\dimexpr\dimen3*\dimen5/393210-% 6*65535
+                          .5\dimen7-%
+                          \dimen3*\dimen3/65535*\dimen3/1769445% 27*65535
+                  \relax%
+         % D = Q^3 + R^2
+         \dimen10=\dimexpr\dimen8*\dimen8/65535*\dimen8/65535+\dimen9*\dimen9/65535\relax%
+         \ifdim\dimen10>0pt %
+            % only one real root: y_1 = S + T - b/3a
+            % S = cbrt(R + sqrt(Q^3 + R^2))
+            % T = cbrt(R - sqrt(Q^3 + R^2))
+            \pgfmathsqrt@{\the\dimen10\@gobbletwo}%
+            \dimen12=\dimexpr\dimen9+\pgfmathresult pt\relax%
+            \dimen13=\dimexpr\dimen9-\pgfmathresult pt\relax%
+            \ifdim\dimen12>0pt %
+               \pgfmathpow@{\the\dimen12\@gobbletwo}{.3333333333}%
+               \dimen11=\pgfmathresult pt %
+            \else%
+               \pgfmathpow@{\the\dimexpr-\dimen12\relax\@gobbletwo}{.3333333333}%
+               \dimen11=-\pgfmathresult pt %
+            \fi%
+            \ifdim\dimen13>0pt %
+               \pgfmathpow@{\the\dimen13\@gobbletwo}{.3333333333}%
+               \dimen11=\dimexpr\dimen11+\pgfmathresult pt-.33333333333\dimen3\relax%
+            \else%
+               \pgfmathpow@{\the\dimexpr-\dimen13\relax\@gobbletwo}{.3333333333}%
+               \dimen11=\dimexpr\dimen11-\pgfmathresult pt-.33333333333\dimen3\relax%
+            \fi%
+            \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+         \else%
+            \ifdim\dimen10=0pt %
+               % easiest case, three real roots, two of which are equal:
+               % y_1 = 2cbrt(R) - b/3a
+               % y_2, x_3 = -cbrt(R) - b/3a
+               \ifdim\dimen9>0pt %
+                  \pgfmathpow@{\the\dimen9\@gobbletwo}{.3333333333}%
+                  \dimen15=\pgfmathresult pt %
+               \else%
+                  \pgfmathpow@{\the\dimexpr-\dimen9\relax\@gobbletwo}{.3333333333}%
+                  \dimen15=-\pgfmathresult pt %
+               \fi%
+               \dimen11=\dimexpr2\dimen15-.33333333333\dimen3\relax%
+               \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+               % check the next candidate
+               \dimen11=\dimexpr-\dimen15-.33333333333\dimen3\relax%
+               \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+            \else%
+               % nastiest case, three distinct real roots which we can find only by taking a complex-valued cube root.
+               % p + i q = cbrt(R + i sqrt(|D|))
+               \pgfmathsqrt@{\the\dimexpr-\dimen10\relax\@gobbletwo}%
+               \dimen10=\pgfmathresult pt %
+               % Let us first find the absolute value
+               \dimen12=\dimexpr\dimen9*\dimen9/65535+\dimen10*\dimen10/65535\relax%
+               \pgfmathpow@{\the\dimen12\@gobbletwo}{.1666666667}%
+               \dimen12=\pgfmathresult pt%
+               % then we need 1/3 the argument of R + i sqrt(|D|).
+               \pgfmathatantwo@{\the\dimen10\@gobbletwo}{\the\dimen9\@gobbletwo}%
+               \dimen13=.3333333333\dimexpr\pgfmathresult pt\relax%
+               % and then the real and imaginary parts as cosine and sine.
+               \pgfmathcos@{\the\dimen13\@gobbletwo}%
+               \dimen14=\dimexpr\pgfmathresult\dimen12\relax%
+               \pgfmathsin@{\the\dimen13\@gobbletwo}%
+               \dimen15=\dimexpr\pgfmathresult\dimen12\relax%
+               % Now the candidates are
+               % y_1 = 2p - b/3a
+               % y_2 = -p - sqrt(3)q - b/3a
+               % y_3 = -p + sqrt(3)q - b/3a
+               \dimen11=\dimexpr2\dimen14-.33333333333\dimen3\relax%
+               \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+               \dimen11=\dimexpr-\dimen14-1.732050808\dimen15-.33333333333\dimen3\relax%
+               \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+               \dimen11=\dimexpr-\dimen14+1.732050808\dimen15-.33333333333\dimen3\relax%
+               \yquant at softpath@extractmaxxat at curveto@checkx{#1}{#3}{#5}%
+            \fi%
+         \fi%
+      \fi%
+      % Now after all these calculations, \dimen0 was updated within the group. Make available outside.
+      \expandafter%
+   \endgroup%
+   \expandafter\dimen\expandafter0\expandafter=\the\dimen0  %
+   \dimen4=#5 %
+   \dimen6=#6 %
+}
+
+\protected\def\yquant at softpath@extractmaxxat at rectto#1#2\pgfsyssoftpath at rectsizetoken#3#4{%
+   % #1: lower left x, #2: lower left y, #3: width, #4: height
+   % note that neither width nor height need be positive!
+   \ifdim#4>0pt %
+      \unless\ifdim#2>\dimen2 %
+         \unless\ifdim\dimexpr#2+#4\relax<\dimen2 %
+            \ifdim#3>0pt %
+               \yquant at softpath@extractmaxxat at update{\dimexpr#1+#3\relax}%
+            \else%
+               \yquant at softpath@extractmaxxat at update{#1}%
+            \fi%
+         \fi%
+      \fi%
+   \else%
+      \unless\ifdim#2<\dimen2 %
+         \unless\ifdim\dimexpr#2+#4\relax>\dimen2 %
+            \ifdim#3>0pt %
+               \yquant at softpath@extractmaxxat at update{\dimexpr#1+#3\relax}%
+            \else%
+               \yquant at softpath@extractmaxxat at update{#1}%
+            \fi%
+         \fi%
+      \fi%
+   \fi%
 }
\ No newline at end of file

Modified: trunk/Master/texmf-dist/tex/latex/yquant/yquant.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquant.sty	2020-06-13 20:57:54 UTC (rev 55536)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquant.sty	2020-06-13 20:58:17 UTC (rev 55537)
@@ -11,11 +11,11 @@
 % and version 1.3c or later is part of all distributions of LaTeX version 2005/12/01 or
 % later.
 %
-% This work has the LPPL maintenance status `maintained'.
+% This work has the LPPL maintenance status `author-maintained'.
 %
 % The Current Maintainer of this work is Benjamin Desef.
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{yquant}[2020/04/11 v0.1.2 Yet another quantum circuit library for LaTeX]
+\ProvidesPackage{yquant}[2020/06/13 v0.3 Yet another quantum circuit library for LaTeX]
 
 \RequirePackage{etoolbox}[2018/02/11]
 \RequirePackage{tikz}[2015/08/29]
@@ -22,6 +22,7 @@
 \RequirePackage{trimspaces}[2009/09/17]
 \usetikzlibrary{arrows.meta,decorations.pathreplacing,decorations.pathmorphing}
 
+\newif\ifyquantdebug
 \input yquant-config.tex
 \input yquant-tools.tex
 \input yquant-registers.tex
@@ -35,6 +36,10 @@
 % usually, \\ is robust; but when using \centering, it is \let to \@centercr, which is not robust (probably because this happens only when not protecting). Fix this, so that \\ does not require \protect if a centered circuit is drawn.
 \robustify\@centercr
 
+\outer\def\useyquantlanguage#1{%
+   \RequirePackage{yquantlanguage-#1}
+}
+
 \endinput
 %%
 %% End of file `yquant.sty'.
\ No newline at end of file

Added: trunk/Master/texmf-dist/tex/latex/yquant/yquantlanguage-qasm.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/yquant/yquantlanguage-qasm.sty	                        (rev 0)
+++ trunk/Master/texmf-dist/tex/latex/yquant/yquantlanguage-qasm.sty	2020-06-13 20:58:17 UTC (rev 55537)
@@ -0,0 +1,681 @@
+% yquantlanguage-qasm.sty
+%  Extend yquant to understand qasm syntax.
+%
+% Copyright 2020 Benjamin Desef
+%
+% This work may be distributed and/or modified under the conditions of the LaTeX Project
+% Public License, either version 1.3c of this license or (at your option) any later
+% version.
+% The latest version of this license is in
+%   http://www.latex-project.org/lppl.txt
+% and version 1.3c or later is part of all distributions of LaTeX version 2005/12/01 or
+% later.
+%
+% This work has the LPPL maintenance status `author-maintained'.
+%
+% The Current Maintainer of this work is Benjamin Desef.
+\NeedsTeXFormat{LaTeX2e}
+\ProvidesPackage{yquantlanguage-qasm}[2020/06/13 v0.3 yquant-qasm]
+
+\RequirePackage{yquant}[2020/06/13]
+
+% BEGIN_FOLD Environment definitions
+\begingroup
+   \catcode`\{=12 \global\let\qasm at env@charbgroup={
+   \catcode`\#=12 \global\let\qasm at env@charhashtag=#
+\endgroup
+{\catcode`\^^M=12 \protected\gdef\qasm at defwithnewline#1#{\def#1^^M}}
+{\catcode`\|=0 \catcode`\\=12 |global|let|qasm at env@charbackslash=\ |gdef|qasm at env@charend{\endqasm}}
+\protected\def\qasm at env@begin{%
+   \begingroup%
+      \qasm at env@setcatcodes%
+      \@ifnextchar[\qasm at env@begin at arg{\qasm at env@begin at arg[]}%
+}
+
+\protected\def\qasm at env@setcatcodes{%
+   % make most TeX specials ordinary - not \dospecials, we want to keep spaces
+   \catcode`\\=12 %
+   \catcode`\{=12 %
+   \catcode`\}=12 %
+   \catcode`\$=12 %
+   \catcode`\&=12 %
+   \catcode`\#=12 %
+   \catcode`\^=12 \catcode`\^^K=12 %
+   \catcode`\_=12 \catcode`\^^A=12 %
+   \catcode`\%=12 %
+   \catcode`\~=12 %
+   \catcode`\^^M=12 %
+}
+
+\def\qasm at env@begin at arg{%
+   \yquant at env@lazyfalse%
+   \let\yquant at env@check=\qasm at env@check%
+   \let\yquant at register@get at ids=\qasm at register@get at ids%
+   \let\yquant at attrs@remaining=\empty% we don't have attributes, but the backend requires this to be set up
+   \let\tikz at ensure@dollar at catcode=\relax% this check will fail, but it does not make sense in our case. Every tikz command will be read with the appropriate dollar catcode.
+   % provide some commands that might be used during gate construction
+   \let\m=\qasm at cmd@m%
+   \let\txt=\qasm at cmd@txt%
+   \let\meter=\qasm at cmd@meter%
+   \let\dmeter=\qasm at cmd@dmeter%
+   \let\dmeterwide=\qasm at cmd@dmeterwide%
+   \yquant at env@begin at generic\qasm%
+}
+
+\def\qasm at env@end{%
+      \yquant at env@end%
+   \endgroup%
+}
+
+% since the backslash is just an `other' character, we cannot leave our environment. Hence, we manually check for \endqasm and \end{qasm}.
+\def\qasm at env@checkend#1#2#3#4#5{%
+   \ifstrequal{#1#2#3#4 }{\end}{% for us, \end is a macro so that TeX will insert another space
+      \ifx q#5%
+         \expandafter\qasm at env@checkend at plain%
+      \else%
+         \ifx\qasm at env@charbgroup#5%
+            \expandafter\expandafter\expandafter\qasm at env@checkend at latex%
+         \else%
+            \PackageError{yquant.sty}%
+                         {Invalid qasm syntax: `\meaning#1'}%
+                         {Adhere to the specs!}%
+         \fi%
+      \fi%
+   }{%
+      \PackageError{yquant.sty}%
+                   {Invalid qasm syntax: `\meaning#1'}%
+                   {Adhere to the specs!}%
+   }%
+}
+
+\def\qasm at env@checkend at plain#1#2#3{%
+   \ifstrequal{#1#2#3}{asm}{%
+         \endgroup% we initially thought this was a normal command, so exit the group
+      \endqasm%
+      \futurelet\qasm at env@checkend at plain@delimiter\qasm at env@checkend at plain@validate%
+   }{%
+      \PackageError{yquant.sty}%
+                   {Invalid qasm syntax: `\meaning\qasm at env@charbackslash'}%
+                   {Adhere to the specs!}%
+   }%
+}
+
+\def\qasm at env@checkend at plain@validate{%
+   % we already ended the qasm environment, based on the assumption that what followed was not a letter
+   \ifcat\qasm at env@checkend at plain@delimiter A%
+      % we were wrong and should never have left the environment. Now it's too late
+      \PackageError{yquant.sty}%
+                   {Invalid qasm syntax: `\meaning\qasm at env@charbackslash'}%
+                   {Adhere to the specs!}%
+   \fi%
+}
+
+\begingroup
+   \catcode`\[=1 \catcode`\]=2 \catcode`\}=12 % we set [ just for editors that scan for matching parenthesis, though we don't need it
+   \gdef\qasm at env@checkend at latex#1#2#3#4#5[%
+      \ifstrequal[#1#2#3#4#5][qasm}][%
+            \endgroup% we initially thought this was a normal command, so exit the group
+         \end[qasm]%
+      ][%
+         \PackageError[yquant.sty]%
+                      [Invalid qasm syntax: `\meaning\qasm at env@charbackslash']%
+                      [Adhere to the specs!]%
+      ]%
+   ]%
+\endgroup
+
+\protected\def\qasm at env@restorecatcodes{%
+   % restore standard catcodes (hopefully they were standard before entering the environment)
+   \catcode`\\=0 % backslash is TeX escape character
+   \catcode`\{=1 % left brace is begin-group character
+   \catcode`\}=2 % right brace is end-group character
+   \catcode`\$=3 % dollar sign is math shift
+   \catcode`\&=4 % ampersand is alignment tab
+   \catcode`\#=6 % hash mark is macro parameter character
+   \catcode`\^=7 \catcode`\^^K=7 % circumflex and uparrow are for superscripts
+   \catcode`\_=8 \catcode`\^^A=8 % underline and downarrow are for subscripts
+   \catcode`\%=14 % percent sign is comment character
+   \catcode`\~=\active % tilde is active
+%   \catcode`\^^M=5 % ascii return is end-line - not needed, is done manually
+}
+
+\tikzaddtikzonlycommandshortcutlet\qasm\qasm at env@begin
+\tikzaddtikzonlycommandshortcutlet\endqasm\qasm at env@end
+
+\protected\def\qasm at env@check{%
+   \let\next=\relax%
+   \ifx\yquant at env@nextchar\qasm at env@charhashtag% comment
+      \let\next=\qasm at env@gobblecomment%
+   \else%
+      % Here we assume standard catcodes for A
+      \ifcat\noexpand\yquant at env@nextchar A% letter
+         \let\next=\qasm at langhelper@check at name%
+      \else%
+         \ifx\yquant at env@nextchar\yquant at env@linebreak% linebreaks are no longer end-of-line characters, but have catcode 12 (other).
+            \let\next=\yquant at env@gobblepar%
+         \else%
+            \ifx\yquant at env@nextchar\par%
+               \let\next=\yquant at env@gobblepar%
+            \else%
+               \ifx\yquant at env@nextchar\qasm at env@charbackslash% backslash might end the environment
+                  \let\next=\qasm at env@checkend%
+               \else%
+                  \PackageError{yquant.sty}%
+                     {Invalid qasm syntax: `\meaning\yquant at env@nextchar'}%
+                     {Adhere to the specs!}%
+               \fi%
+            \fi%
+         \fi%
+      \fi%
+   \fi%
+   \next%
+}
+
+\qasm at defwithnewline\qasm at env@gobblecomment#1{%
+   \yquant at env@contscan%
+}
+
+\def\qasmimport#1{%
+   \ifpgfpicture%
+      \ifnum\yquant at env>0 %
+         \PackageError{yquant.sty}{You cannot use qasm circuits as subcircuits}{}%
+      \else%
+         \begingroup%
+            \let\yquantimport at now=\qasmimport at now%
+            \everyeof{\noexpand}%
+            \qasm at env@setcatcodes%
+            \yquantimportcommand{#1}%
+            \expandafter\expandafter\expandafter\qasm%
+               \expandafter\qasmimport at file%
+            \qasm at env@charend%
+         \endgroup%
+      \fi%
+   \else%
+      \begin{tikzpicture}%
+         \begingroup%
+            \let\yquantimport at now=\qasmimport at now%
+            \everyeof{\noexpand}%
+            \qasm at env@setcatcodes%
+            \yquantimportcommand{#1}%
+            \expandafter\expandafter\expandafter\qasm%
+               \expandafter\qasmimport at file%
+            \qasm at env@charend%
+         \endgroup%
+      \end{tikzpicture}
+   \fi%
+}
+
+\def\qasmimport at now#1{%
+   \edef\qasmimport at file{\@@input #1 }%
+}
+% END_FOLD
+
+% BEGIN_FOLD Langhelper definitions
+% Parsing command itself
+\protected\qasm at defwithnewline\qasm at langhelper@check at name#1 #2{%
+   \lowercase{\edef\cmd{#1}}%
+   \unless\ifcsname qasm at lang@\cmd\endcsname%
+      \PackageError{yquant.sty}{Unsupported qasm command: `#1'}%
+         {You used a command that is unknown to qasm.}%
+   \fi%
+   \catcode`\^^M=5 % this catcode is required for scantokens, which we almost always need (because we use yquant's extensive register name facilities)
+   \csname qasm at lang@\cmd\endcsname#2 \qasm at end%
+}
+
+% Defining commands that accept registers and take an optional value
+% #1: name of the argument
+% #2: command that is to be called
+% #3: number of controls
+% #4: number of targets
+\protected\def\qasm at langhelper@declare at command#1#2#3#4{%
+   \begingroup%
+      \lowercase{\edef\cmd{#1}}%
+      % everything after the next space is just gobbled
+      \protected\long\csgdef{qasm at lang@\cmd}##1 ##2\qasm at end{%
+         \qasm at langhelper@declare at command@@fetchargs{#3}{#4}{##1}{#2}%
+         \yquant at env@rescan%
+      }%
+   \endgroup%
+}
+
+\protected\def\qasm at langhelper@declare at command@@fetchargs#1#2#3#4{%
+   \let\targets=\empty%
+   \let\controls=\empty%
+   \count0=0 %
+   \count2=#1 %
+   \forcsvlist%
+      \qasm at langhelper@declare at command@@fetchargs@%
+      {#3}%
+   \unless\ifnum\count0=\numexpr\count2+#2\relax %
+      \PackageError{yquant.sty}{Invalid command use}%
+         {This gate requires #2 target\unless\ifnum#2=1 s\fi\space and #1 control\unless\ifnum#1=1 s\fi.}%
+   \fi%
+   \edef\cmd{%
+      \unexpanded{#4}%
+         {\expandafter\@gobble\controls\empty}{}%
+         {\expandafter\@gobble\targets\empty}%
+   }%
+   \cmd%
+}
+
+\protected\def\qasm at langhelper@declare at command@@fetchargs@#1{%
+   \advance\count0 by 1 %
+   \unless\ifnum\count0>\count2 %
+      % we are at the beginning
+      \appto\controls{,#1}%
+   \else%
+      \appto\targets{,#1}%
+   \fi%
+}
+
+% converts names to indices
+\protected\def\qasm at register@get at ids#1{%
+   \begingroup%
+      \let\yquant at register@get at ids@list=\empty%
+      \count0=2147483647 % minimal id
+      \count2=0 % maximal id
+      \count4=0 % number of total registers
+      \forcsvlist\qasm at register@get at ids@outerlist{#1}%
+      \global\let\yquant at register@get at ids@list=\yquant at register@get at ids@list%
+      \xdef\yquant at register@get at ids@min{\the\count0}%
+      \xdef\yquant at register@get at ids@max{\the\count2}%
+      \xdef\yquant at register@get at ids@count{\the\count4}%
+      % this is very simple: if multi-registers are allowed and we have more than one, then we are in a multi-register; else not.
+      \ifyquant at register@get at allowmulti%
+         \ifnum\count4> 1%
+            \count6=\count0 % no way to chain the main register
+            \yquant at register@multi at splitparts%
+            \xdef\yquant at register@get at ids@list{%
+               \noexpand\yquant at register@multi%
+                  {\the\count0}{\the\count2}{\the\count4}{\yquant at register@get at ids@list}%
+               \yquant at list@delim%
+            }%
+            \count4=1 %
+         \fi%
+      \fi%
+   \endgroup%
+}
+
+\protected\def\qasm at register@get at ids@outerlist#1{%
+   \yquant at register@get at id\idx{#1[0]}%
+   \listeadd\yquant at register@get at ids@list{\idx}%
+   \ifnum\count0>\idx %
+      \count0=\idx %
+   \fi%
+   \ifnum\count2<\idx %
+      \count2=\idx %
+   \fi%
+   \advance\count4 by 1 %
+}
+% END_FOLD
+
+% BEGIN_FOLD Configuration
+\pgfkeys{%
+   /yquant/operators/every s/.style={%
+      /yquant/operators/every box%
+   },%
+   /yquant/operators/every t/.style={%
+      /yquant/operators/every box%
+   },%
+   /yquant/operators/every utwo/.style={%
+      /yquant/operators/every box%
+   },
+   /qasm/zero/.store in=%
+      \qasm at config@zero,%
+   /qasm/register/default qubit name/.store in=%
+      \qasm at config@qubit at nameonly,%
+   /qasm/register/default qubit name value/.store in=%
+      \qasm at config@qubit at namevalue%
+}
+\def\qasm at ket#1{\mathopen|\mathinner{#1}\rangle}
+\def\qasm at config@zero{$\qasm at ket0$}
+\let\qasm at config@qubit at nameonly=\qasm at ket
+\def\qasm at config@qubit at namevalue#1#2{\qasm at ket{#1} = \qasm at ket{#2}}
+% END_FOLD
+
+% BEGIN_FOLD Automagically convert names
+% if ([A-z]+)(\d+) matches, replace by $1_{$2}, else let unchanged, fully expandable
+\def\qasmname#1{%
+   \expandafter\qasmname at entry\expandafter{#1}%
+}
+
+\def\qasmname at entry#1{%
+   \ifstrempty{#1}{}{%
+      \qasmname at checkletter#1\relax\qasmname at end{}%
+   }%
+}
+
+\def\qasmname at checkletter#1#2\qasmname at end#3{%
+   \ifstrempty{#2}{%
+      % done, and we didn't get a number, so regex doesn't match (#1 is \relax)
+      #3%
+   }{%
+      \ifnum`#1>64 %
+         \ifnum`#1<123 %
+            \expandafter\expandafter\expandafter\@firstoftwo%
+         \else%
+            \expandafter\expandafter\expandafter\@secondoftwo%
+         \fi%
+      \else%
+         \expandafter\@secondoftwo%
+      \fi%
+      {%
+         \qasmname at checkletter#2\qasmname at end{#3#1}%
+      }{%
+         % not in A-z, move to the number part - but only if there was something matching before!
+         \ifstrempty{#3}{%
+            \qasmname at droprelax#1#2\qasmname at end%
+         }{%
+            \qasmname at checknumber#1#2\qasmname at end{#3}{}%
+         }%
+      }%
+   }%
+}
+
+\def\qasmname at checknumber#1#2\qasmname at end#3#4{%
+   \ifstrempty{#2}{%
+      % done - was there a number?
+      \ifstrempty{#4}{%
+         #3%
+      }{%
+         #3_{#4}%
+      }%
+   }{%
+      \ifnum`#1>47 %
+         \ifnum`#1<58 %
+            \expandafter\expandafter\expandafter\@firstoftwo%
+         \else%
+            \expandafter\expandafter\expandafter\@secondoftwo%
+         \fi%
+      \else%
+         \expandafter\@secondoftwo%
+      \fi%
+      {%
+         \qasmname at checknumber#2\qasmname at end{#3}{#4#1}%
+      }{%
+         \qasmname at droprelax#3#4#1#2\qasmname at end%
+      }%
+   }%
+}
+
+\def\qasmname at droprelax#1\relax#2\qasmname at end{%
+   \ifstrempty{#2}{%
+      #1%
+   }{%
+      \qasmname at droprelax{#1\relax}#2\qasmname at end%
+   }%
+}
+% END_FOLD
+
+% BEGIN_FOLD Language definitions
+\protected\def\qasm at lang@qubit#1 #2\qasm at end{%
+   \let\yquant at lang@create at type=\yquant at register@type at q%
+   \def\yquant at lang@create at style{qubit}%
+   \qasm at lang@create#1,,,,\qasm at end%
+}
+
+\protected\def\qasm at lang@cbit#1 #2\qasm at end{%
+   \let\yquant at lang@create at type=\yquant at register@type at c%
+   \def\yquant at lang@create at style{cbit}%
+   \qasm at lang@create#1,,,,\qasm at end%
+}
+
+\protected\def\qasm at lang@create#1,#2,#3,#4,#5\qasm at end{%
+   \ifstrempty{#4}\relax{%
+      \PackageError{yquant.sty}{Invalid number of arguments}%
+                   {Register creation in qasm expects at most three arguments.}%
+   }%
+   \ifstrequal{#5}{,,}{%
+      % we have a third parameter which defines the TeX name - retokenize it
+      \begingroup%
+         \qasm at env@restorecatcodes%
+         \everyeof{\noexpand}%
+         \endlinechar=-1 %
+         \edef\process{%
+            \def\noexpand\yquant at lang@attr at value{%
+               \scantokens{\unexpanded{#3}}%
+            }%
+         }
+         \expandafter%
+      \endgroup%
+      \process%
+   }{%
+      % make num subscript if name = alpha+numbers
+      \edef\yquant at lang@attr at value{\qasmname{#1}}%
+   }%
+   \ifx\yquant at lang@create at type\yquant at register@type at c%
+      \ifstrempty{#2}{% cbit has initial value?
+         \edef\yquant at lang@attr at value{%
+            $\unexpanded\expandafter{\yquant at lang@attr at value}$%
+         }%
+      }{%
+         \begingroup%
+            \qasm at env@restorecatcodes%
+            \everyeof{\noexpand}%
+            \endlinechar=-1 %
+            \edef\process{%
+               \def\noexpand\yquant at lang@attr at value{%
+                  $\unexpanded\expandafter{\yquant at lang@attr at value} = %
+                   \scantokens{\unexpanded{#2}}$%
+               }%
+            }
+            \expandafter%
+         \endgroup%
+         \process%
+      }%
+   \else%
+      \ifstrempty{#2}{% qubit has initial value?
+         \edef\yquant at lang@attr at value{%
+            \unexpanded\expandafter{\expandafter$%
+                \expandafter\qasm at config@qubit at nameonly%
+                \expandafter{\yquant at lang@attr at value}%
+             $}%
+         }%
+      }{%
+         \begingroup%
+            \qasm at env@restorecatcodes%
+            \everyeof{\noexpand}%
+            \endlinechar=-1 %
+            \edef\process{%
+               \def\noexpand\yquant at lang@attr at value{%
+                  $\noexpand\qasm at config@qubit at namevalue%
+                   {\unexpanded\expandafter{\yquant at lang@attr at value}}%
+                   {\scantokens{\unexpanded{#2}}}$%
+               }%
+            }
+            \expandafter%
+         \endgroup%
+         \process%
+      }%
+   \fi%
+   \yquant at lang@create at do{#1}[1][;%
+   \yquant at env@rescan%
+}
+
+\protected\def\qasm at lang@def#1,#2,'#3' #4\qasm at end{%
+      \lowercase{\def\cmd{#1}}%
+      % we need to do a couple of case distictions (it might seem crazy to look for the existence of a particular macro in #3, but this is what qasm does)
+      \ifstrequal{#3}{bullet}{%
+         \edef\process{%
+            \def\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname{%
+               \let\noexpand\yquant at lang@attr at value=\noexpand\empty%
+               \noexpand\yquant at lang@@phase%
+            }%
+         }%
+      }{%
+         % re-tokenize the code
+         \begingroup%
+            \qasm at env@restorecatcodes%
+            \everyeof{\noexpand}%
+            \endlinechar=-1 %
+            \edef\process{%
+               \def\noexpand\texcode{%
+                  \scantokens{\unexpanded{#3}}%
+               }%
+            }%
+            \expandafter%
+         \endgroup%
+         \process%
+         % now check whether the text begins with \meter, \dmeter or \dmeterwide. This is not 100% equivalent to qasm, which only checks whether these macros are present, not whether they are at the beginning. But due to our node approach, anything different from the whole content being this macro does not make sense.
+         \expandafter\ifyquant at firsttoken\expandafter\meter\expandafter{\texcode}{%
+            \edef\process{%
+               \def\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname####1####2{%
+                  \noexpand\yquant at lang@@measure%
+               }%
+            }%
+         }{%
+            \expandafter\ifyquant at firsttoken\expandafter\dmeter\expandafter{\texcode}{%
+               \protected at edef\process{%
+                  \def\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname####1####2{%
+                     \def\noexpand\yquant at lang@attr at value{\texcode}%
+                     \noexpand\yquant at lang@@dmeter%
+                  }%
+               }%
+            }{%
+               \expandafter\ifyquant at firsttoken\expandafter\dmeterwide\expandafter{\texcode}{%
+                  \protected at edef\process{%
+                     \def\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname####1####2{%
+                        \def\noexpand\yquant at lang@attr at value{\texcode}%
+                        \noexpand\yquant at lang@@dmeter%
+                     }%
+                  }%
+               }{%
+                  \edef\process{%
+                     \def\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname{%
+                        \yquant at draw%
+                           {$\unexpanded\expandafter{\texcode}$}%
+                           {/yquant/operators/every box}%
+                     }%
+                  }%
+               }%
+            }%
+         }%
+      }%
+      \eappto\process{%
+         % we don't use \qasm at langhelper@declare at command here, since it works globally
+         \protected\long\def\expandafter\noexpand\csname qasm at lang@\cmd\endcsname####1 ####2\noexpand\qasm at end{%
+            \qasm at langhelper@declare at command@@fetchargs{#2}1{####1}\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname%
+            \noexpand\yquant at env@rescan%
+         }%
+      }%
+      \expandafter%
+   \endgroup%
+   \process%
+   \yquant at env@scan%
+}
+
+\protected\def\qasm at lang@defbox#1,#2,#3,'#4' #5\qasm at end{%
+      \ifnum#2<#3 %
+         \PackageError{yquant.sty}{Invalid box definition}%
+                      {You need at least as many total qubits as controls.}%
+      \fi%
+      \lowercase{\def\cmd{#1}}%
+      % no special cases here
+      \begingroup%
+         \qasm at env@restorecatcodes%
+         \everyeof{\noexpand}%
+         \endlinechar=-1 %
+         \edef\process{%
+            \def\noexpand\texcode{%
+               \scantokens{\unexpanded{#4}}%
+            }%
+         }%
+         \expandafter%
+      \endgroup%
+      \process%
+      \edef\process{%
+         \def\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname{%
+            \noexpand\yquant at register@get at allowmultitrue%
+            \yquant at draw%
+               {$\unexpanded\expandafter{\texcode}$}%
+               {/yquant/operators/every box}%
+         }%
+         % we don't use \qasm at langhelper@declare at command here, since it works globally
+         \protected\long\def\expandafter\noexpand\csname qasm at lang@\cmd\endcsname####1 ####2\noexpand\qasm at end{%
+            \qasm at langhelper@declare at command@@fetchargs{#3}{\the\numexpr#2-#3\relax}{####1}\expandafter\noexpand\csname qasm at lang@@def@\cmd\endcsname%
+            \noexpand\yquant at env@rescan%
+         }%
+      }%
+      \expandafter%
+   \endgroup%
+   \process%
+   \yquant at env@scan%
+}
+
+\qasm at langhelper@declare at command{measure}{\expandafter\yquant at lang@@measure\@gobbletwo}01
+
+\qasm at langhelper@declare at command{H}\yquant at lang@@h01
+
+\qasm at langhelper@declare at command{X}\yquant at lang@@x01
+
+\qasm at langhelper@declare at command{Y}\yquant at lang@@y01
+
+\qasm at langhelper@declare at command{Z}\yquant at lang@@z01
+
+\qasm at langhelper@declare at command{S}\qasm at lang@@s01
+\def\qasm at lang@@s{%
+   \yquant at draw%
+      {$S$}%
+      {/yquant/operators/every s}%
+}
+
+\qasm at langhelper@declare at command{T}\qasm at lang@@t01
+\def\qasm at lang@@t{%
+   \yquant at draw%
+      {$T$}%
+      {/yquant/operators/every t}%
+}
+
+\qasm at langhelper@declare at command{nop}\qasm at lang@@nop01
+\def\qasm at lang@@nop#1#2#3{%
+   \dimdef\yquant at lang@attr at value{\yquant at config@operator at minimum@width+\yquant at config@operator at sep}%
+   \yquant at lang@@hspace{#3}%
+   % we don't use a grid-based layout, so a nop really does nothing
+}
+
+\qasm at langhelper@declare at command{zero}\qasm at lang@@zero01
+\def\qasm at lang@@zero#1#2#3{%
+   \let\yquant at lang@attr at value=\qasm at config@zero%
+   \yquant at lang@@init{#3}%
+}
+
+\qasm at langhelper@declare at command{discard}{\expandafter\yquant at lang@@discard\@gobbletwo}01
+
+\qasm at langhelper@declare at command{slash}{\expandafter\yquant at lang@@slash\@gobbletwo}01
+
+\qasm at langhelper@declare at command{dmeter}{\expandafter\yquant at lang@@dmeter\@gobbletwo}01
+
+\qasm at langhelper@declare at command{cnot}\yquant at lang@@not11
+
+\qasm at langhelper@declare at command{c-z}\yquant at lang@@z11
+
+\qasm at langhelper@declare at command{c-x}\yquant at lang@@x11
+
+\qasm at langhelper@declare at command{toffoli}\yquant at lang@@not21
+
+\qasm at langhelper@declare at command{ZZ}{\expandafter\yquant at lang@@zz\@gobbletwo}02
+
+\qasm at langhelper@declare at command{XX}\yquant at lang@@xx02
+
+\qasm at langhelper@declare at command{swap}\yquant at lang@@swap02
+
+\qasm at langhelper@declare at command{Utwo}\qasm at lang@@Utwo02
+\def\qasm at lang@@Utwo{%
+   \yquant at register@get at allowmultitrue%
+   \yquant at draw%
+      {$U$}%
+      {/yquant/operators/every utwo}%
+}
+
+% space is a hspace _without_ wire - in priciple, we would have to discard and then re-initialize
+\qasm at langhelper@declare at command{space}\qasm at lang@@nop01
+% END_FOLD
+
+% some commands that might be used in boxes
+\def\qasm at cmd@m#1{\begin{pmatrix}#1\end{pmatrix}}
+\def\qasm at cmd@txt#1{$#1$}% do not use \text{...}, but exit math mode - in this way, TikZ can still handle linebreak
+\def\qasm at cmd@meter{}%
+\def\qasm at cmd@dmeterwide#1#2{$#1$}%
+\def\qasm at cmd@dmeter#1{$#1$}%
+
+\endinput
\ No newline at end of file


Property changes on: trunk/Master/texmf-dist/tex/latex/yquant/yquantlanguage-qasm.sty
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property


More information about the tex-live-commits mailing list.