.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/qft_with_tn.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_qft_with_tn.py: Large-scale simulations with tensor network simulator ===================================================== In this example, we demonstrate simulation of MBQC involving 10k+ nodes. Firstly, let us import relevant modules and define the circuit: .. GENERATED FROM PYTHON SOURCE LINES 11-44 .. code-block:: Python from __future__ import annotations import numpy as np from graphix import Circuit def cp(circuit, theta, control, target): circuit.rz(control, theta / 2) circuit.rz(target, theta / 2) circuit.cnot(control, target) circuit.rz(target, -1 * theta / 2) circuit.cnot(control, target) def qft_rotations(circuit, n): circuit.h(n) for qubit in range(n + 1, circuit.width): cp(circuit, np.pi / 2 ** (qubit - n), qubit, n) def swap_registers(circuit, n): for qubit in range(n // 2): circuit.swap(qubit, n - qubit - 1) return circuit def qft(circuit, n): for i in range(n): qft_rotations(circuit, i) swap_registers(circuit, n) .. GENERATED FROM PYTHON SOURCE LINES 45-46 We will simulate 55-qubit QFT, which requires graph states with more than 10000 nodes. .. GENERATED FROM PYTHON SOURCE LINES 46-63 .. code-block:: Python n = 55 print(f"{n}-qubit QFT") circuit = Circuit(n) for i in range(n): circuit.h(i) qft(circuit, n) # standardize pattern pattern = circuit.transpile().pattern pattern.standardize() pattern.shift_signals() nodes, edges = pattern.get_graph() print(f"Number of nodes: {len(nodes)}") print(f"Number of edges: {len(edges)}") .. rst-class:: sphx-glr-script-out .. code-block:: none 55-qubit QFT Number of nodes: 15015 Number of edges: 17930 .. GENERATED FROM PYTHON SOURCE LINES 64-66 Using efficient graph state simulator `graphix.graphsim`, we can classically preprocess Pauli measurements. We are currently improving the speed of this process by using rust-based graph manipulation backend. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python pattern.perform_pauli_measurements() .. GENERATED FROM PYTHON SOURCE LINES 70-72 To specify TN backend of the simulation, simply provide as a keyword argument. here we do a very basic check that one of the statevector amplitudes is what it is expected to be: .. GENERATED FROM PYTHON SOURCE LINES 72-83 .. code-block:: Python import time t1 = time.time() tn = pattern.simulate_pattern(backend="tensornetwork") value = tn.get_basis_amplitude(0) t2 = time.time() print("amplitude of |00...0> is ", value) print("1/2^n (true answer) is", 1 / 2**n) print("approximate execution time in seconds: ", t2 - t1) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/graphix/checkouts/v0.3.1/graphix/sim/tensornet.py:698: ComplexWarning: Casting complex values to real discards the imaginary part Ops.CZ.reshape((2, 2, 2, 2)).astype(np.float64), amplitude of |00...0> is 2.775557561562515e-17 1/2^n (true answer) is 2.7755575615628914e-17 approximate execution time in seconds: 16.89583659172058 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 58.976 seconds) .. _sphx_glr_download_gallery_qft_with_tn.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: qft_with_tn.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: qft_with_tn.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: qft_with_tn.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_