Wolfram Research

Function Repository Resource:

MakeZXDiagram

Source Notebook

Make a diagrammatic representation of a linear map in the ZX-calculus

Contributed by: Jonathan Gorard and Manojna Namuduri (with additional contributions by Xerxes Arsiwalla)

ResourceFunction["MakeZXDiagram"][gen]

makes a ZX-diagram using the list of generators gen.

ResourceFunction["MakeZXDiagram"][assoc]

makes a ZX-diagram using the association of generators assoc.

Details and Options

If ResourceFunction["MakeZXDiagram"] succeeds in constructing the specified ZX-diagram, then it returns a ZXDiagramObject expression.
Diagrammatic generators may be specified in list or association form.
The following generator types are supported:
Z [ name , in , out , phase ] a Z-spider with name name, input arity in, output arity out and phase phase
X [ name , in , out , phase ] an X-spider with name name, input arity in, output arity out and phase phase
H [ name ] a Hadamard gate with name name
B[name] a black diamond with name name
W [ name 1 , name 2 ] a wire connecting generators name1 and name2
When specified in association form <|"ZSpiders"z,"XSpiders"x,"HadamardGates"h,"Diamonds"d,"Wires"w|>, z and x are nested lists of input arities, output arities and phases; h and d are integers representing the number of Hadamard gates and black diamonds; and w is a nested list of generator names connected by wires.
In ZXDiagramObject, the following properties are supported:
"LabeledGraph" graph form of the ZX-diagram with phases labeled
"UnlabeledGraph" graph form of the ZX-diagram without phases labeled
"OperatorForm" ZX-diagram represented as a tensor product of generators
"ListForm" ZX-diagram represented as a list of generators
"MatrixForm" ZX-diagram represented as an explicit linear map on qubits
"ZSpiders" list of Z-spiders in the ZX-diagram
"XSpiders" list of X-spiders in the ZX-diagram
"HadamardGates" list of Hadamard gates in the ZX-diagram
"Diamonds" list of black diamonds in the ZX-diagram
"Wires" list of wires in the ZX-diagram
"ZSpiderCount" number of Z-spiders in the ZX-diagram
"XSpiderCount" number of X-spiders in the ZX-diagram
"HadamardGateCount" number of Hadamard gates in the ZX-diagram
"DiamondCount" number of black diamonds in the ZX-diagram
"WireCount" number of wires in the ZX-diagram
The default convention is to name Z-spiders as z1,z2,; X-spiders as x1,x2,; Hadamard gates as h1,h2,; black diamonds as d1,d2,; inputs as i1,i2,; and outputs as o1,o2,.

Examples

Basic Examples

Construct an elementary ZX-diagram corresponding to a simple unitary map:

In[1]:=
diagram = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x, 1, 1, Pi/2], \[FormalCapitalW][i, x], \[FormalCapitalW][x, o]}]
Out[1]=

Show the labeled graph:

In[2]:=
diagram["LabeledGraph"]
Out[2]=

Show its operator form:

In[3]:=
diagram["OperatorForm"]
Out[3]=

Show its matrix form:

In[4]:=
diagram["MatrixForm"]
Out[4]=

Construct a ZX-diagram from an association:

In[5]:=
diagram = ResourceFunction[
  "MakeZXDiagram"][<|"ZSpiders" -> {{2, 2}, {1, 1}, {Pi, Pi/2}}, "XSpiders" -> {{1, 1}, {2, 2}, {Pi/3, Pi/4}}, "Wires" -> {{i1, x1}, {i2, x2}, {x1, z1}, {x1, z2}, {x2, z1}, {x2, z2}, {z1, o1}, {z2, o2}}, "HadamardGates" -> 0, "Diamonds" -> 0|>]
Out[5]=

Show the unlabeled graph:

In[6]:=
diagram["UnlabeledGraph"]
Out[6]=

Show its operator form:

In[7]:=
diagram["OperatorForm"]
Out[7]=

Show its matrix form:

In[8]:=
diagram["MatrixForm"]
Out[8]=

