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 graph diff not serializing phases properly #144

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
6 changes: 4 additions & 2 deletions pyzx/graph/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..utils import VertexType, EdgeType, FractionLike, FloatInt
from .base import BaseGraph, VT, ET
from .graph_s import GraphS
from .jsonparser import _phase_to_quanto_value, _quanto_value_to_phase

class GraphDiff(Generic[VT, ET]):
removed_verts: List[VT]
Expand Down Expand Up @@ -121,14 +122,15 @@ def to_json(self) -> str:
changed_edge_types_str_dict = {}
for key, value in self.changed_edge_types.items():
changed_edge_types_str_dict[f"{key[0]},{key[1]}"] = value # type: ignore
changed_phases_str = {k: _phase_to_quanto_value(v) for k, v in self.changed_phases.items()}
return json.dumps({
"removed_verts": self.removed_verts,
"new_verts": self.new_verts,
"removed_edges": self.removed_edges,
"new_edges": self.new_edges,
"changed_vertex_types": self.changed_vertex_types,
"changed_edge_types": changed_edge_types_str_dict,
"changed_phases": self.changed_phases,
"changed_phases": changed_phases_str,
"changed_pos": self.changed_pos,
})

Expand All @@ -142,7 +144,7 @@ def from_json(json_str: str) -> "GraphDiff":
gd.new_edges = list(map(tuple, d["new_edges"])) # type: ignore
gd.changed_vertex_types = map_dict_keys(d["changed_vertex_types"], int)
gd.changed_edge_types = map_dict_keys(d["changed_edge_types"], lambda x: tuple(map(int, x.split(","))))
gd.changed_phases = map_dict_keys(d["changed_phases"], int)
gd.changed_phases = {int(k): _quanto_value_to_phase(v) for k, v in d["changed_phases"].items()}
gd.changed_pos = map_dict_keys(d["changed_pos"], int)
return gd

Expand Down
Loading