-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tutorial03.html
741 lines (651 loc) · 35.2 KB
/
Tutorial03.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Tutorial C: Multi-Qubit Circuits — World of Tech</title>
<link href="_static/css/theme.css" rel="stylesheet">
<link href="_static/css/index.ff1ffe594081f20da1ef19478df9384b.css" rel="stylesheet">
<link rel="stylesheet"
href="_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-book-theme.css?digest=c3fdc42140077d1ad13ad2f1588a4309" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.be7d3bbb2ef33a8344ce.js">
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.d59cb220de22ca1c485ebbdc042f0030.js"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
const thebe_selector = ".thebe,.cell"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<link rel="author" title="About these documents" href="about.html" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Tutorial D: Multi-Qubit Operations & Demonstrating Quantum Mechanics" href="comingsoon.html" />
<link rel="prev" title="Tutorial B: More Single Qubit Gates" href="Tutorial02.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="None">
<!-- Google Analytics -->
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
<div class="container-fluid" id="banner"></div>
<div class="container-xl">
<div class="row">
<div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
<div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="index.html">
<!-- `logo` is deprecated in Sphinx 4.0, so remove this when we stop supporting 3 -->
<img src="_static/logo.png" class="logo" alt="logo">
<h1 class="site-logo" id="site-title">World of Tech</h1>
</a>
</div><form class="bd-search d-flex align-items-center" action="search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item active">
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="introqc.html">
The Quantum World
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Quantum vs. Classical
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="TheWorldofQuantum00.html">
<strong>
The Quantum vs. Classical World
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="TheWorldofQuantum01.html">
1. Introduction to the Quantum World: A Video
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="TheWorldofQuantum02.html">
2. Wave-Particle Duality
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="TheWorldofQuantum03.html">
3. Superposition
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="TheWorldofQuantum05.html">
4. Wave-Function Collapse
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="TheWorldofQuantum04.html">
5. Entanglement
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Quantum Computing
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="QuantumComputing01.html">
<strong>
What is Quantum Computing?
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="QuantumComputing01b.html">
<strong>
What is a Qubit?
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="QuantumComputing02.html">
<strong>
How does Quantum Computing work?
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="QuantumComputing02b.html">
<strong>
Types of Quantum Computers
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="QuantumComputing03.html">
<strong>
Why is Quantum Computing important?
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="quantum03.html">
<strong>
How to become a Quantum Scientist
</strong>
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Quantum Programming
</span>
</p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="Tutorial00.html">
<strong>
An Introduction to Quantum Programming
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Tutorial01.html">
Tutorial A : Single Qubit Operations and Visualization of the Qubit State
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Tutorial02.html">
Tutorial B: More Single Qubit Gates
</a>
</li>
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
Tutorial C: Multi-Qubit Circuits
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="comingsoon.html">
Tutorial D: Multi-Qubit Operations & Demonstrating Quantum Mechanics
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Back Matter
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="QuantumGlossary.html">
<strong>
A Quantum Glossary
</strong>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="about.html">
<strong>
About the Author
</strong>
</a>
</li>
</ul>
</div>
</nav> <!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
<div class="topbar container-xl fixed-top">
<div class="topbar-contents row">
<div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show"></div>
<div class="col pl-md-4 topbar-main">
<button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
title="Toggle navigation" data-toggle="tooltip" data-placement="left">
<i class="fas fa-bars"></i>
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-up"></i>
</button>
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
class="fas fa-download"></i></button>
<div class="dropdown-buttons">
<!-- ipynb file if we had a myst markdown file -->
<!-- Download raw file -->
<a class="dropdown-buttons" href="_sources/Tutorial03.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onclick="printPdf(this)" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
<!-- Source interaction buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Connect with source repository"><i class="fab fa-github"></i></button>
<div class="dropdown-buttons sourcebuttons">
<a class="repository-button"
href="https://github.com/Jaim-gem/qc"><button type="button" class="btn btn-secondary topbarbtn"
data-toggle="tooltip" data-placement="left" title="Source repository"><i
class="fab fa-github"></i>repository</button></a>
<a class="issues-button"
href="https://github.com/Jaim-gem/qc/issues/new?title=Issue%20on%20page%20%2FTutorial03.html&body=Your%20issue%20content%20here."><button
type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip" data-placement="left"
title="Open an issue"><i class="fas fa-lightbulb"></i>open issue</button></a>
</div>
</div>
<!-- Full screen (wrap in <a> to have style consistency -->
<a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
data-placement="bottom" onclick="toggleFullScreen()" aria-label="Fullscreen mode"
title="Fullscreen mode"><i
class="fas fa-expand"></i></button></a>
<!-- Launch buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Launch interactive content"><i class="fas fa-rocket"></i></button>
<div class="dropdown-buttons">
<a class="binder-button" href="https://mybinder.org/v2/gh/Jaim-gem/qc/gh-pages?labpath=_sources/v2/gh/Jaim-gem/qc/gh-pages?urlpath=tree/_sources/Tutorial03.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Launch Binder" data-toggle="tooltip"
data-placement="left"><img class="binder-button-logo"
src="_static/images/logo_binder.svg"
alt="Interact on binder">Binder</button></a>
<a class="colab-button" href="https://colab.research.google.com/github/Jaim-gem/qc/blob/gh-pages/_sources/Tutorial03.ipynb"><button type="button" class="btn btn-secondary topbarbtn"
title="Launch Colab" data-toggle="tooltip" data-placement="left"><img class="colab-button-logo"
src="_static/images/logo_colab.png"
alt="Interact on Colab">Colab</button></a>
<button type="button" class="btn btn-secondary topbarbtn"
onclick="initThebeSBT()" title="Launch Thebe" data-toggle="tooltip" data-placement="left"><i
class="fas fa-play"></i><span style="margin-left: .4em;">Live Code</span></button>
</div>
</div>
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show noprint">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
</div>
<nav id="bd-toc-nav" aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h1 nav-item toc-entry">
<a class="reference internal nav-link" href="#">
Tutorial C: Multi-Qubit Circuits
</a>
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#objectives">
Objectives:
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#building-a-multi-qubit-circuit">
1. Building a Multi-Qubit Circuit
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#applying-the-same-gates-to-all-qubits">
2. Applying the same gates to all qubits.
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#combining-circuits">
3. Combining circuits
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#random-sampling-of-bitstrings">
4. Random Sampling of Bitstrings
</a>
</li>
</ul>
</li>
<li class="toc-h1 nav-item toc-entry">
<a class="reference internal nav-link" href="#try-your-own-code">
<strong>
Try Your Own Code
</strong>
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<!-- Table of contents that is only displayed when printing the page -->
<div id="jb-print-docs-body" class="onlyprint">
<h1>Tutorial C: Multi-Qubit Circuits</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h1 nav-item toc-entry">
<a class="reference internal nav-link" href="#">
Tutorial C: Multi-Qubit Circuits
</a>
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#objectives">
Objectives:
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#building-a-multi-qubit-circuit">
1. Building a Multi-Qubit Circuit
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#applying-the-same-gates-to-all-qubits">
2. Applying the same gates to all qubits.
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#combining-circuits">
3. Combining circuits
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#random-sampling-of-bitstrings">
4. Random Sampling of Bitstrings
</a>
</li>
</ul>
</li>
<li class="toc-h1 nav-item toc-entry">
<a class="reference internal nav-link" href="#try-your-own-code">
<strong>
Try Your Own Code
</strong>
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div>
<section class="tex2jax_ignore mathjax_ignore" id="tutorial-c-multi-qubit-circuits">
<h1>Tutorial C: Multi-Qubit Circuits<a class="headerlink" href="#tutorial-c-multi-qubit-circuits" title="Permalink to this headline">¶</a></h1>
<section id="objectives">
<h2>Objectives:<a class="headerlink" href="#objectives" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p>to create quantum circuits with more than one qubit.</p></li>
<li><p>to add gates to multiple qubits.</p></li>
<li><p>to combine circuits.</p></li>
<li><p>to visualize multi-qubit quantum states.</p></li>
<li><p>to see how a quantum circuit may allow for computation on multiple “classical” states simultaneously.
</p></li>
</ul>
<p><strong>Importing Modules</strong></p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">qiskit</span>
<span class="kn">from</span> <span class="nn">qiskit</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">qiskit.visualization</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</pre></div>
</div>
</div>
</div>
<p>In Tutorials A and B, we created quantum circuits comprising a single qubit in order to focus on the action of single-qubit quantum gates and how they alter qubit states.<br></p>
<p>We shall now look at quantum circuits with more than one qubit.</p>
</section>
<section id="building-a-multi-qubit-circuit">
<h2>1. Building a Multi-Qubit Circuit<a class="headerlink" href="#building-a-multi-qubit-circuit" title="Permalink to this headline">¶</a></h2>
<p>We use the same <code class="docutils literal notranslate"><span class="pre">QuantumCircuit()</span></code> function. Let’s create a circuit with 3 qubits.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">more_qubits</span><span class="o">=</span> <span class="n">QuantumCircuit</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">more_qubits</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_6_0.png" src="_images/Tutorial03_6_0.png" />
</div>
</div>
<p><strong>Simple!</strong></p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>We observe that the qubits are numbered starting with ‘0’ so that the 3rd qubit has the index ‘2’.</p>
</div>
<p>Now, let’s apply some gates to the qubits.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">more_qubits</span><span class="o">.</span><span class="n">x</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># Apply the x gate to the first qubit </span>
<span class="n">more_qubits</span><span class="o">.</span><span class="n">ry</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># Apply a rotation-y gate to the second qubit</span>
<span class="n">more_qubits</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># Apply an S gate also to the second qubit</span>
<span class="n">more_qubits</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># Apply a Hadamard gate to the final qubit</span>
<span class="n">more_qubits</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_8_0.png" src="_images/Tutorial03_8_0.png" />
</div>
</div>
<p>So we’ve created a quantum circuit using 3 qubits and 4 different gates.</p>
<p>How about if we want to apply the same gate to all qubits?</p>
</section>
<section id="applying-the-same-gates-to-all-qubits">
<h2>2. Applying the same gates to all qubits.<a class="headerlink" href="#applying-the-same-gates-to-all-qubits" title="Permalink to this headline">¶</a></h2>
<p>We can do this one of two ways. The manual way is simply as follows:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">more_qubits2</span><span class="o">=</span><span class="n">QuantumCircuit</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_10_0.png" src="_images/Tutorial03_10_0.png" />
</div>
</div>
<p>The quicker way would be to use a loop. See below.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">more_qubits2</span><span class="o">=</span><span class="n">QuantumCircuit</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span> <span class="c1"># For i= 0, 1, 2 </span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="c1"># Apply the h gate to the ith qubit</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="c1"># Then apply the s gate to the ith qubit</span>
<span class="n">more_qubits2</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_12_0.png" src="_images/Tutorial03_12_0.png" />
</div>
</div>
<p>We obtain the same circuit by using the <code class="docutils literal notranslate"><span class="pre">for</span></code> loop.</p>
</section>
<section id="combining-circuits">
<h2>3. Combining circuits<a class="headerlink" href="#combining-circuits" title="Permalink to this headline">¶</a></h2>
<p>We now wish to add the circuit the ‘more_qubits’ circuit after the ‘more_qubits2’ circuit. To do so, we start off with the circuit we’d like to have first, then apply the <code class="docutils literal notranslate"><span class="pre">.compose()</span></code> method. This is demonstrated below.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">combined_circuit</span> <span class="o">=</span> <span class="n">more_qubits2</span><span class="o">.</span><span class="n">compose</span><span class="p">(</span><span class="n">more_qubits</span><span class="p">)</span>
<span class="n">combined_circuit</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_15_0.png" src="_images/Tutorial03_15_0.png" />
</div>
</div>
<p><strong>Easy peasy!</strong> And if we wanted to get fancy, we could also change the order in which the qubits are added…</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">combined_circuit2</span> <span class="o">=</span> <span class="n">more_qubits2</span><span class="o">.</span><span class="n">compose</span><span class="p">(</span><span class="n">more_qubits</span><span class="p">,</span> <span class="n">qubits</span><span class="o">=</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
<span class="n">combined_circuit2</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_17_0.png" src="_images/Tutorial03_17_0.png" />
</div>
</div>
<p>Above, we have reversed the order of the qubits being added to the left circuit. Instead of ‘0, 1, 2’, we specified ‘2, 1, 0’.</p>
<p>Ok, we’re ready to explore some interesting things we can do with quantum circuits.</p>
</section>
<section id="random-sampling-of-bitstrings">
<h2>4. Random Sampling of Bitstrings<a class="headerlink" href="#random-sampling-of-bitstrings" title="Permalink to this headline">¶</a></h2>
<p>We know that when we place a qubit in an equal superposition state, the probability of measuring the <span class="math notranslate nohighlight">\(|0\rangle\)</span> and <span class="math notranslate nohighlight">\(|1\rangle\)</span> states are equal.<br></p>
<p>If we were to place multiple qubits into equal superposition and measure them all at once, then we’ll obtain a string of ‘0’s and ‘1’s. The interesting thing happends when we run the circuit again and again. Let’s see..</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">qc</span><span class="o">=</span> <span class="n">QuantumCircuit</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="n">qc</span><span class="o">.</span><span class="n">h</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">qc</span><span class="o">.</span><span class="n">measure_all</span><span class="p">()</span>
<span class="n">qc</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">output</span><span class="o">=</span><span class="s1">'mpl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_20_0.png" src="_images/Tutorial03_20_0.png" />
</div>
</div>
<p>We will run the circuit once to see the results.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">simulator</span> <span class="o">=</span> <span class="n">BasicAer</span><span class="o">.</span><span class="n">get_backend</span><span class="p">(</span><span class="s1">'qasm_simulator'</span><span class="p">)</span>
<span class="n">job</span><span class="o">=</span><span class="n">execute</span><span class="p">(</span><span class="n">qc</span><span class="p">,</span><span class="n">simulator</span><span class="p">,</span><span class="n">shots</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">counts</span><span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">result</span><span class="p">()</span><span class="o">.</span><span class="n">get_counts</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">counts</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>{'1100': 1}
</pre></div>
</div>
</div>
</div>
<p>So we obtained a bitstring of length 4, a measurement for each qubit. <strong>Run the circuit again.</strong></p>
<p>We are likely to obtain different results each time we execute the circuit. Let’s visualize the results we’ll obtain if we ran the circuit a few thousand times.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span><span class="o">=</span><span class="n">execute</span><span class="p">(</span><span class="n">qc</span><span class="p">,</span><span class="n">simulator</span><span class="p">,</span><span class="n">shots</span><span class="o">=</span><span class="mi">8000</span><span class="p">)</span>
<span class="n">counts</span><span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">result</span><span class="p">()</span><span class="o">.</span><span class="n">get_counts</span><span class="p">()</span>
<span class="n">plot_histogram</span><span class="p">(</span><span class="n">counts</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'silver'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Tutorial03_24_0.png" src="_images/Tutorial03_24_0.png" />
</div>
</div>
<p>With 8000 shots of the circuit, we see that all possible bitstrings (there are <span class="math notranslate nohighlight">\(2^4=16\)</span> possibilities) from ‘0000’ to ‘1111’ are observed with fairly equal probability.</p>
<p><strong>Can you think of what this implies for the quantum state of the circuit before the measurement?</strong></p>
<div class="toggle docutils container">
<p>The collective state of the quantum circuit prior to <strong>measurement collapse</strong> is an equal <strong>superposition</strong> of all 16 possible bitstring states.</p>
<p>This means that by putting our qubits into equal superposition we can perform calculations on many possibilities all at once!</p>
</div>
<p><strong>That’s it for this tutorial!</strong> <br> Tune in for the next tutorial where the fun truly begins…</p>
</section>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="try-your-own-code">
<h1><strong>Try Your Own Code</strong><a class="headerlink" href="#try-your-own-code" title="Permalink to this headline">¶</a></h1>
<p><strong>We leave you this space to enter and play around with your own quantum programming code</strong>. You may want to restart the kernel.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">qiskit</span>
<span class="kn">from</span> <span class="nn">qiskit</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">qiskit.visualization</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</pre></div>
</div>
</div>
</div>
</section>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "Jaim-gem/qc",
ref: "gh-pages",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
kernelName: "python3",
path: "./."
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</div>
<!-- Previous / next buttons -->
<div class='prev-next-area'>
<a class='left-prev' id="prev-link" href="Tutorial02.html" title="previous page">
<i class="fas fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Tutorial B: More Single Qubit Gates</p>
</div>
</a>
<a class='right-next' id="next-link" href="comingsoon.html" title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Tutorial D: Multi-Qubit Operations & Demonstrating Quantum Mechanics</p>
</div>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>
</div>
<footer class="footer">
<p>
By TTLAB<br/>
© Copyright 2021.<br/>
</p>
</footer>
</main>
</div>
</div>
<script src="_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
</body>
</html>