From fca9e9596d10f22d6838bd332d502002ef4fbc8e Mon Sep 17 00:00:00 2001 From: Martin Klacan Date: Sat, 7 Jun 2025 21:59:54 +0200 Subject: [PATCH] added tutorial notebook small_molecular_system --- docs/tutorials/small_molecular_system.ipynb | 245 ++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 docs/tutorials/small_molecular_system.ipynb diff --git a/docs/tutorials/small_molecular_system.ipynb b/docs/tutorials/small_molecular_system.ipynb new file mode 100644 index 0000000..251d721 --- /dev/null +++ b/docs/tutorials/small_molecular_system.ipynb @@ -0,0 +1,245 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "9a67bc05", + "metadata": {}, + "source": [ + "# Small Molecular System Simulation with OQD Analog Interface\n", + "\n", + "This tutorial demonstrates how to simulate a minimal model of a small molecule (e.g., H₂) using the OQD analog interface and oqd-analog-emulator. We construct a two-qubit Hamiltonian representing the electronic structure in a minimal basis, simulate its time evolution, and analyze observables such as population and entanglement entropy.\n", + "\n", + "References:\n", + "- See e.g. McArdle et al., Rev. Mod. Phys. 92, 015003 (2020) for quantum simulation of chemistry." + ] + }, + { + "cell_type": "markdown", + "id": "e6bfbf16", + "metadata": {}, + "source": [ + "## 1. Introduction\n", + "\n", + "Quantum simulation of molecules is a key application of quantum computing. Here, we use a minimal two-qubit model to represent the H₂ molecule in a minimal basis. The Hamiltonian is expressed in terms of Pauli operators, and we simulate its dynamics using the OQD analog interface." + ] + }, + { + "cell_type": "markdown", + "id": "a70deb53", + "metadata": {}, + "source": [ + "## 2. Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d336975e", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "from oqd_core.interface.analog.operator import PauliI, PauliX, PauliZ\n", + "from oqd_core.interface.analog.operation import AnalogCircuit, AnalogGate\n", + "from oqd_core.backend.metric import Expectation, EntanglementEntropyVN\n", + "from oqd_core.backend.task import Task, TaskArgsAnalog\n", + "from oqd_analog_emulator.qutip_backend import QutipBackend" + ] + }, + { + "cell_type": "markdown", + "id": "af972b16", + "metadata": {}, + "source": [ + "## 3. Define the Hamiltonian\n", + "\n", + "We use a simple two-qubit Hamiltonian as a minimal model for H₂:\n", + "\n", + "$$\n", + "H = c_0 I + c_1 Z_0 + c_2 Z_1 + c_3 Z_0 Z_1 + c_4 X_0 X_1\n", + "$$\n", + "\n", + "where $I$ is the identity, $Z$ and $X$ are Pauli operators, and the coefficients are chosen for illustration." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55339d47", + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "\n", + "def tensor(ops):\n", + " return reduce(lambda a, b: a @ b, ops)\n", + "\n", + "# Example coefficients for minimal H2\n", + "c0 = -1.0\n", + "c1 = 0.5\n", + "c2 = 0.5\n", + "c3 = 0.3\n", + "c4 = 0.2\n", + "\n", + "n = 2 # two qubits for minimal H₂\n", + "\n", + "H = (\n", + " c0 * tensor([PauliI(), PauliI()]) +\n", + " c1 * tensor([PauliZ(), PauliI()]) +\n", + " c2 * tensor([PauliI(), PauliZ()]) +\n", + " c3 * tensor([PauliZ(), PauliZ()]) +\n", + " c4 * tensor([PauliX(), PauliX()])\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "6a34620f", + "metadata": {}, + "source": [ + "## 4. Prepare the Initial State\n", + "\n", + "We prepare the system in the $|01\\rangle$ state (electron in orbital 1)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "934d96b3", + "metadata": {}, + "outputs": [], + "source": [ + "init_gate = tensor([PauliI(), PauliX()])" + ] + }, + { + "cell_type": "markdown", + "id": "586c1352", + "metadata": {}, + "source": [ + "## 5. Set Up the Analog Quantum Circuit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b12f313e", + "metadata": {}, + "outputs": [], + "source": [ + "circuit = AnalogCircuit()\n", + "circuit.evolve(duration=np.pi/2, gate=AnalogGate(hamiltonian=init_gate)) # prepare |01>\n", + "circuit.evolve(duration=8, gate=AnalogGate(hamiltonian=H))\n", + "circuit.measure()" + ] + }, + { + "cell_type": "markdown", + "id": "9e6ed8a0", + "metadata": {}, + "source": [ + "## 6. Define Observables and Metrics\n", + "\n", + "We measure population observables and entanglement entropy." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8eef2167", + "metadata": {}, + "outputs": [], + "source": [ + "args = TaskArgsAnalog(\n", + " n_shots=1000,\n", + " fock_cutoff=1,\n", + " metrics={\n", + " \"Z0\": Expectation(operator=tensor([PauliZ(), PauliI()])),\n", + " \"Z1\": Expectation(operator=tensor([PauliI(), PauliZ()])),\n", + " \"ZZ\": Expectation(operator=tensor([PauliZ(), PauliZ()])),\n", + " \"XX\": Expectation(operator=tensor([PauliX(), PauliX()])),\n", + " \"S\": EntanglementEntropyVN(qreg=[0])\n", + " },\n", + " dt=1e-2,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "3a5df86a", + "metadata": {}, + "source": [ + "## 7. Run the Simulation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cf29214", + "metadata": {}, + "outputs": [], + "source": [ + "task = Task(program=circuit, args=args)\n", + "backend = QutipBackend()\n", + "results = backend.run(task=task)" + ] + }, + { + "cell_type": "markdown", + "id": "446fd2d8", + "metadata": {}, + "source": [ + "## 8. Plot Results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0af7af97", + "metadata": {}, + "outputs": [], + "source": [ + "sns.set(style=\"whitegrid\")\n", + "plt.figure(figsize=(8, 5))\n", + "plt.plot(results.times, results.metrics[\"Z0\"], label=r\"$\\langle Z_0 \\rangle$\")\n", + "plt.plot(results.times, results.metrics[\"Z1\"], label=r\"$\\langle Z_1 \\rangle$\")\n", + "plt.plot(results.times, results.metrics[\"ZZ\"], label=r\"$\\langle Z_0 Z_1 \\rangle$\")\n", + "plt.plot(results.times, results.metrics[\"XX\"], label=r\"$\\langle X_0 X_1 \\rangle$\")\n", + "plt.plot(results.times, results.metrics[\"S\"], label=\"Entanglement $S$\")\n", + "plt.xlabel(\"Time\")\n", + "plt.ylabel(\"Observable\")\n", + "plt.legend()\n", + "plt.title(\"Dynamics of a Minimal H₂ Molecular Model\")\n", + "plt.tight_layout()\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "id": "4550ae0b", + "metadata": {}, + "source": [ + "## 9. Discussion\n", + "\n", + "This simulation demonstrates the time evolution of a minimal molecular system (H₂) using the OQD analog interface. You can see the dynamics of local observables and entanglement entropy, which are important for understanding quantum chemistry on quantum devices.\n", + "\n", + "For more details on quantum simulation of chemistry, see McArdle et al., Rev. Mod. Phys. 92, 015003 (2020)." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}