.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/deutsch_jozsa.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_deutsch_jozsa.py: Preprocessing Clifford gates ============================ In this example, we implement the Deutsch-Jozsa algorithm which determines whether a function is *balanced* or *constant*. Since this algorithm is written only with Clifford gates, we can expect the preprocessing of Clifford gates would significantly improve the MBQC pattern simulation. You can find nice description of the algorithm `here `_. First, let us import relevant modules: .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: Python from __future__ import annotations import numpy as np from graphix import Circuit from graphix.command import CommandKind .. GENERATED FROM PYTHON SOURCE LINES 23-25 Now we implement the algorithm with quantum circuit, which we can transpile into MBQC. As an example, we look at balanced oracle for 4 qubits. .. GENERATED FROM PYTHON SOURCE LINES 25-57 .. code-block:: Python circuit = Circuit(4) # prepare all qubits in |0> for easier comparison with original algorithm for i in range(4): circuit.h(i) # initialization circuit.h(0) circuit.h(1) circuit.h(2) # prepare ancilla circuit.x(3) circuit.h(3) # balanced oracle - flip qubits 0 and 2 circuit.x(0) circuit.x(2) # algorithm circuit.cnot(0, 3) circuit.cnot(1, 3) circuit.cnot(2, 3) circuit.x(0) circuit.x(2) circuit.h(0) circuit.h(1) circuit.h(2) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Now let us transpile into MBQC measurement pattern and inspect the pattern sequence and graph state .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: Python pattern = circuit.transpile().pattern pattern.print_pattern(lim=15) pattern.draw_graph(flow_from_pattern=False) .. image-sg:: /gallery/images/sphx_glr_deutsch_jozsa_001.png :alt: deutsch jozsa :srcset: /gallery/images/sphx_glr_deutsch_jozsa_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none N, node = 4 E, nodes = (0, 4) M, node = 0, plane = Plane.XY, angle(pi) = 0.0, s_domain = set(), t_domain = set() X byproduct, node = 4, domain = {0} N, node = 5 E, nodes = (1, 5) M, node = 1, plane = Plane.XY, angle(pi) = 0.0, s_domain = set(), t_domain = set() X byproduct, node = 5, domain = {1} N, node = 6 E, nodes = (2, 6) M, node = 2, plane = Plane.XY, angle(pi) = 0.0, s_domain = set(), t_domain = set() X byproduct, node = 6, domain = {2} N, node = 7 E, nodes = (3, 7) M, node = 3, plane = Plane.XY, angle(pi) = 0.0, s_domain = set(), t_domain = set() 99 more commands truncated. Change lim argument of print_pattern() to show more Flow detected in the graph. .. GENERATED FROM PYTHON SOURCE LINES 65-68 this seems to require quite a large graph state. However, we know that Pauli measurements can be preprocessed with graph state simulator. To do so, let us first standardize and shift signals, so that measurements are less interdependent. .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: Python pattern.standardize() pattern.shift_signals() pattern.print_pattern(lim=15) .. rst-class:: sphx-glr-script-out .. code-block:: none N, node = 4 N, node = 5 N, node = 6 N, node = 7 N, node = 8 N, node = 9 N, node = 10 N, node = 11 N, node = 12 N, node = 13 N, node = 14 N, node = 15 N, node = 16 N, node = 17 N, node = 18 77 more commands truncated. Change lim argument of print_pattern() to show more .. GENERATED FROM PYTHON SOURCE LINES 74-75 Now we preprocess all Pauli measurements .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: Python pattern.perform_pauli_measurements() pattern.print_pattern(lim=16, target=[CommandKind.N, CommandKind.M, CommandKind.C]) pattern.draw_graph(flow_from_pattern=True) .. image-sg:: /gallery/images/sphx_glr_deutsch_jozsa_002.png :alt: deutsch jozsa :srcset: /gallery/images/sphx_glr_deutsch_jozsa_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none N, node = 28 N, node = 29 N, node = 30 N, node = 23 Clifford, node = 28, Clifford = \sqrt{iY} Clifford, node = 29, Clifford = \sqrt{iY} Clifford, node = 30, Clifford = \sqrt{iY} Clifford, node = 23, Clifford = Z The pattern is not consistent with flow or gflow structure. .. GENERATED FROM PYTHON SOURCE LINES 81-84 Since all operations of the original circuit are Clifford, all measurements in the measurement pattern are Pauli measurements: So the preprocessing has done all the necessary computations, and all nodes are isolated with no further measurements required. Let us make sure the result is correct: .. GENERATED FROM PYTHON SOURCE LINES 84-89 .. code-block:: Python out_state = pattern.simulate_pattern() state = circuit.simulate_statevector().statevec print("overlap of states: ", np.abs(np.dot(state.psi.flatten().conjugate(), out_state.psi.flatten()))) .. rst-class:: sphx-glr-script-out .. code-block:: none overlap of states: 0.9999999999999982 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.278 seconds) .. _sphx_glr_download_gallery_deutsch_jozsa.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: deutsch_jozsa.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: deutsch_jozsa.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: deutsch_jozsa.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_