This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding sample for QRNG * Update samples/getting-started/qrng/Host.cs Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Host.cs Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Host.cs Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Qrng.qs Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Qrng.csproj Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Qrng.csproj Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Qrng.qs Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/host.py Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/host.py Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/Qrng.qs Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/README.md Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update samples/getting-started/qrng/host.py Co-Authored-By: Chris Granade <cgranade@gmail.com> * Update README.md * Update Host.cs * Update Host.cs Co-authored-by: Chris Granade <cgranade@gmail.com>
- Loading branch information
1 parent
eb5d9a0
commit 2eafaed
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Microsoft.Quantum.Simulation.Core; | ||
using Microsoft.Quantum.Simulation.Simulators; | ||
using System.Linq; | ||
|
||
namespace Qrng | ||
{ | ||
class Driver | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (var sim = new QuantumSimulator()) | ||
{ | ||
// First we initialize all the variables: | ||
var bitString = "0"; // To save the bit string | ||
int max = 50; // The maximum of the range | ||
int size = Convert.ToInt32(Math.Floor(Math.Log(max, 2.0) + 1)); | ||
// To calculate the amount of needed bits | ||
int output = max + 1; // Int to store the output | ||
while (output > max) // Loop to generate the number | ||
{ | ||
bitString = "0"; // Restart the bit string if fails | ||
bitString = String.Join("", Enumerable.Range(0, size).Select(idx => | ||
SampleQuantumRandomNumberGenerator.Run(sim).Result == Result.One ? "1" : "0" | ||
) | ||
); | ||
// Generate and concatenate the bits using using the Q# operation | ||
output = Convert.ToInt32(bitString, 2); | ||
// Convert the bit string to an integer | ||
} | ||
// Print the result | ||
Console.WriteLine($"The random number generated is {output}."); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
<PlatformTarget>x64</PlatformTarget> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.10.1912.501" /> | ||
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.10.1912.501" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
namespace Qrng { | ||
open Microsoft.Quantum.Intrinsic; | ||
|
||
operation SampleQuantumRandomNumberGenerator() : Result { | ||
using (q = Qubit()) { // Allocate a qubit. | ||
H(q); // Put the qubit to superposition. It now has a 50% chance of being 0 or 1. | ||
let r = M(q); // Measure the qubit value. | ||
Reset(q); | ||
return r; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- qsharp | ||
- python | ||
- csharp | ||
products: | ||
- qdk | ||
description: "This sample implements a quantum random number generator using Q#, a good first example to teach how to use the language." | ||
--- | ||
|
||
# Creating random numbers with quantum computing | ||
|
||
This sample implements a quantum random number generator, a very simple application that is useful to learn how to write a first Q# code and it's integration with the host programs in C# or Python. | ||
|
||
In the Q# code (Qrng.qs) you can find the code for extracting a random bit using quantum measurements over a qubit in superposition. For more information, you can take a look at the [full tutorial](https://docs.microsoft.com/quantum/quickstarts/qrng). | ||
|
||
In the classical code (Host.cs for C# and host.py for Python) you will find the code to create a random integer from 0 to a maximum integer by invoking several times the Q# operation for extracting a random bit. | ||
|
||
|
||
## Prerequisites ## | ||
|
||
- The Microsoft [Quantum Development Kit](https://docs.microsoft.com/quantum/install-guide/). | ||
|
||
## Running the Sample ## | ||
|
||
This sample can be run in a number of different ways, depending on your preferred environment. | ||
|
||
### Python in Visual Studio Code or the Command Line ### | ||
|
||
At a terminal, run the following command: | ||
|
||
```bash | ||
python host.py | ||
``` | ||
|
||
### C# in Visual Studio Code or the Command Line ### | ||
|
||
At a terminal, run the following command: | ||
|
||
```dotnetcli | ||
dotnet run | ||
``` | ||
|
||
### C# in Visual Studio 2019 ### | ||
|
||
Open the `getting-started.sln` solution in Visual Studio, then right-click on **Qrng** and select "Set As StartUp Project." | ||
Press Start in Visual Studio to run the sample. | ||
|
||
## Manifest ## | ||
|
||
- [Qrng.qs](https://github.com/microsoft/Quantum/blob/master/samples/getting-started/qrng/Qrng.qs): Q# code implementing quantum operations for this sample. | ||
- [Host.cs](https://github.com/microsoft/Quantum/blob/master/samples/getting-started/qrng/Host.cs): C# code to interact with and print out results of the Q# operations for this sample. | ||
- [Qrng.csproj](https://github.com/microsoft/Quantum/blob/master/samples/getting-started/qrng/Qrng.csproj): Main C# project for the sample. | ||
- [host.py](https://github.com/microsoft/Quantum/blob/master/samples/getting-started/qrng/host.py): Python code to interact with and print out results of the Q# operations for this sample. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
# This Python script contains a quantum random integer generator | ||
# using the operation QuantumRandomNumberGenerator defined in | ||
# the file qrng.qs. | ||
|
||
# For instructions on how to install the qsharp package, | ||
# see: https://docs.microsoft.com/quantum/install-guide/python | ||
|
||
import qsharp | ||
from Qrng import QuantumRandomNumberGenerator # We import the | ||
# quantum operation from the namespace defined in the file Qrng.qs | ||
max = 50 # Here we set the maximum of our range | ||
output = max + 1 # Variable to store the output | ||
while output > max: | ||
bit_string = [] # We initialise a list to store the bits that | ||
# will define our random integer | ||
for i in range(0, max.bit_length()): # We need to call the quantum | ||
# operation as many times as bits are needed to define the | ||
# maximum of our range. For example, if max=7 we need 3 bits | ||
# to generate all the numbers from 0 to 7. | ||
bit_string.append(QuantumRandomNumberGenerator.simulate()) | ||
# Here we call the quantum operation and store the random bit | ||
# in the list | ||
output = int("".join(str(x) for x in bit_string), 2) | ||
# Transform bit string to integer | ||
|
||
print("The random number generated is " + str(output)) | ||
# We print the random number |