Skip to content

Commit

Permalink
format libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottCarda-MS committed Mar 14, 2024
1 parent 80ba9e7 commit de61ac9
Show file tree
Hide file tree
Showing 21 changed files with 362 additions and 426 deletions.
2 changes: 1 addition & 1 deletion library/core/core.qs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Microsoft.Quantum.Core {
}

mutable output = [];
for _ in 1 .. length {
for _ in 1..length {
set output += [value];
}

Expand Down
4 changes: 2 additions & 2 deletions library/src/tests/resources/add_le.qs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace Test {
bitwidth : Int) : Unit {

TestAddLE3(name, adder, bitwidth, bitwidth, bitwidth);
TestAddLE3(name, adder, bitwidth, bitwidth, bitwidth+1);
TestAddLE3(name, adder, bitwidth, bitwidth, bitwidth+2);
TestAddLE3(name, adder, bitwidth, bitwidth, bitwidth + 1);
TestAddLE3(name, adder, bitwidth, bitwidth, bitwidth + 2);
}


Expand Down
16 changes: 8 additions & 8 deletions library/src/tests/resources/compare.qs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ namespace Test {
open Microsoft.Quantum.Diagnostics;

internal operation CompareWithBigInt(
name: String,
name : String,
bitwidth : Int,
quantumComparator : (BigInt, Qubit[], Qubit) => Unit,
classicalComparator : (Int, Int) -> Bool) : Unit {

for n in 1..bitwidth {
use qs = Qubit[n];
use t = Qubit();

for a in 0 .. 2^n+1 { // We want b to have more bits sometimes...
for b in 0 .. 2^n-1 {
for a in 0..2 ^ n + 1 { // We want b to have more bits sometimes...
for b in 0..2 ^ n-1 {
ApplyXorInPlace(b, qs);
quantumComparator(IntAsBigInt(a), qs, t);
let actual = MResetZ(t) == One;
Expand All @@ -29,18 +29,18 @@ namespace Test {
}

internal operation CompareWithLE(
name: String,
name : String,
bitwidth : Int,
quantumComparator : (Qubit[], Qubit[], Qubit) => Unit,
classicalComparator : (Int, Int) -> Bool) : Unit {

for n in 1..bitwidth {
use x = Qubit[n];
use y = Qubit[n];
use t = Qubit();

for a in 0 .. 2^n-1 {
for b in 0 .. 2^n-1 {
for a in 0..2 ^ n-1 {
for b in 0..2 ^ n-1 {
ApplyXorInPlace(a, x);
ApplyXorInPlace(b, y);
quantumComparator(x, y, t);
Expand Down
6 changes: 3 additions & 3 deletions library/src/tests/resources/inc_by_le.qs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Test {
$"{name}: Incorrect sum={yActual}, expected={yExpected}. ctl={isCtl}, |x|={xLen}, |y|={yLen}, x={xValue}, y={yValue}.");
Fact(xActual == xValue,
$"{name}: Incorrect x={xActual}, expected={xValue}. ctl={isCtl}, |x|={xLen}, |y|={yLen}, x={xValue}, y={yValue}.");

ResetAll(x);
ResetAll(y);
Reset(ctl);
Expand All @@ -77,8 +77,8 @@ namespace Test {
bitwidth : Int) : Unit {

TestIncByLE2(name, adder, bitwidth, bitwidth);
TestIncByLE2(name, adder, bitwidth, bitwidth+1);
TestIncByLE2(name, adder, bitwidth, bitwidth+2);
TestIncByLE2(name, adder, bitwidth, bitwidth + 1);
TestIncByLE2(name, adder, bitwidth, bitwidth + 2);
}

internal operation TestIncByLECtl(
Expand Down
18 changes: 9 additions & 9 deletions library/src/tests/resources/qft_le.qs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ namespace Test {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Arrays;

operation PrepareEntangledState (
operation PrepareEntangledState(
left : Qubit[],
right : Qubit[]) : Unit is Adj + Ctl {

for idxQubit in 0 .. Length(left) - 1
for idxQubit in 0..Length(left) - 1
{
H(left[idxQubit]);
Controlled X([left[idxQubit]], right[idxQubit]);
}
}

operation AssertOperationsEqualReferenced (
operation AssertOperationsEqualReferenced(
nQubits : Int,
actual : (Qubit[] => Unit),
expected : (Qubit[] => Unit is Adj)) : Unit {
Expand All @@ -33,14 +33,14 @@ namespace Test {

/// # Summary
/// Hard-code 1 qubit QFT
operation QFT1 (target : Qubit[]) : Unit is Adj {
operation QFT1(target : Qubit[]) : Unit is Adj {
Fact(Length(target) == 1, $"`Length(target!)` must be 1");
H((target)[0]);
}

/// # Summary
/// Hard-code 2 qubit QFT
operation QFT2 (target : Qubit[]) : Unit is Adj {
operation QFT2(target : Qubit[]) : Unit is Adj {
Fact(Length(target) == 2, $"`Length(target!)` must be 2");
let (q1, q2) = ((target)[0], (target)[1]);
H(q1);
Expand All @@ -50,7 +50,7 @@ namespace Test {

/// # Summary
/// Hard-code 3 qubit QFT
operation QFT3 (target : Qubit[]) : Unit is Adj {
operation QFT3(target : Qubit[]) : Unit is Adj {
Fact(Length(target) == 3, $"`Length(target)` must be 3");
let (q1, q2, q3) = ((target)[0], (target)[1], (target)[2]);
H(q1);
Expand All @@ -63,7 +63,7 @@ namespace Test {

/// # Summary
/// Hard-code 4 qubit QFT
operation QFT4 (target : Qubit[]) : Unit is Adj {
operation QFT4(target : Qubit[]) : Unit is Adj {
Fact(Length(target) == 4, $"`Length(target!)` must be 4");
let (q1, q2, q3, q4) = ((target)[0], (target)[1], (target)[2], (target)[3]);
H(q1);
Expand All @@ -80,8 +80,8 @@ namespace Test {

/// # Summary
/// Compares QFT to the hard-coded implementations
operation TestQFT(n: Int) : Unit {
Fact(n>=1 and n<=4, "Only have four tests for QFT.");
operation TestQFT(n : Int) : Unit {
Fact(n >= 1 and n <= 4, "Only have four tests for QFT.");
let testOperations = [QFT1, QFT2, QFT3, QFT4];
AssertOperationsEqualReferenced(n, testOperations[n-1], q => ApplyQFT(Reversed(q)));
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/tests/resources/select.qs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Test {
use temporaryRegister = Qubit[dataBits];
use dataRegister = Qubit[dataBits];

let data = DrawMany(_ => DrawMany(_ => (DrawRandomInt(0, 1) == 1), dataBits, 0), 2^addressBits, 0);
let data = DrawMany(_ => DrawMany(_ => (DrawRandomInt(0, 1) == 1), dataBits, 0), 2 ^ addressBits, 0);

for (index, expected) in Enumerated(data) {
ApplyXorInPlace(index, addressRegister);
Expand All @@ -33,7 +33,7 @@ namespace Test {
for _ in 1..rounds {
let addressBits = DrawRandomInt(2, 6);
let dataBits = 10;
let numData = DrawRandomInt(2^(addressBits - 1) + 1, 2^addressBits - 1);
let numData = DrawRandomInt(2 ^ (addressBits - 1) + 1, 2 ^ addressBits - 1);

let data = DrawMany(_ => DrawMany(_ => (DrawRandomInt(0, 1) == 1), dataBits, 0), numData, 0);

Expand Down
40 changes: 20 additions & 20 deletions library/src/tests/resources/state_preparation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace Test {
open Microsoft.Quantum.Unstable.StatePreparation;


operation TestPlusState(): Unit {
operation TestPlusState() : Unit {
use q = Qubit();
PreparePureStateD([Sqrt(0.5), Sqrt(0.5)], [q]);
DumpMachine();
// Ucompute plus
H(q);
}

operation TestMinusState(): Unit {
operation TestMinusState() : Unit {
use q = Qubit();
PreparePureStateD([Sqrt(0.5), -Sqrt(0.5)], [q]);
DumpMachine();
Expand All @@ -24,7 +24,7 @@ namespace Test {
X(q);
}

operation TestBellState(): Unit {
operation TestBellState() : Unit {
use q = Qubit[2];
PreparePureStateD([Sqrt(0.5), 0.0, 0.0, Sqrt(0.5)], q);
DumpMachine();
Expand All @@ -33,7 +33,7 @@ namespace Test {
H(q[0]);
}

operation TestCat3State(): Unit {
operation TestCat3State() : Unit {
use q = Qubit[3];
PreparePureStateD([Sqrt(0.5), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Sqrt(0.5)], q);
DumpMachine();
Expand All @@ -50,52 +50,52 @@ namespace Test {
T(qs[1]);
}

operation TestPrepareComplex(): Unit {
operation TestPrepareComplex() : Unit {
use q = Qubit[2];
let c00 = ComplexPolar(0.5, 0.0);
let c01 = ComplexPolar(0.5, PI()/4.0);
let c10 = ComplexPolar(0.5, PI()/2.0);
let c11 = ComplexPolar(0.5, 3.0*PI()/4.0);
let c01 = ComplexPolar(0.5, PI() / 4.0);
let c10 = ComplexPolar(0.5, PI() / 2.0);
let c11 = ComplexPolar(0.5, 3.0 * PI() / 4.0);
ApproximatelyPreparePureStateCP(0.0, [c00, c01, c10, c11], q);
DumpMachine();
Adjoint PrepareComplex(q);
}

operation TestPreparationCompletion(): Unit {
operation TestPreparationCompletion() : Unit {
let testCases = [
// Test positive coefficients
[0.773761, 0.633478],
// Test positive coefficients
[0.773761, 0.633478],
[0.183017, 0.406973, 0.604925, 0.659502],
[0.0986553, 0.359005, 0.465689, 0.467395, 0.419893, 0.118445, 0.461883, 0.149609],
[0.271471, 0.0583654, 0.11639, 0.36112, 0.307383, 0.193371, 0.274151, 0.332542, 0.130172, 0.222546, 0.314879, 0.210704, 0.212429, 0.245518, 0.30666, 0.22773],

// Test negative coefficients; should give same probabilities as positive coefficients
[-0.773761, 0.633478],
// Test negative coefficients; should give same probabilities as positive coefficients
[-0.773761, 0.633478],
[0.183017, -0.406973, 0.604925, 0.659502],
[0.0986553, -0.359005, 0.465689, -0.467395, 0.419893, 0.118445, -0.461883, 0.149609],
[-0.271471, 0.0583654, 0.11639, 0.36112, -0.307383, 0.193371, -0.274151, 0.332542, 0.130172, 0.222546, 0.314879, -0.210704, 0.212429, 0.245518, -0.30666, -0.22773],

// Test unnormalized coefficients
[1.0986553, 0.359005, 0.465689, -0.467395, 0.419893, 0.118445, 0.461883, 0.149609],
// Test unnormalized coefficients
[1.0986553, 0.359005, 0.465689, -0.467395, 0.419893, 0.118445, 0.461883, 0.149609],

// Test missing coefficients
[1.0986553, 0.359005, 0.465689, -0.467395, 0.419893, 0.118445]
// Test missing coefficients
[1.0986553, 0.359005, 0.465689, -0.467395, 0.419893, 0.118445]
];

for coefficients in testCases {
let L = Length(coefficients);
let N = Ceiling(Log(IntAsDouble(L))/LogOf2() - 0.001);
let N = Ceiling(Log(IntAsDouble(L)) / LogOf2() - 0.001);
use q = Qubit[N];
PreparePureStateD(coefficients, q);
DumpMachine();
Adjoint PreparePureStateD(coefficients, q);
}
}

operation TestEndianness(): Unit {
operation TestEndianness() : Unit {
let n = 4;
use qs = Qubit[n];
let bitsize = 2^n;
let bitsize = 2 ^ n;
for i in 0..bitsize-1 {
mutable c = Repeated(0.0, bitsize);
set c w/= i <- 1.0;
Expand Down
Loading

0 comments on commit de61ac9

Please sign in to comment.