.. 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 :ref:`Go to the end ` 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. .. GENERATED FROM PYTHON SOURCE LINES 10-31 .. code-block:: Python from __future__ import annotations import networkx as nx import numpy as np from graphix import Circuit rng = np.random.default_rng() n = 4 xi = rng.random(6) theta = rng.random(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 32-33 transpile and get the graph state .. GENERATED FROM PYTHON SOURCE LINES 33-40 .. code-block:: Python 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 41-42 perform Pauli measurements and plot the new (minimal) graph to perform the same quantum computation .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: Python 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 47-48 finally, simulate the QAOA circuit .. GENERATED FROM PYTHON SOURCE LINES 48-54 .. 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()))) # sphinx_gallery_thumbnail_number = 2 .. rst-class:: sphx-glr-script-out .. code-block:: none overlap of states: 0.9999999999999998 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.452 seconds) .. _sphx_glr_download_gallery_qaoa.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: qaoa.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: qaoa.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: qaoa.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_