Skip to content

Instantly share code, notes, and snippets.

@crazy4pi314
Last active February 25, 2022 16:49
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save crazy4pi314/90c74fd23f084de8e78b150ba1987c1c to your computer and use it in GitHub Desktop.
Quantum Random Sweepstakes
FROM mcr.microsoft.com/quantum/iqsharp-base:0.11.2004.2825
ENV IQSHARP_HOSTING_ENV=sckaiser-azure-community-live
# 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}
RUN pip install rise
# Finish by dropping back to the notebook user.
USER ${USER}
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
namespace Sweepstakes {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Math;
/// # Summary
/// This operation uses a register of qubits in superposition to select
/// a random integer in a range {0, numPlayers-1}.
///
/// # Input
/// ## numPlayers
/// The number of participants in the random drawing.
///
/// # Output
/// An `Int` that represents the index of the player that has won the
/// sweepstakes.
///
/// # Remarks
/// ## Example
/// ```Q#
/// // You need to pick one of 7 viewers of your stream to send them
/// // a present! 🎁
/// let winnerIndex = SelectWinner(7);
/// ```
operation SelectWinner(numPlayers : Int) : Int {
// Calculate number of qubits needed to store the number of players.
let numQubits = BitSizeI(numPlayers);
// Setup the variable that will track the result.
mutable result = 0;
// This repeat block is needed because unless the number of players is
// a power of two, you could get a result that does not index a player.
// Since there is no fair way to reuse those spare values, we just try
// again until we get a random index that does represent a player.
repeat {
// Prepare a register of qubits
using (register = Qubit[numQubits]) {
// Put all of the qubits in superposition with the `H` operation.
ApplyToEach(H, register);
// Measure and interpret as an integer.
set result = MeasureInteger(LittleEndian(register));
}
// Make sure the result corresponds to the index of a player.
} until(result < numPlayers);
// Return the winning player!
return result;
}
}
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quantum Sweepstakes Machine\n",
"\n",
"Use the following Q# code to simulate a quantum random number generator that can randomly select an integer for giveaways and sweepstakes!\n",
"\n",
"> _If you want to learn more about what is going on here, check out the [Q# docs](docs.microsoft.com/quantum) or my book [Learn Quantum Computing with Python and Q#](bit.ly/qsharp-book)!_"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/json": "[\"SelectTwitchWinner\"]",
"text/html": [
"<ul><li>SelectTwitchWinner</li></ul>"
],
"text/plain": [
"SelectTwitchWinner"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"open Sweepstakes;\n",
"\n",
"operation SelectTwitchWinner(): Int{\n",
" return SelectWinner(7);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/json": "4",
"text/plain": [
"4"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%simulate SelectTwitchWinner"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"#### Version last tested with:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/json": "{\"rows\":[{\"@type\":\"@tuple\",\"Item1\":\"iqsharp\",\"Item2\":\"0.11.2003.3107\"},{\"@type\":\"@tuple\",\"Item1\":\"Jupyter Core\",\"Item2\":\"1.2.36563.0\"},{\"@type\":\"@tuple\",\"Item1\":\".NET Runtime\",\"Item2\":\".NETCoreApp,Version=v3.1\"}]}",
"text/html": [
"<table><thead><tr><th>Component</th><th>Version</th></tr></thead><tbody><tr><td>iqsharp</td><td>0.11.2003.3107</td></tr><tr><td>Jupyter Core</td><td>1.2.36563.0</td></tr><tr><td>.NET Runtime</td><td>.NETCoreApp,Version=v3.1</td></tr></tbody></table>"
],
"text/plain": [
"Component Version\r\n",
"------------ ------------------------\r\n",
"iqsharp 0.11.2003.3107\r\n",
"Jupyter Core 1.2.36563.0\r\n",
".NET Runtime .NETCoreApp,Version=v3.1\r\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%version"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Q#",
"language": "qsharp",
"name": "iqsharp"
},
"language_info": {
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment