Skip to content

Instantly share code, notes, and snippets.

@philzook58
Created May 8, 2020 16:05
Show Gist options
  • Save philzook58/a522cb979546774532fc9b5f54debc67 to your computer and use it in GitHub Desktop.
Save philzook58/a522cb979546774532fc9b5f54debc67 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ZX calculus in Catlab"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Catlab"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Theory and symbolic expressions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a *generalized algebraic theory (GAT)* for the ZX calculus."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using Catlab.Doctrines\n",
"import Catlab.Doctrines: Ob, Hom, dom, codom, compose, ⋅, id, otimes, ⊗, munit, braid,\n",
" hom, ev, curry, dual, dunit, dcounit, dagger, mate\n",
"\n",
"\"\"\" ZX calculus, presented in biased style.\n",
"\"\"\"\n",
"@theory DaggerCompactCategory(Ob,Hom) => ZXCategory(Ob,Hom) begin\n",
" # Argument α is the phase, usually <: Real\n",
" zphase(A::Ob, α)::(A → A)\n",
" zcopy(A::Ob, α)::(A → (A⊗A))\n",
" zdelete(A::Ob, α)::(A → munit())\n",
" zmerge(A::Ob, α)::((A⊗A) → A)\n",
" zcreate(A::Ob, α)::(munit() → A)\n",
" \n",
" xphase(A::Ob, α)::(A → A)\n",
" xcopy(A::Ob, α)::(A → (A⊗A))\n",
" xdelete(A::Ob, α)::(A → munit())\n",
" xmerge(A::Ob, α)::((A⊗A) → A)\n",
" xcreate(A::Ob, α)::(munit() → A)\n",
" \n",
" hadamard(A::Ob)::(A → A)\n",
" \n",
" # Example axioms.\n",
" zphase(A,0) == id(A) ⊣ (A::Ob)\n",
" zcopy(A,α)⋅(zdelete(A,β) ⊗ id(A)) == zphase(A, α+β) ⊣ (A::Ob, α, β)\n",
" (zcreate(A,α) ⊗ id(A))⋅zmerge(A,β) == zphase(A, α+β) ⊣ (A::Ob, α, β)\n",
"end\n",
"\n",
"# Convenience methods for phaseless spiders.\n",
"zcopy(A) = zcopy(A,0)\n",
"zdelete(A) = zdelete(A,0)\n",
"zmerge(A) = zmerge(A,0)\n",
"zcreate(A) = zcreate(A,0)\n",
"\n",
"xcopy(A) = xcopy(A,0)\n",
"xdelete(A) = xdelete(A,0)\n",
"xmerge(A) = xmerge(A,0)\n",
"xcreate(A) = xcreate(A,0);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Symbolic expressions, with simplification for associativity and unitality laws of monoidal categeory."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Main.ZXCalculus"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@syntax ZXCalculus(ObExpr,HomExpr) ZXCategory begin\n",
" otimes(A::Ob, B::Ob) = associate_unit(new(A,B), munit)\n",
" otimes(f::Hom, g::Hom) = associate(new(f,g))\n",
" compose(f::Hom, g::Hom) = associate_unit(new(f,g; strict=true), identity)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pretty-printing symbolic expressions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import Catlab: show_latex\n",
"\n",
"latex_spider(io, expr, color, head) = Syntax.show_latex_script(io, expr, \"{\\\\color{$color} $head}\")\n",
"\n",
"const zcolor = \"green\"\n",
"const xcolor = \"red\"\n",
"const hcolor = \"yellow\"\n",
"\n",
"show_latex(io::IO, expr::HomExpr{:zphase}; kw...) = latex_spider(io, expr, zcolor, \"\\\\bullet\")\n",
"show_latex(io::IO, expr::HomExpr{:zcopy}; kw...) = latex_spider(io, expr, zcolor, \"\\\\blacktriangle\")\n",
"show_latex(io::IO, expr::HomExpr{:zdelete}; kw...) = latex_spider(io, expr, zcolor, \"\\\\blacklozenge\")\n",
"show_latex(io::IO, expr::HomExpr{:zmerge}; kw...) = latex_spider(io, expr, zcolor, \"\\\\blacktriangledown\")\n",
"show_latex(io::IO, expr::HomExpr{:zcreate}; kw...) = latex_spider(io, expr, zcolor, \"\\\\blacksquare\")\n",
"\n",
"show_latex(io::IO, expr::HomExpr{:xphase}; kw...) = latex_spider(io, expr, xcolor, \"\\\\bullet\")\n",
"show_latex(io::IO, expr::HomExpr{:xcopy}; kw...) = latex_spider(io, expr, xcolor, \"\\\\blacktriangle\")\n",
"show_latex(io::IO, expr::HomExpr{:xdelete}; kw...) = latex_spider(io, expr, xcolor, \"\\\\blacklozenge\")\n",
"show_latex(io::IO, expr::HomExpr{:xmerge}; kw...) = latex_spider(io, expr, xcolor, \"\\\\blacktriangledown\")\n",
"show_latex(io::IO, expr::HomExpr{:xcreate}; kw...) = latex_spider(io, expr, xcolor, \"\\\\blacksquare\")\n",
"\n",
"show_latex(io::IO, expr::HomExpr{:hadamard}; kw...) = Syntax.show_latex_script(io, expr, \"H\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hadamard gate:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$H_{A} : A \\to A$"
],
"text/plain": [
"hadamard(A)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = Ob(ZXCalculus, :A)\n",
"hadamard(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"CNOT (controlled NOT) gate in ZX calculus:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\left({\\color{green} \\blacktriangle}_{A,0} \\otimes \\mathrm{id}_{A}\\right) \\cdot \\left(\\mathrm{id}_{A} \\otimes {\\color{red} \\blacktriangledown}_{A,0}\\right) : A \\otimes A \\to A \\otimes A$"
],
"text/plain": [
"compose(otimes(zcopy(A,0),id(A)),otimes(id(A),xmerge(A,0)))"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cnot = (zcopy(A) ⊗ id(A))⋅(id(A) ⊗ xmerge(A))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another, equivalent CNOT gate:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\left(\\mathrm{id}_{A} \\otimes {\\color{red} \\blacktriangle}_{A,0}\\right) \\cdot \\left({\\color{green} \\blacktriangledown}_{A,0} \\otimes \\mathrm{id}_{A}\\right) : A \\otimes A \\to A \\otimes A$"
],
"text/plain": [
"compose(otimes(id(A),xcopy(A,0)),otimes(zmerge(A,0),id(A)))"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cnot_alt = (id(A) ⊗ xcopy(A))⋅(zmerge(A) ⊗ id(A))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Wiring diagrams"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"using Catlab.WiringDiagrams\n",
"\n",
"# Dagger compact structure.\n",
"dunit(A::Ports{ZXCategory.Hom}) = junction_caps(A)\n",
"dcounit(A::Ports{ZXCategory.Hom}) = junction_cups(A)\n",
"dagger(f::WiringDiagram{ZXCategory.Hom}) = functor(f, identity, dagger, contravariant=true)\n",
"\n",
"struct PhasedValue\n",
" value::Any\n",
" phase::Any\n",
"end\n",
"\n",
"function phased_junction_diagram(A::Ports, phase, nin::Int, nout::Int; op=nothing)\n",
" junction_diagram(A, nin, nout) do port, inputs, outputs\n",
" Junction{op}(PhasedValue(port, phase), inputs, outputs)\n",
" end\n",
"end\n",
"\n",
"zphase(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 1, 1, op=:Z)\n",
"zcopy(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 1, 2, op=:Z)\n",
"zdelete(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 1, 0, op=:Z)\n",
"zmerge(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 2, 1, op=:Z)\n",
"zcreate(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 0, 1, op=:Z)\n",
"\n",
"xphase(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 1, 1, op=:X)\n",
"xcopy(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 1, 2, op=:X)\n",
"xdelete(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 1, 0, op=:X)\n",
"xmerge(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 2, 1, op=:X)\n",
"xcreate(A::Ports{ZXCategory.Hom}, α) = phased_junction_diagram(A, α, 0, 1, op=:X)\n",
"\n",
"hadamard(A::Ports{ZXCategory.Hom}) = junction_diagram(A, 1, 1, op=:H);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Converting expressions to wiring diagrams"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"WiringDiagram{Main.ZXCategory.Hom}([:A], [:A], \n",
"[ 1 => {inputs},\n",
" 2 => {outputs},\n",
" 3 => Junction{:H,Symbol}(:A, [:A], [:A]) ],\n",
"[ Wire((1,1) => (3,1)),\n",
" Wire((3,1) => (2,1)) ])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_wiring_diagram(hadamard(A))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"WiringDiagram{Main.ZXCategory.Hom}([:A,:A], [:A,:A], \n",
"[ 1 => {inputs},\n",
" 2 => {outputs},\n",
" 3 => Junction{:Z,PhasedValue}(PhasedValue(:A, 0), [:A], [:A, :A]),\n",
" 4 => Junction{:X,PhasedValue}(PhasedValue(:A, 0), [:A, :A], [:A]) ],\n",
"[ Wire((1,1) => (3,1)),\n",
" Wire((1,2) => (4,2)),\n",
" Wire((3,1) => (2,1)),\n",
" Wire((3,2) => (4,1)),\n",
" Wire((4,1) => (2,2)) ])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_wiring_diagram(cnot)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Graphics"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"graphviz_box (generic function with 5 methods)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Catlab.Graphics\n",
"import Catlab.Graphics.GraphvizWiringDiagrams: graphviz_box, graphviz_junction\n",
"\n",
"node_label(phased::PhasedValue) = phased.phase == 0 ? \"\" : string(phased.phase)\n",
"\n",
"graphviz_box(junction::Junction{:Z}, node_id; kw...) = graphviz_junction(junction, node_id;\n",
" label=node_label(junction.value), shape=\"circle\", style=\"filled\", fillcolor=zcolor,\n",
" width=\"0.15\", height=\"0.15\")\n",
"graphviz_box(junction::Junction{:X}, node_id; kw...) = graphviz_junction(junction, node_id;\n",
" label=node_label(junction.value), shape=\"circle\", style=\"filled\", fillcolor=xcolor,\n",
" width=\"0.15\", height=\"0.15\")\n",
"graphviz_box(junction::Junction{:H}, node_id; kw...) = graphviz_junction(junction, node_id;\n",
" label=\"\", shape=\"rect\", style=\"filled\", fillcolor=hcolor, width=\"0.15\", height=\"0.15\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualizing wiring diagrams using Graphviz"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"93pt\" height=\"32pt\"\n",
" viewBox=\"0.00 0.00 93.00 32.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 28)\">\n",
"<title>G</title>\n",
"<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-28 89,-28 89,4 -4,4\"/>\n",
"<!-- n1p1 -->\n",
"<!-- n3 -->\n",
"<g id=\"n3\" class=\"node\">\n",
"<title>n3</title>\n",
"<polygon fill=\"#ffff00\" stroke=\"#000000\" points=\"48,-17.5 37,-17.5 37,-6.5 48,-6.5 48,-17.5\"/>\n",
"</g>\n",
"<!-- n1p1&#45;&gt;n3 -->\n",
"<!-- A -->\n",
"<g id=\"e1\" class=\"edge\">\n",
"<title>n1p1:e&#45;&gt;n3</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-12C11.42,-12 23.3117,-12 31.6867,-12\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"31.8615,-13.7501 36.8615,-12 31.8615,-10.2501 31.8615,-13.7501\"/>\n",
"</g>\n",
"<!-- n2p1 -->\n",
"<!-- n3&#45;&gt;n2p1 -->\n",
"<!-- A -->\n",
"<g id=\"e2\" class=\"edge\">\n",
"<title>n3&#45;&gt;n2p1:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M48.1385,-12C55.0612,-12 67.3014,-12 78.794,-12\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"79,-13.7501 84,-12 79,-10.2501 79,-13.7501\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"Catlab.Graphics.Graphviz.Graph(\"G\", true, Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n1p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"source\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n2p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"sink\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Node(\"n3\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"yellow\",:height => \"0.15\",:id => \"n3\",:label => \"\",:shape => \"rect\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e2\"))], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:rankdir => \"LR\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:shape => \"none\",:width => \"0\",:height => \"0\",:margin => \"0\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:arrowsize => \"0.5\",:fontname => \"Serif\"))"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_graphviz(hadamard(A), orientation=LeftToRight)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"140pt\" height=\"74pt\"\n",
" viewBox=\"0.00 0.00 140.00 74.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 70)\">\n",
"<title>G</title>\n",
"<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-70 136,-70 136,4 -4,4\"/>\n",
"<!-- n1p1 -->\n",
"<!-- n1p2 -->\n",
"<!-- n1p1&#45;&gt;n1p2 -->\n",
"<!-- n3 -->\n",
"<g id=\"n3\" class=\"node\">\n",
"<title>n3</title>\n",
"<ellipse fill=\"#00ff00\" stroke=\"#000000\" cx=\"42.5\" cy=\"-54\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n1p1&#45;&gt;n3 -->\n",
"<!-- A -->\n",
"<g id=\"e1\" class=\"edge\">\n",
"<title>n1p1:e&#45;&gt;n3</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-54C11.42,-54 23.3117,-54 31.6867,-54\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"31.8615,-55.7501 36.8615,-54 31.8615,-52.2501 31.8615,-55.7501\"/>\n",
"</g>\n",
"<!-- n4 -->\n",
"<g id=\"n4\" class=\"node\">\n",
"<title>n4</title>\n",
"<ellipse fill=\"#ff0000\" stroke=\"#000000\" cx=\"89.5\" cy=\"-12\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n1p2&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e2\" class=\"edge\">\n",
"<title>n1p2:e&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-12C28.9155,-12 61.7841,-12 78.5142,-12\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"78.9087,-13.7501 83.9087,-12 78.9086,-10.2501 78.9087,-13.7501\"/>\n",
"</g>\n",
"<!-- n2p1 -->\n",
"<!-- n2p2 -->\n",
"<!-- n2p1&#45;&gt;n2p2 -->\n",
"<!-- n3&#45;&gt;n2p1 -->\n",
"<!-- A -->\n",
"<g id=\"e3\" class=\"edge\">\n",
"<title>n3&#45;&gt;n2p1:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M48.0913,-54C61.3039,-54 95.6157,-54 125.8065,-54\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"126,-55.7501 131,-54 126,-52.2501 126,-55.7501\"/>\n",
"</g>\n",
"<!-- n3&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e4\" class=\"edge\">\n",
"<title>n3&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M46.8374,-50.124C54.7117,-43.0875 71.4675,-28.1141 81.4771,-19.1694\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"82.7462,-20.3823 85.3084,-15.7457 80.414,-17.7725 82.7462,-20.3823\"/>\n",
"</g>\n",
"<!-- n4&#45;&gt;n2p2 -->\n",
"<!-- A -->\n",
"<g id=\"e5\" class=\"edge\">\n",
"<title>n4&#45;&gt;n2p2:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M95.1385,-12C102.0612,-12 114.3014,-12 125.794,-12\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"126,-13.7501 131,-12 126,-10.2501 126,-13.7501\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"Catlab.Graphics.Graphviz.Graph(\"G\", true, Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n1p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in1\")), Catlab.Graphics.Graphviz.Node(\"n1p2\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n1p2\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"source\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n2p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out1\")), Catlab.Graphics.Graphviz.Node(\"n2p2\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p2\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"sink\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Node(\"n3\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"green\",:height => \"0.15\",:id => \"n3\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n4\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"red\",:height => \"0.15\",:id => \"n4\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p2\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e3\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e4\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p2\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e5\"))], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:rankdir => \"LR\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:shape => \"none\",:width => \"0\",:height => \"0\",:margin => \"0\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:arrowsize => \"0.5\",:fontname => \"Serif\"))"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_graphviz(to_wiring_diagram(cnot), orientation=LeftToRight)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"140pt\" height=\"74pt\"\n",
" viewBox=\"0.00 0.00 140.00 74.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 70)\">\n",
"<title>G</title>\n",
"<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-70 136,-70 136,4 -4,4\"/>\n",
"<!-- n1p1 -->\n",
"<!-- n1p2 -->\n",
"<!-- n1p1&#45;&gt;n1p2 -->\n",
"<!-- n4 -->\n",
"<g id=\"n4\" class=\"node\">\n",
"<title>n4</title>\n",
"<ellipse fill=\"#00ff00\" stroke=\"#000000\" cx=\"89.5\" cy=\"-54\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n1p1&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e2\" class=\"edge\">\n",
"<title>n1p1:e&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-54C28.9155,-54 61.7841,-54 78.5142,-54\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"78.9087,-55.7501 83.9087,-54 78.9086,-52.2501 78.9087,-55.7501\"/>\n",
"</g>\n",
"<!-- n3 -->\n",
"<g id=\"n3\" class=\"node\">\n",
"<title>n3</title>\n",
"<ellipse fill=\"#ff0000\" stroke=\"#000000\" cx=\"42.5\" cy=\"-12\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n1p2&#45;&gt;n3 -->\n",
"<!-- A -->\n",
"<g id=\"e1\" class=\"edge\">\n",
"<title>n1p2:e&#45;&gt;n3</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-12C11.42,-12 23.3117,-12 31.6867,-12\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"31.8615,-13.7501 36.8615,-12 31.8615,-10.2501 31.8615,-13.7501\"/>\n",
"</g>\n",
"<!-- n2p1 -->\n",
"<!-- n2p2 -->\n",
"<!-- n2p1&#45;&gt;n2p2 -->\n",
"<!-- n3&#45;&gt;n2p2 -->\n",
"<!-- A -->\n",
"<g id=\"e3\" class=\"edge\">\n",
"<title>n3&#45;&gt;n2p2:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M48.0913,-12C61.3039,-12 95.6157,-12 125.8065,-12\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"126,-13.7501 131,-12 126,-10.2501 126,-13.7501\"/>\n",
"</g>\n",
"<!-- n3&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e4\" class=\"edge\">\n",
"<title>n3&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M46.8374,-15.876C54.7117,-22.9125 71.4675,-37.8859 81.4771,-46.8306\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"80.414,-48.2275 85.3084,-50.2543 82.7462,-45.6177 80.414,-48.2275\"/>\n",
"</g>\n",
"<!-- n4&#45;&gt;n2p1 -->\n",
"<!-- A -->\n",
"<g id=\"e5\" class=\"edge\">\n",
"<title>n4&#45;&gt;n2p1:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M95.1385,-54C102.0612,-54 114.3014,-54 125.794,-54\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"126,-55.7501 131,-54 126,-52.2501 126,-55.7501\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"Catlab.Graphics.Graphviz.Graph(\"G\", true, Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n1p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in1\")), Catlab.Graphics.Graphviz.Node(\"n1p2\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n1p2\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"source\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n2p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out1\")), Catlab.Graphics.Graphviz.Node(\"n2p2\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p2\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"sink\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Node(\"n3\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"red\",:height => \"0.15\",:id => \"n3\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n4\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"green\",:height => \"0.15\",:id => \"n4\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p2\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p2\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e3\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e4\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e5\"))], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:rankdir => \"LR\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:shape => \"none\",:width => \"0\",:height => \"0\",:margin => \"0\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:arrowsize => \"0.5\",:fontname => \"Serif\"))"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_graphviz(to_wiring_diagram(cnot_alt), orientation=LeftToRight)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"234pt\" height=\"75pt\"\n",
" viewBox=\"0.00 0.00 234.00 75.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 71)\">\n",
"<title>G</title>\n",
"<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-71 230,-71 230,4 -4,4\"/>\n",
"<!-- n1p1 -->\n",
"<!-- n1p2 -->\n",
"<!-- n1p1&#45;&gt;n1p2 -->\n",
"<!-- n3 -->\n",
"<g id=\"n3\" class=\"node\">\n",
"<title>n3</title>\n",
"<ellipse fill=\"#00ff00\" stroke=\"#000000\" cx=\"42.5\" cy=\"-53\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n1p1&#45;&gt;n3 -->\n",
"<!-- A -->\n",
"<g id=\"e1\" class=\"edge\">\n",
"<title>n1p1:e&#45;&gt;n3</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-54C11.4231,-54 23.3144,-53.6808 31.6884,-53.4031\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"31.9269,-55.1459 36.8626,-53.2219 31.8043,-51.6481 31.9269,-55.1459\"/>\n",
"</g>\n",
"<!-- n4 -->\n",
"<g id=\"n4\" class=\"node\">\n",
"<title>n4</title>\n",
"<ellipse fill=\"#ff0000\" stroke=\"#000000\" cx=\"89.5\" cy=\"-18\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n1p2&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e2\" class=\"edge\">\n",
"<title>n1p2:e&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-12C28.9796,-12 61.8213,-15.0222 78.5304,-16.7768\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"78.7587,-18.5615 83.9176,-17.3579 79.1341,-15.0817 78.7587,-18.5615\"/>\n",
"</g>\n",
"<!-- n2p1 -->\n",
"<!-- n2p2 -->\n",
"<!-- n2p1&#45;&gt;n2p2 -->\n",
"<!-- n3&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e3\" class=\"edge\">\n",
"<title>n3&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M47.1784,-49.5161C54.9908,-43.6983 70.8124,-31.9163 80.7426,-24.5215\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"81.8755,-25.8598 84.8406,-21.4698 79.7851,-23.0526 81.8755,-25.8598\"/>\n",
"</g>\n",
"<!-- n6 -->\n",
"<g id=\"n6\" class=\"node\">\n",
"<title>n6</title>\n",
"<ellipse fill=\"#00ff00\" stroke=\"#000000\" cx=\"183.5\" cy=\"-53\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n3&#45;&gt;n6 -->\n",
"<!-- A -->\n",
"<g id=\"e4\" class=\"edge\">\n",
"<title>n3&#45;&gt;n6</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M48.202,-53C69.4621,-53 144.3373,-53 172.5952,-53\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"172.8816,-54.7501 177.8815,-53 172.8815,-51.2501 172.8816,-54.7501\"/>\n",
"</g>\n",
"<!-- n5 -->\n",
"<g id=\"n5\" class=\"node\">\n",
"<title>n5</title>\n",
"<ellipse fill=\"#ff0000\" stroke=\"#000000\" cx=\"136.5\" cy=\"-18\" rx=\"5.5\" ry=\"5.5\"/>\n",
"</g>\n",
"<!-- n4&#45;&gt;n5 -->\n",
"<!-- A -->\n",
"<g id=\"e5\" class=\"edge\">\n",
"<title>n4&#45;&gt;n5</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M95.2631,-18C102.8857,-18 116.4607,-18 125.9458,-18\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"125.9819,-19.7501 130.9819,-18 125.9818,-16.2501 125.9819,-19.7501\"/>\n",
"</g>\n",
"<!-- n5&#45;&gt;n2p2 -->\n",
"<!-- A -->\n",
"<g id=\"e6\" class=\"edge\">\n",
"<title>n5&#45;&gt;n2p2:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M142.0852,-17.4649C155.2843,-16.2456 189.5689,-13.3416 219.799,-13.0277\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"220.0094,-14.7767 225,-13 219.9907,-11.2767 220.0094,-14.7767\"/>\n",
"</g>\n",
"<!-- n5&#45;&gt;n6 -->\n",
"<!-- A -->\n",
"<g id=\"e7\" class=\"edge\">\n",
"<title>n5&#45;&gt;n6</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M141.1784,-21.4839C148.9908,-27.3017 164.8124,-39.0837 174.7426,-46.4785\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"173.7851,-47.9474 178.8406,-49.5302 175.8755,-45.1402 173.7851,-47.9474\"/>\n",
"</g>\n",
"<!-- n6&#45;&gt;n2p1 -->\n",
"<!-- A -->\n",
"<g id=\"e8\" class=\"edge\">\n",
"<title>n6&#45;&gt;n2p1:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M189.1343,-53.4439C196.0528,-53.9548 208.288,-54.7498 219.7891,-54.952\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"219.9841,-56.7038 225,-55 220.0164,-53.2039 219.9841,-56.7038\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"Catlab.Graphics.Graphviz.Graph(\"G\", true, Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n1p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in1\")), Catlab.Graphics.Graphviz.Node(\"n1p2\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n1p2\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"source\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n2p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out1\")), Catlab.Graphics.Graphviz.Node(\"n2p2\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p2\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"sink\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Node(\"n3\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"green\",:height => \"0.15\",:id => \"n3\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n4\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"red\",:height => \"0.15\",:id => \"n4\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n5\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"red\",:height => \"0.15\",:id => \"n5\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n6\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"green\",:height => \"0.15\",:id => \"n6\",:label => \"\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p2\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e3\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n6\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e4\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n5\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e5\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n5\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p2\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e6\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n5\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n6\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e7\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n6\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e8\"))], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:rankdir => \"LR\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:shape => \"none\",:width => \"0\",:height => \"0\",:margin => \"0\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:arrowsize => \"0.5\",:fontname => \"Serif\"))"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_graphviz(to_wiring_diagram(cnot⋅cnot_alt), orientation=LeftToRight)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"${\\color{green} \\bullet}_{A,\"π/2\"} \\cdot {\\color{red} \\bullet}_{A,\"π/2\"} \\cdot {\\color{green} \\bullet}_{A,\"π/2\"} : A \\to A$"
],
"text/plain": [
"compose(zphase(A,π/2),xphase(A,π/2),zphase(A,π/2))"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"euler_decomp = zphase(A, \"π/2\") ⋅ xphase(A, \"π/2\") ⋅ zphase(A, \"π/2\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"235pt\" height=\"35pt\"\n",
" viewBox=\"0.00 0.00 234.61 34.87\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 30.8701)\">\n",
"<title>G</title>\n",
"<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-30.8701 230.6102,-30.8701 230.6102,4 -4,4\"/>\n",
"<!-- n1p1 -->\n",
"<!-- n3 -->\n",
"<g id=\"n3\" class=\"node\">\n",
"<title>n3</title>\n",
"<ellipse fill=\"#00ff00\" stroke=\"#000000\" cx=\"50.435\" cy=\"-13.435\" rx=\"13.3704\" ry=\"13.3704\"/>\n",
"<text text-anchor=\"middle\" x=\"50.435\" y=\"-9.735\" font-family=\"Serif\" font-size=\"14.00\" fill=\"#000000\">π/2</text>\n",
"</g>\n",
"<!-- n1p1&#45;&gt;n3 -->\n",
"<!-- A -->\n",
"<g id=\"e1\" class=\"edge\">\n",
"<title>n1p1:e&#45;&gt;n3</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M1,-13.435C11.0442,-13.435 22.2363,-13.435 31.4276,-13.435\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"31.5844,-15.1851 36.5844,-13.435 31.5843,-11.6851 31.5844,-15.1851\"/>\n",
"</g>\n",
"<!-- n2p1 -->\n",
"<!-- n4 -->\n",
"<g id=\"n4\" class=\"node\">\n",
"<title>n4</title>\n",
"<ellipse fill=\"#ff0000\" stroke=\"#000000\" cx=\"113.3051\" cy=\"-13.435\" rx=\"13.3704\" ry=\"13.3704\"/>\n",
"<text text-anchor=\"middle\" x=\"113.3051\" y=\"-9.735\" font-family=\"Serif\" font-size=\"14.00\" fill=\"#000000\">π/2</text>\n",
"</g>\n",
"<!-- n3&#45;&gt;n4 -->\n",
"<!-- A -->\n",
"<g id=\"e2\" class=\"edge\">\n",
"<title>n3&#45;&gt;n4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M64.0769,-13.435C72.9746,-13.435 84.7081,-13.435 94.488,-13.435\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"94.6899,-15.1851 99.6898,-13.435 94.6898,-11.6851 94.6899,-15.1851\"/>\n",
"</g>\n",
"<!-- n5 -->\n",
"<g id=\"n5\" class=\"node\">\n",
"<title>n5</title>\n",
"<ellipse fill=\"#00ff00\" stroke=\"#000000\" cx=\"176.1751\" cy=\"-13.435\" rx=\"13.3704\" ry=\"13.3704\"/>\n",
"<text text-anchor=\"middle\" x=\"176.1751\" y=\"-9.735\" font-family=\"Serif\" font-size=\"14.00\" fill=\"#000000\">π/2</text>\n",
"</g>\n",
"<!-- n4&#45;&gt;n5 -->\n",
"<!-- A -->\n",
"<g id=\"e3\" class=\"edge\">\n",
"<title>n4&#45;&gt;n5</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M126.947,-13.435C135.8446,-13.435 147.5782,-13.435 157.3581,-13.435\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"157.5599,-15.1851 162.5599,-13.435 157.5599,-11.6851 157.5599,-15.1851\"/>\n",
"</g>\n",
"<!-- n5&#45;&gt;n2p1 -->\n",
"<!-- A -->\n",
"<g id=\"e4\" class=\"edge\">\n",
"<title>n5&#45;&gt;n2p1:w</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M190.0258,-13.435C198.5428,-13.435 209.8275,-13.435 220.4587,-13.435\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"220.6102,-15.1851 225.6102,-13.435 220.6101,-11.6851 220.6102,-15.1851\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"Catlab.Graphics.Graphviz.Graph(\"G\", true, Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n1p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"in1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"source\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Subgraph(\"\", Catlab.Graphics.Graphviz.Statement[Catlab.Graphics.Graphviz.Node(\"n2p1\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:id => \"out1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}())], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:rank => \"sink\",:rankdir => \"TB\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\",:shape => \"none\",:label => \"\",:width => \"0\",:height => \"0.333\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:style => \"invis\")), Catlab.Graphics.Graphviz.Node(\"n3\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"green\",:height => \"0.15\",:id => \"n3\",:label => \"π/2\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n4\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"red\",:height => \"0.15\",:id => \"n4\",:label => \"π/2\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Node(\"n5\", OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fillcolor => \"green\",:height => \"0.15\",:id => \"n5\",:label => \"π/2\",:shape => \"circle\",:style => \"filled\",:width => \"0.15\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n1p1\", \"e\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e1\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n3\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e2\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n4\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n5\", \"\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e3\")), Catlab.Graphics.Graphviz.Edge(Catlab.Graphics.Graphviz.NodeID[Catlab.Graphics.Graphviz.NodeID(\"n5\", \"\", \"\"), Catlab.Graphics.Graphviz.NodeID(\"n2p1\", \"w\", \"\")], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:comment => \"A\",:id => \"e4\"))], OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:rankdir => \"LR\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:fontname => \"Serif\",:shape => \"none\",:width => \"0\",:height => \"0\",:margin => \"0\"), OrderedCollections.OrderedDict{Symbol,Union{String, Catlab.Graphics.Graphviz.Html}}(:arrowsize => \"0.5\",:fontname => \"Serif\"))"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_graphviz(to_wiring_diagram(euler_decomp), orientation=LeftToRight)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.4.0",
"language": "julia",
"name": "julia-1.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment