Skip to content

Commit

Permalink
added to_dict methods to graph
Browse files Browse the repository at this point in the history
  • Loading branch information
akissinger committed Sep 26, 2024
1 parent ec999ea commit 4b160f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pyzx/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ def to_matrix(self,preserve_scalar:bool=True) -> np.ndarray:
"""Returns a representation of the graph as a matrix using :func:`~pyzx.tensor.tensorfy`"""
return tensor_to_matrix(tensorfy(self, preserve_scalar), self.num_inputs(), self.num_outputs())

def to_dict(self, include_scalar:bool=True) -> dict[str, Any]:
"""Returns a json representation of the graph that follows the Quantomatic .qgraph format.
Convert back into a graph using :meth:`from_json`."""
from .jsonparser import graph_to_dict
return graph_to_dict(self, include_scalar)

def to_json(self, include_scalar:bool=True) -> str:
"""Returns a json representation of the graph that follows the Quantomatic .qgraph format.
Convert back into a graph using :meth:`from_json`."""
Expand Down
14 changes: 10 additions & 4 deletions pyzx/graph/jsonparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def json_to_graph(js: str, backend:Optional[str]=None) -> BaseGraph:

return g

def graph_to_json(g: BaseGraph[VT,ET], include_scalar: bool=True) -> str:
"""Converts a PyZX graph into JSON output compatible with Quantomatic.
def graph_to_dict(g: BaseGraph[VT,ET], include_scalar: bool=True) -> dict[str, Any]:
"""Converts a PyZX graph into Python dict for JSON output.
If include_scalar is set to True (the default), then this includes the value
of g.scalar with the json, which will also be loaded by the ``from_json`` method."""
node_vs: Dict[str, Dict[str, Any]] = {}
Expand Down Expand Up @@ -275,9 +275,15 @@ def graph_to_json(g: BaseGraph[VT,ET], include_scalar: bool=True) -> str:
"variable_types": g.variable_types,
}
if include_scalar:
d["scalar"] = g.scalar.to_json()
d["scalar"] = g.scalar.to_dict()

return json.dumps(d, cls=ComplexEncoder)
return d

def graph_to_json(g: BaseGraph[VT,ET], include_scalar: bool=True) -> str:
"""Converts a PyZX graph into JSON output compatible with Quantomatic.
If include_scalar is set to True (the default), then this includes the value
of g.scalar with the json, which will also be loaded by the ``from_json`` method."""
return json.dumps(graph_to_dict(g, include_scalar), cls=ComplexEncoder)

def to_graphml(g: BaseGraph[VT,ET]) -> str:
gml = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Expand Down
9 changes: 6 additions & 3 deletions pyzx/graph/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import cmath
import copy
from fractions import Fraction
from typing import List
from typing import List, Any
import json

from ..utils import FloatInt, FractionLike
Expand Down Expand Up @@ -178,7 +178,7 @@ def to_unicode(self) -> str:
s += "{:d}/{:d}π)".format(phase.numerator,phase.denominator)
return s

def to_json(self) -> str:
def to_dict(self) -> dict[str, Any]:
d = {"power2": self.power2, "phase": str(self.phase)}
if abs(self.floatfactor - 1) > 0.00001:
d["floatfactor"] = self.floatfactor
Expand All @@ -188,7 +188,10 @@ def to_json(self) -> str:
d["is_zero"] = self.is_zero
if self.is_unknown:
d["is_unknown"] = self.is_unknown,
return json.dumps(d)
return d

def to_json(self) -> str:
return json.dumps(self.to_dict())

@classmethod
def from_json(cls, s: str) -> 'Scalar':
Expand Down

0 comments on commit 4b160f6

Please sign in to comment.