From 5d33e8b8ccafa4117696f08fb7610cd3148ca334 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 19 Jan 2025 19:47:03 +0700 Subject: [PATCH] Create vqe_demo.ipynb --- quantum/notebooks/vqe_demo.ipynb | 159 +++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 quantum/notebooks/vqe_demo.ipynb diff --git a/quantum/notebooks/vqe_demo.ipynb b/quantum/notebooks/vqe_demo.ipynb new file mode 100644 index 000000000..71427b1ff --- /dev/null +++ b/quantum/notebooks/vqe_demo.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Variational Quantum Eigensolver (VQE) Demo\n", + "\n", + "This notebook demonstrates the Variational Quantum Eigensolver (VQE) algorithm, which is used to find the ground state energy of a quantum system. We will use Qiskit to implement VQE and analyze its performance." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Import necessary libraries\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from qiskit import Aer\n", + "from qiskit.circuit import QuantumCircuit\n", + "from qiskit.algorithms import VQE\n", + "from qiskit.algorithms.optimizers import SLSQP\n", + "from qiskit.primitives import Sampler\n", + "from qiskit.quantum_info import Pauli\n", + "from qiskit.circuit.library import TwoLocal\n", + "from qiskit.quantum_info import Statevector\n", + "from qiskit.quantum_info import Operator\n", + "from qiskit.quantum_info import SparsePauliOp\n", + "from qiskit.quantum_info import PauliExpectation\n", + "from qiskit.utils import QuantumInstance" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Problem Setup\n", + "\n", + "We will use VQE to find the ground state energy of the Hamiltonian for a simple quantum system. For this demonstration, we will use the Hamiltonian of a two-qubit system defined by the following Pauli operators:\n", + "\n", + "\\[ H = -1/2 (Z_0 Z_1 + X_0 X_1) \\]\n", + "\n", + "where \(Z_0\) and \(Z_1\) are the Pauli-Z operators acting on qubits 0 and 1, respectively, and \(X_0\) and \(X_1\) are the Pauli-X operators." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the Hamiltonian\n", + "H = (-1/2) * (SparsePauliOp.from_list([('ZZ', -1), ('XX', -1)]))\n", + "print(H)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2. Ansatz Circuit\n", + "\n", + "Next, we will define the ansatz circuit that will be used in the VQE algorithm. The ansatz is a parameterized quantum circuit that prepares a trial state. We will use a `TwoLocal` circuit with rotation and entangling gates." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the ansatz circuit\n", + "num_qubits = 2\n", + "ansatz = TwoLocal(num_qubits, rotation_blocks='ry', entanglement='cz', reps=2)\n", + "ansatz.draw('mpl')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The ansatz circuit is shown above. It consists of rotation gates followed by entangling gates. Now, we will set up the VQE algorithm." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Set up the VQE algorithm\n", + "backend = Aer.get_backend('aer_simulator')\n", + "sampler = Sampler(backend)\n", + "optimizer = SLSQP(maxiter=100)\n", + "vqe = VQE(ansatz, optimizer=optimizer, sampler=sampler)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. Running VQE\n", + "\n", + "Now we will run the VQE algorithm to find the ground state energy of the Hamiltonian." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# Run VQE\n", + "result = vqe.compute_minimum_eigenvalue(H)\n", + "ground_state_energy = result.eigenvalue.real\n", + "print(f'Ground State Energy: {ground_state_energy:.4f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The ground state energy obtained from VQE is displayed above. This value represents the lowest energy state of the quantum system defined by the Hamiltonian." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4. Conclusion\n", + "\n", + "In this notebook, we demonstrated the Variational Quantum Eigensolver (VQE) algorithm to find the ground state energy of a quantum system. We defined the Hamiltonian, constructed the ansatz circuit, and executed the VQE algorithm using Qiskit. This exploration provides insights into the practical applications of VQE in quantum chemistry and material science." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}