Construct a more complicated ZX-diagram involving tensor products, Hadamard gates and diamonds:

In[9]:=
diagram = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x1, 0, 1, Pi/2], \[FormalCapitalZ][z1, 3, 4, 0], \[FormalCapitalZ][z2, 2, 1,
     0], \[FormalCapitalX][x2, 4, 1, Pi/5], \[FormalCapitalH][
    h1], \[FormalCapitalZ][z3, 0, 1, Pi/3], \[FormalCapitalX][x3, 2, 2, 0], \[FormalCapitalH][h2], \[FormalCapitalW][x1, z1], \[FormalCapitalW][i1, z1], \[FormalCapitalW][i2, z1], \[FormalCapitalW][z1, z2], \[FormalCapitalW][z1, z2], \[FormalCapitalW][z1, x2], \[FormalCapitalW][z2, x2], \[FormalCapitalW][z1, o1], \[FormalCapitalW][i3, h1], \[FormalCapitalW][h1, x2], \[FormalCapitalW][z3, x2], \[FormalCapitalW][x2, o4], \[FormalCapitalB][
    d1], \[FormalCapitalW][i4, h2], \[FormalCapitalW][h2, x3], \[FormalCapitalW][i5, x3], \[FormalCapitalW][x3, o5], \[FormalCapitalW][x3, o6]}]
Out[9]=

Show the unlabeled graph:

In[10]:=
diagram["UnlabeledGraph"]
Out[10]=

Show its operator form:

In[11]:=
diagram["OperatorForm"]
Out[11]=

Show its matrix form:

In[12]:=
diagram["MatrixForm"]
Out[12]=

Scope

ZX generators

All of the standard generators of the ZX-calculus are supported (for both the computational and Hadamard-transformed bases), including states:

In[13]:=
diagram1 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalZ][z, 0, 1, \[Alpha]], \[FormalCapitalW][z, o]}]
Out[13]=
In[14]:=
diagram1["LabeledGraph"]
Out[14]=
In[15]:=
diagram1["MatrixForm"]
Out[15]=
In[16]:=
diagram2 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x, 0, 1, \[Alpha]], \[FormalCapitalW][x, 0]}]
Out[16]=
In[17]:=
diagram2["LabeledGraph"]
Out[17]=
In[18]:=
diagram2["MatrixForm"]
Out[18]=

Unitary maps:

In[19]:=
diagram1 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalZ][z, 1, 1, \[Alpha]], \[FormalCapitalW][i, z], \[FormalCapitalW][z, o]}]
Out[19]=
In[20]:=
diagram1["LabeledGraph"]
Out[20]=
In[21]:=
diagram1["MatrixForm"]
Out[21]=
In[22]:=
diagram2 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x, 1, 1, \[Alpha]], \[FormalCapitalW][i, x], \[FormalCapitalW][x, o]}]
Out[22]=
In[23]:=
diagram2["LabeledGraph"]
Out[23]=
In[24]:=
diagram2["MatrixForm"]
Out[24]=

Linear isometries:

In[25]:=
diagram1 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalZ][z, 1, 2, \[Alpha]], \[FormalCapitalW][i, z], \[FormalCapitalW][z, o1], \[FormalCapitalW][z, o2]}]
Out[25]=
In[26]:=
diagram1["LabeledGraph"]
Out[26]=
In[27]:=
diagram1["MatrixForm"]
Out[27]=
In[28]:=
diagram2 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x, 1, 2, \[Alpha]], \[FormalCapitalW][i, x], \[FormalCapitalW][x, o1], \[FormalCapitalW][x, o2]}]
Out[28]=
In[29]:=
diagram2["LabeledGraph"]
Out[29]=
In[30]:=
diagram2["MatrixForm"]
Out[30]=

Partial linear isometries:

In[31]:=
diagram1 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalZ][z, 2, 1, \[Alpha]], \[FormalCapitalW][i1, z], \[FormalCapitalW][i2, z], \[FormalCapitalW][z, o]}]
Out[31]=
In[32]:=
diagram1["LabeledGraph"]
Out[32]=
In[33]:=
diagram1["MatrixForm"]
Out[33]=
In[34]:=
diagram2 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x, 2, 1, \[Alpha]], \[FormalCapitalW][i1, x], \[FormalCapitalW][i2, x], \[FormalCapitalW][x, o]}]
Out[34]=
In[35]:=
diagram2["LabeledGraph"]
Out[35]=
In[36]:=
diagram2["MatrixForm"]
Out[36]=

Projections (i.e. projective measurements):

In[37]:=
diagram1 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalZ][z, 0, 1, \[Alpha]], \[FormalCapitalW][i, z]}]
Out[37]=
In[38]:=
diagram1["LabeledGraph"]
Out[38]=
In[39]:=
diagram1["MatrixForm"]
Out[39]=
In[40]:=
diagram2 = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x, 0, 1, \[Alpha]], \[FormalCapitalW][i, x]}]
Out[40]=
In[41]:=
diagram2["LabeledGraph"]
Out[41]=
In[42]:=
diagram2["MatrixForm"]
Out[42]=

Hadamard gates:

In[43]:=
diagram = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalH][h], \[FormalCapitalW][i, h], \[FormalCapitalW][h, o]}]
Out[43]=
In[44]:=
diagram["LabeledGraph"]
Out[44]=
In[45]:=
diagram["MatrixForm"]
Out[45]=

Black diamonds (i.e. scalar factors):

In[46]:=
diagram = ResourceFunction["MakeZXDiagram"][{\[FormalCapitalB][d]}]
Out[46]=
In[47]:=
diagram["UnlabeledGraph"]
Out[47]=

ZX-diagram properties

Properties supported by ZXDiagramObject include labeled graphs:

In[48]:=
diagram = ResourceFunction[
  "MakeZXDiagram"][{\[FormalCapitalX][x1, 0, 1, Pi/2], \[FormalCapitalZ][z1, 3, 4, 0], \[FormalCapitalZ][z2, 2, 1,
     0], \[FormalCapitalX][x2, 4, 1, Pi/5], \[FormalCapitalH][
    h1], \[FormalCapitalZ][z3, 0, 1, Pi/3], \[FormalCapitalX][x3, 2, 2, 0], \[FormalCapitalH][h2], \[FormalCapitalW][x1, z1], \[FormalCapitalW][i1, z1], \[FormalCapitalW][i2, z1], \[FormalCapitalW][z1, z2], \[FormalCapitalW][z1, z2], \[FormalCapitalW][z1, x2], \[FormalCapitalW][z2, x2], \[FormalCapitalW][z1, o1], \[FormalCapitalW][i3, h1], \[FormalCapitalW][h1, x2], \[FormalCapitalW][z3, x2], \[FormalCapitalW][x2, o4], \[FormalCapitalB][
    d1], \[FormalCapitalW][i4, h2], \[FormalCapitalW][h2, x3], \[FormalCapitalW][i5, x3], \[FormalCapitalW][x3, o5], \[FormalCapitalW][x3, o6]}]
Out[48]=
In[49]:=
diagram["LabeledGraph"]
Out[49]=

Unlabeled graphs:

In[50]:=
diagram["UnlabeledGraph"]
Out[50]=

Operator forms:

In[51]:=
diagram["OperatorForm"]
Out[51]=

List forms:

In[52]:=
diagram["ListForm"]
Out[52]=

Matrix forms:

In[53]:=
diagram["MatrixForm"]
Out[53]=

Z-spider lists:

In[54]:=
diagram["ZSpiders"]
Out[54]=

X-spider lists:

In[55]:=
diagram["XSpiders"]
Out[55]=

Hadamard gate lists:

In[56]:=
diagram["HadamardGates"]
Out[56]=

Black diamond lists:

In[57]:=
diagram["Diamonds"]
Out[57]=

Wire lists:

In[58]:=
diagram["Wires"]
Out[58]=

Resource History

Source Metadata

Related Resources

License Information