|
198 | 198 | None, |
199 | 199 | 'pennylane-implementations'), |
200 | 200 | ('Iris Dataset', 2, None, 'iris-dataset'), |
| 201 | + ('Qiskit implementation', 2, None, 'qiskit-implementation'), |
201 | 202 | ('Plans for next week', 2, None, 'plans-for-next-week')]} |
202 | 203 | end of tocinfo --> |
203 | 204 |
|
|
308 | 309 | <!-- navigation toc: --> <li><a href="#discussion-of-implementation" style="font-size: 80%;">Discussion of Implementation</a></li> |
309 | 310 | <!-- navigation toc: --> <li><a href="#pennylane-implementations" style="font-size: 80%;">PennyLane implementations</a></li> |
310 | 311 | <!-- navigation toc: --> <li><a href="#iris-dataset" style="font-size: 80%;">Iris Dataset</a></li> |
| 312 | + <!-- navigation toc: --> <li><a href="#qiskit-implementation" style="font-size: 80%;">Qiskit implementation</a></li> |
311 | 313 | <!-- navigation toc: --> <li><a href="#plans-for-next-week" style="font-size: 80%;">Plans for next week</a></li> |
312 | 314 |
|
313 | 315 | </ul> |
@@ -2284,6 +2286,72 @@ <h2 id="iris-dataset" class="anchor">Iris Dataset </h2> |
2284 | 2286 | </div> |
2285 | 2287 |
|
2286 | 2288 |
|
| 2289 | +<!-- !split --> |
| 2290 | +<h2 id="qiskit-implementation" class="anchor">Qiskit implementation </h2> |
| 2291 | + |
| 2292 | +<p>A similar implementation on Qiskit is given here (you need to have installed Qiskit as well).</p> |
| 2293 | + |
| 2294 | +<!-- code=python (!bc pycod) typeset with pygments style "default" --> |
| 2295 | +<div class="cell border-box-sizing code_cell rendered"> |
| 2296 | + <div class="input"> |
| 2297 | + <div class="inner_cell"> |
| 2298 | + <div class="input_area"> |
| 2299 | + <div class="highlight" style="background: #f8f8f8"> |
| 2300 | + <pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">qiskit</span> <span style="color: #008000; font-weight: bold">import</span> BasicAer |
| 2301 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">qiskit.utils</span> <span style="color: #008000; font-weight: bold">import</span> QuantumInstance |
| 2302 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">qiskit.circuit.library</span> <span style="color: #008000; font-weight: bold">import</span> ZZFeatureMap |
| 2303 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">qiskit_machine_learning.algorithms</span> <span style="color: #008000; font-weight: bold">import</span> QSVC |
| 2304 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split |
| 2305 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_iris |
| 2306 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler |
| 2307 | + |
| 2308 | +<span style="color: #408080; font-style: italic"># Load and preprocess dataset</span> |
| 2309 | +dataset <span style="color: #666666">=</span> load_iris() |
| 2310 | +X <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>data |
| 2311 | +y <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>target |
| 2312 | + |
| 2313 | +<span style="color: #408080; font-style: italic"># For simplicity, we'll only classify between two classes</span> |
| 2314 | +X <span style="color: #666666">=</span> X[y <span style="color: #666666">!=</span> <span style="color: #666666">2</span>] |
| 2315 | +y <span style="color: #666666">=</span> y[y <span style="color: #666666">!=</span> <span style="color: #666666">2</span>] |
| 2316 | + |
| 2317 | +<span style="color: #408080; font-style: italic"># Standardize features</span> |
| 2318 | +scaler <span style="color: #666666">=</span> StandardScaler() |
| 2319 | +X <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>fit_transform(X) |
| 2320 | + |
| 2321 | +<span style="color: #408080; font-style: italic"># Split into training and test sets</span> |
| 2322 | +X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>, random_state<span style="color: #666666">=42</span>) |
| 2323 | + |
| 2324 | +<span style="color: #408080; font-style: italic"># Define quantum feature map</span> |
| 2325 | +feature_map <span style="color: #666666">=</span> ZZFeatureMap(feature_dimension<span style="color: #666666">=4</span>, reps<span style="color: #666666">=2</span>) |
| 2326 | + |
| 2327 | +<span style="color: #408080; font-style: italic"># Set up quantum instance</span> |
| 2328 | +quantum_instance <span style="color: #666666">=</span> QuantumInstance(BasicAer<span style="color: #666666">.</span>get_backend(<span style="color: #BA2121">'qasm_simulator'</span>), shots<span style="color: #666666">=1024</span>) |
| 2329 | + |
| 2330 | +<span style="color: #408080; font-style: italic"># Initialize QSVC</span> |
| 2331 | +qsvc <span style="color: #666666">=</span> QSVC(quantum_instance<span style="color: #666666">=</span>quantum_instance, feature_map<span style="color: #666666">=</span>feature_map) |
| 2332 | + |
| 2333 | +<span style="color: #408080; font-style: italic"># Train QSVC</span> |
| 2334 | +qsvc<span style="color: #666666">.</span>fit(X_train, y_train) |
| 2335 | + |
| 2336 | +<span style="color: #408080; font-style: italic"># Evaluate QSVC</span> |
| 2337 | +accuracy <span style="color: #666666">=</span> qsvc<span style="color: #666666">.</span>score(X_test, y_test) |
| 2338 | +<span style="color: #008000">print</span>(<span style="color: #BA2121">f'QSVC accuracy: </span><span style="color: #BB6688; font-weight: bold">{</span>accuracy <span style="color: #666666">*</span> <span style="color: #666666">100</span><span style="color: #BB6688; font-weight: bold">:</span><span style="color: #BA2121">.2f</span><span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">%'</span>) |
| 2339 | +</pre> |
| 2340 | +</div> |
| 2341 | + </div> |
| 2342 | + </div> |
| 2343 | + </div> |
| 2344 | + <div class="output_wrapper"> |
| 2345 | + <div class="output"> |
| 2346 | + <div class="output_area"> |
| 2347 | + <div class="output_subarea output_stream output_stdout output_text"> |
| 2348 | + </div> |
| 2349 | + </div> |
| 2350 | + </div> |
| 2351 | + </div> |
| 2352 | +</div> |
| 2353 | + |
| 2354 | + |
2287 | 2355 | <!-- !split --> |
2288 | 2356 | <h2 id="plans-for-next-week" class="anchor">Plans for next week </h2> |
2289 | 2357 | <ol> |
|
0 commit comments