.. 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-29 .. code-block:: default import numpy as np from scipy.optimize import minimize from graphix import Circuit from graphix.simulator import PatternSimulator .. GENERATED FROM PYTHON SOURCE LINES 30-31 Define the Hamiltonian for the VQE problem (Example: H = Z0Z1 + X0 + X1) .. GENERATED FROM PYTHON SOURCE LINES 31-38 .. 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 39-40 Function to build the VQE circuit .. GENERATED FROM PYTHON SOURCE LINES 40-51 .. 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 52-92 .. 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 93-94 Set parameters for VQE .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: default n_qubits = 2 hamiltonian = create_hamiltonian() .. GENERATED FROM PYTHON SOURCE LINES 98-99 Instantiate the MBQCVQE class .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. code-block:: default mbqc_vqe = MBQCVQE(n_qubits, hamiltonian) .. GENERATED FROM PYTHON SOURCE LINES 103-104 Define the cost function .. GENERATED FROM PYTHON SOURCE LINES 104-108 .. code-block:: default def cost_function(params): return mbqc_vqe.compute_energy(params) .. GENERATED FROM PYTHON SOURCE LINES 109-110 Random initial parameters .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default initial_params = np.random.rand(n_qubits * 3) .. GENERATED FROM PYTHON SOURCE LINES 113-114 Perform the optimization using COBYLA .. GENERATED FROM PYTHON SOURCE LINES 114-119 .. 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: [2.39565949 1.21916607 2.93331344 1.48893398 0.47249169 2.28461542] Optimized energy: -1.003547148395943 .. GENERATED FROM PYTHON SOURCE LINES 120-121 Compare with the analytical solution .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. 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.233 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 `_