.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/MBQCvqe.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_MBQCvqe.py: Variational Quantum Eigensolver (VQE) with Measurement-Based Quantum Computing (MBQC) ===================================================================================== In this example, we solve a simple VQE problem using a measurement-based quantum computing (MBQC) approach. The Hamiltonian for the system is given by: .. math:: H = Z_0 Z_1 + X_0 + X_1 where :math:`Z` and :math:`X` are the Pauli-Z and Pauli-X matrices, respectively. This Hamiltonian corresponds to a simple model system often used in quantum computing to demonstrate algorithms like VQE. The goal is to find the ground state energy of this Hamiltonian. We will build a parameterized quantum circuit and optimize its parameters to minimize the expectation value of the Hamiltonian, effectively finding the ground state energy. .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: default import networkx as nx import numpy as np from scipy.optimize import minimize from graphix import Circuit from graphix.simulator import PatternSimulator .. GENERATED FROM PYTHON SOURCE LINES 31-32 Define the Hamiltonian for the VQE problem (Example: H = Z0Z1 + X0 + X1) .. GENERATED FROM PYTHON SOURCE LINES 32-39 .. code-block:: default def create_hamiltonian(): Z = np.array([[1, 0], [0, -1]]) X = np.array([[0, 1], [1, 0]]) H = np.kron(Z, Z) + np.kron(X, np.eye(2)) + np.kron(np.eye(2), X) return H .. GENERATED FROM PYTHON SOURCE LINES 40-41 Function to build the VQE circuit .. GENERATED FROM PYTHON SOURCE LINES 41-52 .. code-block:: default def build_vqe_circuit(n_qubits, params): circuit = Circuit(n_qubits) for i in range(n_qubits): circuit.rx(i, params[i]) circuit.ry(i, params[i + n_qubits]) circuit.rz(i, params[i + 2 * n_qubits]) for i in range(n_qubits - 1): circuit.cnot(i, i + 1) return circuit .. GENERATED FROM PYTHON SOURCE LINES 53-93 .. code-block:: default class MBQCVQE: def __init__(self, n_qubits, hamiltonian): self.n_qubits = n_qubits self.hamiltonian = hamiltonian # %% # Function to build the MBQC pattern def build_mbqc_pattern(self, params): circuit = build_vqe_circuit(self.n_qubits, params) pattern = circuit.transpile().pattern pattern.standardize() pattern.shift_signals() return pattern # %% # Function to simulate the MBQC circuit def simulate_mbqc(self, params, backend="tensornetwork"): pattern = self.build_mbqc_pattern(params) pattern.perform_pauli_measurements() # Perform Pauli measurements simulator = PatternSimulator(pattern, backend=backend) if backend == "tensornetwork": tn = simulator.run() # Simulate the MBQC circuit using tensor network tn.default_output_nodes = pattern.output_nodes # Set the default_output_nodes attribute if tn.default_output_nodes is None: raise ValueError("Output nodes are not set for tensor network simulation.") return tn else: out_state = simulator.run() # Simulate the MBQC circuit using other backends return out_state # %% # Function to compute the energy def compute_energy(self, params): # Simulate the MBQC circuit using tensor network backend tn = self.simulate_mbqc(params, backend="tensornetwork") # Compute the expectation value using MBQCTensornet.expectation_value energy = tn.expectation_value(self.hamiltonian, qubit_indices=range(self.n_qubits)) return energy .. GENERATED FROM PYTHON SOURCE LINES 94-95 Set parameters for VQE .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: default n_qubits = 2 hamiltonian = create_hamiltonian() .. GENERATED FROM PYTHON SOURCE LINES 99-100 Instantiate the MBQCVQE class .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: default mbqc_vqe = MBQCVQE(n_qubits, hamiltonian) .. GENERATED FROM PYTHON SOURCE LINES 104-105 Define the cost function .. GENERATED FROM PYTHON SOURCE LINES 105-109 .. code-block:: default def cost_function(params): return mbqc_vqe.compute_energy(params) .. GENERATED FROM PYTHON SOURCE LINES 110-111 Random initial parameters .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: default initial_params = np.random.rand(n_qubits * 3) .. GENERATED FROM PYTHON SOURCE LINES 114-115 Perform the optimization using COBYLA .. GENERATED FROM PYTHON SOURCE LINES 115-120 .. code-block:: default result = minimize(cost_function, initial_params, method="COBYLA", options={"maxiter": 100}) print(f"Optimized parameters: {result.x}") print(f"Optimized energy: {result.fun}") .. rst-class:: sphx-glr-script-out .. code-block:: none Optimized parameters: [7.92559649e-01 1.60045028e-01 3.37733745e-05 4.63651204e-01 3.09991182e-05 3.14167999e+00] Optimized energy: -2.2360679697233765 .. GENERATED FROM PYTHON SOURCE LINES 121-122 Compare with the analytical solution .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: default analytical_solution = -np.sqrt(2) - 1 print(f"Analytical solution: {analytical_solution}") .. rst-class:: sphx-glr-script-out .. code-block:: none Analytical solution: -2.414213562373095 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 7.712 seconds) .. _sphx_glr_download_gallery_MBQCvqe.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: MBQCvqe.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: MBQCvqe.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_