This page was generated from notebooks/rules.ipynb [download].

PyZX rules with examples

[1]:
import sys

import pyzx.rewrite

sys.path.insert(0, '../..')
import pyzx as zx
from pyzx.graph import Graph
from pyzx.rewrite_rules import *

import pyzx.simplify as simplify

from pyzx import compare_tensors
import pyzx.hsimplify as hsimplify


import json

Bialgebra simplification

showing multiple ways to call it, including the bialgebra_op rule

[2]:
with open('../diagrams/bialgebra.json', 'r') as file: js = json.load(file)

g = Graph.from_json(js)
zx.draw(g, labels=True)

[3]:
g1 = g.copy()
simplify.bialg_simp(g1)
zx.draw(g1)
[4]:
simplify.bialg_simp.apply(g, 3, 4)
zx.draw(g, labels=True)

[5]:
simplify.bialg_op_simp.apply(g, [10, 11, 12, 13])
zx.draw(g, labels=True)

Color change rule

[8]:
with open('../diagrams/colorChange.json', 'r') as file: js = json.load(file)

g = Graph.from_json(js)
zx.draw(g)

[9]:
g1 = g.copy()
simplify.color_change_rewrite.apply(g1, 2)
zx.draw(g1)

[10]:
g2 = g.copy()
color_change_diagram(g2)
zx.draw(g2)

Copy rule

[11]:
with open('../diagrams/copy.json', 'r') as file: js = json.load(file)

g = Graph.from_json(js)
zx.draw(g,labels=True)

[12]:
simplify.to_gh(g)
simplify.copy_simp(g)

zx.draw(g)

Spider Fusion

[13]:
with open('../diagrams/spiderFuse.json', 'r') as file: js = json.load(file)

g = Graph.from_json(js)
zx.draw(g)

[14]:
simplify.spider_simp(g)
zx.draw(g)

Pivot

[15]:
with open('../diagrams/pivot.json', 'r') as file: js = json.load(file)

g = Graph.from_json(js)

g.auto_detect_io()

zx.draw(g, labels=True)

pivot_simp applies also when boundary but only with 0 phase

[16]:
g1 = g.copy()
simplify.pivot_simp(g1)
g1.auto_detect_io()

print(compare_tensors(g1.to_tensor(), g.to_tensor()))

zx.draw(g1, labels=True)
True
[17]:
simplify.pivot_simp.apply(g, 6, 7)

g.auto_detect_io()

print(compare_tensors(g1.to_tensor(),g.to_tensor()))

zx.draw(g, labels=True)

True

LComp

[18]:
with open('../diagrams/lcomp.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[19]:
simplify.lcomp_simp(g)
zx.draw(g)

Identity

[20]:
with open('../diagrams/identity.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[21]:
simplify.id_simp(g)

zx.draw(g)

Gadget fuse

only triggers if the gadget phases are equal and not 0. Ask if this is intentional

[22]:
with open('../diagrams/gadget.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[23]:
simplify.gadget_simp(g)
zx.draw(g)

Turn z into z-box

[24]:
with open('../diagrams/z_to_z_box.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[25]:
simplify.z_to_z_box_simp(g)
zx.draw(g)

Remove and add identity

[26]:
with open('../diagrams/rem_id.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[27]:
simplify.id_simp(g)
zx.draw(g, labels=True)
[28]:
simplify.add_identity_rewrite.apply(g, 1, 0)
zx.draw(g)

Hopf

removes parallel edges. Works only on multigraphs

[29]:
with open('../diagrams/hopf.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[30]:
simplify.hopf_simp(g)
zx.draw(g)

Remove self loop

[31]:
with open('../diagrams/self_loop.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[32]:
simplify.remove_self_loop_simp(g)
zx.draw(g)

Supplementarity

[33]:
with open('../diagrams/supplementarity.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[34]:
simplify.supplementarity_simp(g)

zx.draw(g)

Phasepoly

Not fuctioning yet!

[35]:
with open('../diagrams/phasepoly.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[36]:
simplify.spider_simp(g)
zx.draw(g)

simplify.gadget_simp(g)

zx.draw(g)

# simplify.full_reduce(g)
# zx.draw(g)

Push pauli

[37]:
with open('../diagrams/pauli_push.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g, labels=True)
[38]:
simplify.push_pauli_rewrite.apply(g, 6, 4)
zx.draw(g)

Euler expansion

decompose a had edge into z and x spiders

[39]:
with open('../diagrams/euler.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g, labels=True)
[40]:
simplify.euler_expansion_rewrite(g)
zx.draw(g)

Pi commute

[41]:
with open('../diagrams/pi_commute.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g,labels=True)
[42]:
simplify.pi_commute_rewrite.apply(g, 5)
zx.draw(g)

Hadamard edge to Hbox and back

[43]:
with open('../diagrams/edge_to_hbox.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g,labels=True)
[44]:
for e in g.edges():
    print (e)

hsimplify.had_edge_to_hbox_simp(g)
zx.draw(g, labels=True)
(3, 4, <EdgeType.HADAMARD: 2>)
[45]:
hsimplify.hbox_to_had_edge_simp(g)
zx.draw(g, labels=True)

Fuse hboxes

[46]:
with open('../diagrams/fuse_hboxes.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g,labels=True)
[47]:
hsimplify.hspider_simp(g)
zx.draw(g, labels=True)

hbox parallel not removal

Triggers when the same H-box is connected via both a regular edge and a NOT edge to the same spider.

Automatically checks for isolated vertices

[48]:
with open('../diagrams/hbox_not_remove.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[49]:
hsimplify.hbox_parallel_not_remove_simp(g)
zx.draw(g)

Multiply

[50]:
with open('../diagrams/multiply.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g, labels=True)
[51]:
hsimplify.par_hbox_simp.apply(g, [6, 7])
zx.draw(g)

Intro rule

From https://arxiv.org/pdf/2103.06610

[52]:
with open('../diagrams/intro.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g, labels=True)
[53]:
hsimplify.par_hbox_intro_simp.apply(g, [4, 5])
zx.draw(g)

Zero Hboxes

[54]:
with open('../diagrams/zero_hbox.json', 'r') as file: js = json.load(file)
g = Graph.from_json(js)

zx.draw(g)
[55]:
hsimplify.zero_hbox_simp(g)
zx.draw(g, labels=True)

Hyperpivot

[56]:
with open('../diagrams/hpivot.json', 'r') as file: js = json.load(file)

g = Graph.from_json(js)

zx.draw(g, labels=True)

[57]:
hsimplify.hpivot_simp(g)

zx.draw(g)