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

Commit eb6baf8

Browse files
liupibmjaygambetta
authored andcommitted
optimization + ai notebooks updated (#457)
* optimization + ai * optimization + ai * variational rerun
1 parent de9c824 commit eb6baf8

15 files changed

Lines changed: 1746 additions & 370 deletions

community/aqua/artificial_intelligence/qsvm_kernel_directly.ipynb

Lines changed: 120 additions & 34 deletions
Large diffs are not rendered by default.

community/aqua/artificial_intelligence/qsvm_kernel_multiclass.ipynb

Lines changed: 18 additions & 21 deletions
Large diffs are not rendered by default.

community/aqua/artificial_intelligence/qsvm_variational.ipynb

Lines changed: 128 additions & 26 deletions
Large diffs are not rendered by default.

community/aqua/artificial_intelligence/svm_classical.ipynb

Lines changed: 32 additions & 37 deletions
Large diffs are not rendered by default.

community/aqua/artificial_intelligence/svm_classical_multiclass.ipynb

Lines changed: 13 additions & 14 deletions
Large diffs are not rendered by default.
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## _*Using Qiskit Aqua for clique problems*_\n",
8+
"\n",
9+
"This Qiskit Aqua Optimization notebook demonstrates how to use the VQE quantum algorithm to compute the clique of a given graph. \n",
10+
"\n",
11+
"The problem is defined as follows. A clique in a graph $G$ is a complete subgraph of $G$. That is, it is a subset $K$ of the vertices such that every two vertices in $K$ are the two endpoints of an edge in $G$. A maximal clique is a clique to which no more vertices can be added. A maximum clique is a clique that includes the largest possible number of vertices. \n",
12+
"\n",
13+
"We will go through three examples to show (1) how to run the optimization in the non-programming way, (2) how to run the optimization in the programming way, (3) how to run the optimization with the VQE.\n",
14+
"We will omit the details for the support of CPLEX, which are explained in other notebooks such as maxcut.\n",
15+
"\n",
16+
"Note that the solution may not be unique."
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"### The problem and a brute-force method."
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 1,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"import numpy as np\n",
33+
"\n",
34+
"from qiskit import Aer\n",
35+
"\n",
36+
"from qiskit_aqua import run_algorithm\n",
37+
"from qiskit_aqua.input import EnergyInput\n",
38+
"from qiskit_aqua.translators.ising import clique\n",
39+
"from qiskit_aqua.algorithms import ExactEigensolver"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"first, let us have a look at the graph, which is in the adjacent matrix form."
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 2,
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"name": "stdout",
56+
"output_type": "stream",
57+
"text": [
58+
"[[ 0. 4. 5. 3. -5.]\n",
59+
" [ 4. 0. 7. 0. 6.]\n",
60+
" [ 5. 7. 0. -4. 0.]\n",
61+
" [ 3. 0. -4. 0. 8.]\n",
62+
" [-5. 6. 0. 8. 0.]]\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"K = 3 # K means the size of the clique\n",
68+
"np.random.seed(100)\n",
69+
"num_nodes = 5\n",
70+
"w = clique.random_graph(num_nodes, edge_prob=0.8, weight_range=10)\n",
71+
"print(w) "
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"Let us try a brute-force method. Basically, we exhaustively try all the binary assignments. In each binary assignment, the entry of a vertex is either 0 (meaning the vertex is not in the clique) or 1 (meaning the vertex is in the clique). We print the binary assignment that satisfies the definition of the clique (Note the size is specified as K)."
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 3,
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stdout",
88+
"output_type": "stream",
89+
"text": [
90+
"solution is [1, 0, 0, 1, 1]\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"def brute_force():\n",
96+
" # brute-force way: try every possible assignment!\n",
97+
" def bitfield(n, L):\n",
98+
" result = np.binary_repr(n, L)\n",
99+
" return [int(digit) for digit in result]\n",
100+
"\n",
101+
" L = num_nodes # length of the bitstring that represents the assignment\n",
102+
" max = 2**L\n",
103+
" has_sol = False\n",
104+
" for i in range(max):\n",
105+
" cur = bitfield(i, L)\n",
106+
" cur_v = clique.satisfy_or_not(np.array(cur), w, K)\n",
107+
" if cur_v:\n",
108+
" has_sol = True\n",
109+
" break\n",
110+
" return has_sol, cur\n",
111+
"\n",
112+
"has_sol, sol = brute_force()\n",
113+
"if has_sol:\n",
114+
" print(\"solution is \", sol)\n",
115+
"else:\n",
116+
" print(\"no solution found for K=\", K)"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"### Part I: run the optimization in the non-programming way"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 4,
129+
"metadata": {},
130+
"outputs": [
131+
{
132+
"name": "stdout",
133+
"output_type": "stream",
134+
"text": [
135+
"solution is [1. 0. 1. 1. 0.]\n"
136+
]
137+
}
138+
],
139+
"source": [
140+
"qubit_op, offset = clique.get_clique_qubitops(w, K)\n",
141+
"algo_input = EnergyInput(qubit_op)\n",
142+
"params = {\n",
143+
" 'problem': {'name': 'ising'},\n",
144+
" 'algorithm': {'name': 'ExactEigensolver'}\n",
145+
"}\n",
146+
"result = run_algorithm(params, algo_input)\n",
147+
"x = clique.sample_most_likely(len(w), result['eigvecs'][0])\n",
148+
"ising_sol = clique.get_graph_solution(x)\n",
149+
"if clique.satisfy_or_not(ising_sol, w, K):\n",
150+
" print(\"solution is\", ising_sol)\n",
151+
"else:\n",
152+
" print(\"no solution found for K=\", K)"
153+
]
154+
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {},
158+
"source": [
159+
"### Part II: run the optimization in the programming way"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 5,
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"solution is [1. 0. 1. 1. 0.]\n"
172+
]
173+
}
174+
],
175+
"source": [
176+
"\n",
177+
"algo = ExactEigensolver(algo_input.qubit_op, k=1, aux_operators=[])\n",
178+
"result = algo.run()\n",
179+
"x = clique.sample_most_likely(len(w), result['eigvecs'][0])\n",
180+
"ising_sol = clique.get_graph_solution(x)\n",
181+
"if clique.satisfy_or_not(ising_sol, w, K):\n",
182+
" print(\"solution is\", ising_sol)\n",
183+
"else:\n",
184+
" print(\"no solution found for K=\", K) "
185+
]
186+
},
187+
{
188+
"cell_type": "markdown",
189+
"metadata": {},
190+
"source": [
191+
"### Part III: run the optimization with the VQE"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 6,
197+
"metadata": {},
198+
"outputs": [
199+
{
200+
"name": "stdout",
201+
"output_type": "stream",
202+
"text": [
203+
"solution is [1. 0. 1. 1. 0.]\n"
204+
]
205+
}
206+
],
207+
"source": [
208+
"algorithm_cfg = {\n",
209+
" 'name': 'VQE',\n",
210+
" 'operator_mode': 'matrix'\n",
211+
"}\n",
212+
"\n",
213+
"optimizer_cfg = {\n",
214+
" 'name': 'COBYLA'\n",
215+
"}\n",
216+
"\n",
217+
"var_form_cfg = {\n",
218+
" 'name': 'RY',\n",
219+
" 'depth': 5,\n",
220+
" 'entanglement': 'linear'\n",
221+
"}\n",
222+
"\n",
223+
"params = {\n",
224+
" 'problem': {'name': 'ising', 'random_seed': 10598},\n",
225+
" 'algorithm': algorithm_cfg,\n",
226+
" 'optimizer': optimizer_cfg,\n",
227+
" 'variational_form': var_form_cfg\n",
228+
"}\n",
229+
"backend = Aer.get_backend('statevector_simulator')\n",
230+
"result = run_algorithm(params, algo_input, backend=backend)\n",
231+
"x = clique.sample_most_likely(len(w), result['eigvecs'][0])\n",
232+
"ising_sol = clique.get_graph_solution(x)\n",
233+
"\n",
234+
"if clique.satisfy_or_not(ising_sol, w, K):\n",
235+
" print(\"solution is\", ising_sol)\n",
236+
"else:\n",
237+
" print(\"no solution found for K=\", K)"
238+
]
239+
}
240+
],
241+
"metadata": {
242+
"kernelspec": {
243+
"display_name": "mykernel",
244+
"language": "python",
245+
"name": "mykernel"
246+
},
247+
"language_info": {
248+
"codemirror_mode": {
249+
"name": "ipython",
250+
"version": 3
251+
},
252+
"file_extension": ".py",
253+
"mimetype": "text/x-python",
254+
"name": "python",
255+
"nbconvert_exporter": "python",
256+
"pygments_lexer": "ipython3",
257+
"version": "3.7.1"
258+
}
259+
},
260+
"nbformat": 4,
261+
"nbformat_minor": 2
262+
}

0 commit comments

Comments
 (0)