Last active
October 11, 2017 16:45
-
-
Save JamesCropcho/286c4c17cd5861e87e9cfbd99409e82b to your computer and use it in GitHub Desktop.
Applying a Hadamard Gate to a Qubit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Applying a Hadamard Gate to a Qubit\n", | |
"_by James Cropcho_" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 305, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"BASIS_VECTOR_REPRESENTATIONS = (0, 1) # not fantastic naming ―JC" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"First, I created my Hadamard gate:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 306, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.70710678 0.70710678]\n", | |
" [ 0.70710678 -0.70710678]]\n" | |
] | |
} | |
], | |
"source": [ | |
"hadamard_gate = 2**-0.5 * np.matrix('1 1; 1 -1')\n", | |
"print(hadamard_gate)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Then, I created my qubit, instantiated at $\\left|0\\right\\rangle$:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 307, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 1.]\n", | |
" [ 0.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"qubit = np.matrix('1.; 0.')\n", | |
"print(qubit)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Then, I applied the Hadamard gate to the qubit as a unitary transformation, in the hope that I would transform its state such that the amplitudes of $\\left|0\\right\\rangle$ and $\\left|1\\right\\rangle$ would each now be $\\frac{1}{\\sqrt2} \\approx 0.7071$. _I have overwritten the qubit's previous state to simulate the No-Cloning Theorem._" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 308, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.70710678]\n", | |
" [ 0.70710678]]\n" | |
] | |
} | |
], | |
"source": [ | |
"qubit = hadamard_gate * qubit\n", | |
"print(qubit)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Success! Because a qubit's state (i.e. a vector of amplitudes) must be in 2-norm, I then squared each component's amplitude to get the probability of measuring those values upon collapse of superposition:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 309, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.5]\n", | |
" [ 0.5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.square(qubit))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"50/50 for measuring either $0$ or $1$, just as one would expect. However, we are _not_ yet measuring, so once again, the qubit's new, present state:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 310, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.70710678]\n", | |
" [ 0.70710678]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(qubit)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Though the Hadamard gate is a fully deterministic operator, it transforms a qubit whose result will be certain upon measurment (e.g. $\\left|0\\right\\rangle$) into a \"quasi-probabilistic distribution\" superposition qubit state. The value one measures is not known until measurement. And so it was with my qubit at this time.\n", | |
"\n", | |
"Under classical probability theory, applying a deterministic operation to a probability may only ever yield another probabilistic result. However, under quantum mechanics amplitudes may also be negative and complex (unlike probabilities). This can have completely counterintuitive implications. Applying a Hadamard gate _again_ to this qubit results in a qubit state whose result will once more be certain upon measurment.\n", | |
"\n", | |
"Here, I applied my Hadamard gate for the second time to my qubit:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 311, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 1.]\n", | |
" [ 0.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"qubit = hadamard_gate * qubit\n", | |
"print(qubit)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Finally, I measure for the first time, and the system returns its first output, which like any measurement is some specific \"classical\" value! Below:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 312, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 312, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def measure(qubit):\n", | |
" amplitudes = np.stack(qubit, axis=1).tolist()[0]\n", | |
" probabilities = [amplitude**2 for amplitude in amplitudes]\n", | |
" \n", | |
" measurement = np.random.choice(BASIS_VECTOR_REPRESENTATIONS, p=probabilities)\n", | |
" del qubit # our measurement destroys the qubit's state\n", | |
"\n", | |
" return measurement\n", | |
"\n", | |
"measure(qubit)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The qubit's \"post-collapse\" measured state is $0$ (_not_ $\\left|0\\right\\rangle$) i.e. a non-error measurement of the qubit at line 311 will certainly be $0$.\n", | |
"\n", | |
"Thank you for exploring the Hadamard gate with me." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment