.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/visualization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_visualization.py: Visualizing the patterns and flows ================================== :class:`~graphix.visualization.GraphVisualizer` tool offers a wide selection of visualization methods for inspecting the causal structure of the graph associated with the pattern, graph or the (generalized-)flow. .. GENERATED FROM PYTHON SOURCE LINES 11-22 Causal flow ----------- First, let us inspect the flow and gflow associated with the resource graph of a pattern. simply call :meth:`~graphix.pattern.Pattern.draw_graph` method. Below we list the meaning of the node boundary and face colors. - Nodes with red boundaries are the *input nodes* where the computation starts. - Nodes with gray color is the *output nodes* where the final state end up in. - Nodes with blue color is the nodes that are measured in *Pauli basis*, one of *X*, *Y* or *Z* computational bases. - Nodes in white are the ones measured in *non-Pauli basis*. .. GENERATED FROM PYTHON SOURCE LINES 22-38 .. code-block:: Python from __future__ import annotations from graphix import Circuit from graphix.fundamentals import ANGLE_PI circuit = Circuit(3) circuit.cnot(0, 1) circuit.cnot(2, 1) circuit.rx(0, ANGLE_PI / 3) circuit.x(2) circuit.cnot(2, 1) pattern = circuit.transpile().pattern # note that this visualization is not always consistent with the correction set of pattern, # since we find the correction sets with flow-finding algorithms. pattern.draw_graph(flow_from_pattern=False, show_measurement_planes=True) .. image-sg:: /gallery/images/sphx_glr_visualization_001.png :alt: visualization :srcset: /gallery/images/sphx_glr_visualization_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Pauli flow detected in the graph (causal flow not detected) .. GENERATED FROM PYTHON SOURCE LINES 39-40 next, show the gflow: .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: Python pattern.remove_input_nodes() pattern.perform_pauli_measurements() pattern.draw_graph(flow_from_pattern=False, show_measurement_planes=True, node_distance=(1, 0.6)) .. image-sg:: /gallery/images/sphx_glr_visualization_002.png :alt: visualization :srcset: /gallery/images/sphx_glr_visualization_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Flow detected in the graph. .. GENERATED FROM PYTHON SOURCE LINES 46-50 Correction set ('xflow' and 'zflow' of pattern) ----------------------------------------------- next let us visualize the X and Z correction set in the pattern by :code:`flow_from_pattern=False` statement. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python # node_distance argument specifies the scale of the node arrangement in x and y directions. pattern.draw_graph(flow_from_pattern=True, show_measurement_planes=True, node_distance=(0.7, 0.6)) .. image-sg:: /gallery/images/sphx_glr_visualization_003.png :alt: visualization :srcset: /gallery/images/sphx_glr_visualization_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none The pattern is consistent with causal flow structure. .. GENERATED FROM PYTHON SOURCE LINES 55-58 Instead of the measurement planes, we can show the local Clifford of the resource graph. see *clifford.py* for the details of the indices of each single-qubit Clifford operators. 6 is the Hadamard and 8 is the :math:`\sqrt{iY}` operator. .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python pattern.draw_graph(flow_from_pattern=True, show_local_clifford=True, node_distance=(0.7, 0.6)) .. image-sg:: /gallery/images/sphx_glr_visualization_004.png :alt: visualization :srcset: /gallery/images/sphx_glr_visualization_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none The pattern is consistent with causal flow structure. .. GENERATED FROM PYTHON SOURCE LINES 61-64 Visualize based on the graph ---------------------------- The visualizer also works without the pattern. Simply supply the graph. .. GENERATED FROM PYTHON SOURCE LINES 64-80 .. code-block:: Python import networkx as nx from graphix.measurements import Measurement from graphix.opengraph import OpenGraph from graphix.visualization import GraphVisualizer # graph with gflow but no flow graph: nx.Graph[int] = nx.Graph([(1, 4), (1, 6), (2, 4), (2, 5), (2, 6), (3, 5), (3, 6)]) inputs = [1, 2, 3] outputs = [4, 5, 6] measurements = {node: Measurement.XY(0) for node in graph.nodes() if node not in outputs} og = OpenGraph(graph, inputs, outputs, measurements) vis = GraphVisualizer(og) vis.visualize(show_measurement_planes=True) .. image-sg:: /gallery/images/sphx_glr_visualization_005.png :alt: visualization :srcset: /gallery/images/sphx_glr_visualization_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/graphix/checkouts/v0.3.5/graphix/visualization.py:110: UserWarning: Open graph with non-inferred Pauli measurements. pauli_flow = self.og.find_pauli_flow() Pauli flow detected in the graph (causal flow not detected) .. GENERATED FROM PYTHON SOURCE LINES 81-96 .. code-block:: Python # graph with extended gflow but no flow graph = nx.Graph([(0, 1), (0, 2), (0, 4), (1, 5), (2, 4), (2, 5), (3, 5)]) inputs = [0, 1] outputs = [4, 5] measurements = { 0: Measurement.XY(0), 1: Measurement.XY(0), 2: Measurement.XZ(0), 3: Measurement.YZ(0), } og = OpenGraph(graph, inputs, outputs, measurements) vis = GraphVisualizer(og) vis.visualize(show_measurement_planes=True) .. image-sg:: /gallery/images/sphx_glr_visualization_006.png :alt: visualization :srcset: /gallery/images/sphx_glr_visualization_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Pauli flow detected in the graph (causal flow not detected) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.269 seconds) .. _sphx_glr_download_gallery_visualization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: visualization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: visualization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: visualization.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_