This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Improvements to Microsoft.Quantum.Random namespace #328
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
1dbe742
Initial Q# API for new Random namespace
cgranade 445dd5f
Consolidate RNG logic in SimulatorBase.
cgranade 6410342
Consolidate RNG logic in Toffoli simulator as well.
cgranade c036b7f
Fix build errors.
cgranade 045b493
Fix typo in previous commit.
cgranade e112042
Adapt namespaces in test project.
cgranade 90281c1
One more namespace fix.
cgranade 4e7bc13
Started some API documentation comments.
cgranade c620ef6
Add more API docs and tests.
cgranade 2acbaf1
Merge branch 'master' into cgranade/random
b35b26a
Fix open statements.
cgranade 990750a
Merge branch 'cgranade/random' of https://github.com/microsoft/qsharp…
cgranade 586c6fb
Fix SampleNBits.
cgranade 98c423d
Merge branch 'master' into cgranade/random
13250ef
Update src/Simulation/Common/SimulatorBase.cs
65aafb0
Update src/Simulation/Common/SimulatorBase.cs
b320b94
Update src/Simulation/QsharpCore/Random/Convienence.qs
6536eb7
Update src/Simulation/QsharpCore/Random/Types.qs
ff41894
Merge branch 'master' into cgranade/random
01801de
API docs, review feedback, more tests.
cgranade 5be6285
fixed typo
cgranade dbc2458
Added missing open.
cgranade 0599d92
Add another missing open.
cgranade 88f3482
Discrete → continuous
cgranade 3ad221e
Split out intrinsic tests to avoid duplicated types.
cgranade 4ebe4b1
Fix off by one error in unit tests.
cgranade 9af99a4
Added a little bit more testing.
cgranade 9467ede
Merge branch 'master' into cgranade/random
3622495
Merge branch 'cgranade/random' of https://github.com/microsoft/qsharp…
cgranade c7e9d32
A bit more testing.
cgranade cf02e6b
Avoid depending on Delay.
cgranade d938e4b
Break down tests a bit more.
cgranade ec19b73
Fix categorical tests.
cgranade e18a5a0
Update src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/In…
87e26c1
Update src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Ra…
53161bc
Update src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Ra…
7c1424a
Merge branch 'master' into cgranade/random
5ba050b
Merge branch 'master' into cgranade/random
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,51 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
cgranade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Diagnostics { | ||
|
|
||
| /// # Summary | ||
| /// Declares that a classical condition is true. | ||
| /// | ||
| /// # Input | ||
| /// ## actual | ||
| /// The condition to be declared. | ||
| /// ## message | ||
| /// Failure message string to be printed in the case that the classical | ||
| /// condition is false. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Diagnostics.Contradiction | ||
| /// | ||
| /// # Example | ||
| /// The following Q# snippet will fail: | ||
| /// ```Q# | ||
| /// Fact(false, "Expected true."); | ||
| /// ``` | ||
| function Fact(actual : Bool, message : String) : Unit { | ||
| if (not actual) { fail message; } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Declares that a classical condition is false. | ||
| /// | ||
| /// # Input | ||
| /// ## actual | ||
| /// The condition to be declared. | ||
| /// ## message | ||
| /// Failure message string to be printed in the case that the classical | ||
| /// condition is true. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Diagnostics.Fact | ||
| /// | ||
| /// # Example | ||
| /// The following Q# code will print "Hello, world": | ||
| /// ```Q# | ||
| /// Contradiction(2 == 3, "2 is not equal to 3."); | ||
| /// Message("Hello, world."); | ||
| /// ``` | ||
| function Contradiction(actual : Bool, message : String) : Unit { | ||
| if (actual) { fail message; } | ||
| } | ||
|
|
||
| } | ||
7 changes: 7 additions & 0 deletions
7
src/Simulation/QsharpCore/Diagnostics/Properties/NamespaceInfo.qs
This file contains hidden or 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,7 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| /// # Summary | ||
| /// This namespace contains functions and operations useful for diagnostic | ||
| /// purposes, including assert operations and claim functions. | ||
| namespace Microsoft.Quantum.Diagnostics {} |
This file contains hidden or 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
This file contains hidden or 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,115 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Random { | ||
| open Microsoft.Quantum.Intrinsic; | ||
| open Microsoft.Quantum.Math; | ||
| open Microsoft.Quantum.Diagnostics; | ||
|
|
||
| /// # Summary | ||
| /// Draws a random sample from a categorical distribution specified by a | ||
| /// list of probablities. | ||
| /// | ||
| /// # Description | ||
| /// The probability of selecting a specific index is proportional to the value | ||
| /// of the array element at that index. | ||
| /// Array elements that are equal to zero are ignored and their indices are never | ||
| /// returned. If any array element is less than zero, | ||
| /// or if no array element is greater than zero, then the operation fails. | ||
| /// | ||
| /// # Input | ||
| /// ## probs | ||
| /// An array of floating-point numbers proportional to the probability of | ||
| /// selecting each index. | ||
| /// | ||
| /// # Output | ||
| /// An integer $i$ with probability $\Pr(i) = p_i / \sum_i p_i$, where $p_i$ | ||
| /// is the $i$th element of `probs`. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Random.CategoricalDistribution | ||
| operation DrawCategorical(probs : Double[]) : Int { | ||
| // There are nicer ways of doing this, but they require the full | ||
| // standard library to be available. | ||
| mutable sum = 0.0; | ||
| for (prob in probs) { | ||
| Fact(prob >= 0.0, "Probabilities must be positive."); | ||
| set sum += prob; | ||
| } | ||
|
|
||
| let variate = DrawRandomDouble(0.0, sum); | ||
| mutable acc = 0.0; | ||
| for (idx in 0..Length(probs) - 1) { | ||
| set acc += probs[idx]; | ||
| if (variate <= acc) { | ||
| return idx; | ||
| } | ||
| } | ||
|
|
||
| return Length(probs) - 1; | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Draws a random Pauli value. | ||
| /// | ||
| /// # Output | ||
| /// Either `PauliI`, `PauliX`, `PauliY`, or `PauliZ` with equal | ||
| /// probability. | ||
| operation DrawRandomPauli() : Pauli { | ||
| return [PauliI, PauliX, PauliY, PauliZ][DrawRandomInt(0, 3)]; | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Given an array of data and an a distribution over its indices, | ||
| /// attempts to choose an element at random. | ||
| /// | ||
| /// # Input | ||
| /// ## data | ||
| /// The array from which an element should be chosen. | ||
| /// ## indexDistribution | ||
| /// A distribution over the indices of `data`. | ||
| /// | ||
| /// # Ouput | ||
| /// A tuple `(succeeded, element)` where `succeeded` is `true` if and only | ||
| /// if the sample chosen from `indexDistribution` was a valid index into | ||
| /// `data`, and where `element` is an element of `data` chosen at random. | ||
| /// | ||
| /// # Example | ||
| /// The following Q# snippet chooses an element at random from an array: | ||
| /// ```Q# | ||
| /// let (succeeded, element) = MaybeChooseElement( | ||
| /// data, | ||
| /// DiscreteUniformDistribution(0, Length(data) - 1) | ||
| /// ); | ||
| /// Fact(succeeded, "Index chosen by MaybeChooseElement was not valid."); | ||
| /// ``` | ||
| operation MaybeChooseElement<'T>(data : 'T[], indexDistribution : DiscreteDistribution) : (Bool, 'T) { | ||
| let index = indexDistribution::Sample(); | ||
| if (index >= 0 and index < Length(data)) { | ||
| return (true, data[index]); | ||
| } else { | ||
| return (false, Default<'T>()); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Given a success probability, returns a single Bernoulli trial that | ||
| /// is true with the given probability. | ||
| /// | ||
| /// # Input | ||
| /// ## successProbability | ||
| /// The probability with which `true` should be returned. | ||
| /// | ||
| /// # Output | ||
| /// `true` with probability `successProbability` and `false` with | ||
| /// probability `1.0 - successProbability`. | ||
| /// | ||
| /// # Example | ||
| /// The following Q# snippet samples flips from a biased coin: | ||
| /// ```Q# | ||
| /// let flips = DrawMany(DrawRandomBool, 10, 0.6); | ||
| /// ``` | ||
| operation DrawRandomBool(successProbability : Double) : Bool { | ||
| return DrawRandomDouble(0.0, 1.0) <= successProbability; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.