Skip to content

Commit a8fe2de

Browse files
committed
update week14
1 parent 69dc8b8 commit a8fe2de

File tree

8 files changed

+601
-372
lines changed

8 files changed

+601
-372
lines changed

doc/pub/week14/html/week14-bs.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
None,
199199
'pennylane-implementations'),
200200
('Iris Dataset', 2, None, 'iris-dataset'),
201+
('Qiskit implementation', 2, None, 'qiskit-implementation'),
201202
('Plans for next week', 2, None, 'plans-for-next-week')]}
202203
end of tocinfo -->
203204

@@ -308,6 +309,7 @@
308309
<!-- navigation toc: --> <li><a href="#discussion-of-implementation" style="font-size: 80%;">Discussion of Implementation</a></li>
309310
<!-- navigation toc: --> <li><a href="#pennylane-implementations" style="font-size: 80%;">PennyLane implementations</a></li>
310311
<!-- 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>
311313
<!-- navigation toc: --> <li><a href="#plans-for-next-week" style="font-size: 80%;">Plans for next week</a></li>
312314

313315
</ul>
@@ -2284,6 +2286,72 @@ <h2 id="iris-dataset" class="anchor">Iris Dataset </h2>
22842286
</div>
22852287

22862288

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&#39;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">&#39;qasm_simulator&#39;</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&#39;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">%&#39;</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+
22872355
<!-- !split -->
22882356
<h2 id="plans-for-next-week" class="anchor">Plans for next week </h2>
22892357
<ol>

doc/pub/week14/html/week14-reveal.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,72 @@ <h2 id="iris-dataset">Iris Dataset </h2>
22702270
</div>
22712271
</section>
22722272

2273+
<section>
2274+
<h2 id="qiskit-implementation">Qiskit implementation </h2>
2275+
2276+
<p>A similar implementation on Qiskit is given here (you need to have installed Qiskit as well).</p>
2277+
2278+
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
2279+
<div class="cell border-box-sizing code_cell rendered">
2280+
<div class="input">
2281+
<div class="inner_cell">
2282+
<div class="input_area">
2283+
<div class="highlight" style="background: #eeeedd">
2284+
<pre style="font-size: 80%; line-height: 125%;"><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit</span> <span style="color: #8B008B; font-weight: bold">import</span> BasicAer
2285+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> QuantumInstance
2286+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit.circuit.library</span> <span style="color: #8B008B; font-weight: bold">import</span> ZZFeatureMap
2287+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit_machine_learning.algorithms</span> <span style="color: #8B008B; font-weight: bold">import</span> QSVC
2288+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
2289+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_iris
2290+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
2291+
2292+
<span style="color: #228B22"># Load and preprocess dataset</span>
2293+
dataset = load_iris()
2294+
X = dataset.data
2295+
y = dataset.target
2296+
2297+
<span style="color: #228B22"># For simplicity, we&#39;ll only classify between two classes</span>
2298+
X = X[y != <span style="color: #B452CD">2</span>]
2299+
y = y[y != <span style="color: #B452CD">2</span>]
2300+
2301+
<span style="color: #228B22"># Standardize features</span>
2302+
scaler = StandardScaler()
2303+
X = scaler.fit_transform(X)
2304+
2305+
<span style="color: #228B22"># Split into training and test sets</span>
2306+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=<span style="color: #B452CD">0.2</span>, random_state=<span style="color: #B452CD">42</span>)
2307+
2308+
<span style="color: #228B22"># Define quantum feature map</span>
2309+
feature_map = ZZFeatureMap(feature_dimension=<span style="color: #B452CD">4</span>, reps=<span style="color: #B452CD">2</span>)
2310+
2311+
<span style="color: #228B22"># Set up quantum instance</span>
2312+
quantum_instance = QuantumInstance(BasicAer.get_backend(<span style="color: #CD5555">&#39;qasm_simulator&#39;</span>), shots=<span style="color: #B452CD">1024</span>)
2313+
2314+
<span style="color: #228B22"># Initialize QSVC</span>
2315+
qsvc = QSVC(quantum_instance=quantum_instance, feature_map=feature_map)
2316+
2317+
<span style="color: #228B22"># Train QSVC</span>
2318+
qsvc.fit(X_train, y_train)
2319+
2320+
<span style="color: #228B22"># Evaluate QSVC</span>
2321+
accuracy = qsvc.score(X_test, y_test)
2322+
<span style="color: #658b00">print</span>(<span style="color: #CD5555">f&#39;QSVC accuracy: {</span>accuracy * <span style="color: #B452CD">100</span><span style="color: #CD5555">:.2f}%&#39;</span>)
2323+
</pre>
2324+
</div>
2325+
</div>
2326+
</div>
2327+
</div>
2328+
<div class="output_wrapper">
2329+
<div class="output">
2330+
<div class="output_area">
2331+
<div class="output_subarea output_stream output_stdout output_text">
2332+
</div>
2333+
</div>
2334+
</div>
2335+
</div>
2336+
</div>
2337+
</section>
2338+
22732339
<section>
22742340
<h2 id="plans-for-next-week">Plans for next week </h2>
22752341
<ol>

doc/pub/week14/html/week14-solarized.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
None,
226226
'pennylane-implementations'),
227227
('Iris Dataset', 2, None, 'iris-dataset'),
228+
('Qiskit implementation', 2, None, 'qiskit-implementation'),
228229
('Plans for next week', 2, None, 'plans-for-next-week')]}
229230
end of tocinfo -->
230231

@@ -2188,6 +2189,72 @@ <h2 id="iris-dataset">Iris Dataset </h2>
21882189
</div>
21892190

21902191

2192+
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
2193+
<h2 id="qiskit-implementation">Qiskit implementation </h2>
2194+
2195+
<p>A similar implementation on Qiskit is given here (you need to have installed Qiskit as well).</p>
2196+
2197+
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
2198+
<div class="cell border-box-sizing code_cell rendered">
2199+
<div class="input">
2200+
<div class="inner_cell">
2201+
<div class="input_area">
2202+
<div class="highlight" style="background: #eeeedd">
2203+
<pre style="line-height: 125%;"><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit</span> <span style="color: #8B008B; font-weight: bold">import</span> BasicAer
2204+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> QuantumInstance
2205+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit.circuit.library</span> <span style="color: #8B008B; font-weight: bold">import</span> ZZFeatureMap
2206+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">qiskit_machine_learning.algorithms</span> <span style="color: #8B008B; font-weight: bold">import</span> QSVC
2207+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
2208+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_iris
2209+
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
2210+
2211+
<span style="color: #228B22"># Load and preprocess dataset</span>
2212+
dataset = load_iris()
2213+
X = dataset.data
2214+
y = dataset.target
2215+
2216+
<span style="color: #228B22"># For simplicity, we&#39;ll only classify between two classes</span>
2217+
X = X[y != <span style="color: #B452CD">2</span>]
2218+
y = y[y != <span style="color: #B452CD">2</span>]
2219+
2220+
<span style="color: #228B22"># Standardize features</span>
2221+
scaler = StandardScaler()
2222+
X = scaler.fit_transform(X)
2223+
2224+
<span style="color: #228B22"># Split into training and test sets</span>
2225+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=<span style="color: #B452CD">0.2</span>, random_state=<span style="color: #B452CD">42</span>)
2226+
2227+
<span style="color: #228B22"># Define quantum feature map</span>
2228+
feature_map = ZZFeatureMap(feature_dimension=<span style="color: #B452CD">4</span>, reps=<span style="color: #B452CD">2</span>)
2229+
2230+
<span style="color: #228B22"># Set up quantum instance</span>
2231+
quantum_instance = QuantumInstance(BasicAer.get_backend(<span style="color: #CD5555">&#39;qasm_simulator&#39;</span>), shots=<span style="color: #B452CD">1024</span>)
2232+
2233+
<span style="color: #228B22"># Initialize QSVC</span>
2234+
qsvc = QSVC(quantum_instance=quantum_instance, feature_map=feature_map)
2235+
2236+
<span style="color: #228B22"># Train QSVC</span>
2237+
qsvc.fit(X_train, y_train)
2238+
2239+
<span style="color: #228B22"># Evaluate QSVC</span>
2240+
accuracy = qsvc.score(X_test, y_test)
2241+
<span style="color: #658b00">print</span>(<span style="color: #CD5555">f&#39;QSVC accuracy: {</span>accuracy * <span style="color: #B452CD">100</span><span style="color: #CD5555">:.2f}%&#39;</span>)
2242+
</pre>
2243+
</div>
2244+
</div>
2245+
</div>
2246+
</div>
2247+
<div class="output_wrapper">
2248+
<div class="output">
2249+
<div class="output_area">
2250+
<div class="output_subarea output_stream output_stdout output_text">
2251+
</div>
2252+
</div>
2253+
</div>
2254+
</div>
2255+
</div>
2256+
2257+
21912258
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
21922259
<h2 id="plans-for-next-week">Plans for next week </h2>
21932260
<ol>

0 commit comments

Comments
 (0)