{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic example of use of simulated annealing\n", "\n", "Let us consider the one-dimensional Ising model of an antiferromagnet described by the Hamiltonian \n", "\n", "$$ H = J \\sum_{\\langle i, j \\rangle} \\sigma_i \\sigma_j$$\n", "\n", "where $\\langle i, j \\rangle$ denotes nearest neighbour sites, magnetic coupling J > 0 for antiferromagnetism and $\\sigma_i$ is the respective Pauli spin matrix acting on site $i$.\n", "\n", "We want to use the simulated annealing implemented on the QLM to solve this problem. The following example presents how one can do so." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Encoding and solving the problem" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from qat.qpus import SimulatedAnnealing\n", "from qat.core import Observable, Schedule, Variable, Term\n", "\n", "# Define the the problem\n", "n_spins = 100\n", "mag_coupling = 1 # >0 is antiferromagnetic, <0 is ferromagnetic\n", " \n", "# Create a temperature function\n", "t = Variable(\"t\", float)\n", "temp_t = 2 * (1 - t) + 0.001 # annealing requires going from a high to a very low temperature\n", "\n", "# Create a QPU\n", "n_steps = 500\n", "qpu = SimulatedAnnealing(temp_t=temp_t,\n", " n_steps=n_steps,\n", " seed=817)\n", "\n", "# Create an observable\n", "observable = Observable(n_spins, pauli_terms=[Term(mag_coupling, \"ZZ\", [i, i + 1])\n", " for i in range(n_spins - 1)])\n", "\n", "# Create a schedule for the annealing, a job, and send it to the QPU\n", "drive = [(1, observable)]\n", "schedule = Schedule(drive=drive)\n", "job = schedule.to_job(nbshots=1)\n", "result = qpu.submit(job)\n", "\n", "# Show the solution\n", "for sample in result:\n", " print(sample.state)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution\n", "\n", "We can see that as expected for an antiferromagnet, the resulting spin configuration is alternating spins up and spins down which translates to alternating $|0>$ and $|1>$ qubit states." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "documentation-tags": { "icon": [ ":material-outlined:`swap_vert;40px`", ":material-outlined:`grid_4x4;40px`" ], "level": "beginner" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.11" } }, "nbformat": 4, "nbformat_minor": 4 }