.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/qaoa.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_qaoa.py: QAOA ==== Here we generate and optimize pattern for QAOA circuit. 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=qaoa.ipynb .. GENERATED FROM PYTHON SOURCE LINES 15-32 .. code-block:: default import networkx as nx import numpy as np from graphix import Circuit n = 4 xi = np.random.rand(6) theta = np.random.rand(4) g = nx.complete_graph(n) circuit = Circuit(n) for i, (u, v) in enumerate(g.edges): circuit.cnot(u, v) circuit.rz(v, xi[i]) circuit.cnot(u, v) for v in g.nodes: circuit.rx(v, theta[v]) .. GENERATED FROM PYTHON SOURCE LINES 33-34 transpile and get the graph state .. GENERATED FROM PYTHON SOURCE LINES 34-41 .. code-block:: default pattern = circuit.transpile().pattern pattern.standardize() pattern.shift_signals() pattern.draw_graph(flow_from_pattern=False) .. image-sg:: /gallery/images/sphx_glr_qaoa_001.png :alt: qaoa :srcset: /gallery/images/sphx_glr_qaoa_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 42-43 perform Pauli measurements and plot the new (minimal) graph to perform the same quantum computation .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: default pattern.perform_pauli_measurements() pattern.draw_graph(flow_from_pattern=False) .. image-sg:: /gallery/images/sphx_glr_qaoa_002.png :alt: qaoa :srcset: /gallery/images/sphx_glr_qaoa_002.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 48-49 finally, simulate the QAOA circuit .. GENERATED FROM PYTHON SOURCE LINES 49-55 .. 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()))) # sphinx_gallery_thumbnail_number = 2 .. rst-class:: sphx-glr-script-out .. code-block:: none overlap of states: 0.9999999999999993 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.284 seconds) .. _sphx_glr_download_gallery_qaoa.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: qaoa.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: qaoa.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_