{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![(book cover)](https://covers.oreillystatic.com/images/0636920167433/cat.gif \"(book cover)\")\n", "### **Programming Quantum Computers** by O'Reilly Media - [**book Info**](http://shop.oreilly.com/product/0636920167433.do) - [**all code samples**](https://oreilly-qc.github.io)\n", "\n", "#### **Code samples for Chapter 2**\n", "These code samples were written by Andres Paz and Mariia Mykhailova." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Example 2-1: Random bit\n", "\n", "operation RandomBit () : Unit {\n", " // allocate one qubit\n", " using (q = Qubit()) {\n", " // put it into superposition of 0 and 1\n", " H(q);\n", "\n", " // measure the qubit and store the result\n", " let bit = M(q);\n", " \n", " // make sure the qubit is back to the 0 state\n", " Reset(q);\n", " \n", " Message($\"{bit}\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%simulate RandomBit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Example 2-2: Random byte\n", "\n", "// open namespace which defines arithmetic operations\n", "open Microsoft.Quantum.Arithmetic;\n", "\n", "operation RandomByte () : Unit {\n", " // allocate 8 qubits\n", " using (qs = Qubit[8]) {\n", " // put each qubit into superposition of 0 and 1\n", " ApplyToEach(H, qs);\n", "\n", " // measure the register and store the result\n", " let randomByte = MeasureInteger(LittleEndian(qs));\n", " \n", " // make sure the qubits are back to the 0 state\n", " ResetAll(qs);\n", " \n", " Message($\"{randomByte}\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "%simulate RandomByte" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Example 2-3: Root-of-NOT\n", "\n", "open Microsoft.Quantum.Diagnostics;\n", "\n", "operation SqrtNOT (q : Qubit) : Unit {\n", " H(q);\n", " S(q);\n", " H(q);\n", "}\n", "\n", "operation RunSqrtNOT () : Unit {\n", " // allocate a qubit\n", " using (q = Qubit()) {\n", " // apply SqrtNOT gate to the |0⟩ state\n", " SqrtNOT(q);\n", " Message(\"Qubit state after the first application of SqrtNOT\");\n", " DumpMachine();\n", "\n", " // apply SqrtNOT gate again and verify that the qubit ends up in the |1⟩ state\n", " SqrtNOT(q);\n", " Message(\"Qubit state after the second application of SqrtNOT\");\n", " DumpMachine();\n", " \n", " // make sure the qubit is back to the 0 state\n", " Reset(q);\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "%simulate RunSqrtNOT" ] } ], "metadata": { "kernelspec": { "display_name": "Q#", "language": "qsharp", "name": "iqsharp" }, "language_info": { "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", "version": "0.4" } }, "nbformat": 4, "nbformat_minor": 2 }