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

JSON importer should probably call auto_detect_io #168

Closed
zickgraf opened this issue Oct 11, 2023 · 3 comments
Closed

JSON importer should probably call auto_detect_io #168

zickgraf opened this issue Oct 11, 2023 · 3 comments

Comments

@zickgraf
Copy link

Consider the following example importing a ZX diagram from JSON and extracting a circuit:

import pyzx as zx
import numpy as np
g = zx.Graph.from_json('{"wire_vertices": {"b0": {"annotation": {"boundary": true, "coord": [2.338, 0.253], "input": false, "output": true}}, "b1": {"annotation": {"boundary": true, "coord": [2.358, -0.787], "input": false, "output": true}}, "b2": {"annotation": {"boundary": true, "coord": [0.398, -0.827], "input": true, "output": false}}, "b3": {"annotation": {"boundary": true, "coord": [0.478, 0.333], "input": true, "output": false}}}, "node_vertices": {"v0": {"annotation": {"coord": [1.408, 0.293]}, "data": {"type": "hadamard", "is_edge": "true"}}, "v1": {"annotation": {"coord": [1.378, -0.807]}, "data": {"type": "hadamard", "is_edge": "true"}}}, "undir_edges": {"e0": {"src": "b0", "tgt": "v0"}, "e1": {"src": "b3", "tgt": "v0"}, "e2": {"src": "b1", "tgt": "v1"}, "e3": {"src": "b2", "tgt": "v1"}}, "scalar": "{\\"power2\\": 0, \\"phase\\": \\"0\\"}"}')
#g.auto_detect_io()
zx.full_reduce(g)
zx.draw(g)
circ = zx.extract_circuit(g.copy())
np.allclose(circ.to_matrix(), g.to_matrix())

The last line displays False, i.e. the matrix of the graph and the matrix of the extracted circuit do not match. Uncommenting the line g.auto_detect_io() fixes the issue.

PyZX version: 669f5a2 (current master at the time of writing)

@zickgraf
Copy link
Author

I have analyzed this a bit more: It seems like Graph.from_json populates g.inputs() in the order in which the inputs appear in the JSON. The order in the JSON in the example above in turn matches the order in which I drew the nodes in the interactive editor. This order is different from the visual order, which g.auto_detect_io() enforces.

So there are actually two bugs:

  1. Exporting and re-importing a graph does not preserve the order of g.inputs(). I guess the JSON should explicitly store the order of inputs and outputs to not have to rely on guesswork.
  2. There is an inconsistency between g.to_matrix and zx.extract_circuit: g.to_matrix seems to always use the visual order while zx.extract_circuit seems to always use the order of g.inputs().

jvdwetering added a commit that referenced this issue Oct 12, 2023
@jvdwetering
Copy link
Collaborator

In commit 4ad8938 I updated the jsonparser as well. It should now remember the input and output order correctly. Note that this does invalidate the json you have above (it is not backwards compatible). If this lack of backwards compatibility is a big problem I can see what I can do to save it.

@zickgraf
Copy link
Author

In commit 4ad8938 I updated the jsonparser as well. It should now remember the input and output order correctly.

Thanks a lot, that allows to further process the JSON much more safely.

Note that this does invalidate the json you have above (it is not backwards compatible). If this lack of backwards compatibility is a big problem I can see what I can do to save it.

That's not a problem, I'm very happy with the new data structure :-)

Now everything seems to work, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants