Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit bea3999

Browse files
woodsp-ibmjaygambetta
authored andcommitted
Aqua 0.5 AI domain and Aqua updates (#649)
1 parent 0f9eae8 commit bea3999

14 files changed

Lines changed: 2310 additions & 374 deletions
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<img src=\"../../images/qiskit-heading.gif\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# _*Qiskit Aqua: Tutorial to build a pluggable algorithm/component*_ \n",
15+
"\n",
16+
"The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorial.\n",
17+
"\n",
18+
"***\n",
19+
"### Contributors\n",
20+
"Richard Chen<sup>[1]</sup>, Shaohan Hu<sup>[1]</sup>, Antonio Mezzacapo<sup>[1]</sup>, Peng Liu<sup>[1]</sup>, Marco Pistoia<sup>[1]</sup>, Stephen Wood<sup>[1]</sup>\n",
21+
"### Affiliation\n",
22+
"- <sup>[1]</sup>IBMQ"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"### Introduction\n",
30+
"\n",
31+
"This notebook is part of the simple example of an algorithm for `Qiskit Aqua`. This sample, called `Evolution Fidelity`, illustrates how to implement an algorithm and what steps to take to configure and run it. The implementation of the algorithm itself is located in the `evolutionfidelity` directory."
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"### Register a pluggable algorithm/components\n",
39+
"After you complete the algorithm implementation, there are two approaches to register your pluggable algorithm/components.\n",
40+
"\n",
41+
"**Note: you can use the developed algorithm directly in Python without any issue.**\n",
42+
"\n",
43+
"1. Register it permentally. \n",
44+
"2. Register it on-the-fly.\n",
45+
"\n",
46+
"\n",
47+
"#### Register it permentally\n",
48+
"If you complete the pluggable algorithm/components as a Python package, you can refer to this [instruction](https://qiskit.org/documentation/aqua/extending.html#extending-aqua) to prepare the `setup.py` file to register the pluggable algorithm/component, which will be discovered in Qiskit-Aqua. We prepare a [setup.py](evolutionfidelity/setup.py) example for this tutorial. \n",
49+
"\n",
50+
"Go to the `qiskit/aqua/general/evolutionfidelity` folder, and then do `python3 setup.py install` to install the package.\n",
51+
"\n",
52+
"#### Register it on-the-fly\n",
53+
"You can also register it on-the-fly, Aqua provides a function to register your pluggable algorithm/compomenet easily. The following cell shows how to do that."
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 1,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"from qiskit.aqua import register_pluggable\n",
63+
"from evolutionfidelity.evolutionfidelity import EvolutionFidelity\n",
64+
"try:\n",
65+
" register_pluggable(EvolutionFidelity)\n",
66+
"except Exception as e:\n",
67+
" print(e)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 2,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"from qiskit import BasicAer\n",
77+
"import numpy as np\n",
78+
"from qiskit.aqua.operator import Operator\n",
79+
"from qiskit.aqua import local_pluggables, PluggableType"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"List all registered algorithms, and we will find `EvolutionFidelity` in the list if you registered."
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 3,
92+
"metadata": {},
93+
"outputs": [
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"['QAOA.Variational', 'VQC', 'VQE', 'ExactEigensolver', 'ExactLSsolver', 'SVM', 'EOH', 'QSVM', 'AmplitudeEstimation', 'BernsteinVazirani', 'DeutschJozsa', 'Grover', 'HHL', 'IQPE', 'QPE', 'Shor', 'Simon', 'EvolutionFidelity']\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"print(local_pluggables(PluggableType.ALGORITHM))"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"### Use the algorithm directly\n",
111+
"Without registration, you can still use the designed algorithm/components as is."
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 4,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"0.934847761060059\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"from evolutionfidelity.evolutionfidelity import EvolutionFidelity\n",
129+
"from qiskit.aqua.components.initial_states import Zero\n",
130+
"from qiskit.aqua import QuantumInstance\n",
131+
"num_qubits = 2\n",
132+
"temp = np.random.random((2 ** num_qubits, 2 ** num_qubits))\n",
133+
"qubit_op = Operator(matrix=temp + temp.T)\n",
134+
"\n",
135+
"initial_state = Zero(qubit_op.num_qubits)\n",
136+
"\n",
137+
"algo = EvolutionFidelity(qubit_op, initial_state, expansion_order=1)\n",
138+
"\n",
139+
"backend = BasicAer.get_backend('statevector_simulator')\n",
140+
"quantum_instance = QuantumInstance(backend=backend)\n",
141+
"\n",
142+
"result = algo.run(quantum_instance)\n",
143+
"print(result['score'])"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"### Use the algorithm via a declarative approach\n",
151+
"After you register the package, you will be able to discover the algorithm declaratively."
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 5,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": [
160+
"from qiskit.aqua.input import EnergyInput\n",
161+
"from qiskit.aqua import run_algorithm\n",
162+
"\n",
163+
"params = {\n",
164+
" 'problem': {\n",
165+
" 'name': 'eoh'\n",
166+
" },\n",
167+
" 'algorithm': {\n",
168+
" 'name': 'EvolutionFidelity',\n",
169+
" 'expansion_order': 1\n",
170+
" },\n",
171+
" 'initial_state': {\n",
172+
" 'name': 'ZERO'\n",
173+
" }\n",
174+
"}\n",
175+
"\n",
176+
"algo_input = EnergyInput(qubit_op)"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": 6,
182+
"metadata": {},
183+
"outputs": [
184+
{
185+
"name": "stdout",
186+
"output_type": "stream",
187+
"text": [
188+
"0.9348477610600592\n"
189+
]
190+
}
191+
],
192+
"source": [
193+
"result = run_algorithm(params, algo_input, backend=backend)\n",
194+
"print(result['score'])"
195+
]
196+
}
197+
],
198+
"metadata": {
199+
"kernelspec": {
200+
"display_name": "Python 3",
201+
"language": "python",
202+
"name": "python3"
203+
},
204+
"language_info": {
205+
"codemirror_mode": {
206+
"name": "ipython",
207+
"version": 3
208+
},
209+
"file_extension": ".py",
210+
"mimetype": "text/x-python",
211+
"name": "python",
212+
"nbconvert_exporter": "python",
213+
"pygments_lexer": "ipython3",
214+
"version": "3.6.1"
215+
}
216+
},
217+
"nbformat": 4,
218+
"nbformat_minor": 2
219+
}

