%% A LaTeX package for typesetting string diagrams in various categorical structures, including monoidal, dagger compact closed, rigid, braided, symmetric, and compact categories.
%% Copyright (C) 2025 Niina Ryota
%% 
%% This work may be distributed and/or modified under the conditions of the LaTeX Project Public License (LPPL), 
%% version 1.3c or later. The latest version of this license is in:
%%   https://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 "maintained".
%% 
%% The Current Maintainer of this work is Niina Ryota.
%% 
%% This work consists of the following files:
%%   qworld.sty
%%   qworld.pdf
%% Change Log
%% -----------
%%
%% Version 1.1.0 (2025-04-03)
%% - The following commands have been updated:
%%   - \symbolI, \symbols \symbolS, \symbolO, \symboln \symbolN.
%% - The ID, which was previously a required argument for these commands, has been made optional.
%% - Users can now specify the ID via an optional key, providing more flexibility when using the commands.
%% - English documentation has been added to the package.
%% Version 1.0.0 (2025-04-01)
%% - Initial release of the package.
\ProvidesPackage{qworld}[2025/04/03 Version 1.1.0]
\RequirePackage{tikz, pgffor, ifthen, xparse, xfp, xstring}
\usetikzlibrary{cd,positioning,shapes.symbols,decorations.pathmorphing,arrows,calc,shapes.misc,intersections,decorations.pathreplacing,decorations.markings,arrows.meta, shapes.geometric}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifflowup
\newif\ifflowdown
\newif\ifflowleft
\newif\ifflowright
% Default: bottom-to-top (flow up)
\flowuptrue
\flowdownfalse
\flowleftfalse
\flowrightfalse

\newif\ifsimplify
\simplifytrue
\DeclareOption{simplify=false}{
    \simplifyfalse
}
\DeclareOption{flow direction=up}{
    \flowuptrue
    \flowdownfalse
    \flowleftfalse
    \flowrightfalse
}
\DeclareOption{flow direction=down}{
    \flowupfalse
    \flowdowntrue
    \flowleftfalse
    \flowrightfalse
}
\DeclareOption{flow direction=left}{
    \flowupfalse
    \flowdownfalse
    \flowlefttrue
    \flowrightfalse
}
\DeclareOption{flow direction=right}{%
    \flowupfalse
    \flowdownfalse
    \flowleftfalse
    \flowrighttrue
}
\ProcessOptions\relax
\AtEndOfPackage{
    \ifsimplify
        \qwsimplify
    \fi
}
\newcounter{portNnum}
\newcounter{portSnum}
\newcounter{portNtempnum}
\newcounter{portStempnum}
\newcounter{bbcount}
\newcounter{qworldnumeros}\setcounter{qworldnumeros}{1}

\newcommand{\qwreset}[1][0,0]
{
    \coordinate (qworldat) at (#1);
}

\newcommand{\qwbr}[1][1]
{
    \coordinate (O) at (0,0);
    \coordinate (qworldattemp) at ([shift={(0,{\fpeval{-1*#1}})}]qworldat);
    \coordinate (qworldat) at (O |- qworldattemp);
}

\newcommand{\qwmove}[1][0,0]
{
    \coordinate (qworldattemp) at ([shift={{\fpeval{(#1)}}}]qworldat);
    \coordinate (qworldat) at (qworldattemp);
}

\newcommand{\qwspace}[1][1]
{
    \qwmove[#1,0]
}

\def\qwepsilony{0.2}
\def\qwepsilonx{0}
\def\qwinnersep{3.1415926536pt}
\def\qwcontrolepsilon{0.5}
\def\qwfillrate{0.13}
\def\qwfillratex{0.37}
\def\qwasymrate{0.05}
\def\qwasymstress{2.5}
\def\qwvelocityx{0}
\def\qwvelocityy{0.2}
\def\qwthickness{semithick}
\def\qwspideradjust{1}
\def\qwireinterval{1}
\def\qcanvasslantstyle{notentered}
\def\qwcompariaccu{1000}

\newcommand{\qwtext}[2]
{
    \node at (#2) {#1};
}

\NewDocumentCommand{\qwid}{O{}}
{
    \qwidprocess[#1]
}

\NewDocumentCommand{\qwbox}{O{at=qworldat}}{
    \qwboxprocess[box]{#1}
}

\NewDocumentCommand{\qwasymbox}{O{at=qworldat}}{
    \qwboxprocess[asym]{#1}
}

\NewDocumentCommand{\qwadjbox}{O{at=qworldat}}{
    \qwboxprocess[adj]{#1}
}

\NewDocumentCommand{\qwtransposebox}{O{at=qworldat}}{
    \qwboxprocess[transpose]{#1}
}

\NewDocumentCommand{\qwconjbox}{O{at=qworldat}}{
    \qwboxprocess[conj]{#1}
}


\NewDocumentCommand{\qwscalar}{O{at=qworldat}}{
    \qwboxprocess[scalar]{p=0,#1}
}

\NewDocumentCommand{\qwstate}{O{at=qworldat}}{
    \qwboxprocess[state]{s=0,#1}
}

\NewDocumentCommand{\qweffect}{O{at=qworldat}}{
    \qwboxprocess[effect]{n=0,#1}
}

\NewDocumentCommand{\qwbraid}{O{at=qworldat}}{
    \qwboxprocess[braid]{#1}
}

\NewDocumentCommand{\qwbraidinv}{O{at=qworldat}}{
    \qwboxprocess[braidinv]{#1}
}

\NewDocumentCommand{\qwsymmetry}{O{at=qworldat}}{
    \qwboxprocess[sym]{#1}
}

\NewDocumentCommand{\qwidup}{O{at=qworldat}}{
    \qwidprocess[arrowtype=->-, #1]
}

\NewDocumentCommand{\qwiddown}{O{at=qworldat}}
{
    \qwidprocess[arrowtype=-<-, #1]
}

\NewDocumentCommand{\qwidupup}{O{at=qworldat}}
{
    \qwidprocess[arrowtype=->>-, #1]
}

\NewDocumentCommand{\qwiddowndown}{O{at=qworldat}}
{
    \qwidprocess[arrowtype=-<<-, #1]
}

\NewDocumentCommand{\qwcap}{O{at=qworldat}}
{
    \qwboxprocess[cap]{#1}
}

\NewDocumentCommand{\qwcup}{O{at=qworldat}}
{
    \qwboxprocess[cup]{#1}
}

\NewDocumentCommand{\qwcaprev}{O{at=qworldat}}
{
    \qwboxprocess[caprev]{#1}
}

\NewDocumentCommand{\qwcuprev}{O{at=qworldat}}
{
    \qwboxprocess[cuprev]{#1}
}

\NewDocumentCommand{\qwmul}{O{at={qworldat}}}
{
     \qwboxprocess[spider]{s=2,n=1,#1}
}

\NewDocumentCommand{\qwunit}{O{at={qworldat}}}
{
     \qwboxprocess[spider]{s=0,n=1,#1}
}
\NewDocumentCommand{\qwcounit}{O{at={qworldat}}}
{
     \qwboxprocess[spider]{s=1,n=0,#1}
}

\NewDocumentCommand{\qwcomul}{O{at={qworldat}}}
{
    \qwboxprocess[spider]{s=1,n=2,#1}
}

\NewDocumentCommand{\qwspider}{O{at={qworldat}}}
{
    \qwboxprocess[spider]{#1}
}

\newcommand{\qwdiagram}[2][]{
    \ifmmode
        {\vcenter{\hbox{
                \begin{tikzpicture}[#1]
                    \setcounter{qworldnumeros}{1}
                    \qwreset
                    #2
                    \qwreset
                \end{tikzpicture}
        }}}
    \else
        \({\vcenter{\hbox{
            \begin{tikzpicture}[#1]
                \setcounter{qworldnumeros}{1}
                \qwreset
                #2
                \qwreset
            \end{tikzpicture}
        }}}\)
    \fi
}

\newcommand{\qwcanvas}[2][]{
    \qwdiagram[#1]{#2}
}

\NewDocumentCommand{\qwbb}{O{notentered} O{notentered}}{
    \ifthenelse{\equal{#2}{notentered}}
    {
        \ifthenelse{\equal{#1}{notentered}}{
            \def\BBcolor{}
            \def\BBP{1}
        }{
            \def\BBcolor{}
            \def\BBP{#1}
        }
    }{
        \def\BBcolor{#1}
        \def\BBP{#2}
    }
  
    \setcounter{bbcount}{0}
    \foreach \i in \BBP{
        \stepcounter{bbcount}
    }

    \def\minX{10000}\def\maxX{-10000}
    \def\minY{10000}\def\maxY{-10000}
    \foreach \hogaku in {NE, NW, SE, SW}{
        \foreach \qwindex in \BBP {
            \path let \p1 = (\hogaku-\qwindex)
            in \pgfextra
            {
                \xdef\px{\x1} 
                \xdef\py{\y1}
            };
            \xdef\minX{\fpeval{min(\minX, \px/1pt)}}
            \xdef\minY{\fpeval{min(\minY, \py/1pt)}}
            \xdef\maxX{\fpeval{max(\maxX, \px/1pt)}}
            \xdef\maxY{\fpeval{max(\maxY, \py/1pt)}}
        }
    }
    \ifnum\thebbcount=1
        \foreach \i in \BBP{
            \ifthenelse{\equal{#2}{notentered}}{
                \draw[densely dotted, \BBcolor] (NW-\i) -- (NE-\i) -- (SE-\i) -- (SW-\i) --cycle;
            }{
                \draw[densely dotted, fill=#1, fill opacity=0.3] (NW-\i) -- (NE-\i) -- (SE-\i) -- (SW-\i) --cycle;
            }
        }
    \else
        \ifthenelse{\equal{#2}{notentered}}{
            \draw[densely dotted, \BBcolor] (\minX pt,\minY pt) rectangle (\maxX pt,\maxY pt);%
        }{
            \draw[densely dotted, fill=#1, fill opacity=0.3] (\minX pt,\minY pt) rectangle (\maxX pt,\maxY pt);%
        }
    \fi
    \coordinate (BB-NE) at (\maxX pt, \maxY pt);
    \coordinate (BB-SW) at (\minX pt, \minY pt);
    \coordinate (BB-NW) at (BB-SW |- BB-NE);
    \coordinate (BB-SE) at (BB-SW -| BB-NE);
    \coordinate (BB-N) at ($(BB-NW)!0.5!(BB-NE)$);
    \coordinate (BB-S) at ($(BB-SW)!0.5!(BB-SE)$);
    \coordinate (BB-W) at ($(BB-NW)!0.5!(BB-SW)$);
    \coordinate (BB-E) at ($(BB-NE)!0.5!(BB-SE)$);
    \coordinate (BB-C) at ($(BB-N)!0.5!(BB-S)$);
}

\newcommand{\qwbball}[1][notentered]{
    \ifthenelse{\equal{#1}{notentered}}{
        \foreach\j in{1,...,\theqworldnumeros}
        {
            \ifnum\j<\theqworldnumeros
                \qwbb[\j]
            \fi
        }
    }{
        \foreach\j in{1,...,\theqworldnumeros}{
            \ifnum\j<\theqworldnumeros
                \qwbb[#1][\j]
            \fi
        }
    }
}

\NewDocumentCommand{\qwbbsymbol}{O{E} m O{right}}
{
    \node[#3] at (BB-#1) {#2};
}

\newcommand{\qwBBall}[1][notentered]{
    \addtocounter{qworldnumeros}{-1}
    \ifthenelse{\equal{#1}{notentered}}{
        \qwbb[1,...,\theqworldnumeros]
    }{
        \qwbb[#1][1,...,\theqworldnumeros]
    }
    \stepcounter{qworldnumeros}
}

\newcommand{\qwloop}[2][1]{
    \foreach \i in {1,...,#1}
    {
        #2
    }
}
\tikzset{
    midarrow/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{>}}},
        postaction={decorate}
    }
}
\tikzset{
    ->-/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{>}}},
        postaction={decorate}
    }
}
\tikzset{
    ->--/.style={
        decoration={markings, mark=at position 0.25 with {\arrow{>}}},
        postaction={decorate}
    }
}
\tikzset{
    -->-/.style={
        decoration={markings, mark=at position 0.75 with {\arrow{>}}},
        postaction={decorate}
    }
}
\tikzset{
    midarrowinv/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{<}}},
        postaction={decorate}
  }
}
\tikzset{
    -<-/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{<}}},
        postaction={decorate}
  }
}
\tikzset{
    -<--/.style={
        decoration={markings, mark=at position 0.25 with {\arrow{<}}},
        postaction={decorate}
    }
}
\tikzset{
    --<-/.style={
        decoration={markings, mark=at position 0.75 with {\arrow{<}}},
        postaction={decorate}
    }
}
\tikzset{
    midddarrow/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{>>}}},
        postaction={decorate}
    }
}
\tikzset{
    ->>-/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{>>}}},
        postaction={decorate}
    }
}
\tikzset{
    ->>--/.style={
        decoration={markings, mark=at position 0.25 with {\arrow{>>}}},
        postaction={decorate}
    }
}
\tikzset{
    -->>-/.style={
        decoration={markings, mark=at position 0.75 with {\arrow{>>}}},
        postaction={decorate}
    }
}
\tikzset{
    midddarrowinv/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{<<}}},
        postaction={decorate}
  }
}
\tikzset{
    -<<-/.style={
        decoration={markings, mark=at position 0.5 with {\arrow{<<}}},
        postaction={decorate}
  }
}
\tikzset{
    -<<--/.style={
        decoration={markings, mark=at position 0.25 with {\arrow{<<}}},
        postaction={decorate}
    }
}
\tikzset{
    --<<-/.style={
        decoration={markings, mark=at position 0.75 with {\arrow{<<}}},
        postaction={decorate}
    }
}

\newcommand{\qwPhantomArrow}[2][>]{
  \tikzset{
        phantomarrow/.style={
        decoration={markings, mark=at position #2 with {\arrow{#1}}},
        postaction={decorate}
    }
  }
}
\def\qworiginalarrow{\qwPhantomArrow}
\newcommand{\qwsimplify}{
    \ifdefined\qid
    \else
        \def\qid{\qwid}
    \fi
    \ifdefined\qwire
    \else
        \def\qwire{\qwid}
    \fi
    \ifdefined\qbox
    \else
        \def\qbox{\qwbox}
    \fi
    \ifdefined\qasym
    \else
        \def\qasym{\qwasymbox}
    \fi
    \ifdefined\qasymbox
    \else
        \def\qasymbox{\qwasymbox}
    \fi
    \ifdefined\qadj
    \else
        \def\qadj{\qwadjbox}
    \fi
    \ifdefined\qadjbox
    \else
        \def\qadjbox{\qwadjbox}
    \fi
    \ifdefined\qtranspose
    \else
        \def\qtranspose{\qwtransposebox}
    \fi
    \ifdefined\qtransposebox
    \else
        \def\qtransposebox{\qwtransposebox}
    \fi
    \ifdefined\qtrans
    \else
        \def\qtrans{\qwtransposebox}
    \fi
    \ifdefined\qconj
    \else
        \def\qconj{\qwconjbox}
    \fi
    \ifdefined\qconjbox
    \else
        \def\qconjbox{\qwconjbox}
    \fi
    \ifdefined\qsc
    \else
        \def\qsc{\qwscalar}
    \fi
    \ifdefined\qwsc
    \else
        \def\qwsc{\qwscalar}
    \fi
    \ifdefined\qscalar
    \else
        \def\qscalar{\qwscalar}
    \fi
    \ifdefined\qstate
    \else
        \def\qstate{\qwstate}
    \fi
    \ifdefined\qeffect
    \else
        \def\qeffect{\qweffect}
    \fi
    \ifdefined\qbraid
    \else
        \def\qbraid{\qwbraid}
    \fi
    \ifdefined\qbraidinv
    \else
        \def\qbraidinv{\qwbraidinv}
    \fi
    \ifdefined\qsymmetry
    \else
        \def\qsymmetry{\qwsymmetry}
    \fi
    \ifdefined\qsym
    \else
        \def\qsym{\qwsymmetry}
    \fi
    \ifdefined\qidup
    \else
        \def\qidup{\qwidup}
    \fi
    \ifdefined\qwireup
    \else
        \def\qwireup{\qwidup}
    \fi
    \ifdefined\qwireu
    \else
        \def\qwireu{\qwidup}
    \fi
    \ifdefined\qiddown
    \else
        \def\qiddown{\qwiddown}
    \fi
    \ifdefined\qidown
    \else
        \def\qidown{\qwiddown}
    \fi
    \ifdefined\qwiredown
    \else
        \def\qwiredown{\qwiddown}
    \fi
    \ifdefined\qwired
    \else
        \def\qwired{\qwiddown}
    \fi
    \ifdefined\qwireupup
    \else
        \def\qwireupup{\qwidupup}
    \fi
    \ifdefined\qwireuu
    \else
        \def\qwireuu{\qwidupup}
    \fi
    \ifdefined\qwiredowndown
    \else
        \def\qwiredowndown{\qwiddowndown}
    \fi
    \ifdefined\qwiredd
    \else
        \def\qwiredd{\qwiddowndown}
    \fi
    \ifdefined\qcap
    \else
        \def\qcap{\qwcap}
    \fi
    \ifdefined\qcup
    \else
        \def\qcup{\qwcup}
    \fi
    \ifdefined\qcaprev
    \else
        \def\qcaprev{\qwcaprev}
    \fi
    \ifdefined\qcuprev
    \else
        \def\qcuprev{\qwcuprev}
    \fi
    \ifdefined\qmul
    \else
        \def\qmul{\qwmul}
    \fi
    \ifdefined\qunit
    \else
        \def\qunit{\qwunit}
    \fi
    \ifdefined\qcomul
    \else
        \def\qcomul{\qwcomul}
    \fi
    \ifdefined\qcounit
    \else
        \def\qcounit{\qwcounit}
    \fi
    \ifdefined\qspider
    \else
        \def\qspider{\qwspider}
    \fi
    \ifdefined\qunitobject
    \else
        \def\qunitobject{\qwunitobject}
    \fi
    \ifdefined\qunitob
    \else
        \def\qunitob{\qwunitobject}
    \fi
    \ifdefined\qdiagram
    \else
      \def\qdiagram{\qwdiagram}
    \fi
    \ifdefined\qwpuzzle
    \else
      \def\qwpuzzle{\qwdiagram}
    \fi
    \ifdefined\qpuzzle
    \else
      \def\qpuzzle{\qwdiagram}
    \fi
    \ifdefined\qpuz
    \else
      \def\qpuz{\qwdiagram}
    \fi
    \ifdefined\qcanvas
    \else
      \def\qcanvas{\qwcanvas}
    \fi
    \ifdefined\qcv
    \else
      \def\qcv{\qwcanvas}
    \fi
    \ifdefined\q
    \else
      \def\q{\qwcanvas}
    \fi
    \ifdefined\Q
    \else
        \def\Q{\qwcanvas}
    \fi
    \ifdefined\qbb
    \else
        \def\qbb{\qwbb}
    \fi
    \ifdefined\qbball
    \else
        \def\qbball{\qwbball}
    \fi
    \ifdefined\qBBall
    \else
        \def\qBBall{\qwBBall}
    \fi
    \ifdefined\qloop
    \else
        \def\qloop{\qwloop}
    \fi
    \ifdefined\qI
    \else
        \def\qI{\qwI}
    \fi
    \ifdefined\qs
    \else
        \def\qs{\qws}
    \fi
    \ifdefined\qS
    \else
        \def\qS{\qwS}
    \fi
    \ifdefined\qO
    \else
        \def\qO{\qwO}
    \fi
    \ifdefined\qn
    \else
        \def\qn{\qwn}
    \fi
    \ifdefined\qN
    \else
        \def\qN{\qwN}
    \fi
    \ifdefined\qshowI
    \else
        \def\qshowI{\qwI}
    \fi
    \ifdefined\qshows
    \else
        \def\qshows{\qws}
    \fi
    \ifdefined\qshowS
    \else
        \def\qshowS{\qwS}
    \fi
    \ifdefined\qshowO
    \else
        \def\qshowO{\qwO}
    \fi
    \ifdefined\qshown
    \else
        \def\qshown{\qwn}
    \fi
    \ifdefined\qshowN
    \else
        \def\qshowN{\qwN}
    \fi
    \ifdefined\showI
    \else
        \def\showI{\qwI}
    \fi
    \ifdefined\shows
    \else
        \def\shows{\qws}
    \fi
    \ifdefined\showS
    \else
        \def\showS{\qwS}
    \fi
    \ifdefined\showO
    \else
        \def\showO{\qwO}
    \fi
    \ifdefined\shown
    \else
        \def\shown{\qwn}
    \fi
    \ifdefined\showN
    \else
        \def\showN{\qwN}
    \fi
    \ifdefined\symbolI
    \else
        \def\symbolI{\qwI}
    \fi
    \ifdefined\symbols
    \else
        \def\symbols{\qws}
    \fi
    \ifdefined\symbolS
    \else
        \def\symbolS{\qwS}
    \fi
    \ifdefined\symbolO
    \else
        \def\symbolO{\qwO}
    \fi
    \ifdefined\symboln
    \else
        \def\symboln{\qwn}
    \fi
    \ifdefined\symbolN
    \else
        \def\symbolN{\qwN}
    \fi
    \ifdefined\qbbsymbol
    \else
        \def\qbbsymbol{\qwbbsymbol}
    \fi
    \ifdefined\bbsymbol
    \else
        \def\bbsymbol{\qwbbsymbol}
    \fi
    \ifdefined\qreset
    \else
      \def\qreset{\qwreset}
    \fi
    \ifdefined\qbr
    \else
        \def\qbr{\qwbr}
    \fi
    \ifdefined\xx
    \else
        \def\xx{\qwbr}
    \fi
    \ifdefined\n
    \else
        \def\n{\qwbr}
    \fi
    \ifdefined\qcirc
    \else
        \def\qcirc{\qwbr}
    \fi
    \ifdefined\qspace
    \else
        \def\qspace{\qwspace}
    \fi
    \ifdefined\qmove
    \else
        \def\qmove{\qwmove}
    \fi
    \ifdefined\qmv
    \else
        \def\qmv{\qwmove}
    \fi
}
\pgfkeys{
    /qworld/wire/.cd,
    at/.store in=\wireat,
    dom/.store in=\startpoint,
    cod/.store in=\endpoint,
    label/.store in=\wirelabel,
    label side/.store in=\wirelabelside,
    label at/.store in=\wirelabelat,
    label option/.store in=\wirelabeloption,
    name/.store in=\wirename,
    name side/.store in=\wirenameside,
    name at/.store in=\wirenameat,
    name option/.store in=\wirenameoption,
    id/.store in=\wireid,
    sv/.store in=\startvelocity,
    ev/.store in=\endvelocity,
    arrowtype/.store in=\wirearrowtype,
    innersep/.store in=\wireinnersep,
    color/.store in=\wirecolor,
    internal invocation/.store in=\internalinvocation,
    vertical length/.store in=\wireverlen,
    vlen/.store in=\wirevlen,
    slant style/.store in=\wireslantstyle,
    at/.default={qworldat},
    dom/.default={qworldat},
    cod/.default={[shift={(0,1)}]qworldat},
    label/.default={},
    label side/.default={right},
    label at/.default={midway},
    label option/.default={right,midway},
    name/.default={notentered},
    name at/.default={notentered},
    name side/.default={notentered},
    name option/.default={notentered},
    id/.default={},
    sv/.default={\qwvelocityx,\qwvelocityy},
    ev/.default={\qwvelocityx,\qwvelocityy},
    arrowtype/.default={},
    innersep/.default={5pt},
    color/.default={},
    internal invocation/.default={0},
    vertical length/.default={1},
    vlen/.default={notentered},
    slant style/.default={}
}
\newcommand{\qwidprocess}[1][]
{
    \pgfkeys{/qworld/wire/.cd, 
    at={}, dom={}, cod={}, 
    label={}, label side={right},label at={midway}, label option={right,midway}, 
    name={notentered}, name side={notentered}, name at={notentered}, name option={notentered},
    id={},
    sv={\qwvelocityx,\qwvelocityy}, ev={\qwvelocityx,\qwvelocityy},
    arrowtype={}, innersep={\qwinnersep}, color={}, internal invocation={0},
    vertical length={1}, 
    vlen={notentered},
    slant style={},
    #1}
    \ifthenelse{\equal{\wirename}{notentered}\or\equal{\wirename}{{notentered}}}
    {}{
        \pgfkeys{/qworld/wire/.cd, label=\wirename}
    }
    \ifthenelse{\equal{\wirenameat}{notentered}\or\equal{\wirenameat}{{notentered}}}
    {}{
        \pgfkeys{/qworld/wire/.cd, label at=\wirenameat}
    }
    \ifthenelse{\equal{\wirenameside}{notentered}\or\equal{\wirenameside}{{notentered}}}
    {}{
        \pgfkeys{/qworld/wire/.cd, label side=\wirenameside}
    }
    \ifthenelse{\equal{\wirenameoption}{notentered}\or\equal{\wirenameoption}{{notentered}}}
    {}{
        \pgfkeys{/qworld/wire/.cd, label option=\wirenameoption}
    }

    \ifthenelse{\equal{\wirevlen}{notentered}}
    {}{
        \pgfkeys{/qworld/wire/.cd, vertical length=\wirevlen}
    }
    \ifx\wireat\empty
        \ifx\endpoint\empty
            \ifx\startpoint\empty
                \coordinate (I-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},0)}]qworldat);
            \else
                \coordinate (I-\theqworldnumeros) at (\startpoint);          
            \fi
            \coordinate (O-\theqworldnumeros) at ([shift={(0,\wireverlen)}]I-\theqworldnumeros);
        \else
            \coordinate (O-\theqworldnumeros) at (\endpoint);
            \ifx\startpoint\empty
                \coordinate (I-\theqworldnumeros) at ([shift={(0,-\wireverlen)}]O-\theqworldnumeros);
            \else
                \coordinate (I-\theqworldnumeros) at (\startpoint);
            \fi
        \fi
    \else
        \coordinate (I-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},0)}]\wireat);
        \ifx\endpoint\empty
            \coordinate (O-\theqworldnumeros) at ([shift={(0,\wireverlen)}]I-\theqworldnumeros);
        \else
            \coordinate (O-\theqworldnumeros) at (\endpoint);
        \fi
    \fi
    \coordinate (I-1-\theqworldnumeros) at (I-\theqworldnumeros);
    \coordinate (O-1-\theqworldnumeros) at (O-\theqworldnumeros);
    \ifx\wireid\empty
    \else
        \coordinate (I-\wireid) at (I-\theqworldnumeros);
        \coordinate (I-1-\wireid) at (I-\theqworldnumeros);
        \coordinate (O-\wireid) at (O-\theqworldnumeros);
        \coordinate (O-1-\wireid) at (O-\theqworldnumeros);
    \fi
    \ifx\wirelabel\empty
        \draw [\wirearrowtype,\qwthickness,\wirecolor] 
          (I-\theqworldnumeros) ..controls ([shift={\fpeval{\wireverlen*(\startvelocity)}}]I-\theqworldnumeros) 
          and ([shift={\fpeval{-1*\wireverlen*(\endvelocity)}}]O-\theqworldnumeros) .. (O-\theqworldnumeros);
    \else
        \draw [\wirearrowtype,\qwthickness,\wirecolor] (I-\theqworldnumeros) ..controls ([shift={\fpeval{\wireverlen*(\startvelocity)}}]I-\theqworldnumeros) 
        and ([shift={\fpeval{-1*\wireverlen*(\endvelocity)}}]O-\theqworldnumeros) ..(O-\theqworldnumeros) node[\wirelabelat,\wirelabelside,\wireslantstyle] {\wirelabel};
    \fi
    \ifnum\internalinvocation=0
        \path let \p1 = (I-\theqworldnumeros),\p2=(O-\theqworldnumeros) in \pgfextra{\xdef\inputx{\x1} \xdef\inputy{\y1} \xdef\outputx{\x2}\xdef\outputy{\y2}};
        \ifnum\fpeval{ceil(\qwcompariaccu*\inputx/1pt)}<\fpeval{ceil(\qwcompariaccu*\outputx/1pt)}
            \ifnum\fpeval{ceil(\qwcompariaccu*\inputy/1pt)}<\fpeval{ceil(\qwcompariaccu*\outputy/1pt)}
                \coordinate (NE-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},0)}]O-\theqworldnumeros);
                \coordinate (SW-\theqworldnumeros) at ([shift={({\fpeval{-0.5*\qwireinterval}},0)}]I-\theqworldnumeros);
                \coordinate (NW-\theqworldnumeros) at (SW-\theqworldnumeros |- NE-\theqworldnumeros);
                \coordinate (SE-\theqworldnumeros) at (SW-\theqworldnumeros -| NE-\theqworldnumeros);
            \else
                \coordinate (NW-\theqworldnumeros) at ([shift={({\fpeval{-0.5*\qwireinterval}},0)}]I-\theqworldnumeros);
                \coordinate (SE-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},0)}]O-\theqworldnumeros);
                \coordinate (NE-\theqworldnumeros) at (NW-\theqworldnumeros -| SE-\theqworldnumeros);
                \coordinate (SW-\theqworldnumeros) at (NW-\theqworldnumeros |- SE-\theqworldnumeros);
            \fi
        \else
            \ifnum\fpeval{ceil(\qwcompariaccu*\inputy/1pt)}<\fpeval{ceil(\qwcompariaccu*\outputy/1pt)}
                \coordinate (NW-\theqworldnumeros) at ([shift={({\fpeval{-0.5*\qwireinterval}},0)}]O-\theqworldnumeros);
                \coordinate (SE-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},0)}]I-\theqworldnumeros);
                \coordinate (NE-\theqworldnumeros) at (NW-\theqworldnumeros -| SE-\theqworldnumeros);
                \coordinate (SW-\theqworldnumeros) at (NW-\theqworldnumeros |- SE-\theqworldnumeros);
            \else
                \coordinate (NE-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},0)}]I-\theqworldnumeros);
                \coordinate (SW-\theqworldnumeros) at ([shift={({\fpeval{-0.5*\qwireinterval}},0)}]O-\theqworldnumeros);
                \coordinate (NW-\theqworldnumeros) at (SW-\theqworldnumeros |- NE-\theqworldnumeros);
                \coordinate (SE-\theqworldnumeros) at (SW-\theqworldnumeros -| NE-\theqworldnumeros);
            \fi
        \fi
        \coordinate (N-\theqworldnumeros) at ($(NE-\theqworldnumeros)!0.5!(NW-\theqworldnumeros)$);
        \coordinate (S-\theqworldnumeros) at ($(SE-\theqworldnumeros)!0.5!(SW-\theqworldnumeros)$);
        \coordinate (W-\theqworldnumeros) at ($(NW-\theqworldnumeros)!0.5!(SW-\theqworldnumeros)$);
        \coordinate (E-\theqworldnumeros) at ($(NE-\theqworldnumeros)!0.5!(SE-\theqworldnumeros)$);
        \coordinate (C-\theqworldnumeros) at ($(N-\theqworldnumeros)!0.5!(S-\theqworldnumeros)$);
        \ifx\wireid\empty
        \else
            \coordinate (SE-\wireid) at (SE-\theqworldnumeros);
            \coordinate (NE-\wireid) at (NE-\theqworldnumeros);
            \coordinate (SW-\wireid) at (SW-\theqworldnumeros);
            \coordinate (NW-\wireid) at (NW-\theqworldnumeros);
            \coordinate (N-\wireid) at (N-\theqworldnumeros);
            \coordinate (S-\wireid) at (S-\theqworldnumeros);
            \coordinate (W-\wireid) at (W-\theqworldnumeros);
            \coordinate (E-\wireid) at (E-\theqworldnumeros);
            \coordinate (C-\wireid) at (c-\theqworldnumeros);
        \fi
        \coordinate (qworldat) at (SE-\theqworldnumeros);
            \stepcounter{qworldnumeros}
    \fi
}

\pgfkeys{
    /qworld/box/.cd,
    at/.store in=\boxat,
    name/.store in=\boxname,
    arity/.store in=\boxarity,
    coarity/.store in=\boxcoarity,
    id/.store in=\boxid,
    index/.store in=\LuminousIndex,
    show id/.store in=\boxshowid,
    with wire/.store in=\boxwithwire,
    align/.store in=\boxalign,
    horizontal length/.store in=\boxhorilen,
    vertical length/.store in=\boxverlen,
    hlen/.store in=\boxhlen,
    vlen/.store in=\boxvlen,
    num L/.store in=\braidfsc,
    num R/.store in=\braidbsc,
    center margin length/.store in=\cml,
    sv/.store in=\startvelocity,
    ev/.store in=\endvelocity,
    color/.store in=\boxcolor,
    morphism color/.store in=\morphismcolor,
    frame color/.store in=\framecolor,
    arrowtype/.store in=\arrowtype,
    dom arrowtype/.store in=\domarrowtype,
    cod arrowtype/.store in=\codarrowtype,
    internal invocation/.store in=\internalinvocation,
    num ports/.store in=\copynumPorts,
    num ports N/.store in=\copynumPortsN,
    num ports S/.store in=\copynumPortsS,
    num ports E/.store in=\copynumPortsE,
    num ports W/.store in=\copynumPortsW,
    ports/.store in=\copyPorts,
    ports N/.store in=\copyPortsN,
    ports S/.store in=\copyPortsS,
    ports W/.store in=\copyPortsW,
    ports E/.store in=\copyPortsE,
    p/.store in=\numPorts,
    n/.store in=\numPortsN,
    s/.store in=\numPortsS,
    e/.store in=\numPortsE,
    w/.store in=\numPortsW,
    P/.store in=\Ports,
    N/.store in=\PortsN,
    S/.store in=\PortsS,
    W/.store in=\PortsW,
    E/.store in=\PortsE,
    braid smoothness/.store in=\braidsmoothness,
    dual angle/.store in=\dualangle,
    slant style/.store in=\boxslantstyle,
    at/.default={qworldat},
    name/.default={},
    arity/.default={1},
    coarity/.default={1},
    id/.default={},
    index/.default={},
    show id/.default={notentered},
    with wire/.default={notentered},
    align/.default={true},
    horizontal length/.default={},
    vertical length/.default={},
    hlen/.default={},
    vlen/.default={},
    num L/.default={1},
    num R/.default={1},
    center margin length/.default={1},
    sv/.default={\qwvelocityx,\qwvelocityy},
    ev/.default={\qwvelocityx,-\qwvelocityy},
    color/.default={white},
    morphism color/.default={black},
    frame color/.default={black},
    arrowtype/.default={},
    dom arrowtype/.default={},
    cod arrowtype/.default={},
    internal invocation/.default={0},
    num ports/.default={notentered},
    num ports N/.default={notentered},
    num ports S/.default={notentered},
    num ports W/.default={notentered},
    num ports E/.default={notentered},
    ports/.default={notentered},
    ports N/.default={notentered},
    ports S/.default={notentered},
    ports W/.default={notentered},
    ports E/.default={notentered},
    p/.default={notentered},
    n/.default={notentered},
    s/.default={notentered},
    w/.default={notentered},
    e/.default={notentered},
    P/.default={notentered},
    N/.default={notentered},
    S/.default={notentered},
    W/.default={notentered},
    E/.default={notentered},
    braid smoothness/.default={0.85},
    dual angle/.default={55},
    slant style/.default={}
}
\newcommand{\qwboxprocess}[2][box]{
    \def\boxtype{#1}
    \pgfkeys{/qworld/box/.cd, 
    at={qworldat},
    name={},
    id={},
    index={},
    show id={notentered},
    arity={1},
    coarity={1},
    with wire={notentered},
    align={notentered},
    horizontal length={notentered},
    vertical length={1},
    hlen={notentered},
    vlen={notentered},
    num L={1},
    num R={1},
    center margin length={1},
    sv={\qwvelocityx,\qwvelocityy},
    ev={\qwvelocityx,\qwvelocityy},
    color={white},
    morphism color={black},
    frame color={black},
    arrowtype={},
    dom arrowtype={},
    cod arrowtype={},
    internal invocation=0,
    num ports={notentered},
    num ports N={notentered},
    num ports S={notentered},
    num ports W={notentered},
    num ports E={notentered},
    ports={notentered},
    ports N={notentered},
    ports S={notentered},
    ports W={notentered},
    ports E={notentered},
    p={notentered},
    n={notentered},
    s={notentered},
    w={notentered},
    e={notentered},
    P={notentered},
    N={notentered},
    S={notentered},
    W={notentered},
    E={notentered},
    braid smoothness={0.85},
    dual angle={55},
    slant style={},
    #2}
    \ifthenelse{\equal{\boxshowid}{notentered}}
    {

    }{
        \pgfkeys{/qworld/box/.cd, index={true}} 
    }
    \ifthenelse{\equal{\boxhlen}{notentered}}
    {}{
        \pgfkeys{/qworld/box/.cd, horizontal length=\boxhlen} 
    }
    \ifthenelse{\equal{\boxvlen}{notentered}}
    {}{
        \pgfkeys{/qworld/box/.cd, vertical length=\boxvlen} 
    }
    \ifthenelse{\equal{\LuminousIndex}{true}\or\equal{\LuminousIndex}{True}\or\equal{\LuminousIndex}{TRUE}}
    {
        \pgfkeys{/qworld/box/.cd, name={\theqworldnumeros}}
    }{}
    \ifx\domarrowtype\empty
        \pgfkeys{/qworld/box/.cd, dom arrowtype=\arrowtype}
    \fi
    \ifx\codarrowtype\empty
        \pgfkeys{/qworld/box/.cd, cod arrowtype=\arrowtype}
    \fi
    \ifthenelse{\equal{\boxtype}{cap}\or\equal{\boxtype}{caprev}\or\equal{\boxtype}{effect}\or\equal{\boxtype}{scalar}}
    {
        \pgfkeys{/qworld/box/.cd, n=0}
        \ifthenelse{\equal{\boxtype}{cap}\or\equal{\boxtype}{caprev}}
        {
            \ifthenelse{\equal{\numPortsS}{notentered}}{
                \pgfkeys{/qworld/box/.cd, s=2}
            }{}
        }{}
    }{}
    \ifthenelse{\equal{\boxtype}{cup}\or\equal{\boxtype}{cuprev}\or\equal{\boxtype}{state}\or\equal{\boxtype}{scalar}}
    {
        \pgfkeys{/qworld/box/.cd, s=0}
        \ifthenelse{\equal{\boxtype}{cup}\or\equal{\boxtype}{cuprev}}
        {
            \ifthenelse{\equal{\numPortsN}{notentered}}{
                \pgfkeys{/qworld/box/.cd, n=2}
            }{}
        }{}
    }{}
    \ifthenelse{\equal{\braidfsc}{0}}
    {
        \def\braidfsc{1}
        \def\lstrandtype{dashed}
    }{
        \def\lstrandtype{}
    }
    \ifthenelse{\equal{\braidbsc}{0}}
    {
        \def\braidbsc{1}
        \def\rstrandtype{dashed}
    }{
        \def\rstrandtype{}
    }
    \ifthenelse{\equal{\boxtype}{braid}\or\equal{\boxtype}{braidinv}\or\equal{\boxtype}{sym}}
    {
        \pgfkeys{/qworld/box/.cd, p={\fpeval{\braidfsc+\braidbsc}}}
    }{}
    \ifthenelse{\equal{\numPorts}{notentered}}
    {}{
        \pgfkeys{/qworld/box/.cd, n=\numPorts, s=\numPorts}
    }
    \ifthenelse{\equal{\numPorts}{0}}
    {
        \pgfkeys{/qworld/box/.cd, n=0,s=0}
    }{}
    \ifthenelse{\equal{\numPortsS}{0}}
    {
        \pgfkeys{/qworld/box/.cd, s=1,S={}}
    }{}
    \ifthenelse{\equal{\numPortsN}{0}}
    {
        \pgfkeys{/qworld/box/.cd, n=1,N={}}
    }{}
    \ifthenelse{\equal{\Ports}{notentered}}
    {}{
        \pgfkeys{/qworld/box/.cd, S=\Ports, N=\Ports}
    }
    \ifthenelse{\equal{\PortsS}{notentered}}
    {
        \ifthenelse{\equal{\numPortsS}{notentered}}
        {
            \pgfkeys{/qworld/box/.cd,s=1,S={1}}
        }{
            \pgfkeys{/qworld/box/.cd, S={1,...,\fpeval{\numPortsS}}}
        }
    }{}
    \ifthenelse{\equal{\PortsN}{notentered}}
    {
        \ifthenelse{\equal{\numPortsN}{notentered}}
        {
            \pgfkeys{/qworld/box/.cd,n=1,N={1}}
        }{
            \pgfkeys{/qworld/box/.cd, N={1,...,\fpeval{\numPortsN}}}
        }
    }{}
    \setcounter{portSnum}{0}
    \ifthenelse{\equal{\PortsS}{{}}\or\equal{\PortsS}{}}{}{
        \foreach\qwtempindex in \PortsS
        {
            \stepcounter{portSnum}
        }
    }
    \def\cardportsS{\fpeval{\theportSnum}}

    \setcounter{portNnum}{0}
    \ifthenelse{\equal{\PortsN}{{}}\or\equal{\PortsN}{}}{
    }{
        \foreach\qwindex in \PortsN
        {
            \stepcounter{portNnum}
        }
    }
    \def\cardportsN{\theportNnum}
    \def\maxN{0}
    \ifthenelse{\equal{\PortsN}{}\or\equal{\PortsN}{{}}}
    {
    }{
        \foreach \qwiii in\PortsN
        {
            \ifnum\maxN<\qwiii
                \xdef\maxNtemp{\qwiii}
            \fi
            \xdef\maxN{\maxNtemp}
        }
    }
    \ifthenelse{\equal{\numPortsN}{notentered}\or\equal{\numPortsN}{{notentered}}}
    {
        \ifthenelse{\equal{\PortsN}{}\or\equal{\PortsN}{{}}}
        {
            \pgfkeys{/qworld/box/.cd, n=1}
        }{
            \pgfkeys{/qworld/box/.cd, n=\maxN}
        }
    }{}
    \def\maxS{0}
    \ifthenelse{\equal{\PortsS}{}\or\equal{\PortsS}{{}}}
    {
    }{
        \foreach \qwiii in\PortsS
        {
            \ifnum\maxS<\qwiii
                \xdef\maxStemp{\qwiii}
            \fi
            \xdef\maxS{\maxStemp}
        }
    }
    \ifthenelse{\equal{\numPortsS}{notentered}\or\equal{\numPortsS}{{notentered}}}
    {
        \ifthenelse{\equal{\PortsS}{}\or\equal{\PortsS}{{}}}
        {
            \pgfkeys{/qworld/box/.cd, s=1}
        }{
            \pgfkeys{/qworld/box/.cd, s=\maxS}
        }
    }{}
    \ifnum\numPortsN<\maxN
        \pgfkeys{/qworld/box/.cd, n=\maxN}
    \fi
    \ifnum\numPortsS<\maxS
        \pgfkeys{/qworld/box/.cd, s=\maxS}
    \fi
    \def\qworldwireScorrection{0}
    \def\qworldwireNcorrection{0}
    \ifthenelse{\equal{\boxhorilen}{notentered}}
    {
        \def\smargin{1}
        \def\nmargin{1}
        \pgfkeys{/qworld/box/.cd, horizontal length={\fpeval{max(1,\smargin*(\numPortsS-1)+1,\nmargin*(\numPortsN-1)+1)}}}
    }{
        \ifnum\numPortsS=1
            \def\qworldwireScorrection{\fpeval{0.5*\boxhorilen-0.5*\qwireinterval}}
        \fi
        \ifnum\numPortsN=1
            \def\qworldwireNcorrection{\fpeval{0.5*\boxhorilen-0.5*\qwireinterval}}
        \fi
        \def\smargin{\fpeval{(\boxhorilen-\qwireinterval)/(max(1,\numPortsS-1))}}
        \def\nmargin{\fpeval{(\boxhorilen-\qwireinterval)/(max(1,\numPortsN-1))}}
    }
    \coordinate (SW-\theqworldnumeros) at (\boxat);
    \coordinate (SE-\theqworldnumeros) at ([shift={(\boxhorilen,0)}]SW-\theqworldnumeros);
    \coordinate (NE-\theqworldnumeros) at ([shift={(0,\boxverlen)}]SE-\theqworldnumeros);
    \coordinate (NW-\theqworldnumeros) at (SW-\theqworldnumeros |- NE-\theqworldnumeros);
    \coordinate (N-\theqworldnumeros) at ($(NE-\theqworldnumeros)!0.5!(NW-\theqworldnumeros)$);
    \coordinate (S-\theqworldnumeros) at ($(SE-\theqworldnumeros)!0.5!(SW-\theqworldnumeros)$);
    \coordinate (W-\theqworldnumeros) at ($(NW-\theqworldnumeros)!0.5!(SW-\theqworldnumeros)$);
    \coordinate (E-\theqworldnumeros) at ($(NE-\theqworldnumeros)!0.5!(SE-\theqworldnumeros)$);
    \coordinate (C-\theqworldnumeros) at ($(N-\theqworldnumeros)!0.5!(S-\theqworldnumeros)$);
    \ifx\boxid\empty
    \else
        \coordinate (SW-\boxid) at (SW-\theqworldnumeros);
        \coordinate (SE-\boxid) at (SE-\theqworldnumeros);
        \coordinate (NE-\boxid) at (NE-\theqworldnumeros);
        \coordinate (NW-\boxid) at (NW-\theqworldnumeros);
        \coordinate (N-\boxid) at (N-\theqworldnumeros);
        \coordinate (S-\boxid) at (S-\theqworldnumeros);
        \coordinate (W-\boxid) at (W-\theqworldnumeros);
        \coordinate (E-\boxid) at (E-\theqworldnumeros);
        \coordinate (C-\boxid) at (C-\theqworldnumeros);
    \fi
    \ifnum\numPortsS>0
        \foreach \qwindex in {1,...,\numPortsS}
        {
            \coordinate (s-\qwindex-\theqworldnumeros) at ([shift={({\fpeval{(0.5*\qwireinterval)+((\qwindex-1)*\smargin)+\qworldwireScorrection}},{\fpeval{\qwfillrate}})}]\boxat);
            \coordinate (S-\qwindex-\theqworldnumeros) at (SW-\theqworldnumeros -| s-\qwindex-\theqworldnumeros);
            \ifx\boxid\empty
            \else
                \coordinate (s-\qwindex-\boxid) at (s-\qwindex-\theqworldnumeros);
                \coordinate (S-\qwindex-\boxid) at (S-\qwindex-\theqworldnumeros);
            \fi
        }
    \else
        \coordinate (s-1-\theqworldnumeros) at ([shift={({\fpeval{(0.5*\qwireinterval)}},{\fpeval{\qwfillrate}})}]\boxat);
        \coordinate (S-1-\theqworldnumeros) at (SW-\theqworldnumeros -| s-1-\theqworldnumeros);
        \ifx\boxid\empty
        \else
            \coordinate (s-1-\boxid) at (s-1-\theqworldnumeros);
            \coordinate (S-1-\boxid) at (S-1-\theqworldnumeros);
        \fi
    \fi
    \ifnum\numPortsN>0
        \foreach \qwindex in {1,...,\numPortsN}
        {
            \coordinate (n-\qwindex-\theqworldnumeros) at ([shift={({\fpeval{(0.5*\qwireinterval)+((\qwindex-1)*\nmargin)+\qworldwireNcorrection}},{\fpeval{\boxverlen-\qwfillrate}})}]\boxat);
            \coordinate (N-\qwindex-\theqworldnumeros) at (NW-\theqworldnumeros -| n-\qwindex-\theqworldnumeros);
            \ifx\boxid\empty
            \else
                \coordinate (n-\qwindex-\boxid) at (n-\qwindex-\theqworldnumeros);
                \coordinate (N-\qwindex-\boxid) at (N-\qwindex-\theqworldnumeros);
            \fi
        }
    \else
        \coordinate (n-1-\theqworldnumeros) at ([shift={({\fpeval{0.5*\qwireinterval}},{\fpeval{\boxverlen-\qwfillrate}})}]\boxat);
        \coordinate (N-1-\theqworldnumeros) at (NW-\theqworldnumeros -| n-1-\theqworldnumeros);
        \ifx\boxid\empty
        \else
            \coordinate (n-1-\boxid) at (n-1-\theqworldnumeros);
            \coordinate (N-1-\boxid) at (N-1-\theqworldnumeros);
        \fi
    \fi
    \coordinate (n-\theqworldnumeros) at (N-\theqworldnumeros |- n-1-\theqworldnumeros);
    \coordinate (s-\theqworldnumeros) at (S-\theqworldnumeros |- s-1-\theqworldnumeros);
    \ifx\boxid\empty
    \else
        \coordinate (n-\boxid) at (n-\theqworldnumeros);
        \coordinate (s-\boxid) at (s-\theqworldnumeros);
    \fi
    \setcounter{portStempnum}{0}
    \ifnum\cardportsS>0
        \foreach\qwindex in \PortsS
        {
            \stepcounter{portStempnum}
            \draw[\qwthickness, \domarrowtype] (S-\qwindex-\theqworldnumeros)--(s-\qwindex-\theqworldnumeros);
            \coordinate (i-\theportStempnum-\theqworldnumeros) at (s-\qwindex-\theqworldnumeros);
            \coordinate (I-\theportStempnum-\theqworldnumeros) at (S-\qwindex-\theqworldnumeros);
            \ifx\boxid\empty
            \else
                \coordinate (i-\theportStempnum-\boxid) at (i-\theportStempnum-\theqworldnumeros);
                \coordinate (I-\theportStempnum-\boxid) at (I-\theportStempnum-\theqworldnumeros);
            \fi
        }
    \fi
    \setcounter{portNtempnum}{0}
    \ifnum\cardportsN>0
        \foreach\qwindex in \PortsN
        {
            \stepcounter{portNtempnum}
            \draw[\qwthickness, \codarrowtype] (n-\qwindex-\theqworldnumeros)--(N-\qwindex-\theqworldnumeros);
            \coordinate (o-\theportNtempnum-\theqworldnumeros) at (n-\qwindex-\theqworldnumeros);
            \coordinate (O-\theportNtempnum-\theqworldnumeros) at (N-\qwindex-\theqworldnumeros);
            \ifx\boxid\empty
            \else
                \coordinate (o-\theportNtempnum-\boxid) at (o-\theportNtempnum-\theqworldnumeros);
                \coordinate (O-\theportNtempnum-\boxid) at (O-\theportNtempnum-\theqworldnumeros);
            \fi
        }
    \fi
    \ifthenelse{\equal{\boxtype}{effect}}
    {
        \coordinate (sw-\theqworldnumeros) at ($(SW-\theqworldnumeros |- s-\theqworldnumeros)!0.1!(s-1-\theqworldnumeros)$);
        \coordinate (se-\theqworldnumeros) at ($(SE-\theqworldnumeros |- s-\theqworldnumeros)!0.1!(s-\numPortsS-\theqworldnumeros)$);
        \draw[{\qwthickness},draw = {\framecolor}, fill = {\boxcolor}] (se-\theqworldnumeros)--(sw-\theqworldnumeros)--(n-\theqworldnumeros)--cycle;
        \node[\boxslantstyle] at ([shift={(0,{\fpeval{-0.1*\boxverlen}})}]C-\theqworldnumeros) {{\scalebox{\fpeval{min(\boxhorilen-\qwireinterval+2*\qwfillratex,\boxverlen-2*\qwfillrate)}}{\color{\morphismcolor}\boxname}}};
    }{}
    \ifthenelse{\equal{\boxtype}{state}}
    {
        \coordinate (ne-\theqworldnumeros) at ($(n-\numPortsN-\theqworldnumeros -| NE-\theqworldnumeros)!0.1!(n-\numPortsN-\theqworldnumeros)$);
        \coordinate (nw-\theqworldnumeros) at ($(n-\theqworldnumeros -| NW-\theqworldnumeros)!0.1!(n-1-\theqworldnumeros)$);
        \draw[{\qwthickness}, draw = {\framecolor}, fill = {\boxcolor}] (ne-\theqworldnumeros)--(nw-\theqworldnumeros)--(s-\theqworldnumeros)--cycle;
        \node[\boxslantstyle] at ([shift={(0,{\fpeval{0.1*\boxverlen}})}]C-\theqworldnumeros) [anchor=center] {{\scalebox{\fpeval{min(\boxhorilen-\qwireinterval+2*\qwfillratex,\boxverlen-2*\qwfillrate)}}{\color{\morphismcolor}\boxname}}};
    }{}
    \ifthenelse{\equal{\boxtype}{scalar}}
    {
        \def\xradius{\fpeval{0.45*\boxhorilen}}
        \def\yradius{\fpeval{0.5*\boxverlen-\qwfillrate}}
        \def\radis{\fpeval{min(\xradius,\yradius)}}    
        \node[draw={\framecolor}, {\qwthickness}, fill={\boxcolor},
              ellipse, minimum width={\fpeval{2*\xradius}cm}, minimum height={\fpeval{2*\yradius}cm}
              \boxslantstyle, transform shape] 
              (SCALAR-\theqworldnumeros) at (C-\theqworldnumeros) {{\scalebox{\fpeval{min(\boxhorilen-\qwireinterval+2*\qwfillratex,\boxverlen-2*\qwfillrate)}}{\color{\morphismcolor}\boxname}}};
    }{}
    \def\ner{0}
    \def\nwr{0}
    \def\ser{0}
    \def\swr{0}
    \ifthenelse{\equal{\boxtype}{asym}}
    {
        \def\ner{\fpeval{-0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\ser{\fpeval{0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\boxtype{box}
    }{}
    \ifthenelse{\equal{\boxtype}{adj}}
    {
        \def\ner{\fpeval{0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\ser{\fpeval{-0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\boxtype{box}
    }{}
    \ifthenelse{\equal{\boxtype}{transpose}}
    {
        \def\nwr{\fpeval{-0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\swr{\fpeval{0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\boxtype{box}
    }{}
    \ifthenelse{\equal{\boxtype}{conj}}
    {
        \def\nwr{\fpeval{0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\swr{\fpeval{-0.5*(0.5*\qwireinterval-\qwfillratex)}}
        \def\boxtype{box}
    }{}
    \ifthenelse{\equal{\boxtype}{box}}
    {
        \coordinate (nw-\theqworldnumeros) at ([shift={({0.5*\qwireinterval-\qwfillratex+\nwr},{-\qwfillrate})}]NW-\theqworldnumeros);
        \coordinate (ne-\theqworldnumeros) at ([shift={({-0.5*\qwireinterval+\qwfillratex+\ner},{-\qwfillrate})}]NE-\theqworldnumeros);
        \coordinate (se-\theqworldnumeros) at ([shift={({-0.5*\qwireinterval+\qwfillratex+\ser},{\qwfillrate})}]SE-\theqworldnumeros);
        \coordinate (sw-\theqworldnumeros) at ([shift={({0.5*\qwireinterval-\qwfillratex+\swr},{\qwfillrate})}]SW-\theqworldnumeros);
        \draw[draw = \framecolor, fill = \boxcolor,\qwthickness] (nw-\theqworldnumeros)--(ne-\theqworldnumeros)--(se-\theqworldnumeros)--(sw-\theqworldnumeros)--cycle;
        \ifx\boxid\empty
        \else
            \coordinate (nw-\boxid) at (nw-\theqworldnumeros);
            \coordinate (ne-\boxid) at (ne-\theqworldnumeros);
            \coordinate (se-\boxid) at (se-\theqworldnumeros);
            \coordinate (sw-\boxid) at (sw-\theqworldnumeros);
        \fi
        \node[\boxslantstyle] at (C-\theqworldnumeros) [anchor=center] {{\scalebox{\fpeval{min(\boxhorilen-1+2*\qwfillratex,\boxverlen-2*\qwfillrate)}}{\color{\morphismcolor}\boxname}}};
    }{}
    \def\qwwhitewidth{0.8pt}
    \ifthenelse{\equal{\boxtype}{braid}}
    {
        \foreach\x in {1,...,\braidbsc}
        {
            \def\i{\fpeval{round(\x+\braidfsc)}}
            \coordinate (tempA) at ($(o-\i-\theqworldnumeros)!{\fpeval{1-\braidsmoothness}}!(C-\theqworldnumeros)$);
            \coordinate (tempB) at ($(C-\theqworldnumeros)!{\fpeval{\braidsmoothness}}!(o-\x-\theqworldnumeros)$);
            \draw [\qwthickness,\framecolor,\rstrandtype] (i-\i-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\x-\theqworldnumeros);
        }
        \foreach\x in{1,...,\braidfsc}
        {
            \def\i{\fpeval{round(\x+\braidbsc)}}
            \coordinate (tempA) at ($(i-\x-\theqworldnumeros)!{\fpeval{1-\braidsmoothness}}!(C-\theqworldnumeros)$);
            \coordinate (tempB) at ($(C-\theqworldnumeros)!{\fpeval{\braidsmoothness}}!(o-\i-\theqworldnumeros)$);
            \draw [ultra thick, draw = white, double = white, double distance =\pgflinewidth] 
            (i-\x-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\i-\theqworldnumeros);
            \draw [\qwthickness, draw = white, double = \framecolor, double distance =\pgflinewidth,\lstrandtype] 
            (i-\x-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\i-\theqworldnumeros);
        }
    }{}
    \ifthenelse{\equal{\boxtype}{braidinv}}
    {
        \foreach\x in{1,...,\braidfsc}
        {
            \def\i{\fpeval{round(\x+\braidbsc)}}
            \coordinate (tempA) at ($(i-\x-\theqworldnumeros)!{\fpeval{1-\braidsmoothness}}!(C-\theqworldnumeros)$);
            \coordinate (tempB) at ($(C-\theqworldnumeros)!{\fpeval{\braidsmoothness}}!(o-\i-\theqworldnumeros)$);
            \draw [\qwthickness,\framecolor,\rstrandtype] (i-\x-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\i-\theqworldnumeros);
        }
        \foreach\x in {1,...,\braidbsc}
        {
            \def\i{\fpeval{round(\x+\braidfsc)}}
            \coordinate (tempA) at ($(i-\i-\theqworldnumeros)!{\fpeval{1-\braidsmoothness}}!(C-\theqworldnumeros)$);
            \coordinate (tempB) at ($(C-\theqworldnumeros)!{\fpeval{\braidsmoothness}}!(o-\x-\theqworldnumeros)$);
            \draw[ultra thick,draw = white, double = white, double distance = \pgflinewidth,\lstrandtype]
            (i-\i-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\x-\theqworldnumeros);
            \draw[\qwthickness,draw = white, double = \framecolor, double distance = \pgflinewidth,\lstrandtype]
            (i-\i-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\x-\theqworldnumeros);
        }
    }{}
    \ifthenelse{\equal{\boxtype}{sym}}
    {
        \foreach\x in {1,...,\braidbsc}
        {
            \def\i{\fpeval{round(\x+\braidfsc)}}
            \coordinate (tempA) at ($(i-\i-\theqworldnumeros)!{\fpeval{1-\braidsmoothness}}!(C-\theqworldnumeros)$);
            \coordinate (tempB) at ($(C-\theqworldnumeros)!{\fpeval{\braidsmoothness}}!(o-\x-\theqworldnumeros)$);
            \draw [\qwthickness,\framecolor,\rstrandtype] (i-\i-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\x-\theqworldnumeros);
        }
        \foreach\x in{1,...,\braidfsc}
        {
            \def\i{\fpeval{round(\x+\braidbsc)}}
            \coordinate (tempA) at ($(i-\x-\theqworldnumeros)!{\fpeval{1-\braidsmoothness}}!(C-\theqworldnumeros)$);
            \coordinate (tempB) at ($(C-\theqworldnumeros)!{\fpeval{\braidsmoothness}}!(o-\i-\theqworldnumeros)$);
            \draw [\qwthickness,\framecolor,\lstrandtype] (i-\x-\theqworldnumeros) ..controls (tempA |- C-\theqworldnumeros) and (tempB |- C-\theqworldnumeros)..(o-\i-\theqworldnumeros);
        }
    }{}
    \ifthenelse{\equal{\boxtype}{cap}}
    {
        \foreach \qwi in {1,...,\cardportsS}{
            \def\qwj{\fpeval{\cardportsS-\qwi+1}}
            \ifnum\qwi<\qwj
                \draw[midarrow,\qwthickness,\framecolor]
                    (i-\qwi-\theqworldnumeros) to [out={\dualangle},in={\fpeval{180-\dualangle}}] (i-\qwj-\theqworldnumeros);
            \fi
        }
    }{}
    \ifthenelse{\equal{\boxtype}{cup}}
    {
        \foreach \qwi in {1,...,\cardportsN}{
            \def\qwj{\fpeval{\cardportsN-\qwi+1}}
            \ifnum\qwi<\qwj
                \draw[midarrow,\qwthickness,\framecolor]
                    (o-\qwi-\theqworldnumeros) to [out={\fpeval{-1*\dualangle}},in={\fpeval{\dualangle-180}}] (o-\qwj-\theqworldnumeros);
            \fi
        }
    }{}
    \ifthenelse{\equal{\boxtype}{caprev}}
    {
        \foreach \qwi in {1,...,\cardportsS}{
            \def\qwj{\fpeval{\cardportsS-\qwi+1}}
            \ifnum\qwi<\qwj
                \draw[midarrowinv,\qwthickness,\framecolor]
                    (i-\qwi-\theqworldnumeros) to [out={\dualangle},in={\fpeval{180-\dualangle}}] (i-\qwj-\theqworldnumeros);
            \fi
        }
    }{}
    \ifthenelse{\equal{\boxtype}{cuprev}}
    {
        \foreach \qwi in {1,...,\cardportsN}{
            \def\qwj{\fpeval{\cardportsN-\qwi+1}}
            \ifnum\qwi<\qwj
                \draw[midarrowinv,\qwthickness,\framecolor]
                    (o-\qwi-\theqworldnumeros) to [out={\fpeval{-1*\dualangle}},in={\fpeval{\dualangle-180}}] (o-\qwj-\theqworldnumeros);
            \fi
        }
    }{}
    \ifthenelse{\equal{\boxtype}{spider}}
    {
        \ifnum\cardportsS>0
            \foreach\x in\PortsS
            {
                \coordinate (tempD) at ($(C-\theqworldnumeros)!0.5!(s-\x-\theqworldnumeros)$);
                \coordinate (tempA) at ($(tempD)!0.5!(C-\theqworldnumeros)$);
                \coordinate (tempB) at ($(tempD)!0.5!(s-\x-\theqworldnumeros)$);
                \coordinate (tempG) at (tempA -| tempD);
                \coordinate (tempH) at (tempD -| tempB);
                \draw [\qwthickness,\domarrowtype] (s-\x-\theqworldnumeros) ..controls (tempH) and (tempG) .. (C-\theqworldnumeros);
            }
        \fi
        \ifnum\cardportsN>0
            \foreach\x in\PortsN
            {
                \coordinate (tempD) at ($(C-\theqworldnumeros)!0.5!(n-\x-\theqworldnumeros)$);
                \coordinate (tempA) at ($(tempD)!0.5!(C-\theqworldnumeros)$);
                \coordinate (tempB) at ($(tempD)!0.5!(n-\x-\theqworldnumeros)$);
                \coordinate (tempG) at (tempA -| tempD);
                \coordinate (tempH) at (tempD -| tempB);
                \draw [\qwthickness,\codarrowtype] (C-\theqworldnumeros) ..controls (tempG) and (tempH) .. (n-\x-\theqworldnumeros);
                  %\draw [\qwthickness,\codarrowtype] (C-\theqworldnumeros) ..controls ([shift={(\qwvelocityx,-\qwspideradjust*\qwvelocityy)}]C-\theqworldnumeros) and ([shift={(\qwvelocityx,-\qwspideradjust*\qwvelocityy)}]n-\x-\theqworldnumeros) .. (n-\x-\theqworldnumeros);
            }
        \fi
        \ifx\boxname\empty
            \draw[\qwthickness,draw=\framecolor,fill=\boxcolor] (C-\theqworldnumeros) circle [x radius={0.1},y radius={0.1}];
        \else
            % \draw[\qwthickness,draw=\framecolor,fill=\boxcolor] (C-\theqworldnumeros) circle [x radius={0.4},y radius={0.4}];
            % \node[\boxslantstyle] at (C-\theqworldnumeros) [anchor=center]{{\scalebox{\fpeval{min(0.9*\boxhorilen,1-2*\qwfillrate)}}{\color{\morphismcolor}\boxname}}};
            \def\xradius{\fpeval{0.4*\boxhorilen}}
            \def\yradius{\fpeval{0.4*\boxverlen-\qwfillrate}}
            \def\radis{\fpeval{min(\xradius,\yradius)}}    
            \node[draw={\framecolor}, {\qwthickness}, fill={\boxcolor},
                  ellipse, minimum width={\fpeval{\xradius}cm}, minimum height={\fpeval{\yradius}cm}
                  \boxslantstyle, transform shape] 
                  (SCALAR-\theqworldnumeros) at (C-\theqworldnumeros) {{\scalebox{\fpeval{0.6*min(\boxhorilen-\qwireinterval+2*\qwfillratex,\boxverlen-2*\qwfillrate)}}{\color{\morphismcolor}\boxname}}};
        \fi
    }{}
    \ifthenelse{\equal{\boxtype}{unitobject}}
    {
        \draw[draw = {\framecolor}, fill = {\boxcolor}, opacity=0] (NE-\theqworldnumeros)--(NW-\theqworldnumeros)--(SW-\theqworldnumeros)--(SE-\theqworldnumeros)--cycle;
    }{}
    \ifthenelse{\equal{\boxtype}{rpcurl}}
    {
        \foreach \qwindex in {1,...,\cardportsN}{
            \coordinate (temptempE) at ([shift={({\fpeval{-0.3*\smargin*(\qwindex-1)}},0)}]E-\theqworldnumeros);
            \draw[\qwthickness] (temptempE) to [in=-\dualangle, out=\dualangle-180] (n-\qwindex-\theqworldnumeros);
        }
        \foreach \qwindex in {1,...,\cardportsS}{
            \coordinate (temptempE) at ([shift={({\fpeval{-0.3*\smargin*(\qwindex-1)}},0)}]E-\theqworldnumeros);
            \draw[ultra thick,draw = white, double = white, double distance = \pgflinewidth] (s-\qwindex-\theqworldnumeros) to [out=\dualangle, in=180-\dualangle] (temptempE);
            \draw[\qwthickness,draw = white, double = \framecolor, double distance = \pgflinewidth] (s-\qwindex-\theqworldnumeros) to [out=\dualangle, in=180-\dualangle] (temptempE);
        }
    }{}

    \coordinate (qworldat) at (SE-\theqworldnumeros);
    \ifnum\internalinvocation=0
        \stepcounter{qworldnumeros}
    \fi
}

\pgfkeys{
    /qworld/dom/.cd,
    id/.store in=\domid,
    label/.store in=\domlabel,
    num/.store in=\domnum,
    at/.store in=\domat,
    type/.store in=\domtype,
    id/.default={1},
    label/.default={},
    num/.default={1},
    at/.default={below},
    type/.default={I}
}
\NewDocumentCommand{\qwdom}{O{}}
{
    \pgfkeys{
        /qworld/dom/.cd,
        id={1},
        label={},
        num={1},
        at={below},
        type={I},
        #1
    }
    \node[\domat] at (\domtype-\domnum-\domid) {\domlabel};
}
\pgfkeys{
    /qworld/cod/.cd,
    id/.store in=\codid,
    label/.store in=\codlabel,
    num/.store in=\codnum,
    at/.store in=\codat,
    type/.store in=\codtype,
    id/.default={1},
    label/.default={},
    num/.default={1},
    at/.default={above},
    type/.default={O}
}
\NewDocumentCommand{\qwcod}{O{}}
{
    \pgfkeys{
        /qworld/cod/.cd,
        id={1},
        label={},
        num={1},
        at={above},
        type={O},
        #1
    }
    \node[\codat] at (\codtype-\codnum-\codid) {\codlabel};
}

\NewDocumentCommand{\qwI}{O{1} m O{below}}
{
    \addtocounter{qworldnumeros}{-1}%
    \qwforkeysanalyst{#3}
    \node[below,\atinfo] at (I-#1-\qwforkeysid) {#2};%
    \stepcounter{qworldnumeros}
}

\NewDocumentCommand{\qws}{O{1} m O{below}}
{
    \addtocounter{qworldnumeros}{-1}%
    \qwforkeysanalyst{#3}
    \node[below,\atinfo] at (s-#1-\qwforkeysid) {#2};%
    \stepcounter{qworldnumeros}}

\NewDocumentCommand{\qwS}{O{1} m O{below}}
{
    \addtocounter{qworldnumeros}{-1}%
    \qwforkeysanalyst{#3}
    \node[below,\atinfo] at (S-#1-\qwforkeysid) {#2};%
    \stepcounter{qworldnumeros}}

\NewDocumentCommand{\qwO}{O{1} m O{above}}
{
    \addtocounter{qworldnumeros}{-1}%
    \qwforkeysanalyst{#3}
    \node[above,\atinfo] at (O-#1-\qwforkeysid) {#2};%
    \stepcounter{qworldnumeros}
}

\NewDocumentCommand{\qwn}{O{1} m O{above}}
{
    \addtocounter{qworldnumeros}{-1}%
    \qwforkeysanalyst{#3}
    \node[above,\atinfo] at (n-#1-\qwforkeysid) {#2};%
    \stepcounter{qworldnumeros}
}

\NewDocumentCommand{\qwN}{O{1} m O{above}}
{
    \addtocounter{qworldnumeros}{-1}%
    \qwforkeysanalyst{#3}
    \node[above,\atinfo] at (N-#1-\qwforkeysid) {#2};%
    \stepcounter{qworldnumeros}
}

\newcommand{\qwunitobject}[1][at={qworldat}]{
    \qwboxprocess[unitobject]{#1, P={}}
}
\pgfkeys{
    /qworld/forkeys/.cd,
    id/.store in=\forkeysid,
    id/.default=0
}

\newcommand{\qwforkeysanalyst}[1]
{
    \def\atinfo{}
    \pgfkeys{
        /qworld/forkeys/.cd, 
        id=\theqworldnumeros
    }
    \def\qwforkeysid{\forkeysid}
    \foreach \kvornot in {#1}
    {
        \IfSubStr{\kvornot}{=}
        {
            \StrBehind{\kvornot}{id=}[\result]%
            \pgfkeys{
                /qworld/forkeys/.cd,id=\result
            }
            \xdef\qwforkeysid{\forkeysid}
        }{
            \xdef\atinfo{\kvornot}
        }
    }
}
\newcommand{\extractid}[1]{%
    \StrBehind{#1}{id=}[\result]%
}
