Skip to content

Commit 14b2a78

Browse files
author
Abraham Asfaw
authored
Merge pull request #43 from ordmoj/master
09_23_19_Hassi_Norlen
2 parents 933b8aa + 9b99f9f commit 14b2a78

File tree

7 files changed

+2151
-0
lines changed

7 files changed

+2151
-0
lines changed
Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"slideshow": {
7+
"slide_type": "slide"
8+
}
9+
},
10+
"source": [
11+
"# Coin toss quantum circuit\n",
12+
"In this exercise we create a quantum circuit that simulates the probabilistic nature of a single qubit in superposition. The one qubit circuit initializes the qubit in the ground state $|0\\rangle$ and then uses a Hadamard gate to put the qubit in superposition $|\\psi\\rangle = \\left(|0\\rangle+|1\\rangle\\right)/\\sqrt{2}.$ \n",
13+
"Measuring the qubit causes it to collapse into one of the states $|0\\rangle$ or $|1\\rangle$ with a 50% probability, i.e. a coin toss. \n",
14+
"In this exercise we introduce the Hadamard gate, which puts a qubit in superposition.\n",
15+
"```\n",
16+
" ┌───┐ \n",
17+
"q_0: |0>┤ H ├ \n",
18+
" └───┘ \n",
19+
"```\n",
20+
"\n",
21+
"We also introduce the X gate that flips the qubit from $|0\\rangle$ to $|1\\rangle$ and vice versa.\n",
22+
"```\n",
23+
" ┌───┐\n",
24+
"q_0: |0>┤ X ├\n",
25+
" └───┘\n",
26+
"```"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {
32+
"slideshow": {
33+
"slide_type": "slide"
34+
}
35+
},
36+
"source": [
37+
"Import the required libraries."
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {
44+
"slideshow": {
45+
"slide_type": "fragment"
46+
}
47+
},
48+
"outputs": [],
49+
"source": [
50+
"from qiskit import QuantumCircuit, execute, Aer\n",
51+
"\n",
52+
"# Import Blochsphere visualization\n",
53+
"from qiskit.visualization import plot_bloch_multivector, plot_histogram"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {
59+
"slideshow": {
60+
"slide_type": "slide"
61+
}
62+
},
63+
"source": [
64+
"As we will be using the Bloch sphere visualization (`plot_bloch_multivector`) a bit, here's a quick function that calculates the state vector ($|\\psi\\rangle$) for the circuit to let you display the Bloch vector for any given state.\n"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {
71+
"slideshow": {
72+
"slide_type": "fragment"
73+
}
74+
},
75+
"outputs": [],
76+
"source": [
77+
"def get_psi(circuit): \n",
78+
" global psi\n",
79+
" backend = Aer.get_backend('statevector_simulator') \n",
80+
" psi = execute(circuit, backend).result().get_statevector(circuit)"
81+
]
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"metadata": {
86+
"slideshow": {
87+
"slide_type": "slide"
88+
}
89+
},
90+
"source": [
91+
"Create an empty quantum circuit. We start out with the qubit in the $|0\\rangle$ state."
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {
98+
"slideshow": {
99+
"slide_type": "fragment"
100+
}
101+
},
102+
"outputs": [],
103+
"source": [
104+
"qc = QuantumCircuit(1,1)\n",
105+
"\n",
106+
"# Print out the circuit\n",
107+
"print(qc)\n",
108+
"\n",
109+
"# Display the Bloch sphere\n",
110+
"get_psi(qc)\n",
111+
"plot_bloch_multivector(psi)"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {
117+
"slideshow": {
118+
"slide_type": "slide"
119+
}
120+
},
121+
"source": [
122+
"Add a Hadamard (super position) gate to the quantum circuit. This puts the qubit in a superposition: $|\\psi\\rangle = \\left(|0\\rangle+|1\\rangle\\right)/\\sqrt{2}.$"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"metadata": {
129+
"slideshow": {
130+
"slide_type": "fragment"
131+
}
132+
},
133+
"outputs": [],
134+
"source": [
135+
"qc.h(0)\n",
136+
"\n",
137+
"# Print out the circuit\n",
138+
"print(qc)\n",
139+
"\n",
140+
"# Display the Bloch sphere\n",
141+
"get_psi(qc)\n",
142+
"plot_bloch_multivector(psi)\n"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {
148+
"slideshow": {
149+
"slide_type": "slide"
150+
}
151+
},
152+
"source": [
153+
"Finally, add a measurement gate to complete the circuit."
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"metadata": {
160+
"slideshow": {
161+
"slide_type": "fragment"
162+
}
163+
},
164+
"outputs": [],
165+
"source": [
166+
"# Add measure gate\n",
167+
"qc.measure(0,0)\n",
168+
"print(qc)"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"metadata": {
174+
"slideshow": {
175+
"slide_type": "slide"
176+
}
177+
},
178+
"source": [
179+
"Set the backend to a local simulator."
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": null,
185+
"metadata": {
186+
"slideshow": {
187+
"slide_type": "fragment"
188+
}
189+
},
190+
"outputs": [],
191+
"source": [
192+
"backend = Aer.get_backend('qasm_simulator')"
193+
]
194+
},
195+
{
196+
"cell_type": "markdown",
197+
"metadata": {
198+
"slideshow": {
199+
"slide_type": "slide"
200+
}
201+
},
202+
"source": [
203+
"Create a quantum job that runs just one shot to simulate a coin toss. Then run the job and display the result; either 0 for up (base) or 1 for down (excited). Display the result as a histogram."
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": null,
209+
"metadata": {
210+
"slideshow": {
211+
"slide_type": "fragment"
212+
}
213+
},
214+
"outputs": [],
215+
"source": [
216+
"job = execute(qc, backend, shots=1)\n",
217+
"counts = job.result().get_counts()\n",
218+
"print(counts)\n",
219+
"plot_histogram(counts)"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {
225+
"slideshow": {
226+
"slide_type": "slide"
227+
}
228+
},
229+
"source": [
230+
"Now, lets run a thousand coin tosses in a row and see what we get."
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": null,
236+
"metadata": {
237+
"slideshow": {
238+
"slide_type": "fragment"
239+
}
240+
},
241+
"outputs": [],
242+
"source": [
243+
"job = execute(qc, backend, shots=1000)\n",
244+
"counts = job.result().get_counts()\n",
245+
"print(counts)\n",
246+
"plot_histogram(counts)"
247+
]
248+
},
249+
{
250+
"cell_type": "markdown",
251+
"metadata": {
252+
"slideshow": {
253+
"slide_type": "fragment"
254+
}
255+
},
256+
"source": [
257+
"In the histogram we see that we get 0 and 1 with ~50% probability. Which is what we expect from tossing some coins."
258+
]
259+
},
260+
{
261+
"cell_type": "markdown",
262+
"metadata": {
263+
"slideshow": {
264+
"slide_type": "slide"
265+
}
266+
},
267+
"source": [
268+
"We can also do our coin flip with the qubit starting in the $|1\\rangle$ state by first flipping the qubit by using the X gate. The Hadamard gate still flips the qubit to the equator, but now on the -X side."
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": null,
274+
"metadata": {
275+
"slideshow": {
276+
"slide_type": "fragment"
277+
}
278+
},
279+
"outputs": [],
280+
"source": [
281+
"qc2 = QuantumCircuit(1,1)\n",
282+
"qc2.x(0)\n",
283+
"qc2.h(0)\n",
284+
"print(qc2)\n",
285+
"# Display the Bloch sphere\n",
286+
"get_psi(qc2)\n",
287+
"plot_bloch_multivector(psi)"
288+
]
289+
},
290+
{
291+
"cell_type": "markdown",
292+
"metadata": {
293+
"slideshow": {
294+
"slide_type": "slide"
295+
}
296+
},
297+
"source": [
298+
"Add the measure gate and run the circuit 1,000 times."
299+
]
300+
},
301+
{
302+
"cell_type": "code",
303+
"execution_count": null,
304+
"metadata": {
305+
"slideshow": {
306+
"slide_type": "fragment"
307+
}
308+
},
309+
"outputs": [],
310+
"source": [
311+
"qc2.measure(0,0)\n",
312+
"\n",
313+
"job2 = execute(qc2, backend, shots=1000)\n",
314+
"counts2 = job2.result().get_counts()\n",
315+
"print(counts2)\n",
316+
"plot_histogram(counts2)"
317+
]
318+
},
319+
{
320+
"cell_type": "markdown",
321+
"metadata": {
322+
"slideshow": {
323+
"slide_type": "fragment"
324+
}
325+
},
326+
"source": [
327+
"As you can see, there is no real difference in the outcome. "
328+
]
329+
}
330+
],
331+
"metadata": {
332+
"celltoolbar": "Slideshow",
333+
"kernelspec": {
334+
"display_name": "Python 3",
335+
"language": "python",
336+
"name": "python3"
337+
},
338+
"language_info": {
339+
"codemirror_mode": {
340+
"name": "ipython",
341+
"version": 3
342+
},
343+
"file_extension": ".py",
344+
"mimetype": "text/x-python",
345+
"name": "python",
346+
"nbconvert_exporter": "python",
347+
"pygments_lexer": "ipython3",
348+
"version": "3.7.3"
349+
}
350+
},
351+
"nbformat": 4,
352+
"nbformat_minor": 2
353+
}

0 commit comments

Comments
 (0)