qiskit/aqua/amplitude_estimation.ipynb

Lines changed: 255 additions & 0 deletions
Large diffs are not rendered by default.

qiskit/aqua/artificial_intelligence/index.ipynb

Lines changed: 0 additions & 50 deletions
This file was deleted.

qiskit/aqua/general/evolutionfidelity/evolutionfidelity/__init__.py renamed to qiskit/aqua/evolutionfidelity/evolutionfidelity/__init__.py

File renamed without changes.

qiskit/aqua/general/evolutionfidelity/evolutionfidelity/evolutionfidelity.py renamed to qiskit/aqua/evolutionfidelity/evolutionfidelity/evolutionfidelity.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
This is a simple tutorial example to show how to build an algorithm to extend
2020
Qiskit Aqua library. Algorithms are designed to be dynamically discovered within
2121
Qiskit Aqua. For this the entire parent directory 'evolutionfidelity' should
22-
be moved under the 'qiskit_aqua' directory. The current demonstration notebook
22+
be moved under the 'qiskit/aqua' directory. The current demonstration notebook
2323
shows how to explicitly register the algorithm and works without re-locating this
2424
code. The former automatic discovery does however allow the algorithm to be found
2525
and seen in the UI browser, and selected from the GUI when choosing an algorithm.
@@ -31,8 +31,8 @@
3131
from qiskit import QuantumRegister
3232
from qiskit.quantum_info import state_fidelity
3333

34-
from qiskit_aqua.algorithms import QuantumAlgorithm
35-
from qiskit_aqua import AquaError, PluggableType, get_pluggable_class
34+
from qiskit.aqua.algorithms import QuantumAlgorithm
35+
from qiskit.aqua import AquaError, Pluggable, PluggableType, get_pluggable_class
3636

3737
logger = logging.getLogger(__name__)
3838

@@ -74,18 +74,21 @@ class EvolutionFidelity(QuantumAlgorithm):
7474
'additionalProperties': False
7575
},
7676
'problems': ['eoh'],
77-
'depends': ['initial_state'],
78-
'defaults': {
79-
'initial_state': {
80-
'name': 'ZERO'
81-
}
82-
}
77+
'depends': [
78+
{
79+
'pluggable_type': 'initial_state',
80+
'default': {
81+
'name': 'ZERO',
82+
}
83+
},
84+
]
8385
}
8486

8587
"""
8688
If directly use these objects programmatically then the constructor is more convenient to call
8789
than init_params. init_params itself uses this to do the actual object initialization.
8890
"""
91+
8992
def __init__(self, operator, initial_state, expansion_order=1):
9093
self.validate(locals())
9194
super().__init__()
@@ -113,14 +116,14 @@ def init_params(cls, params, algo_input):
113116

114117
operator = algo_input.qubit_op
115118

116-
evolution_fidelity_params = params.get(QuantumAlgorithm.SECTION_KEY_ALGORITHM)
119+
evolution_fidelity_params = params.get(Pluggable.SECTION_KEY_ALGORITHM)
117120
expansion_order = evolution_fidelity_params.get(EvolutionFidelity.PROP_EXPANSION_ORDER)
118121

119122
# Set up initial state, we need to add computed num qubits to params
120-
initial_state_params = params.get(QuantumAlgorithm.SECTION_KEY_INITIAL_STATE)
123+
initial_state_params = params.get(Pluggable.SECTION_KEY_INITIAL_STATE)
121124
initial_state_params['num_qubits'] = operator.num_qubits
122125
initial_state = get_pluggable_class(PluggableType.INITIAL_STATE,
123-
initial_state_params['name']).init_params(initial_state_params)
126+
initial_state_params['name']).init_params(params)
124127

125128
return cls(operator, initial_state, expansion_order)
126129

@@ -130,6 +133,7 @@ def init_params(cls, params, algo_input):
130133
131134
E.g., the `_run` method is required to be implemented for an algorithm.
132135
"""
136+
133137
def _run(self):
134138
evo_time = 1
135139
# get the groundtruth via simple matrix * vector

0 commit comments

Comments
 (0)