.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pattern_fragments.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_pattern_fragments.py: Optimized pattern fragments =========================== Graphix offers several pattern fragments for quantum gates, pre-optimized by reducing the Pauli measurements, with fewer resource requirement than the standard pattern. .. GENERATED FROM PYTHON SOURCE LINES 11-13 First, for Toffoli gate, here is the pattern based on the decomposition of CCX gate with CNOT and single-qubit rotations, turned into a measurement pattern: .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: default import numpy as np from graphix import Circuit circuit = Circuit(3) circuit.ccx(0, 1, 2) pattern = circuit.transpile().pattern pattern.draw_graph(flow_from_pattern=False) .. image-sg:: /gallery/images/sphx_glr_pattern_fragments_001.png :alt: pattern fragments :srcset: /gallery/images/sphx_glr_pattern_fragments_001.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 23-25 Using :code:`opt=True` option for :code:`transpile` method, we switch to patterns with non-XY plane measurements allowed, which has gflow (not flow). For CCX gate, the number of ancilla qubits required is nearly halved: .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: default pattern = circuit.transpile(opt=True).pattern pattern.draw_graph(node_distance=(1.2, 0.8)) # sphinx_gallery_thumbnail_number = 2 .. image-sg:: /gallery/images/sphx_glr_pattern_fragments_002.png :alt: pattern fragments :srcset: /gallery/images/sphx_glr_pattern_fragments_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none The pattern is not consistent with flow or gflow structure. .. GENERATED FROM PYTHON SOURCE LINES 30-32 Now let us add z-rotation gates, requiring two ancillas in the original pattern fragment, which becomes one for patterns with :code:`opt=True`. .. GENERATED FROM PYTHON SOURCE LINES 32-39 .. code-block:: default circuit = Circuit(3) circuit.ccx(0, 1, 2) for i in range(3): circuit.rz(i, np.pi / 4) pattern = circuit.transpile(opt=True).pattern pattern.draw_graph(flow_from_pattern=True, node_distance=(1, 0.5)) .. image-sg:: /gallery/images/sphx_glr_pattern_fragments_003.png :alt: pattern fragments :srcset: /gallery/images/sphx_glr_pattern_fragments_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none The pattern is not consistent with flow or gflow structure. .. GENERATED FROM PYTHON SOURCE LINES 40-41 Swap gate is just a swap of node indices during compilation, requiring no ancillas. .. GENERATED FROM PYTHON SOURCE LINES 41-51 .. code-block:: default circuit = Circuit(3) circuit.ccx(0, 1, 2) circuit.swap(1, 2) circuit.swap(2, 0) for i in range(3): circuit.rz(i, np.pi / 4) pattern = circuit.transpile(opt=True).pattern pattern.draw_graph(flow_from_pattern=False, node_distance=(1, 0.4)) .. image-sg:: /gallery/images/sphx_glr_pattern_fragments_004.png :alt: pattern fragments :srcset: /gallery/images/sphx_glr_pattern_fragments_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Gflow detected in the graph. (flow not detected) .. GENERATED FROM PYTHON SOURCE LINES 52-53 using :code:`opt=True` and with either CCX, Rzz or Rz gates, the graph will have gflow. .. GENERATED FROM PYTHON SOURCE LINES 53-68 .. code-block:: default circuit = Circuit(4) circuit.cnot(1, 2) circuit.cnot(0, 3) circuit.ccx(2, 1, 0) circuit.rx(0, np.pi / 3) circuit.cnot(0, 3) circuit.rzz(0, 3, np.pi / 3) circuit.rx(2, np.pi / 3) circuit.ccx(3, 1, 2) circuit.rx(0, np.pi / 3) circuit.rx(3, np.pi / 3) pattern = circuit.transpile(opt=True).pattern pattern.draw_graph(flow_from_pattern=False, node_distance=(1, 0.4)) .. image-sg:: /gallery/images/sphx_glr_pattern_fragments_005.png :alt: pattern fragments :srcset: /gallery/images/sphx_glr_pattern_fragments_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Gflow detected in the graph. (flow not detected) .. GENERATED FROM PYTHON SOURCE LINES 69-70 reducing the size further .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default pattern.perform_pauli_measurements() pattern.draw_graph(flow_from_pattern=False, node_distance=(1, 0.6)) .. image-sg:: /gallery/images/sphx_glr_pattern_fragments_006.png :alt: pattern fragments :srcset: /gallery/images/sphx_glr_pattern_fragments_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Gflow detected in the graph. (flow not detected) .. GENERATED FROM PYTHON SOURCE LINES 74-75 For linear optical QPUs with single photons, such a resource state can be generated by fusing the follwoing microcluster states: .. GENERATED FROM PYTHON SOURCE LINES 75-83 .. code-block:: default from graphix.extraction import get_fusion_network_from_graph nodes, edges = pattern.get_graph() from graphix import GraphState gs = GraphState(nodes=nodes, edges=edges) get_fusion_network_from_graph(gs, max_ghz=4, max_lin=4) .. rst-class:: sphx-glr-script-out .. code-block:: none [GHZ[35, 30, 28, 26], GHZ[36, 39, 34, 30], GHZ[18, 24, 12, 11], GHZ[32, 33, 29, 28], GHZ[35, 1, 17], GHZ[35, 8, 12], LINEAR[9, 18, 10, 35], LINEAR[24, 25, 32, 26], GHZ[36, 27, 29], GHZ[28, 36], LINEAR[34, 22, 23, 20], GHZ[37, 38, 20], GHZ[39, 40]] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.652 seconds) .. _sphx_glr_download_gallery_pattern_fragments.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pattern_fragments.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pattern_fragments.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_