Skip to content

Commit

Permalink
Merge pull request #282 from zxcalc/remove-quantomatic-features
Browse files Browse the repository at this point in the history
Remove quantomatic features
  • Loading branch information
jvdwetering authored Jan 30, 2025
2 parents aa912c3 + 3de5969 commit daf933c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 408 deletions.
78 changes: 40 additions & 38 deletions demos/AllFeatures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"\n",
"## Contents:\n",
"* [Loading and saving circuits](#circuits)\n",
"* [Interacting with Quantomatic](#quantomatic)\n",
"* [Importing, exporting and editing diagrams](#diagram-io)\n",
"* [Optimizing ZX-diagrams](#optimization-zx)\n",
"* [Extracting and optimizing circuits](#optimization-circuits)\n",
"* [Phase Teleportation](#phase-teleportation)"
Expand Down Expand Up @@ -172,11 +172,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"quantomatic\"></a>\n",
"# Interacting with Quantomatic\n",
"PyZX allows easy integration with quantomatic.\n",
"\n",
"First of all, Quantomatic graph files can be imported into PyZX:"
"<a id=\"diagram-io\"></a>\n",
"# Importing, exporting and editing diagrams\n",
"PyZX also has its own json format for exporting graphs and interacts with [ZXLive](https://github.com/zxcalc/zxlive) to make manually modifying diagrams easy. First, let's see that we can indeed load diagrams:"
]
},
{
Expand All @@ -195,24 +193,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"PyZX saves the names of the vertices:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(g.vdata(12,'name'))\n",
"print(g.vdata(1,'name'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because this graph was originally exported from PyZX, it has automatically remembered what its inputs and outputs are:"
"Because this graph was originally exported from PyZX starting as a circuit, it has automatically remembered what its inputs and outputs are:"
]
},
{
Expand All @@ -228,7 +209,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"For a graph that originated from Quantomatic we need to tell it what its inputs and outputs are.\n",
"For a graph that was built manually, we might need to tell it what its inputs and outputs are.\n",
"\n",
"This can be done either manually:\n",
"\n",
Expand All @@ -243,17 +224,17 @@
"metadata": {},
"outputs": [],
"source": [
"g.set_inputs(())\n",
"g.set_inputs(()) # Reset the inputs and outputs, so we can let PyZX auto-detect them\n",
"g.set_outputs(())\n",
"g.auto_detect_io()\n",
"print(g.inputs(), g.outputs())"
]
},
{
"cell_type": "markdown",
"cell_type": "raw",
"metadata": {},
"source": [
"We can also call Quantomatic from PyZX. To do this we first need to tell PyZX where the Quantomatic executable can be found:"
"we can export a diagram to a JSON format that can be loaded back into PyZX (or ZXLive):"
]
},
{
Expand All @@ -262,14 +243,14 @@
"metadata": {},
"outputs": [],
"source": [
"zx.quantomatic.quantomatic_location = os.path.join('path', 'to', 'Quantomatic.jar')"
"print(g.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we can load a PyZX graph into Quantomatic using the following line:"
"We can call ZXLive from within a Jupyter notebook, in order to modify diagrams on the fly:"
]
},
{
Expand All @@ -278,18 +259,39 @@
"metadata": {},
"outputs": [],
"source": [
"result = zx.quantomatic.edit_graph(g)"
"%gui qt6\n",
"\n",
"# First make sure zxlive is installed by `pip install zxlive`\n",
"from zxlive import app\n",
"\n",
"g = zx.Graph()\n",
"g.add_vertex(zx.VertexType.Z, 0, 0)\n",
"g.add_vertex(zx.VertexType.X, 0, 1)\n",
"g.add_edge((0, 1))\n",
"zx.draw(g)\n",
"\n",
"zxl = app.get_embedded_app()\n",
"zxl.edit_graph(g, 'g1')\n",
"zxl.edit_graph(g, 'g2')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This starts Quantomatic with the graph ``g`` loaded. When you are done editing the graph, you simply save the file in Quantomatic, and close it. The result is then loaded and returned.\n",
"\n",
"NOTE1: The Notebook will be blocked until the Quantomatic executable is closed.\n",
"\n",
"NOTE2: Currently this only works with a recent build of Quantomatic that is as of yet only available via the repository, so make sure you are working with an up-to-date branch of Quantomatic."
"After making some edits within ZXLive, we can get the diagram back into this window so we can continue to do further work with them:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"zx.draw(zxl.get_copy_of_graph('g1'))\n",
"zx.draw(zxl.get_copy_of_graph('g2'))\n",
"#Note that ZXLive only works with MultiGraph's, and hence zxl.get_copy_of_graph() always returns an instance of MultiGraph, \n",
"#and not of the default graph backend."
]
},
{
Expand Down Expand Up @@ -689,7 +691,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -703,7 +705,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.11.2"
}
},
"nbformat": 4,
Expand Down
10 changes: 1 addition & 9 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Below is listed the content of ``drawing.py``.
:undoc-members:


Tikz and Quantomatic functionality
Tikz functionality
----------------------------------

.. _tikz:
Expand All @@ -165,11 +165,3 @@ Below is listed the content of ``tikz.py``.

.. _quanto:

Below is listed the content of ``quantomatic.py``.

.. module:: quantomatic

.. automodule:: pyzx.quantomatic
:members:
:undoc-members:

1 change: 0 additions & 1 deletion pyzx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from .local_search.genetic import GeneticOptimizer
from .circuit.qasmparser import qasm
from .circuit.sqasm import sqasm
from . import quantomatic
from . import generate
from . import todd
from . import linalg
Expand Down
19 changes: 0 additions & 19 deletions pyzx/graph/jsonparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,22 +431,3 @@ def to_graphml(g: BaseGraph[VT,ET]) -> str:
return gml


# class ComplexEncoder(json.JSONEncoder):
# def default(self, obj):
# if isinstance(obj, complex):
# return str(obj)
# return super().default(obj)

# class ComplexDecoder(json.JSONDecoder):
# def __init__(self, *args, **kwargs):
# json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)

# def object_hook(self, dct):
# for k, v in dct.items():
# if isinstance(v, str):
# try:
# dct[k] = complex(v)
# except ValueError:
# pass
# return dct

Loading

0 comments on commit daf933c

Please sign in to comment.