\documentclass[tikz,border=5pt]{standalone}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{inputenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,decorations.pathmorphing,arrows.meta,calc,fit,quotes,cd,positioning,backgrounds}
\tikzcdset{
arrow style=tikz,
diagrams={>={Classical TikZ Rightarrow[angle=63:4pt, line width=.6pt]}},
arrow={semithick}
}
\tikzset{
tick/.style={postaction={
decorate,
decoration={markings, mark=at position 0.5 with {\draw[-] (0,.4ex) -- (0,-.4ex);}}}}
}
}
\tikzset{
dom/.style={append after command={coordinate[alias=dom#1]}},
domA/.style={dom=A}, domB/.style={dom=B},
domC/.style={dom=C}, domD/.style={dom=D},
domE/.style={dom=E}, domF/.style={dom=F},
cod/.style={append after command={coordinate[alias=cod#1]}},
codA/.style={cod=A}, codB/.style={cod=B},
codC/.style={cod=C}, codD/.style={cod=D},
codE/.style={cod=E}, codF/.style={cod=F}
}
\tikzset{
wiring diagram/.style={
every to/.style={out=0,in=180,draw},
label/.style={
font=\everymath\expandafter{\the\everymath\scriptstyle},
inner sep=0pt,
node distance=2pt and -2pt},
semithick,
node distance=1 and 1,
decoration={markings, mark=at position .5 with {\arrow{stealth};}},
ar/.style={postaction={decorate}},
execute at begin picture={\tikzset{
x=\bbx, y=\bby,
every fit/.style={inner xsep=\bbx, inner ysep=\bby}}}
},
bbx/.store in=\bbx,
bbx = 1.5cm,
bby/.store in=\bby,
bby = 1.75ex,
bb port sep/.store in=\bbportsep,
bb port sep=2,
bb port length/.store in=\bbportlen,
bb port length=4pt,
bb min width/.store in=\bbminwidth,
bb min width=1cm,
bb rounded corners/.store in=\bbcorners,
bb rounded corners=2pt,
bb small/.style={bb port sep=1, bb port length=2.5pt, bbx=.4cm, bb min width=.4cm, bby=.7ex},
bb/.code 2 args={
\pgfmathsetlengthmacro{\bbheight}{\bbportsep * (max(#1,#2)+1) * \bby}
\pgfkeysalso{draw,minimum height=\bbheight,minimum width=\bbminwidth,outer sep=0pt,
rounded corners=\bbcorners,thick,
prefix after command={\pgfextra{\let\fixname\tikzlastnode}},
append after command={\pgfextra{\draw
\ifnum #1=0{} \else foreach \i in {1,...,#1} {
($(\fixname.north west)!{\i/(#1+1)}!(\fixname.south west)$) +(-\bbportlen,0) coordinate (\fixname_in\i) -- +(\bbportlen,0) coordinate (\fixname_in\i')}\fi
\ifnum #2=0{} \else foreach \i in {1,...,#2} {
($(\fixname.north east)!{\i/(#2+1)}!(\fixname.south east)$) +(-\bbportlen,0) coordinate (\fixname_out\i') -- +(\bbportlen,0) coordinate (\fixname_out\i)}\fi;
}}}
},
bb name/.style={append after command={\pgfextra{\node[anchor=north] at (\fixname.north) {#1};}}}
}
\tikzset{
string diagram/.style={
every to/.style={out=0,in=180,draw},
semithick,
ar/.style={postaction={
decorate,
decoration={
markings,
mark=at position ##1 with {\arrow{stealth};}
}
}},
ar/.default=.5,
every matrix/.style={
column sep={1.2cm,between origins},
row sep={1cm,between origins},
every rectangle node/.style={minimum size=.6cm},
every circle node/.style={minimum size=.66cm},
nodes={
inner sep=0pt,
fill=white,
execute at begin node=$,
execute at end node=$
}
},
every edge quotes/.style={
auto,
inner sep=2pt,
shape=circle,
execute at begin node=$\scriptstyle,
execute at end node=$
},
over/.style={preaction={draw,line width=2.4pt,white}},
every fit/.style={inner sep=.15cm,fill=white!80!black},
caption/.style={
at=(current bounding box.north),
above=.3cm,
inner sep=2pt,
append after command={\pgfextra{
\draw[line width=.03em] (\tikzlastnode.south west) -- (\tikzlastnode.south east);
}}
},
baseline=(current bounding box)
}
}
\begin{document}
\begin{tikzpicture}[wiring diagram,baseline]
\label{dia:string_diagram}
\node[bb={2}{1}, bb name=$X_1$] (X1) {};
\node[bb={2}{2}, right=of X1, bb name=$X_2$] (X2) {};
\node[bb={2}{1}, dashed, fit={(X1) (X2) ($(X1.north)+(0,1.5)$) ($(X1.south)-(0,1)$)},
bb name=$Y$] (Y) {};
\draw[label]
node[above left=of Y_in1] {$a$}
node[above left=of Y_in2] {$b$}
node[above right=of Y_out1] {$c$}
node[above left=of X1_in1] {$1a$}
node[above left=of X1_in2] {$1b$}
node[above right=of X1_out1] {$1c$}
node[above left=of X2_in1] {$2a$}
node[above left=of X2_in2] {$2b$}
node[above right=of X2_out1] {$2c$}
node[above right=of X2_out2] {$2d$};
\draw[ar] (Y_in2') to (X1_in1);
\draw[ar] (X1_out1) to (X2_in2);
\draw[ar] (X2_out1) to (Y_out1');
\draw[ar] let \p1=(X1.north west), \p2=(X1.north east), \n1={\y1+\bby}, \n2=\bbportlen in
(Y_in1') to (\x1-\n2,\n1) -- (\x2+\n2,\n1) to (X2_in1);
\draw[ar] let \p1=(X2.south east), \p2=(X1.south west), \n1={\y1-\bby}, \n2=\bbportlen in
(X2_out2) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (X1_in2);
\end{tikzpicture}
\end{document}