Skip to content

Instantly share code, notes, and snippets.

@csferrie
Created August 25, 2020 07:24
Show Gist options
  • Save csferrie/0172173a6807e4e20e6e3e0ace6f64e9 to your computer and use it in GitHub Desktop.
Save csferrie/0172173a6807e4e20e6e3e0ace6f64e9 to your computer and use it in GitHub Desktop.
Hello Entanglement (Lab 4)
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Hello Entanglement (Lab 4)",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyN7VFSGNHMHvngltaVNXfQB",
"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/0172173a6807e4e20e6e3e0ace6f64e9/hello-entanglement-lab-4.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 Entanglement!\n",
"\n",
"This exercise is part of [Lab 4](https://medium.com/@csferrie/non-transitive-project-based-learning-90b596f030a0) of [Introduction to Quantum Computing](https://medium.com/@csferrie/introduction-to-quantum-computing-df9e1182a831).\n",
"\n",
"This notebook covers 3 different quantum programming languages: cirq, qiskit, and projectq. In each one, you will create the entangled state $\\frac1{\\sqrt 2}(|00\\rangle + |11\\rangle)$.\n",
"\n",
"The skeleton of your program is below. You need to fill in the #TODOs."
]
},
{
"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": "5b336459-ecda-4b3f-b304-e2edf297f322"
},
"source": [
"# install cirq\n",
"!pip install cirq --quiet"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[K |████████████████████████████████| 1.4MB 2.7MB/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": 68
},
"outputId": "e25d2080-3e7f-4174-dcf9-6464c4075e05"
},
"source": [
"import cirq\n",
"\n",
"# create two qubits\n",
"# TODO\n",
"\n",
"# create cirquit\n",
"my_circuit = cirq.Circuit()\n",
"\n",
"# add your gates (aka \"moments\")\n",
"my_circuit.append([\n",
"# TODO\n",
"])\n",
"\n",
"# simulate the circuit\n",
"simulator = cirq.Simulator()\n",
"results = simulator.simulate(my_circuit)\n",
"\n",
"print('Me: Can I get some entanglement?')\n",
"print('Quantum computer: ')\n",
"print(results.state_vector())\n"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Me: Can I get some entanglement?\n",
"Quantum computer: \n",
"[1.+0.j]\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": "d5f51db1-2e75-4f3a-e0c9-7b462ff2686c"
},
"source": [
"# install qiskit\n",
"!pip install qiskit --quiet"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[K |████████████████████████████████| 8.3MB 2.7MB/s \n",
"\u001b[K |████████████████████████████████| 16.5MB 240kB/s \n",
"\u001b[K |████████████████████████████████| 163kB 37.2MB/s \n",
"\u001b[K |████████████████████████████████| 174kB 53.4MB/s \n",
"\u001b[K |████████████████████████████████| 1.9MB 47.1MB/s \n",
"\u001b[K |████████████████████████████████| 962kB 44.2MB/s \n",
"\u001b[K |████████████████████████████████| 51kB 6.2MB/s \n",
"\u001b[K |████████████████████████████████| 5.8MB 38.5MB/s \n",
"\u001b[K |████████████████████████████████| 296kB 46.2MB/s \n",
"\u001b[K |████████████████████████████████| 71kB 8.2MB/s \n",
"\u001b[K |████████████████████████████████| 583kB 41.9MB/s \n",
"\u001b[K |████████████████████████████████| 102kB 10.0MB/s \n",
"\u001b[K |████████████████████████████████| 2.7MB 46.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 contextvars (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for dlx (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for docplex (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for yfinance (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": "ad5a4772-d29b-4b72-a9a9-52876219fcb0"
},
"source": [
"import qiskit\n",
"\n",
"# create two qubit registers\n",
"my_circuit = qiskit.QuantumCircuit() # TODO\n",
"\n",
"# add gates to your circuit\n",
"#TODO\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: Can I get some entanglement?')\n",
"print('Quantum computer: ')\n",
"print(results.get_statevector(my_circuit))"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"Me: Can I get some entanglement?\n",
"Quantum computer: \n",
"[1.+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": "c8c2d3c2-4949-4eda-92f2-bf8d0d771620"
},
"source": [
"# install ProjectQ\n",
"!pip install projectq --quiet"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[?25l\r\u001b[K |█▍ | 10kB 16.9MB/s eta 0:00:01\r\u001b[K |██▉ | 20kB 1.8MB/s eta 0:00:01\r\u001b[K |████▎ | 30kB 2.3MB/s eta 0:00:01\r\u001b[K |█████▊ | 40kB 2.6MB/s eta 0:00:01\r\u001b[K |███████▏ | 51kB 2.0MB/s eta 0:00:01\r\u001b[K |████████▋ | 61kB 2.3MB/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.8MB/s eta 0:00:01\r\u001b[K |███████████████▊ | 112kB 2.8MB/s eta 0:00:01\r\u001b[K |█████████████████▏ | 122kB 2.8MB/s eta 0:00:01\r\u001b[K |██████████████████▋ | 133kB 2.8MB/s eta 0:00:01\r\u001b[K |████████████████████ | 143kB 2.8MB/s eta 0:00:01\r\u001b[K |█████████████████████▌ | 153kB 2.8MB/s eta 0:00:01\r\u001b[K |███████████████████████ | 163kB 2.8MB/s eta 0:00:01\r\u001b[K |████████████████████████▍ | 174kB 2.8MB/s eta 0:00:01\r\u001b[K |█████████████████████████▉ | 184kB 2.8MB/s eta 0:00:01\r\u001b[K |███████████████████████████▎ | 194kB 2.8MB/s eta 0:00:01\r\u001b[K |████████████████████████████▋ | 204kB 2.8MB/s eta 0:00:01\r\u001b[K |██████████████████████████████ | 215kB 2.8MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▌| 225kB 2.8MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 235kB 2.8MB/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": 68
},
"outputId": "008b0a73-fd16-4d84-d7c8-7222b3a274f1"
},
"source": [
"import projectq\n",
"\n",
"# in projectq you create a simulator first (your \"circuit\")\n",
"simulator = projectq.MainEngine() \n",
"\n",
"# create two qubits\n",
"# TODO\n",
"\n",
"# add your gates\n",
"# TODO\n",
"\n",
"simulator.flush()\n",
"\n",
"print('Me: Can I get some entanglement?')\n",
"print('Quantum computer: ')\n",
"print(simulator.backend.cheat())"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"Me: Can I get some entanglement?\n",
"Quantum computer: \n",
"({}, [(1+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