{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# An Introduction to Algorithms in Qiskit\n", "\n", "This is an introduction to algorithms in Qiskit and provides a high-level overview to help understand the various aspects of the functionality to get started. Other tutorials will provide more in-depth material, on given algorithms, and ways to use them etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How is the algorithm library structured?\n", "\n", "Qiskit provides a number of [Algorithms](https://qiskit.org/documentation/apidoc/algorithms.html) and they are grouped by category according to the task they can perform. For instance `Minimum Eigensolvers` to find the smallest eigen value of an operator, for example ground state energy of a chemistry Hamiltonian or a solution to an optimization problem when expressed as an Ising Hamiltonian. There are `Linear Solvers` for linear systems of equations problems and `Amplitude Estimators` for value estimation that can be used say in financial applications. The full set of categories can be seen in the Algorithms documentation link above.\n", "\n", "Algorithms are configurable and often part of the configuration will be in the form of smaller building blocks, of which different instances of the building block type can be given. For instance with `VQE`, the Variational Quantum Eigensolver, it takes a trial wavefunction, in the form of a `QuantumCircuit` and a classical optimizer among other things.\n", "\n", "Let's take a look at an example to construct a VQE instance. Here `TwoLocal` is the variational form (trial wavefunction), a parameterized circuit which can be varied, and `SLSQP` a classical optimizer. These are created as separate instances and passed to VQE when it is constructed. Trying, for example, a different classical optimizer, or variational form is simply a case of creating an instance of the one you want and passing it into VQE." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from qiskit.algorithms import VQE\n", "from qiskit.algorithms.optimizers import SLSQP\n", "from qiskit.circuit.library import TwoLocal\n", "\n", "num_qubits = 2\n", "ansatz = TwoLocal(num_qubits, 'ry', 'cz')\n", "opt = SLSQP(maxiter=1000)\n", "vqe = VQE(ansatz, optimizer=opt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's draw the ansatz so we can see it's a QuantumCircuit where θ\\[0\\] through θ\\[7\\] will be the parameters that are varied as VQE optimizer finds the minimum eigenvalue. We'll come back to the parameters later in a working example below." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
```     ┌──────────┐   ┌──────────┐   ┌──────────┐   ┌──────────┐\n",
"q_0: ┤ RY(θ) ├─■─┤ RY(θ) ├─■─┤ RY(θ) ├─■─┤ RY(θ) ├\n",
"     ├──────────┤ │ ├──────────┤ │ ├──────────┤ │ ├──────────┤\n",
"q_1: ┤ RY(θ) ├─■─┤ RY(θ) ├─■─┤ RY(θ) ├─■─┤ RY(θ) ├\n",
"     └──────────┘   └──────────┘   └──────────┘   └──────────┘```

### Version Information

Qiskit SoftwareVersion
QiskitNone
Terra0.18.0.dev0+5920b66
Aer0.9.0
Ignis0.7.0.dev0+8195559
AquaNone
IBM Q ProviderNone
System information
Python3.8.8 (default, Apr 13 2021, 12:59:45) \n", "[Clang 10.0.0 ]
OSDarwin
CPUs2
Memory (Gb)12.0
Thu May 27 10:59:11 2021 EDT
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "