Created
August 6, 2020 08:04
-
-
Save csferrie/f465690c4f4ae3daa37c7ac94abb52ff to your computer and use it in GitHub Desktop.
Hello Quantum Worlds.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Hello Quantum Worlds.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMnpyDuneioaHA76kePa1yX", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/csferrie/f465690c4f4ae3daa37c7ac94abb52ff/hello-quantum-worlds.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "xV7VUIMwH2IT", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Hello Quantum Worlds!\n", | |
"\n", | |
"This exercise is part of [Lab 1](https://medium.com/@csferrie/hello-quantum-worlds-39b000fdf9b3) of [Introduction to Quantum Computing](https://medium.com/@csferrie/introduction-to-quantum-computing-df9e1182a831).\n", | |
"\n", | |
"In this notebook, you'll be introduced to several quantum programming environments. In each one, you will create a qubit and place it in *superposition* using the \"Hadamard\" gate. This is the most primitive quantum computation, and it is one we will use over and over again." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "zp0yynx1I9Jl", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Cirq\n", | |
"\n", | |
"Since we are using Google Colab, it is only fair that we start with the Google quantum language Cirq. \n", | |
"\n", | |
"You can read the documentation for Cirq here: https://cirq.readthedocs.io/en/stable/index.html." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "MFG2OUCoyJY1", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "a3a7bc6a-452a-40fd-ed40-83deb2d27901" | |
}, | |
"source": [ | |
"# install cirq\n", | |
"!pip install cirq --quiet" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"\u001b[?25l\r\u001b[K |▎ | 10kB 17.6MB/s eta 0:00:01\r\u001b[K |▌ | 20kB 2.2MB/s eta 0:00:01\r\u001b[K |▊ | 30kB 2.8MB/s eta 0:00:01\r\u001b[K |█ | 40kB 3.0MB/s eta 0:00:01\r\u001b[K |█▏ | 51kB 2.5MB/s eta 0:00:01\r\u001b[K |█▍ | 61kB 2.8MB/s eta 0:00:01\r\u001b[K |█▋ | 71kB 3.0MB/s eta 0:00:01\r\u001b[K |██ | 81kB 3.4MB/s eta 0:00:01\r\u001b[K |██▏ | 92kB 3.5MB/s eta 0:00:01\r\u001b[K |██▍ | 102kB 3.4MB/s eta 0:00:01\r\u001b[K |██▋ | 112kB 3.4MB/s eta 0:00:01\r\u001b[K |██▉ | 122kB 3.4MB/s eta 0:00:01\r\u001b[K |███ | 133kB 3.4MB/s eta 0:00:01\r\u001b[K |███▎ | 143kB 3.4MB/s eta 0:00:01\r\u001b[K |███▌ | 153kB 3.4MB/s eta 0:00:01\r\u001b[K |███▉ | 163kB 3.4MB/s eta 0:00:01\r\u001b[K |████ | 174kB 3.4MB/s eta 0:00:01\r\u001b[K |████▎ | 184kB 3.4MB/s eta 0:00:01\r\u001b[K |████▌ | 194kB 3.4MB/s eta 0:00:01\r\u001b[K |████▊ | 204kB 3.4MB/s eta 0:00:01\r\u001b[K |█████ | 215kB 3.4MB/s eta 0:00:01\r\u001b[K |█████▏ | 225kB 3.4MB/s eta 0:00:01\r\u001b[K |█████▍ | 235kB 3.4MB/s eta 0:00:01\r\u001b[K |█████▊ | 245kB 3.4MB/s eta 0:00:01\r\u001b[K |██████ | 256kB 3.4MB/s eta 0:00:01\r\u001b[K |██████▏ | 266kB 3.4MB/s eta 0:00:01\r\u001b[K |██████▍ | 276kB 3.4MB/s eta 0:00:01\r\u001b[K |██████▋ | 286kB 3.4MB/s eta 0:00:01\r\u001b[K |██████▉ | 296kB 3.4MB/s eta 0:00:01\r\u001b[K |███████ | 307kB 3.4MB/s eta 0:00:01\r\u001b[K |███████▍ | 317kB 3.4MB/s eta 0:00:01\r\u001b[K |███████▋ | 327kB 3.4MB/s eta 0:00:01\r\u001b[K |███████▉ | 337kB 3.4MB/s eta 0:00:01\r\u001b[K |████████ | 348kB 3.4MB/s eta 0:00:01\r\u001b[K |████████▎ | 358kB 3.4MB/s eta 0:00:01\r\u001b[K |████████▌ | 368kB 3.4MB/s eta 0:00:01\r\u001b[K |████████▊ | 378kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████ | 389kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████▎ | 399kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████▌ | 409kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████▊ | 419kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████ | 430kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████▏ | 440kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████▍ | 450kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████▋ | 460kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████▉ | 471kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████▏ | 481kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████▍ | 491kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████▋ | 501kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████▉ | 512kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████ | 522kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████▎ | 532kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████▌ | 542kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████▉ | 552kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████ | 563kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████▎ | 573kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████▌ | 583kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████▊ | 593kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████ | 604kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████▏ | 614kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████▍ | 624kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████▊ | 634kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████ | 645kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████▏ | 655kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████▍ | 665kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████▋ | 675kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████▉ | 686kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████ | 696kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████▎ | 706kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████▋ | 716kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████▉ | 727kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████ | 737kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████▎ | 747kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████▌ | 757kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████▊ | 768kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████ | 778kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████▏ | 788kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████▌ | 798kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████▊ | 808kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████ | 819kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████▏ | 829kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████▍ | 839kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████▋ | 849kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████▉ | 860kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████▏ | 870kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████▍ | 880kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████▋ | 890kB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████▉ | 901kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████ | 911kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████▎ | 921kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████▌ | 931kB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████▊ | 942kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████ | 952kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████▎ | 962kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████▌ | 972kB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████▊ | 983kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████ | 993kB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████▏ | 1.0MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████▍ | 1.0MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████▋ | 1.0MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████ | 1.0MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████▏ | 1.0MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████▍ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████▋ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████▉ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████▎ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████▋ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████▉ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████▎ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████▌ | 1.1MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████▊ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████▏ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████▌ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████▊ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████▏ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████▍ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████▋ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████▉ | 1.2MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████████ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████████▍ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████████▋ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |█████████████████████████████▉ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████████ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████████▎ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████████▌ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████████▊ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████████ | 1.3MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▎| 1.4MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▌| 1.4MB 3.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▊| 1.4MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 1.4MB 3.4MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 1.4MB 3.4MB/s \n", | |
"\u001b[?25h" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "M7uLB5v1JtHm", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"There are many ways to acheive the given task in Cirq, but we will use the simplest. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "lCK97MrEWMjn", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
}, | |
"outputId": "72879d2e-340c-4c87-ad1a-0c26081df409" | |
}, | |
"source": [ | |
"import cirq\n", | |
"\n", | |
"my_first_qubit = cirq.NamedQubit('qubie')\n", | |
"\n", | |
"superposition = cirq.H(my_first_qubit)\n", | |
"\n", | |
"my_circuit = cirq.Circuit()\n", | |
"\n", | |
"my_circuit.append(superposition)\n", | |
"\n", | |
"simulator = cirq.Simulator()\n", | |
"results = simulator.simulate(my_circuit)\n", | |
"\n", | |
"print('Me: Hello Quantum World!')\n", | |
"print('Quantum computer: ')\n", | |
"print(results.state_vector())\n" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Me: Hello Quantum World!\n", | |
"Quantum computer: \n", | |
"[0.70710677+0.j 0.70710677+0.j]\n", | |
"\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NZzbwcRBXy8h", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Qiskit\n", | |
"\n", | |
"Qiskit is another Python based quantum programming language. You can find the documentation for Qiskit here: https://qiskit.org/documentation/." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "u-QBwkUVyNtU", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 340 | |
}, | |
"outputId": "58b7d0dd-bfbe-4c40-bb2d-9f0ed46c608d" | |
}, | |
"source": [ | |
"# install qiskit\n", | |
"!pip install qiskit --quiet" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"\u001b[K |████████████████████████████████| 6.7MB 3.1MB/s \n", | |
"\u001b[K |████████████████████████████████| 23.3MB 166kB/s \n", | |
"\u001b[K |████████████████████████████████| 163kB 34.5MB/s \n", | |
"\u001b[K |████████████████████████████████| 184kB 31.6MB/s \n", | |
"\u001b[K |████████████████████████████████| 1.9MB 33.1MB/s \n", | |
"\u001b[K |████████████████████████████████| 51kB 6.1MB/s \n", | |
"\u001b[K |████████████████████████████████| 962kB 36.0MB/s \n", | |
"\u001b[K |████████████████████████████████| 5.8MB 39.0MB/s \n", | |
"\u001b[K |████████████████████████████████| 51kB 5.9MB/s \n", | |
"\u001b[K |████████████████████████████████| 296kB 42.5MB/s \n", | |
"\u001b[K |████████████████████████████████| 71kB 8.2MB/s \n", | |
"\u001b[K |████████████████████████████████| 26.0MB 1.6MB/s \n", | |
"\u001b[K |████████████████████████████████| 583kB 41.0MB/s \n", | |
"\u001b[K |████████████████████████████████| 2.7MB 40.1MB/s \n", | |
"\u001b[?25h Building wheel for qiskit (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Building wheel for marshmallow-polyfield (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Building wheel for python-constraint (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Building wheel for docplex (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Building wheel for dlx (setup.py) ... \u001b[?25l\u001b[?25hdone\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VI6SUnX6yo_a", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 68 | |
}, | |
"outputId": "96615e9a-b1e5-48d3-ff4f-15e1ece12cc7" | |
}, | |
"source": [ | |
"import qiskit\n", | |
"\n", | |
"my_first_qubit = qiskit.QuantumRegister(1)\n", | |
"\n", | |
"my_circuit = qiskit.QuantumCircuit(my_first_qubit)\n", | |
"\n", | |
"my_circuit.h(0)\n", | |
"\n", | |
"simulator = qiskit.Aer.get_backend('statevector_simulator')\n", | |
"\n", | |
"job = qiskit.execute(my_circuit, simulator)\n", | |
"\n", | |
"results = job.result()\n", | |
"\n", | |
"print('Me: Hello Quantum World!')\n", | |
"print('Quantum computer: ')\n", | |
"print(results.get_statevector(my_circuit))" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Me: Hello Quantum World!\n", | |
"Quantum computer: \n", | |
"[0.70710678+0.j 0.70710678+0.j]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "TIlzUVBd2XyP", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# ProjectQ\n", | |
"\n", | |
"Last, but not least, is ProjectQ. The documentation for it is here: https://projectq.readthedocs.io/en/latest/index.html." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uMSG0Dxg01oB", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 102 | |
}, | |
"outputId": "a56e34fd-be7d-4bb8-91b0-a027eb00e8c3" | |
}, | |
"source": [ | |
"# install ProjectQ\n", | |
"!pip install projectq --quiet" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"\u001b[K |████████████████████████████████| 235kB 2.8MB/s \n", | |
"\u001b[K |████████████████████████████████| 296kB 8.6MB/s \n", | |
"\u001b[?25h Building wheel for projectq (setup.py) ... \u001b[?25lerror\n", | |
"\u001b[31m ERROR: Failed building wheel for projectq\u001b[0m\n", | |
"\u001b[?25h Running setup.py install for projectq ... \u001b[?25l\u001b[?25hdone\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "GRTPXPnK2FoX", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 68 | |
}, | |
"outputId": "291b2966-7fa8-4322-c37a-6eae2d9d58c8" | |
}, | |
"source": [ | |
"import projectq\n", | |
"\n", | |
"simulator = projectq.MainEngine() \n", | |
"\n", | |
"my_first_qubit = simulator.allocate_qubit()\n", | |
"\n", | |
"projectq.ops.H | my_first_qubit\n", | |
"\n", | |
"simulator.flush()\n", | |
"\n", | |
"print('Me: Hello Quantum World!')\n", | |
"print('Quantum computer: ')\n", | |
"print(simulator.backend.cheat())" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Me: Hello Quantum World!\n", | |
"Quantum computer: \n", | |
"({0: 0}, [(0.7071067811865475+0j), (0.7071067811865475+0j)])\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "D5bIzIoj_0KK", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# More!\n", | |
"\n", | |
"There are of course many more [quantum software development kits](https://en.wikipedia.org/wiki/Quantum_programming#Quantum_software_development_kits) available. Most are free. A couple I'd like to highlight, which we didn't look at here since they don't play nice with Google Colab, are [Microsoft's QDK](https://docs.microsoft.com/en-us/quantum/?view=qsharp-preview/) and [Rigetti's Forest](http://docs.rigetti.com/en/stable/)." | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment