|
141 | 141 | 2, |
142 | 142 | None, |
143 | 143 | 'training-svm-with-precomputed-quantum-kernels'), |
144 | | - ('Example: Kernel SVM in PennyLane', |
145 | | - 2, |
146 | | - None, |
147 | | - 'example-kernel-svm-in-pennylane'), |
148 | 144 | ('Discussion of Implementation', |
149 | 145 | 2, |
150 | 146 | None, |
|
157 | 153 | 2, |
158 | 154 | None, |
159 | 155 | 'steps-in-quantum-kernel-svm'), |
| 156 | + ('The Iris data and classical SVM', |
| 157 | + 2, |
| 158 | + None, |
| 159 | + 'the-iris-data-and-classical-svm'), |
160 | 160 | ('Iris Dataset', 2, None, 'iris-dataset'), |
161 | 161 | ('Qiskit implementation', 2, None, 'qiskit-implementation'), |
162 | 162 | ('Credit data classification', |
|
318 | 318 | <!-- navigation toc: --> <li><a href="#defining-quantum-feature-maps-in-pennylane" style="font-size: 80%;">Defining Quantum Feature Maps in PennyLane</a></li> |
319 | 319 | <!-- navigation toc: --> <li><a href="#computing-quantum-kernel-matrices" style="font-size: 80%;">Computing Quantum Kernel Matrices</a></li> |
320 | 320 | <!-- navigation toc: --> <li><a href="#training-svm-with-precomputed-quantum-kernels" style="font-size: 80%;">Training SVM with Precomputed Quantum Kernels</a></li> |
321 | | - <!-- navigation toc: --> <li><a href="#example-kernel-svm-in-pennylane" style="font-size: 80%;">Example: Kernel SVM in PennyLane</a></li> |
322 | 321 | <!-- navigation toc: --> <li><a href="#discussion-of-implementation" style="font-size: 80%;">Discussion of Implementation</a></li> |
323 | 322 | <!-- navigation toc: --> <li><a href="#pennylane-implementations" style="font-size: 80%;">PennyLane implementations</a></li> |
324 | 323 | <!-- navigation toc: --> <li><a href="#steps-in-quantum-kernel-svm" style="font-size: 80%;">Steps in Quantum Kernel SVM</a></li> |
| 324 | + <!-- navigation toc: --> <li><a href="#the-iris-data-and-classical-svm" style="font-size: 80%;">The Iris data and classical SVM</a></li> |
325 | 325 | <!-- navigation toc: --> <li><a href="#iris-dataset" style="font-size: 80%;">Iris Dataset</a></li> |
326 | 326 | <!-- navigation toc: --> <li><a href="#qiskit-implementation" style="font-size: 80%;">Qiskit implementation</a></li> |
327 | 327 | <!-- navigation toc: --> <li><a href="#credit-data-classification" style="font-size: 80%;">Credit data classification</a></li> |
@@ -1342,67 +1342,6 @@ <h2 id="training-svm-with-precomputed-quantum-kernels" class="anchor">Training S |
1342 | 1342 |
|
1343 | 1343 | <p>It is also possible to integrate PennyLane’s differentiable capabilities by defining a parameterized kernel and optimizing parameters via gradient descent, but here we keep a fixed feature map.</p> |
1344 | 1344 |
|
1345 | | -<!-- !split --> |
1346 | | -<h2 id="example-kernel-svm-in-pennylane" class="anchor">Example: Kernel SVM in PennyLane </h2> |
1347 | | - |
1348 | | -<p>Below is a compact example that ties it together for a toy 2D dataset:</p> |
1349 | | - |
1350 | | -<!-- code=python (!bc pycod) typeset with pygments style "default" --> |
1351 | | -<div class="cell border-box-sizing code_cell rendered"> |
1352 | | - <div class="input"> |
1353 | | - <div class="inner_cell"> |
1354 | | - <div class="input_area"> |
1355 | | - <div class="highlight" style="background: #f8f8f8"> |
1356 | | - <pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pennylane</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">qml</span> |
1357 | | -<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pennylane</span> <span style="color: #008000; font-weight: bold">import</span> numpy <span style="color: #008000; font-weight: bold">as</span> np |
1358 | | -<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> svm |
1359 | | - |
1360 | | -<span style="color: #408080; font-style: italic"># Example dataset</span> |
1361 | | -X_train <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[<span style="color: #666666">0.1</span>, <span style="color: #666666">0.2</span>], [<span style="color: #666666">1.1</span>, <span style="color: #666666">-0.4</span>], [<span style="color: #666666">0.0</span>, <span style="color: #666666">0.9</span>], [<span style="color: #666666">1.0</span>, <span style="color: #666666">0.5</span>]]) |
1362 | | -Y_train <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">+1</span>, <span style="color: #666666">+1</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">-1</span>]) |
1363 | | -X_test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[<span style="color: #666666">0.2</span>, <span style="color: #666666">0.1</span>], [<span style="color: #666666">0.9</span>, <span style="color: #666666">0.0</span>]]) |
1364 | | - |
1365 | | -<span style="color: #408080; font-style: italic"># Define device and feature map</span> |
1366 | | -dev <span style="color: #666666">=</span> qml<span style="color: #666666">.</span>device(<span style="color: #BA2121">"default.qubit"</span>, wires<span style="color: #666666">=2</span>) |
1367 | | - |
1368 | | -<span style="color: #AA22FF">@qml</span><span style="color: #666666">.</span>qnode(dev) |
1369 | | -<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">kernel_circuit</span>(x1, x2): |
1370 | | - qml<span style="color: #666666">.</span>templates<span style="color: #666666">.</span>AngleEmbedding(x1, wires<span style="color: #666666">=</span>[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]) |
1371 | | - qml<span style="color: #666666">.</span>adjoint(qml<span style="color: #666666">.</span>templates<span style="color: #666666">.</span>AngleEmbedding)(x2, wires<span style="color: #666666">=</span>[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]) |
1372 | | - <span style="color: #008000; font-weight: bold">return</span> qml<span style="color: #666666">.</span>probs(wires<span style="color: #666666">=</span>[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>])[<span style="color: #666666">0</span>] |
1373 | | - |
1374 | | -kernel <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">lambda</span> a, b: kernel_circuit(a, b) |
1375 | | - |
1376 | | -<span style="color: #408080; font-style: italic"># Compute kernel matrices</span> |
1377 | | -K_train <span style="color: #666666">=</span> qml<span style="color: #666666">.</span>kernels<span style="color: #666666">.</span>kernel_matrix(X_train, X_train, kernel_function<span style="color: #666666">=</span>kernel) |
1378 | | -K_test <span style="color: #666666">=</span> qml<span style="color: #666666">.</span>kernels<span style="color: #666666">.</span>kernel_matrix(X_test, X_train, kernel_function<span style="color: #666666">=</span>kernel) |
1379 | | - |
1380 | | -<span style="color: #408080; font-style: italic"># Train SVM</span> |
1381 | | -clf <span style="color: #666666">=</span> svm<span style="color: #666666">.</span>SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">'precomputed'</span>) |
1382 | | -clf<span style="color: #666666">.</span>fit(K_train, Y_train) |
1383 | | -predictions <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(K_test) |
1384 | | -<span style="color: #008000">print</span>(<span style="color: #BA2121">"Predicted labels:"</span>, predictions) |
1385 | | -</pre> |
1386 | | -</div> |
1387 | | - </div> |
1388 | | - </div> |
1389 | | - </div> |
1390 | | - <div class="output_wrapper"> |
1391 | | - <div class="output"> |
1392 | | - <div class="output_area"> |
1393 | | - <div class="output_subarea output_stream output_stdout output_text"> |
1394 | | - </div> |
1395 | | - </div> |
1396 | | - </div> |
1397 | | - </div> |
1398 | | -</div> |
1399 | | - |
1400 | | -<p>This code snippet demonstrates defining a quantum kernel via |
1401 | | -AngleEmbedding and using it with scikit-learn. In a real application, |
1402 | | -one would use more data points and possibly include more sophisticated |
1403 | | -feature maps. |
1404 | | -</p> |
1405 | | - |
1406 | 1345 | <!-- !split --> |
1407 | 1346 | <h2 id="discussion-of-implementation" class="anchor">Discussion of Implementation </h2> |
1408 | 1347 |
|
@@ -1445,6 +1384,69 @@ <h2 id="steps-in-quantum-kernel-svm" class="anchor">Steps in Quantum Kernel SVM |
1445 | 1384 | <li> New data is classified using the trained SVM model with the quantum kernel.</li> |
1446 | 1385 | </ul> |
1447 | 1386 | </ol> |
| 1387 | +<!-- !split --> |
| 1388 | +<h2 id="the-iris-data-and-classical-svm" class="anchor">The Iris data and classical SVM </h2> |
| 1389 | + |
| 1390 | + |
| 1391 | +<!-- code=python (!bc pycod) typeset with pygments style "default" --> |
| 1392 | +<div class="cell border-box-sizing code_cell rendered"> |
| 1393 | + <div class="input"> |
| 1394 | + <div class="inner_cell"> |
| 1395 | + <div class="input_area"> |
| 1396 | + <div class="highlight" style="background: #f8f8f8"> |
| 1397 | + <pre style="line-height: 125%;"><span style="color: #408080; font-style: italic"># import the required packages</span> |
| 1398 | +<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span> |
| 1399 | +<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span> |
| 1400 | +<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 |
| 1401 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC |
| 1402 | +<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 |
| 1403 | +<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score,classification_report,f1_score |
| 1404 | +iris <span style="color: #666666">=</span> load_iris() |
| 1405 | +dataset<span style="color: #666666">=</span>pd<span style="color: #666666">.</span>DataFrame(iris<span style="color: #666666">.</span>data,columns<span style="color: #666666">=</span>iris<span style="color: #666666">.</span>feature_names) |
| 1406 | +dataset[<span style="color: #BA2121">"target"</span>]<span style="color: #666666">=</span>iris<span style="color: #666666">.</span>target |
| 1407 | +dataset |
| 1408 | +features<span style="color: #666666">=</span> dataset[dataset<span style="color: #666666">.</span>columns[<span style="color: #666666">1</span>:<span style="color: #666666">4</span>]] |
| 1409 | +target<span style="color: #666666">=</span>dataset[<span style="color: #BA2121">"target"</span>] |
| 1410 | +<span style="color: #408080; font-style: italic"># split the data into training set and testing set </span> |
| 1411 | +X_train,X_test,Y_train,Y_test<span style="color: #666666">=</span>train_test_split(features,target,test_size<span style="color: #666666">=0.25</span>,random_state<span style="color: #666666">=42</span>) |
| 1412 | +<span style="color: #408080; font-style: italic"># Scale the data</span> |
| 1413 | +<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> MinMaxScaler |
| 1414 | + |
| 1415 | +minmax<span style="color: #666666">=</span>MinMaxScaler() |
| 1416 | +X_train<span style="color: #666666">=</span>minmax<span style="color: #666666">.</span>fit_transform(X_train) |
| 1417 | +X_test<span style="color: #666666">=</span>minmax<span style="color: #666666">.</span>transform(X_test) |
| 1418 | +X_test<span style="color: #666666">=</span>np<span style="color: #666666">.</span>clip(X_test,<span style="color: #666666">0</span>,<span style="color: #666666">1</span>) |
| 1419 | +<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">time</span> |
| 1420 | +start_time<span style="color: #666666">=</span>time<span style="color: #666666">.</span>time() |
| 1421 | +svm_model<span style="color: #666666">=</span>SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">"rbf"</span>) |
| 1422 | +svm_model<span style="color: #666666">.</span>fit(X_train, Y_train) |
| 1423 | +end_time<span style="color: #666666">=</span>time<span style="color: #666666">.</span>time() |
| 1424 | +<span style="color: #008000">print</span>(end_time<span style="color: #666666">-</span>start_time) |
| 1425 | +predictions <span style="color: #666666">=</span> svm_model<span style="color: #666666">.</span>predict(X_test) |
| 1426 | + |
| 1427 | +<span style="color: #408080; font-style: italic"># Calculate accuracy and other metrics</span> |
| 1428 | +predictions<span style="color: #666666">=</span>svm_model<span style="color: #666666">.</span>predict(X_test) |
| 1429 | +accuracy<span style="color: #666666">=</span>accuracy_score(Y_test,predictions) |
| 1430 | +<span style="color: #008000">print</span>(<span style="color: #BA2121">f"the accuracy is: </span><span style="color: #BB6688; font-weight: bold">{</span>accuracy<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>) |
| 1431 | +f1score<span style="color: #666666">=</span>f1_score(Y_test,predictions,average<span style="color: #666666">=</span><span style="color: #BA2121">'macro'</span>) |
| 1432 | +<span style="color: #008000">print</span>(<span style="color: #BA2121">f"the f1-score is:</span><span style="color: #BB6688; font-weight: bold">{</span>f1score<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>) |
| 1433 | +<span style="color: #008000">print</span>(classification_report(Y_test,predictions)) |
| 1434 | +</pre> |
| 1435 | +</div> |
| 1436 | + </div> |
| 1437 | + </div> |
| 1438 | + </div> |
| 1439 | + <div class="output_wrapper"> |
| 1440 | + <div class="output"> |
| 1441 | + <div class="output_area"> |
| 1442 | + <div class="output_subarea output_stream output_stdout output_text"> |
| 1443 | + </div> |
| 1444 | + </div> |
| 1445 | + </div> |
| 1446 | + </div> |
| 1447 | +</div> |
| 1448 | + |
| 1449 | + |
1448 | 1450 | <!-- !split --> |
1449 | 1451 | <h2 id="iris-dataset" class="anchor">Iris Dataset </h2> |
1450 | 1452 |
|
@@ -2495,6 +2497,7 @@ <h2 id="more-on-applications" class="anchor">More on applications </h2> |
2495 | 2497 | <!-- M. Zhao et al., “A tutorial on quantum machine learning and quantum neural networks,” arXiv:2504.16131 (2025) . --> |
2496 | 2498 | <!-- D. M. Houshmand, Quantum Machine Learning Lecture Notes, (online, 2023) . (Tutorial using PennyLane QNNs.) --> |
2497 | 2499 | <!-- PennyLane documentation and tutorials: 〈https://pennylane.ai〉 (for code examples and parameter-shift rules). --> |
| 2500 | + |
2498 | 2501 | <!-- ------------------- end of main content --------------- --> |
2499 | 2502 | </div> <!-- end container --> |
2500 | 2503 | <!-- include javascript, jQuery *first* --> |
|
0 commit comments