Skip to content

Commit 50936d5

Browse files
committed
corrected typos in code
1 parent 1d6256a commit 50936d5

File tree

7 files changed

+545
-534
lines changed

7 files changed

+545
-534
lines changed

doc/pub/week15/html/week15-bs.html

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@
141141
2,
142142
None,
143143
'training-svm-with-precomputed-quantum-kernels'),
144-
('Example: Kernel SVM in PennyLane',
145-
2,
146-
None,
147-
'example-kernel-svm-in-pennylane'),
148144
('Discussion of Implementation',
149145
2,
150146
None,
@@ -157,6 +153,10 @@
157153
2,
158154
None,
159155
'steps-in-quantum-kernel-svm'),
156+
('The Iris data and classical SVM',
157+
2,
158+
None,
159+
'the-iris-data-and-classical-svm'),
160160
('Iris Dataset', 2, None, 'iris-dataset'),
161161
('Qiskit implementation', 2, None, 'qiskit-implementation'),
162162
('Credit data classification',
@@ -318,10 +318,10 @@
318318
<!-- navigation toc: --> <li><a href="#defining-quantum-feature-maps-in-pennylane" style="font-size: 80%;">Defining Quantum Feature Maps in PennyLane</a></li>
319319
<!-- navigation toc: --> <li><a href="#computing-quantum-kernel-matrices" style="font-size: 80%;">Computing Quantum Kernel Matrices</a></li>
320320
<!-- 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>
322321
<!-- navigation toc: --> <li><a href="#discussion-of-implementation" style="font-size: 80%;">Discussion of Implementation</a></li>
323322
<!-- navigation toc: --> <li><a href="#pennylane-implementations" style="font-size: 80%;">PennyLane implementations</a></li>
324323
<!-- 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>
325325
<!-- navigation toc: --> <li><a href="#iris-dataset" style="font-size: 80%;">Iris Dataset</a></li>
326326
<!-- navigation toc: --> <li><a href="#qiskit-implementation" style="font-size: 80%;">Qiskit implementation</a></li>
327327
<!-- 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
13421342

13431343
<p>It is also possible to integrate PennyLane&#8217;s differentiable capabilities by defining a parameterized kernel and optimizing parameters via gradient descent, but here we keep a fixed feature map.</p>
13441344

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">&quot;default.qubit&quot;</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">&#39;precomputed&#39;</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">&quot;Predicted labels:&quot;</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-
14061345
<!-- !split -->
14071346
<h2 id="discussion-of-implementation" class="anchor">Discussion of Implementation </h2>
14081347

@@ -1445,6 +1384,69 @@ <h2 id="steps-in-quantum-kernel-svm" class="anchor">Steps in Quantum Kernel SVM
14451384
<li> New data is classified using the trained SVM model with the quantum kernel.</li>
14461385
</ul>
14471386
</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">&quot;target&quot;</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">&quot;target&quot;</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">&quot;rbf&quot;</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&quot;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">%&quot;</span>)
1431+
f1score<span style="color: #666666">=</span>f1_score(Y_test,predictions,average<span style="color: #666666">=</span><span style="color: #BA2121">&#39;macro&#39;</span>)
1432+
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;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">%&quot;</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+
14481450
<!-- !split -->
14491451
<h2 id="iris-dataset" class="anchor">Iris Dataset </h2>
14501452

@@ -2495,6 +2497,7 @@ <h2 id="more-on-applications" class="anchor">More on applications </h2>
24952497
<!-- M. Zhao et al., &#8220;A tutorial on quantum machine learning and quantum neural networks,&#8221; arXiv:2504.16131 (2025) . -->
24962498
<!-- D. M. Houshmand, Quantum Machine Learning Lecture Notes, (online, 2023) . (Tutorial using PennyLane QNNs.) -->
24972499
<!-- PennyLane documentation and tutorials: &#12296;https://pennylane.ai&#12297; (for code examples and parameter-shift rules). -->
2500+
24982501
<!-- ------------------- end of main content --------------- -->
24992502
</div> <!-- end container -->
25002503
<!-- include javascript, jQuery *first* -->

0 commit comments

Comments
 (0)