Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Qir/Tests/QIR-static/qir-test-math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]")
REQUIRE(0 == strcmp(exc.what(), Quantum::Qis::Internal::excStrDrawRandomVal));
}

// Test equal minimum and maximum:
for(uint64_t num: { -5, 0, 3 } )
{
REQUIRE(Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomInt__body(num, num) == num);
}

// There is a strong difference in the opinions about how the random number generator must be tested.
// More or less agreed-upon items are:
// * The test must be 100% deterministic, i.e. must not fail, even with a very low probability.
Expand Down
4 changes: 2 additions & 2 deletions src/Simulation/QSharpFoundation/Random/Intrinsic.qs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Quantum.Random {
/// probability.
///
/// # Remarks
/// Fails if `max <= min`.
/// Fails if `max < min`.
///
/// # Example
/// The following Q# snippet randomly rolls a six-sided die:
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace Microsoft.Quantum.Random {
/// uniform probability.
///
/// # Remarks
/// Fails if `max <= min`.
/// Fails if `max < min`.
///
/// # Example
/// The following Q# snippet randomly draws an angle between $0$ and $2 \pi$:
Expand Down
8 changes: 4 additions & 4 deletions src/Simulation/QSharpFoundation/Random/Uniform.qs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.Quantum.Random {
/// interval from `min` to `max` with uniform probability.
///
/// # Remarks
/// Fails if `max <= min`.
/// Fails if `max < min`.
///
/// # Example
/// The following Q# snippet randomly draws an angle between $0$ and $2 \pi$:
Expand All @@ -34,7 +34,7 @@ namespace Microsoft.Quantum.Random {
function ContinuousUniformDistribution(
min : Double, max : Double
) : ContinuousDistribution {
Fact(max > min, $"Max must be larger than min, but {max} <= {min}.");
Fact(max >= min, $"Max must be greater than or equal to min, but {max} < {min}.");
return ContinuousDistribution(Delay(DrawRandomDouble, (min, max), _));
}

Expand All @@ -52,7 +52,7 @@ namespace Microsoft.Quantum.Random {
/// range from `min` to `max` with uniform probability.
///
/// # Remarks
/// Fails if `max <= min`.
/// Fails if `max < min`.
///
/// # Example
/// The following Q# snippet randomly rolls a six-sided die:
Expand All @@ -64,7 +64,7 @@ namespace Microsoft.Quantum.Random {
/// # See Also
/// - Microsoft.Quantum.DrawRandomDouble
function DiscreteUniformDistribution(min : Int, max : Int) : DiscreteDistribution {
Fact(max > min, $"Max must be larger than min, but {max} <= {min}.");
Fact(max >= min, $"Max must be greater than or equal to min, but {max} < {min}.");
return DiscreteDistribution(Delay(DrawRandomInt, (min, max), _));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,25 @@ namespace Microsoft.Quantum.Tests {
@Test("QuantumSimulator")
@Test("ToffoliSimulator")
operation CheckDrawRandomIntObeysRanges() : Unit {
let randomInt = DrawRandomInt(0, 45);
mutable randomInt = DrawRandomInt(0, 45);
if (randomInt > 45 or randomInt < 0) {
fail $"DrawRandomInt(0, 45) returned {randomInt}, outside the allowed range.";
}

set randomInt = DrawRandomInt(0, 0);
if (randomInt > 0 or randomInt < 0) {
fail $"DrawRandomInt(0, 0) returned {randomInt}, outside the allowed range.";
}

set randomInt = DrawRandomInt(-3, -3);
if (randomInt > -3 or randomInt < -3) {
fail $"DrawRandomInt(-3, -3) returned {randomInt}, outside the allowed range.";
}

set randomInt = DrawRandomInt(3, 3);
if (randomInt > 3 or randomInt < 3) {
fail $"DrawRandomInt(3, 3) returned {randomInt}, outside the allowed range.";
}
}

/// # Summary
Expand Down
10 changes: 5 additions & 5 deletions src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,16 @@ public DrawRandomInt(SimulatorBase m) : base(m) =>
public override Func<(long, long), long> __Body__ => arg =>
{
var (min, max) = arg;
if (max <= min)
if (max < min)
{
throw new ExecutionFailException($"Max must be greater than min, but {max} <= {min}.");
throw new ExecutionFailException($"Max must be greater than or equal to min, but {max} < {min}.");
}
return sim.RandomGenerator.NextLong(min, max);
};
}

/// <summary>
/// Implements the DrawRandomInt operation from the
/// Implements the DrawRandomDouble operation from the
/// Microsoft.Quantum.Random namespace.
/// </summary>
public class DrawRandomDouble : Random.DrawRandomDouble
Expand All @@ -490,9 +490,9 @@ public DrawRandomDouble(SimulatorBase m) : base(m) =>
public override Func<(double, double), double> __Body__ => arg =>
{
var (min, max) = arg;
if (max <= min)
if (max < min)
{
throw new ExecutionFailException($"Max must be greater than min, but {max} <= {min}.");
throw new ExecutionFailException($"Max must be greater than or equal to min, but {max} < {min}.");
}
var delta = max - min;
return min + delta * sim.RandomGenerator.NextDouble();
Expand Down