Last active
March 14, 2020 20:23
-
-
Save cgranade/09dabfe1cc2e650cd88d2cfc98185791 to your computer and use it in GitHub Desktop.
Values in Q#
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
FROM mcr.microsoft.com/quantum/iqsharp-base:0.10.2002.2610 | |
ENV IQSHARP_HOSTING_ENV=dev-to-sckaiser-arrays | |
# Make sure the contents of our repo are in ${HOME}. | |
# These steps are required for use on mybinder.org. | |
USER root | |
COPY . ${HOME} | |
RUN chown -R ${USER} ${HOME} | |
# While root, upgrade the .NET Core SDK. | |
RUN apt-get -y update && \ | |
apt-get -y install dotnet-sdk-3.1 && \ | |
apt-get clean && rm -rf /var/lib/apt/lists/ | |
# Finish by dropping back to the notebook user and installing support needed | |
# for C# notebooks. | |
USER ${USER} | |
RUN dotnet tool install --global \ | |
--add-source "https://dotnet.myget.org/F/dotnet-try/api/v3/index.json" \ | |
Microsoft.dotnet-interactive && \ | |
dotnet interactive jupyter install |
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": [ | |
"## Working with Q# Arrays" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"FirstExample\"]", | |
"text/html": [ | |
"<ul><li>FirstExample</li></ul>" | |
], | |
"text/plain": [ | |
"FirstExample" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function FirstExample() : Unit {\n", | |
" mutable a = [2, 4, 6];\n", | |
" let b = a;\n", | |
"\n", | |
" // Change the first element of `a` to 1.\n", | |
" set a w/= 0 <- 1;\n", | |
"\n", | |
" Message($\"{b}\");\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2,4,6]\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/json": "{\"@type\":\"tuple\"}", | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%simulate FirstExample" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"SecondExample\"]", | |
"text/html": [ | |
"<ul><li>SecondExample</li></ul>" | |
], | |
"text/plain": [ | |
"SecondExample" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function SecondExample() : Unit {\n", | |
" let a = [2, 4, 6] w/ 0 <- 1;\n", | |
" Message($\"{a}\");\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1,4,6]\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/json": "{\"@type\":\"tuple\"}", | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%simulate SecondExample" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"ThirdExample\"]", | |
"text/html": [ | |
"<ul><li>ThirdExample</li></ul>" | |
], | |
"text/plain": [ | |
"ThirdExample" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function ThirdExample() : Unit {\n", | |
" let a = ([2, 4, 6] w/ 0 <- 1 w/ 2 <- 10) + [20];\n", | |
" Message($\"{a}\");\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1,4,10,20]\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/json": "{\"@type\":\"tuple\"}", | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%simulate ThirdExample" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"FourthExample\"]", | |
"text/html": [ | |
"<ul><li>FourthExample</li></ul>" | |
], | |
"text/plain": [ | |
"FourthExample" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function FourthExample() : Unit {\n", | |
" mutable a = 10;\n", | |
" set a += 1;\n", | |
" Message($\"{a}\");\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"11\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/json": "{\"@type\":\"tuple\"}", | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%simulate FourthExample" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"FifthExample\"]", | |
"text/html": [ | |
"<ul><li>FifthExample</li></ul>" | |
], | |
"text/plain": [ | |
"FifthExample" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function FifthExample() : Unit {\n", | |
" mutable a = [2, 4, 6];\n", | |
" // Both of the following lines are equivalent:\n", | |
" set a = a w/ 0 <- 1;\n", | |
" set a w/= 0 <- 1;\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "{\"@type\":\"tuple\"}", | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%simulate FifthExample" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Quicksort in Q\\# ## " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"SampleUnsortedArray\"]", | |
"text/html": [ | |
"<ul><li>SampleUnsortedArray</li></ul>" | |
], | |
"text/plain": [ | |
"SampleUnsortedArray" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"open Microsoft.Quantum.Arrays;\n", | |
"open Microsoft.Quantum.Math;\n", | |
"\n", | |
"operation SampleUnsortedArray(length : Int) : Int[] {\n", | |
" mutable a = new Int[length];\n", | |
"\n", | |
" for (i in IndexRange(a)) {\n", | |
" set a w/= i <- RandomInt(100);\n", | |
" }\n", | |
" return a;\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"Sorted\"]", | |
"text/html": [ | |
"<ul><li>Sorted</li></ul>" | |
], | |
"text/plain": [ | |
"Sorted" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"open Microsoft.Quantum.Arrays;\n", | |
"open Microsoft.Quantum.Logical;\n", | |
"\n", | |
"function Sorted<'T>(comparisonFunction : (('T, 'T) -> Bool), inputArray: 'T[]) : 'T[] {\n", | |
" if (Length(inputArray) <= 1) {\n", | |
" return inputArray;\n", | |
" }\n", | |
" let pivot = Head(inputArray);\n", | |
" let left = Filtered(comparisonFunction(_, pivot), Rest(inputArray));\n", | |
" let right = Filtered(Compose(Not, comparisonFunction(_, pivot)), Rest(inputArray));\n", | |
"\n", | |
" return Sorted(comparisonFunction, left) + [pivot] + Sorted(comparisonFunction, right);\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/json": "[\"TestQuickSort\"]", | |
"text/html": [ | |
"<ul><li>TestQuickSort</li></ul>" | |
], | |
"text/plain": [ | |
"TestQuickSort" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"open Microsoft.Quantum.Logical;\n", | |
"\n", | |
"operation TestQuickSort() : Unit {\n", | |
" let unsorted = SampleUnsortedArray(10);\n", | |
" Message($\"Unsorted: {unsorted}\");\n", | |
" let sortedArray = Sorted(LessThanI, unsorted); \n", | |
" Message($\"Sorted: {sortedArray}\");\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Unsorted: [71,4,89,53,27,56,75,46,11,24]\n", | |
"Sorted: [4,11,24,27,46,53,56,71,75,89]\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/json": "{\"@type\":\"tuple\"}", | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%simulate TestQuickSort" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment