Skip to content

Instantly share code, notes, and snippets.

@cgranade
Last active July 9, 2019 16:22
Show Gist options
  • Save cgranade/acc846ee187e19a65a23bfd37e171ae7 to your computer and use it in GitHub Desktop.
Save cgranade/acc846ee187e19a65a23bfd37e171ae7 to your computer and use it in GitHub Desktop.
Using Q# from Python
# The same image used by mybinder.org
FROM mcr.microsoft.com/quantum/iqsharp-base:0.8.1906.2007-beta
# Add metadata indicating that this image is used for the katas.
ENV IQSHARP_HOSTING_ENV=reddit
# Make sure the contents of our repo are in ${HOME}
# Required for mybinder.org
COPY . ${HOME}
USER root
RUN chown -R ${USER} ${HOME}
USER ${USER}
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import qsharp"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"hello = qsharp.compile(\"\"\"\n",
" function Hello() : Unit {\n",
" Message(\"Hello, world!\");\n",
" }\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, world!\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hello.simulate()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"qrng = qsharp.compile(\"\"\"\n",
" open Microsoft.Quantum.Measurement;\n",
" \n",
" operation NextRandomBit() : Result {\n",
" using (q = Qubit()) {\n",
" return MResetX(q);\n",
" }\n",
" }\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"key = hex(int(\"\".join([\"1\" if qrng.simulate() else \"0\" for _ in range(512)]), 2))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xcbf21e57060fccb035635ebfc0a7c01e3008ec5d34db84bb7623ccda8434b1c7fe8befe08ca81a3d0ee63939407f4b4aa43a8c25a470804352c2fd3bdf0128fe\n"
]
}
],
"source": [
"print(key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment