.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/mbqc_vqe.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_mbqc_vqe.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-32 .. code-block:: default import numpy as np from scipy.optimize import minimize from graphix import Circuit from graphix.simulator import PatternSimulator Z = np.array([[1, 0], [0, -1]]) X = np.array([[0, 1], [1, 0]]) .. GENERATED FROM PYTHON SOURCE LINES 33-34 Define the Hamiltonian for the VQE problem (Example: H = Z0Z1 + X0 + X1) .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: default def create_hamiltonian(): return np.kron(Z, Z) + np.kron(X, np.eye(2)) + np.kron(np.eye(2), X) .. 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-90 .. 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": simulator.run() # Simulate the MBQC circuit using tensor network tn = simulator.backend.state 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 return simulator.run() # Simulate the MBQC circuit using other backends # %% # 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 return tn.expectation_value(self.hamiltonian, qubit_indices=range(self.n_qubits)) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Set parameters for VQE .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: default n_qubits = 2 hamiltonian = create_hamiltonian() .. GENERATED FROM PYTHON SOURCE LINES 96-97 Instantiate the MBQCVQE class .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: default mbqc_vqe = MBQCVQE(n_qubits, hamiltonian) .. GENERATED FROM PYTHON SOURCE LINES 101-102 Define the cost function .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: default def cost_function(params): return mbqc_vqe.compute_energy(params) .. GENERATED FROM PYTHON SOURCE LINES 107-108 Random initial parameters .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default rng = np.random.default_rng() initial_params = rng.random(n_qubits * 3) .. GENERATED FROM PYTHON SOURCE LINES 112-113 Perform the optimization using COBYLA .. GENERATED FROM PYTHON SOURCE LINES 113-118 .. 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: [1.29540853e+00 1.26456421e+00 7.17120092e-05 2.67797424e+00 6.49803101e-05 4.71655912e-05] Optimized energy: -2.2360679703687287 .. GENERATED FROM PYTHON SOURCE LINES 119-120 Compare with the analytical solution .. GENERATED FROM PYTHON SOURCE LINES 120-122 .. 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 3.876 seconds) .. _sphx_glr_download_gallery_mbqc_vqe.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: mbqc_vqe.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: mbqc_vqe.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_