Skip to content

Instantly share code, notes, and snippets.

@csferrie
Created August 6, 2020 08:08
Show Gist options
  • Save csferrie/46aa29fe3eaa6dec28ae3a1af9c0891d to your computer and use it in GitHub Desktop.
Save csferrie/46aa29fe3eaa6dec28ae3a1af9c0891d to your computer and use it in GitHub Desktop.
Hello Quantum Worlds (Lab 1)
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Hello Quantum Worlds (Lab 1)",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNmXQ+qE8FloZ7IODS0G6xp",
"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/46aa29fe3eaa6dec28ae3a1af9c0891d/hello-quantum-worlds-lab-1.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.\n",
"\n",
"**Most of the code here is designed to fail**\n",
"\n",
"It is your job to make it succeed. Fill in the blanks to create your superposition."
]
},
{
"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": "f4a40fc2-76bd-416f-9a97-5459eabf1bfe"
},
"source": [
"# install cirq\n",
"!pip install cirq --quiet"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[K |████████████████████████████████| 1.4MB 2.6MB/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": 130
},
"outputId": "cc9a51a4-4f3a-493b-d8ab-e0298d3d52d8"
},
"source": [
"import cirq\n",
"\n",
"my_first_qubit = cirq.NamedQubit('qubie')\n",
"\n",
"superposition = # apply a H gates to your 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": "error",
"ename": "SyntaxError",
"evalue": "ignored",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-2-0996824364fd>\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m superposition = # apply a H gates to your qubit\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
]
},
{
"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": "d8db7381-3673-4b57-9ca7-68b5b7e71f77"
},
"source": [
"# install qiskit\n",
"!pip install qiskit --quiet"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[K |████████████████████████████████| 6.7MB 2.5MB/s \n",
"\u001b[K |████████████████████████████████| 23.3MB 1.4MB/s \n",
"\u001b[K |████████████████████████████████| 163kB 49.8MB/s \n",
"\u001b[K |████████████████████████████████| 184kB 53.3MB/s \n",
"\u001b[K |████████████████████████████████| 1.9MB 50.2MB/s \n",
"\u001b[K |████████████████████████████████| 962kB 46.0MB/s \n",
"\u001b[K |████████████████████████████████| 51kB 6.6MB/s \n",
"\u001b[K |████████████████████████████████| 5.8MB 47.2MB/s \n",
"\u001b[K |████████████████████████████████| 51kB 6.2MB/s \n",
"\u001b[K |████████████████████████████████| 296kB 47.6MB/s \n",
"\u001b[K |████████████████████████████████| 71kB 9.5MB/s \n",
"\u001b[K |████████████████████████████████| 583kB 46.3MB/s \n",
"\u001b[K |████████████████████████████████| 26.0MB 132kB/s \n",
"\u001b[K |████████████████████████████████| 2.7MB 44.6MB/s \n",
"\u001b[?25h Building wheel for qiskit (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for python-constraint (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for marshmallow-polyfield (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": "6e090f5d-4bb6-46f3-c42a-5b6760ee3790"
},
"source": [
"import qiskit\n",
"\n",
"my_first_qubit = qiskit.QuantumRegister(1)\n",
"\n",
"my_circuit = qiskit.QuantumCircuit(my_first_qubit)\n",
"\n",
"# apply a H gate to your qubit (This code will actually run with doing this!)\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",
"[1.+0.j 0.+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": 51
},
"outputId": "e18dfe33-9b60-463c-f307-f8aa4912d52c"
},
"source": [
"# install ProjectQ\n",
"!pip install projectq --quiet"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[?25l\r\u001b[K |█▍ | 10kB 19.8MB/s eta 0:00:01\r\u001b[K |██▉ | 20kB 1.7MB/s eta 0:00:01\r\u001b[K |████▎ | 30kB 2.3MB/s eta 0:00:01\r\u001b[K |█████▊ | 40kB 2.5MB/s eta 0:00:01\r\u001b[K |███████▏ | 51kB 2.0MB/s eta 0:00:01\r\u001b[K |████████▋ | 61kB 2.2MB/s eta 0:00:01\r\u001b[K |██████████ | 71kB 2.5MB/s eta 0:00:01\r\u001b[K |███████████▌ | 81kB 2.7MB/s eta 0:00:01\r\u001b[K |█████████████ | 92kB 2.9MB/s eta 0:00:01\r\u001b[K |██████████████▎ | 102kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████▊ | 112kB 2.7MB/s eta 0:00:01\r\u001b[K |█████████████████▏ | 122kB 2.7MB/s eta 0:00:01\r\u001b[K |██████████████████▋ | 133kB 2.7MB/s eta 0:00:01\r\u001b[K |████████████████████ | 143kB 2.7MB/s eta 0:00:01\r\u001b[K |█████████████████████▌ | 153kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████████████ | 163kB 2.7MB/s eta 0:00:01\r\u001b[K |████████████████████████▍ | 174kB 2.7MB/s eta 0:00:01\r\u001b[K |█████████████████████████▉ | 184kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████████████████▎ | 194kB 2.7MB/s eta 0:00:01\r\u001b[K |████████████████████████████▋ | 204kB 2.7MB/s eta 0:00:01\r\u001b[K |██████████████████████████████ | 215kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▌| 225kB 2.7MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 235kB 2.7MB/s \n",
"\u001b[?25h Building wheel for projectq (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "GRTPXPnK2FoX",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "3a6cd3ff-2224-4623-c179-d5db6af90da2"
},
"source": [
"import projectq\n",
"\n",
"simulator = projectq.MainEngine() \n",
"\n",
"my_first_qubit = simulator.allocate_qubit()\n",
"\n",
"# apply a H gate to your qubit (This code will actually run with doing this!)\n",
"\n",
"simulator.flush()\n",
"\n",
"print(simulator.backend.cheat())"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"({0: 0}, [(1+0j), 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