Skip to content

Commit

Permalink
Tutorial housecleaning: VQE (#1379)
Browse files Browse the repository at this point in the history
Related to #1286

---------

Co-authored-by: Rebecca Dimock <66339736+beckykd@users.noreply.github.com>
  • Loading branch information
abbycross and beckykd authored Jun 13, 2024
1 parent 53a670c commit 01fe8d4
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions tutorials/variational-quantum-eigensolver/vqe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
"id": "a6f69b77",
"metadata": {},
"source": [
"*Usage estimate: 73 minutes on ibm_kyiv or 28 minutes on ibm_manila. (NOTE: This is an estimate only. Your runtime may vary.)*\n",
"\n",
"## Background\n",
"\n",
"[Variational quantum algorithms](https://arxiv.org/abs/2012.09265) are promising candidate hybrid-algorithms for observing the utility of quantum computation on noisy near-term devices. Variational algorithms are characterized by the use of a classical optimization algorithm to iteratively update a parameterized trial solution, or \"ansatz\". Chief among these methods is the Variational Quantum Eigensolver (VQE) that aims to solve for the ground state of a given Hamiltonian represented as a linear combination of Pauli terms, with an ansatz circuit where the number of parameters to optimize over is polynomial in the number of qubits. Given that size of the full solution vector is exponential in the number of qubits, successful minimization using VQE requires, in general, additional problem specific information to define the structure of the ansatz circuit.\n",
"[Variational quantum algorithms](https://arxiv.org/abs/2012.09265) are promising candidate hybrid-algorithms for observing quantum computation utility on noisy near-term devices. Variational algorithms are characterized by the use of a classical optimization algorithm to iteratively update a parameterized trial solution, or \"ansatz\". Chief among these methods is the Variational Quantum Eigensolver (VQE) that aims to solve for the ground state of a given Hamiltonian represented as a linear combination of Pauli terms, with an ansatz circuit where the number of parameters to optimize over is polynomial in the number of qubits. Given that the size of the full solution vector is exponential in the number of qubits, successful minimization using VQE requires, in general, additional problem-specific information to define the structure of the ansatz circuit.\n",
"\n",
"\n",
"Executing a VQE algorithm requires the following 3 components:\n",
"Executing a VQE algorithm requires the following components:\n",
"\n",
" 1. Hamiltonian and ansatz (problem specification)\n",
" 2. Qiskit Runtime estimator\n",
" 3. Classical optimizer\n",
"\n",
"Although the Hamiltonian and ansatz require domain specific knowledge to construct, these details are immaterial to the Runtime, and we can execute a wide class of VQE problems in the same manner."
"Although the Hamiltonian and ansatz require domain-specific knowledge to construct, these details are immaterial to the Runtime, and we can execute a wide class of VQE problems in the same manner."
]
},
{
Expand Down Expand Up @@ -68,30 +70,18 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "bc380c46",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ibm_algiers'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# runtime imports\n",
"from qiskit_ibm_runtime import QiskitRuntimeService, Session\n",
"from qiskit_ibm_runtime import EstimatorV2 as Estimator\n",
"\n",
"# To run on hardware, select the backend with the fewest number of jobs in the queue\n",
"service = QiskitRuntimeService(channel=\"ibm_quantum\")\n",
"backend = service.least_busy(operational=True, simulator=False)\n",
"backend.name"
"backend = service.least_busy(operational=True, simulator=False)"
]
},
{
Expand All @@ -102,9 +92,9 @@
"source": [
"## Step 1: Map classical inputs to a quantum problem\n",
"\n",
"Here we define the problem instance for our VQE algorithm. Although the problem in question can come from a variety of domains, the form for execution through Qiskit Runtime is the same. Qiskit provides a convenience class for expressing Hamiltonians in Pauli form, and a collection of widely used ansatz circuits in the [`qiskit.circuit.library`](https://docs.quantum-computing.ibm.com/api/qiskit/circuit_library).\n",
"Although the problem instance in question for the VQE algorithm can come from a variety of domains, the form for execution through Qiskit Runtime is the same. Qiskit provides a convenience class for expressing Hamiltonians in Pauli form, and a collection of widely used ansatz circuits in the [`qiskit.circuit.library`](https://docs.quantum-computing.ibm.com/api/qiskit/circuit_library).\n",
"\n",
"Here, our example Hamiltonian is derived from a quantum chemistry problem"
"This example Hamiltonian is derived from a quantum chemistry problem."
]
},
{
Expand Down Expand Up @@ -187,33 +177,33 @@
"id": "ac6f36e3",
"metadata": {},
"source": [
"## Step 2: Optimize problem for quantum execution."
"## Step 2: Optimize problem for quantum execution"
]
},
{
"cell_type": "markdown",
"id": "ed01c675-6506-4779-bf71-74f0de9212fb",
"metadata": {},
"source": [
"To reduce the total job execution time, Qiskit Runtime V2 primitives only accept circuits (ansatz) and observables (Hamiltonian) that conforms to the instructions and connectivity supported by the target system (referred to as instruction set architecture (ISA) circuits and observables, respectively)."
"To reduce the total job execution time, Qiskit primitives only accept circuits (ansatz) and observables (Hamiltonian) that conform to the instructions and connectivity supported by the target system (referred to as instruction set architecture (ISA) circuits and observables)."
]
},
{
"cell_type": "markdown",
"id": "3390069d-572c-472c-abb5-9cde12fd82a2",
"metadata": {},
"source": [
"### ISA Circuit"
"### ISA circuit"
]
},
{
"cell_type": "markdown",
"id": "ad6ddd99-b680-4ac4-b2d8-c0ac6266e6e8",
"metadata": {},
"source": [
"We can schedule a series of [`qiskit.transpiler`](https://docs.quantum-computing.ibm.com/api/qiskit/transpiler) passes to optimize our circuit for a selected backend and make it compatible with the instruction set architecture (ISA) of the backend. This can be easily done using a preset pass manager from `qiskit.transpiler` and its `optimization_level` parameter.\n",
"Schedule a series of [`qiskit.transpiler`](https://docs.quantum-computing.ibm.com/api/qiskit/transpiler) passes to optimize the circuit for a selected backend and make it compatible with the backend's ISA. This can be easily done with a preset pass manager from `qiskit.transpiler` and its `optimization_level` parameter.\n",
"\n",
"- [`optimization_level`](https://docs.quantum-computing.ibm.com/api/qiskit/transpiler_preset#preset-pass-manager-generation): The lowest optimization level just does the bare minimum needed to get the circuit running on the device; it maps the circuit qubits to the device qubits and adds swap gates to allow all 2-qubit operations. The highest optimization level is much smarter and uses lots of tricks to reduce the overall gate count. Since multi-qubit gates have high error rates and qubits decohere over time, the shorter circuits should give better results."
"The lowest optimization level does the minimum needed to get the circuit running on the device; it maps the circuit qubits to the device qubits and adds swap gates to allow all two-qubit operations. The highest optimization level is much smarter and uses lots of tricks to reduce the overall gate count. Since multi-qubit gates have high error rates and qubits decohere over time, the shorter circuits should give better results."
]
},
{
Expand Down Expand Up @@ -258,15 +248,15 @@
"id": "aab9e309-d643-496f-ad4b-c90173102ad6",
"metadata": {},
"source": [
"### ISA Observable"
"### ISA observable"
]
},
{
"cell_type": "markdown",
"id": "6c9e5dcd",
"metadata": {},
"source": [
"Similarly, we need to transform the Hamiltonian to make it backend compatible before running jobs with [`Runtime Estimator V2`](https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/qiskit_ibm_runtime.EstimatorV2#run). We can perform the transformation using the `apply_layout` the method of `SparsePauliOp` object."
"Transform the Hamiltonian to make it backend-compatible before running jobs with [`Runtime Estimator V2`](https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/qiskit_ibm_runtime.EstimatorV2#run). Perform the transformation by using the `apply_layout` method of `SparsePauliOp` object."
]
},
{
Expand All @@ -285,7 +275,7 @@
"id": "b4d480b3",
"metadata": {},
"source": [
"## Step 3: Execute using Qiskit Primitives.\n",
"## Step 3: Execute using Qiskit Primitives\n",
"\n",
"Like many classical optimization problems, the solution to a VQE problem can be formulated as minimization of a scalar cost function. By definition, VQE looks to find the ground state solution to a Hamiltonian by optimizing the ansatz circuit parameters to minimize the expectation value (energy) of the Hamiltonian. With the Qiskit Runtime [`Estimator`](https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/qiskit_ibm_runtime.EstimatorV2) directly taking a Hamiltonian and parameterized ansatz, and returning the necessary energy, the cost function for a VQE instance is quite simple.\n",
"\n",
Expand Down Expand Up @@ -611,7 +601,7 @@
"id": "50b94af2",
"metadata": {},
"source": [
"## Step 4: Post-process, return result in classical format."
"## Step 4: Post-process, return result in classical format"
]
},
{
Expand Down

0 comments on commit 01fe8d4

Please sign in to comment.