[Tex/LaTex] Penrose graphical notation with TikZ

tikz-pgf

How can I draw a tensor network like this using TikZ?
enter image description here

Perhaps adding some words on them.

enter image description here

I'm very comfortable with LaTeX, but still a noob to TikZ. I will draw these kinds of shape extensively, and hope to know a template on them. Thanks!

EDIT

This is what I've done so far. I'm not sure how to connect line at specified border, the correct way of placing node (relative position or coordinate?), and how to put tokens at the specified positions.

\documentclass[tikz,multi,border=10pt]{standalone}
\usetikzlibrary{shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}
[
    triangle/.style = {regular polygon, regular polygon sides=3, draw=black, fill=green!60!black, inner sep=0pt, minimum size=2cm},
    border rotated/.style = {shape border rotate=180},
    rectangular/.style={fill=brown!80!black, rectangle, rounded corners = 5pt, draw=black, inner sep=0pt, minimum width=4.5cm, minimum height=1cm},
    square/.style={fill=blue!60!black, rectangle, draw=black, inner sep=0pt, minimum size = 1cm}
]

    \node[square] (1) {};
    \node[triangle, border rotated] (2) [below left = 1cm  of 1] {};
    \node[triangle, border rotated] (3) [below right = 1cm  of 1] {};
    \node[rectangular] (4) [below=3cm of 1] {};
    \node[square] (7) [below = 7cm of 1]{};
    \node[triangle] (5) [above left = 1cm  of 7] {};
    \node[triangle] (6) [above right = 1cm  of 7]{};

    \draw (2.35) -- +(0,0.2) -- (1);
    \draw (2.145) to [bend right=45] (5.215);
    \draw (3.145) -- +(0,0.2) -- (1);

\end{tikzpicture}
\end{document}

Best Answer

I'm not entirely certain I understand all of the 3 points you've asked about. For example, I'm not sure what 'tokens' refers to, though I'm guessing the filled black circles.

Generally, relative positioning makes it easier to modify code later. For example, it is easier to add new things into the diagram and have other things auto-adjust. But it is really a question of what works best in a particular case. Often, absolute positioning is quicker to do for a one-off, for example, although it makes the code less flexible.

I've renamed triangle to triangular to avoid overwriting the triangle shape.

The code below shows one way to:

  • connect the triangles from the appropriate points;
  • connect the triangles to the rectangle from/to appropriate points;
  • add black circles to the rectangle;
  • add a curved arrow with the label s'.

Hopefully, this should enable you to build further on what you have already.

\documentclass[tikz,multi,border=10pt]{standalone}
\usetikzlibrary{shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}
[
    triangular/.style = {regular polygon, regular polygon sides=3, draw=black, fill=green!60!black, inner sep=0pt, minimum size=2cm},
    border rotated/.style = {shape border rotate=180},
    rectangular/.style={fill=brown!80!black, rectangle, rounded corners = 5pt, draw=black, inner sep=0pt, minimum width=4.5cm, minimum height=1cm},
    square/.style={fill=blue!60!black, rectangle, draw=black, inner sep=0pt, minimum size = 1cm}
]

    \node[square] (1) {};
    \node[triangular, border rotated] (2) [below left = 1cm  of 1] {};
    \node[triangular, border rotated] (3) [below right = 1cm  of 1] {};
    \node[rectangular] (4) [below=3cm of 1] {};
    \node[square] (7) [below = 7cm of 1]{};
    \node[triangular] (5) [above left = 1cm  of 7] {};
    \node[triangular] (6) [above right = 1cm  of 7]{};

    \draw (2.35) -- +(0,0.2) -- (1);
    \draw (2.145) .. controls +(-7.5mm,35mm) and +(-7.5mm,-35mm) .. (5.215);
    \draw (3.145) -- +(0,0.2) -- (1);
    \draw (5.-35) -- +(0,-0.2) -- (7);
    \draw (6.-145) -- +(0,-0.2) -- (7);
    \draw (3.35) .. controls +(7.5mm,35mm) and +(7.5mm,-35mm) .. (6.-35);
    \draw (2.south) -- (4.north -| 2.south) (3.south) -- (4.north -| 3.south) (4.south -| 5.north) -- (5.north) (4.south -| 6.north) -- (6.north);

    \path (2.south) -- node [fill, circle] {} (5.north);
    \path (3.south) -- node [fill, circle] {} (6.north);
    \draw [<-] (4.north west) [bend right] to ++(-10mm,10mm) node [anchor=south] {$s'$};

\end{tikzpicture}
\end{document}

connections