Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Add ResultArrayAsInt override to target packages (#1055)
Browse files Browse the repository at this point in the history
This adds a `ResultArrayAsInt` implementation that is compatible with QIR hardware compilation to the target packages. This will allow it to act as an override for the libraries definition while still supporting calculation of the integer value on platforms that can support it.
  • Loading branch information
swernli authored Dec 2, 2022
1 parent c46da38 commit d106fba
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Microsoft.Quantum.Convert {
open Microsoft.Quantum.Arrays;

/// # Summary
/// Produces a non-negative integer from a string of Results in little endian format.
///
/// # Input
/// ## results
/// Results in binary representation of number.
function ResultArrayAsInt(results : Result[]) : Int {
mutable val = 0;
for i in IndexRange(results) {
set val += results[i] == One ? 2 ^ i | 0;
}
return val;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1FromSinglyControlled.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1Frac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResetAll.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResultArrayAsInt.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RFrac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RxFromSinglyControlled.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RyFromSinglyControlled.qs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1Frac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResetAll.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResultArrayAsInt.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RFrac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\SetToBasisState.qs" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1FromSinglyControlled.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1Frac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResetAll.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResultArrayAsInt.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RFrac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RxFromSinglyControlled.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RyFromSinglyControlled.qs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1FromSinglyControlled.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\R1Frac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResetAll.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\ResultArrayAsInt.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RFrac.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RxFromSinglyControlled.qs" />
<QSharpCompile Include="..\TargetDefinitions\Decompositions\RyFromSinglyControlled.qs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<QsharpCompile Include="..\Decompositions\ExpUtil.qs" />
<QsharpCompile Include="..\Decompositions\HFromSinglyControlled.qs" />
<QSharpCompile Include="..\Decompositions\MResetZExplicit.qs" />
<QSharpCompile Include="..\Decompositions\ResultArrayAsInt.qs" />
<QsharpCompile Include="..\Decompositions\RFromSinglyControlledR1.qs" />
<QsharpCompile Include="..\Decompositions\R1FromSinglyControlled.qs" />
<QsharpCompile Include="..\Decompositions\RxFromSinglyControlled.qs" />
Expand Down
16 changes: 16 additions & 0 deletions src/Simulation/TargetDefinitions/Tests/UtilitiesTest.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace UtilitiesTests {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Diagnostics;

@Test("SparseSimulator")
operation VerifyResultArrayAsInt() : Unit {
Fact(ResultArrayAsInt([Zero, Zero, Zero]) == 0, "ResultArrayAsInt of 0 value should work");
Fact(ResultArrayAsInt([One, Zero, Zero]) == 1, "ResultArrayAsInt of 1 value should work");
Fact(ResultArrayAsInt([Zero, One, Zero]) == 2, "ResultArrayAsInt of 2 value should work");
Fact(ResultArrayAsInt([One, One, Zero]) == 3, "ResultArrayAsInt of 3 value should work");
Fact(ResultArrayAsInt([Zero, Zero, One]) == 4, "ResultArrayAsInt of 4 value should work");
Fact(ResultArrayAsInt([One, Zero, One]) == 5, "ResultArrayAsInt of 5 value should work");
Fact(ResultArrayAsInt([Zero, One, One]) == 6, "ResultArrayAsInt of 6 value should work");
Fact(ResultArrayAsInt([One, One, One]) == 7, "ResultArrayAsInt of 7 value should work");
}
}

0 comments on commit d106fba

Please sign in to comment.