.. 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 Click :ref:`here ` 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 `_. You can run this code on your browser with `mybinder.org `_ - click the badge below. .. image:: https://mybinder.org/badge_logo.svg :target: https://mybinder.org/v2/gh/TeamGraphix/graphix-examples/HEAD?labpath=deutsch-jozsa.ipynb First, let us import relevant modules: .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: default import matplotlib.pyplot as plt import networkx as nx .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: default import numpy as np from graphix import Circuit .. GENERATED FROM PYTHON SOURCE LINES 29-31 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 31-63 .. code-block:: default 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 64-65 Now let us transpile into MBQC measurement pattern and inspect the pattern sequence and graph state .. GENERATED FROM PYTHON SOURCE LINES 65-70 .. code-block:: default 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 = [], t_domain = [], Clifford index = 0 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 = [], t_domain = [], Clifford index = 0 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 = [], t_domain = [], Clifford index = 0 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 = [], t_domain = [], Clifford index = 0 99 more commands truncated. Change lim argument of print_pattern() to show more Flow detected in the graph. .. GENERATED FROM PYTHON SOURCE LINES 71-74 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 74-79 .. code-block:: default 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 80-81 Now we preprocess all Pauli measurements .. GENERATED FROM PYTHON SOURCE LINES 81-86 .. code-block:: default pattern.perform_pauli_measurements() pattern.print_pattern(lim=16, filter=["N", "M", "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 The pattern is not consistent with flow or gflow structure. .. GENERATED FROM PYTHON SOURCE LINES 87-90 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 90-95 .. code-block:: default 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.9999999999999989 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.686 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-python :download:`Download Python source code: deutsch-jozsa.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: deutsch-jozsa.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_