Skip to content

Commit

Permalink
The jsonparser for graphs now correctly remembers the ordering of inp…
Browse files Browse the repository at this point in the history
…uts and outputs and also reads these out correctly, ignoring the visual placement.
  • Loading branch information
jvdwetering committed Oct 12, 2023
1 parent 1a1a074 commit 4ad8938
Show file tree
Hide file tree
Showing 3 changed files with 1,363 additions and 19 deletions.
22 changes: 12 additions & 10 deletions pyzx/graph/jsonparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def json_to_graph(js: str, backend:Optional[str]=None) -> BaseGraph:
value = _quanto_value_to_phase(value)
g.set_vdata(v, key, value)

inputs = []
outputs = []
inputs = {}
outputs = {}

for name,attr in j.get('wire_vertices',{}).items():
ann = attr['annotation']
Expand All @@ -104,17 +104,15 @@ def json_to_graph(js: str, backend:Optional[str]=None) -> BaseGraph:
v = g.add_vertex(VertexType.BOUNDARY,q,r)
g.set_vdata(v,'name',name)
names[name] = v
if "input" in ann and ann["input"]: inputs.append(v)
if "output" in ann and ann["output"]: outputs.append(v)
#g.set_vdata(v, 'x', c[0])
#g.set_vdata(v, 'y', c[1])
if "input" in ann: inputs[v] = ann["input"]
if "output" in ann: outputs[v] = ann["output"]
for key, value in attr['annotation'].items():
if key in ('boundary','coord','input','output'):
continue
g.set_vdata(v, key, value)

g.set_inputs(tuple(inputs))
g.set_outputs(tuple(outputs))
g.set_inputs(tuple(sorted(inputs.keys(),key=lambda v:inputs[v])))
g.set_outputs(tuple(sorted(outputs.keys(),key=lambda v:outputs[v])))

edges: Dict[Any, List[int]] = {} # TODO: Any = ET
for edge in j.get('undir_edges',{}).values():
Expand Down Expand Up @@ -182,8 +180,12 @@ def graph_to_json(g: BaseGraph[VT,ET], include_scalar: bool=True) -> str:

names[v] = name
if t == VertexType.BOUNDARY:
wire_vs[name] = {"annotation":{"boundary":True,"coord":coord,
"input":(v in inputs), "output":(v in outputs)}}
wire_vs[name] = {"annotation":{"boundary":True,"coord":coord}}
if v in inputs:
wire_vs[name]["annotation"]["input"] = inputs.index(v)
if v in outputs:
wire_vs[name]["annotation"]["output"] = outputs.index(v)

for key in g.vdata_keys(v):
if key in wire_vs[name]["annotation"]:
continue
Expand Down
Loading

0 comments on commit 4ad8938

Please sign in to comment.