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
25 changes: 25 additions & 0 deletions src/Simulation/QCTraceSimulator.Tests/Circuits/RandomTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.


namespace Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.Tests {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;

internal function Fact(condition : Bool, message : String) : Unit {
if (not condition) {
fail message;
}
}

@Test("ResourcesEstimator")
/// # Summary
/// Checks for regression against microsoft/qsharp-runtime#256.
operation CheckRandomInCorrectRange() : Unit {
for (idxTrial in 0..99) {
let sample = Random([1.0, 2.0, 2.0]);

Fact(0 <= sample and sample <= 2, $"sample was {sample}, not in range [0, 2]");
}
}
}
15 changes: 15 additions & 0 deletions src/Simulation/Simulators/QCTraceSimulator/InterfaceUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics;

using Microsoft.Quantum.Simulation.Core;
Expand Down Expand Up @@ -73,5 +74,19 @@ public static partial class Extensions
{
return InterfaceType(t, typeof(IControllable<>));
}

internal static IEnumerable<TResult> SelectAggregates<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TResult, TSource, TResult> aggregate,
TResult initial = default
)
{
var acc = initial;
foreach (var element in source)
{
acc = aggregate(acc, element);
yield return acc;
}
}
}
}
42 changes: 24 additions & 18 deletions src/Simulation/Simulators/QCTraceSimulator/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime;

Expand Down Expand Up @@ -214,32 +216,36 @@ static class SimulatorsUtils
/// <param name="uniformZeroOneSample"> Number between Zero and one, uniformly distributed</param>
public static long SampleDistribution(IQArray<double> unnormalizedDistribution, double uniformZeroOneSample)
{
double total = 0.0;
foreach (double prob in unnormalizedDistribution)
if (unnormalizedDistribution.Any(prob => prob < 0.0))
{
if (prob < 0)
{
throw new ExecutionFailException("Random expects array of non-negative doubles.");
}
total += prob;
throw new ExecutionFailException("Random expects array of non-negative doubles.");
}

var total = unnormalizedDistribution.Sum();
if (total == 0)
{
throw new ExecutionFailException("Random expects array of non-negative doubles with positive sum.");
}

double sample = uniformZeroOneSample * total;
double sum = unnormalizedDistribution[0];
for (int i = 0; i < unnormalizedDistribution.Length - 1; ++i)
{
if (sum >= sample)
{
return i;
}
sum += unnormalizedDistribution[i];
}
return unnormalizedDistribution.Length;
var sample = uniformZeroOneSample * total;

return unnormalizedDistribution
// Get the unnormalized CDF of the distribution.
.SelectAggregates((double acc, double x) => acc + x)
// Look for the first index at which the CDF is bigger
// than the random sample of 𝑈(0, 1) that we were given
// as a parameter.
.Select((cumulativeProb, idx) => (cumulativeProb, idx))
.Where(item => item.cumulativeProb >= sample)
// Cast that index to long, and default to returning
// the last item.
.Select(
item => (long)item.idx
)
.DefaultIfEmpty(
unnormalizedDistribution.Length - 1
)
.First();
}
}
}