.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/rotation.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_rotation.py: Simple example & visualizing graphs =================================== Here, we show a most basic MBQC proramming using graphix library. In this example, we consider trivial problem of the rotation of two qubits in ``|0>`` states. We show how transpiler (:class:`~graphix.transpiler.Circuit` class) can be used, and show the resulting meausrement pattern. In the next example, we describe our visualization tool :class:`~graphix.visualization.GraphVisualizer` and how to understand the plot. First, let us import relevant modules: .. GENERATED FROM PYTHON SOURCE LINES 17-27 .. code-block:: Python from __future__ import annotations import numpy as np from graphix import Circuit, Statevec from graphix.ops import Ops from graphix.states import BasicStates rng = np.random.default_rng() .. GENERATED FROM PYTHON SOURCE LINES 28-32 Here, :class:`~graphix.sim.statevec.Statevec` is our simple statevector simulator class. Next, let us define the problem with a standard quantum circuit. Note that in graphix all qubits starts in ``|+>`` states. For this example, we use Hadamard gate (:meth:`graphix.transpiler.Circuit.h`) to start with ``|0>`` states instead. .. GENERATED FROM PYTHON SOURCE LINES 32-44 .. code-block:: Python circuit = Circuit(2) # initialize qubits in |0>, not |+> circuit.h(1) circuit.h(0) # apply rotation gates theta = rng.random(2) circuit.rx(0, theta[0]) circuit.rx(1, theta[1]) .. GENERATED FROM PYTHON SOURCE LINES 45-47 Now we transpile into measurement pattern using :meth:`~graphix.transpiler.Circuit.transpile` method. This returns :class:`~graphix.pattern.Pattern` object containing measurement pattern: .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python pattern = circuit.transpile().pattern pattern.print_pattern(lim=10) .. rst-class:: sphx-glr-script-out .. code-block:: none N, node = 2 E, nodes = (1, 2) M, node = 1, plane = Plane.XY, angle(pi) = 0.0, s_domain = set(), t_domain = set() X byproduct, node = 2, domain = {1} N, node = 3 E, nodes = (0, 3) M, node = 0, plane = Plane.XY, angle(pi) = 0.0, s_domain = set(), t_domain = set() X byproduct, node = 3, domain = {0} N, node = 4 N, node = 5 14 more commands truncated. Change lim argument of print_pattern() to show more .. GENERATED FROM PYTHON SOURCE LINES 52-64 We can plot the graph state to run the above pattern. Since there's no two-qubit gates applied to the two qubits in the original gate sequence, we see decoupled 1D graphs representing the evolution of single qubits. The arrows are the ``information flow `` of the MBQC pattern, obtained using the flow-finding algorithm implemented in :class:`graphix.gflow.flow`. 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 64-66 .. code-block:: Python pattern.draw_graph(flow_from_pattern=False) .. image-sg:: /gallery/images/sphx_glr_rotation_001.png :alt: rotation :srcset: /gallery/images/sphx_glr_rotation_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 67-70 we can directly simulate the measurement pattern, to obtain the output state. Internally, we are executing the command sequence we inspected above on a statevector simulator. We also have a tensornetwork simulation backend to handle larger MBQC patterns. see other examples for how to use it. .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: Python out_state = pattern.simulate_pattern(backend="statevector") print(out_state.flatten()) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0.95069005-0.04346661j -0.01078664-0.23592248j -0.00870058-0.19029674j -0.04722389+0.00215913j] .. GENERATED FROM PYTHON SOURCE LINES 75-76 Let us compare with statevector simulation of the original circuit: .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: Python state = Statevec(nqubit=2, data=BasicStates.ZERO) # starts with |0> states state.evolve_single(Ops.rx(theta[0]), 0) state.evolve_single(Ops.rx(theta[1]), 1) 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: 1.0 .. GENERATED FROM PYTHON SOURCE LINES 83-87 Now let us compile more complex pattern and inspect the graph using the visualization tool. Here, the additional edges with dotted lines are the ones that correspond to CNOT gates, which creates entanglement between the 1D clusters (nodes connected with directed edges) corresponding to the time evolution of a single qubit in the original circuit. .. GENERATED FROM PYTHON SOURCE LINES 87-102 .. code-block:: Python circuit = Circuit(2) # apply rotation gates theta = rng.random(4) circuit.rz(0, theta[0]) circuit.rz(1, theta[1]) circuit.cnot(0, 1) circuit.s(0) circuit.cnot(1, 0) circuit.rz(1, theta[2]) circuit.cnot(1, 0) circuit.rz(0, theta[3]) pattern = circuit.transpile().pattern pattern.draw_graph(flow_from_pattern=False) .. image-sg:: /gallery/images/sphx_glr_rotation_002.png :alt: rotation :srcset: /gallery/images/sphx_glr_rotation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Flow detected in the graph. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.201 seconds) .. _sphx_glr_download_gallery_rotation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: rotation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: rotation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: rotation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_