Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yurijmikhalevich committed Feb 10, 2024
0 parents commit 88e0e35
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 0_RandomNumberGenerator/0_RandomNumberGenerator.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace QuantumRandomNumberGenerator {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;

@EntryPoint()
operation Main() : Int {
let max = 100;
Message($"Sampling a random number between 0 and {max}: ");

// Generate random number in the 0..max range.
return GenerateRandomNumberInRange(max);
}

/// Generates a random number between 0 and `max`.
operation GenerateRandomNumberInRange(max : Int) : Int {
// Determine the number of bits needed to represent `max` and store it
// in the `nBits` variable.
let nBits = BitSizeI(max);

// Then generate `nBits` random bits which will
// represent the generated random number.
mutable bits = [];
for idxBit in 1..nBits {
set bits += [GenerateRandomBit()];
}
let sample = ResultArrayAsInt(bits);

// Return random number if it is within the requested range.
// Generate it again if it is outside the range.
return sample > max ? GenerateRandomNumberInRange(max) | sample;
}

operation GenerateRandomBit() : Result {
// Allocate a qubit, by default it is in zero state
use q = Qubit();

// We apply a Hadamard operation H to the state
// It now has a 50% chance of being measured 0 or 1
H(q);

// Now we measure the qubit in Z-basis.
let result = M(q);

// Reset the qubit so it can be safely released.
Reset(q);

return result;
}
}
7 changes: 7 additions & 0 deletions 0_RandomNumberGenerator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Quantum Random Number Generator in Q#

This is a solution to the Quantum Random Number Generator problem in Q#. The problem is described in detail on [this page](https://learn.microsoft.com/en-us/training/modules/qsharp-create-first-quantum-development-kit/3-random-bit-generator):

> Classical computers don't produce random numbers, but rather pseudorandom numbers. A pseudorandom number generator generates a deterministic sequence of numbers based on some initial value, called a seed. To better approximate random values, this seed is often the current time from the CPU's clock.
>
> Quantum computers, on the other hand, can generate truly random numbers. This is because the measurement of a qubit in superposition is a probabilistic process. The result of the measurement is random, and there's no way to predict the outcome. This is the basic principle of quantum random number generators.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Quantum Computing sample programs and exercises in Q#

<div align="center">
<img alt="Quantum Computing" src="https://raw.githubusercontent.com/move-fast-and-break-things/quantum-computing-exercises/main/quantum-computing.webp" width="600px" />
</div>

This repository is a collection of sample Quantum Computing programs and exercises written in Q#.

## Getting started

- Acquire basic knowledge of classical programming concepts
- Acquire a basic understanding of quantum computing concepts; start with the amazing [Quantum Computing for Computer Scientists presentation from Microsoft Research](https://www.youtube.com/watch?v=F_Riqjdh2oM)
- Install the latest version of [Visual Studio Code](https://code.visualstudio.com/download)
- Install the latest version of the [Azure Quantum Development Kit](https://marketplace.visualstudio.com/items?itemName=quantum.qsharp-lang-vscode) extension
Binary file added quantum-computing.webp
Binary file not shown.

0 comments on commit 88e0e35

Please sign in to comment.