diff --git a/demos/PauliWebs.ipynb b/demos/PauliWebs.ipynb index 75c05e97..d3d1b54d 100644 --- a/demos/PauliWebs.ipynb +++ b/demos/PauliWebs.ipynb @@ -44,7 +44,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -440,7 +440,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -818,7 +818,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -1196,7 +1196,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -1574,7 +1574,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -1966,7 +1966,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -2344,7 +2344,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -2735,7 +2735,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -3135,7 +3135,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -3513,7 +3513,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -3891,7 +3891,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -4269,7 +4269,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -4647,7 +4647,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -5025,7 +5025,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -5418,7 +5418,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -5796,7 +5796,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -6190,7 +6190,7 @@ "\n", "# Adjacent spiders of the same color need to be fused for the Pauli web computation to work.\n", "# We can accomplish this with spider_simp:\n", - "zx.spider_simp(g, quiet=True)\n", + "zx.spider_simp(g)\n", "\n", "# This is not a fundamental problem, it just requires more case distinctions for compute_pauli_webs\n", "# to handle unfused spiders.\n", @@ -6208,7 +6208,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -6586,7 +6586,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -6964,7 +6964,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -7342,7 +7342,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -7720,7 +7720,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -8098,7 +8098,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -8491,7 +8491,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Next, we'll look at a single H gate\n", + "c = zx.qasm(\"\"\"\n", + "qreg q[1];\n", + "h q[0];\n", + "\"\"\")\n", + "g = c.to_graph()\n", + "\n", + "# PyZX renders this as a single H-edge connected to an identity spider\n", + "zx.draw(g)\n", + "\n", + "# we don't really need this id-spider, so we can remove it with id_simp\n", + "zx.id_simp(g)\n", + "zx.draw(g, labels=True)\n", + "\n", + "order, zwebs, xwebs = compute_pauli_webs(g)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# The single output has two Pauli webs\n", + "zx.draw(g, labels=True, pauli_web=zwebs[2])\n", + "zx.draw(g, labels=True, pauli_web=xwebs[2])" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "" @@ -8877,13 +10425,13 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -9278,7 +10826,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -9316,7 +10864,7 @@ " (58, 14): 'X'}" ] }, - "execution_count": 11, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -9328,13 +10876,13 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -9727,13 +11275,13 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -10111,7 +11659,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -10511,13 +12059,13 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
\n", + "
\n", "" @@ -10895,7 +12443,7 @@ { "data": { "text/html": [ - "
\n", + "
\n", "" diff --git a/pyzx/circuit/graphparser.py b/pyzx/circuit/graphparser.py index 1ccea9e6..500873c5 100644 --- a/pyzx/circuit/graphparser.py +++ b/pyzx/circuit/graphparser.py @@ -129,7 +129,7 @@ def circuit_to_graph(c: Circuit, compress_rows:bool=True, backend:Optional[str]= except ValueError: raise ValueError("PostSelect label {} is not in use".format(str(l))) v = g.add_vertex(VertexType.Z, q, r) - g.add_edge(g.edge(u,v),EdgeType.SIMPLE) + g.add_edge((u,v),EdgeType.SIMPLE) else: if not compress_rows: #or not isinstance(gate, (ZPhase, XPhase, HAD)): r = max(q_mapper.max_row(), c_mapper.max_row()) @@ -148,7 +148,7 @@ def circuit_to_graph(c: Circuit, compress_rows:bool=True, backend:Optional[str]= v = g.add_vertex(VertexType.BOUNDARY, o, r) outputs.append(v) u = mapper.prev_vertex(l) - g.add_edge(g.edge(u,v)) + g.add_edge((u,v)) g.set_inputs(tuple(inputs)) g.set_outputs(tuple(outputs)) diff --git a/pyzx/pauliweb.py b/pyzx/pauliweb.py index 9908d382..ff9e8d08 100644 --- a/pyzx/pauliweb.py +++ b/pyzx/pauliweb.py @@ -93,7 +93,7 @@ def transpose_corrections(c: Dict[VT, Set[VT]]) -> Dict[VT, Set[VT]]: def compute_pauli_webs(g: BaseGraph[VT,ET]) -> Tuple[Dict[VT, int], Dict[VT, PauliWeb[VT,ET]], Dict[VT, PauliWeb[VT,ET]]]: g1 = g.clone() out_edge: Dict[VT, Tuple[VT,VT]] = dict() - for j,o in enumerate(g1.outputs()): + for o in g1.outputs(): e = g1.incident_edges(o)[0] v = next(iter(g1.neighbors(o))) vt = g1.type(v) @@ -103,8 +103,7 @@ def compute_pauli_webs(g: BaseGraph[VT,ET]) -> Tuple[Dict[VT, int], Dict[VT, Pau (vt == VertexType.X and et == EdgeType.HADAMARD)): v0 = g1.add_vertex(VertexType.X) v1 = g1.add_vertex(VertexType.Z) - if ((vt == VertexType.X and et == EdgeType.SIMPLE) or - (vt == VertexType.Z and et == EdgeType.HADAMARD)): + else: v0 = g1.add_vertex(VertexType.Z) v1 = g1.add_vertex(VertexType.X) g1.add_edge((v, v0), et) @@ -150,5 +149,12 @@ def compute_pauli_webs(g: BaseGraph[VT,ET]) -> Tuple[Dict[VT, int], Dict[VT, Pau elif g.type(v) == VertexType.X: pw.add_edge((v, i), 'X') xwebs[v] = pw + elif g.type(v) == VertexType.BOUNDARY: + pw.add_edge((v, i), 'X') + xwebs[v] = pw + pw1 = PauliWeb(g) + pw1.add_edge((v, i), 'Z') + zwebs[v] = pw1 + return (order, zwebs, xwebs) \ No newline at end of file