Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pivot basic rewrite with new PyZX version #334

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions zxlive/custom_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,8 @@ def filter_matchings_if_symbolic_compatible(matchings: list[Dict[VT, VT]], left:
for matching in matchings:
if len(matching) != len(left):
continue
try:
match_symbolic_parameters(matching, left, right)
new_matchings.append(matching)
except ValueError:
pass
match_symbolic_parameters(matching, left, right)
new_matchings.append(matching)
return new_matchings


Expand Down
5 changes: 1 addition & 4 deletions zxlive/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ def save_diagram_dialog(graph: GraphT, parent: QWidget) -> Optional[tuple[str, F
file_path, selected_format = file_path_and_format

if selected_format in (FileFormat.QGraph, FileFormat.Json):
try:
graph.auto_detect_io()
except TypeError:
pass
graph.auto_detect_io()
data = graph.to_json()
elif selected_format == FileFormat.QASM:
try:
Expand Down
2 changes: 2 additions & 0 deletions zxlive/rewrite_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def do_rewrite(self, panel: ProofPanel) -> None:

try:
g, rem_verts = self.apply_rewrite(g, matches)
except ValueError as ex:
raise ex
except Exception as ex:
show_error_msg('Error while applying rewrite rule', str(ex))
return
Expand Down
38 changes: 19 additions & 19 deletions zxlive/rewrite_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ def read_custom_rules() -> list[RewriteData]:
custom_rules.append(rule)
return custom_rules

const_true = lambda graph, matches: matches


def apply_simplification(simplification: Callable[[GraphT], Optional[int]]) -> Callable[
[GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]]:
def rule(g: GraphT, matches: list) -> pyzx.rules.RewriteOutputType[VT, ET]:
simplification(g)
return ({}, [], [], True)

return rule


def _extract_circuit(graph: GraphT, matches: list) -> GraphT:
graph.auto_detect_io()
simplify.full_reduce(graph)
return cast(GraphT, extract_circuit(graph).to_graph())


# We want additional actions that are not part of the original PyZX editor
# So we add them to operations

Expand All @@ -67,7 +85,7 @@ def read_custom_rules() -> list[RewriteData]:
"text": "pivot",
"tooltip": "Deletes a pair of spiders with 0/pi phases by performing a pivot",
"matcher": lambda g, matchf: pyzx.rules.match_pivot_parallel(g, matchf, check_edge_types=True),
"rule": pyzx.rules.pivot,
"rule": apply_simplification(simplify.pivot_simp),
"type": MATCHES_EDGES,
"copy_first": True,
"picture": "pivot_regular.png"
Expand Down Expand Up @@ -108,24 +126,6 @@ def read_custom_rules() -> list[RewriteData]:
},
}

const_true = lambda graph, matches: matches


def apply_simplification(simplification: Callable[[GraphT], Optional[int]]) -> Callable[
[GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]]:
def rule(g: GraphT, matches: list) -> pyzx.rules.RewriteOutputType[VT, ET]:
simplification(g)
return ({}, [], [], True)

return rule


def _extract_circuit(graph: GraphT, matches: list) -> GraphT:
graph.auto_detect_io()
simplify.full_reduce(graph)
return cast(GraphT, extract_circuit(graph).to_graph())


# The OCM action simply saves the current graph without modifying anything.
# This can be used to make repositioning the vertices an explicit proof step.
def ocm_rule(_graph: GraphT, _matches: list) -> pyzx.rules.RewriteOutputType[VT, ET]:
Expand Down
15 changes: 6 additions & 9 deletions zxlive/rule_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,9 @@ def add_edge(self, u: VT, v: VT, verts: list[VItem]) -> None:
self.update_io_labels(self.graph_scene)

def update_io_labels(self, scene: EditGraphScene) -> None:
try:
scene.g.auto_detect_io()
for v in scene.g.vertices():
if v in scene.g.inputs():
scene.vertex_map[v].phase_item.setPlainText("in-" + str(scene.g.inputs().index(v)))
elif v in scene.g.outputs():
scene.vertex_map[v].phase_item.setPlainText("out-" + str(scene.g.outputs().index(v)))
except TypeError:
return
scene.g.auto_detect_io()
for v in scene.g.vertices():
if v in scene.g.inputs():
scene.vertex_map[v].phase_item.setPlainText("in-" + str(scene.g.inputs().index(v)))
elif v in scene.g.outputs():
scene.vertex_map[v].phase_item.setPlainText("out-" + str(scene.g.outputs().index(v)))
1 change: 1 addition & 0 deletions zxlive/vitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def g(self) -> GraphT:

@property
def ty(self) -> VertexType:
# https://github.com/zxcalc/zxlive/pull/281
_ty: VertexType = self.g.type(self.v)
return _ty

Expand Down
Loading