Skip to content

Commit

Permalink
samples formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottCarda-MS committed Mar 14, 2024
1 parent dd9b10f commit 80ba9e7
Show file tree
Hide file tree
Showing 35 changed files with 175 additions and 166 deletions.
2 changes: 1 addition & 1 deletion samples/algorithms/BernsteinVazirani.qs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace Sample {

// Apply the quantum operations that encode the bit string.
for index in IndexRange(xRegister) {
if ((bitStringAsInt &&& 2^index) != 0) {
if ((bitStringAsInt &&& 2 ^ index) != 0) {
CNOT(xRegister[index], yQubit);
}
}
Expand Down
14 changes: 7 additions & 7 deletions samples/algorithms/BitFlipCode.qs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ namespace Sample {
// parity measurements.
let indexOfError =
if (parity01, parity12) == (One, Zero) {
0
} elif (parity01, parity12) == (One, One) {
1
} elif (parity01, parity12) == (Zero, One) {
2
} else {
0
} elif (parity01, parity12) == (One, One) {
1
} elif (parity01, parity12) == (Zero, One) {
2
} else {
-1
};
};

// If an error was detected, correct that qubit.
if indexOfError > -1 {
Expand Down
8 changes: 4 additions & 4 deletions samples/algorithms/DeutschJozsa.qs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Sample {
let isConstant = DeutschJozsa(fn, 5);
if (isConstant != shouldBeConstant) {
let shouldBeConstantStr = shouldBeConstant ?
"constant" |
"constant" |
"balanced";
fail $"{name} should be detected as {shouldBeConstantStr}";
}
Expand Down Expand Up @@ -104,7 +104,7 @@ namespace Sample {
// state so that they can be safely deallocated at the end of the block.
// The loop also sets `result` to `true` if all measurement results are
// `Zero`, i.e. if the function is a constant function, and sets
// `result` to `false` if not, which according to the assumption on 𝑓
// `result` to `false` if not, which according to the assumption on 𝑓
// means that it must be balanced.
mutable result = true;
for q in queryRegister {
Expand All @@ -131,15 +131,15 @@ namespace Sample {
// A more complex constant Boolean function.
// It applies X to every input basis vector.
operation ConstantBoolF(args : Qubit[], target : Qubit) : Unit {
for i in 0..(2^Length(args))-1 {
for i in 0..(2 ^ Length(args))-1 {
ApplyControlledOnInt(i, X, args, target);
}
}

// A more complex balanced Boolean function.
// It applies X to half of the input basis vectors.
operation BalancedBoolF(args : Qubit[], target : Qubit) : Unit {
for i in 0..2..(2^Length(args))-1 {
for i in 0..2..(2 ^ Length(args))-1 {
ApplyControlledOnInt(i, X, args, target);
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/algorithms/Entanglement.qs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Sample {
// Allocate the two qubits that will be entangled.
use (q1, q2) = (Qubit(), Qubit());

// Set the first qubit in superposition by calling the `H` operation,
// Set the first qubit in superposition by calling the `H` operation,
// which applies a Hadamard transformation to the qubit.
// Then, entangle the two qubits using the `CNOT` operation.
H(q1);
Expand Down
4 changes: 2 additions & 2 deletions samples/algorithms/HiddenShift.qs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace Sample {
/// - [*Martin Roetteler*,
/// Proc. SODA 2010, ACM, pp. 448-457, 2010]
/// (https://doi.org/10.1137/1.9781611973075.37)
operation FindHiddenShift (
operation FindHiddenShift(
Ufstar : (Qubit[] => Unit),
Ug : (Qubit[] => Unit),
n : Int)
Expand Down Expand Up @@ -142,7 +142,7 @@ namespace Sample {
operation BentFunction(register : Qubit[]) : Unit {
Fact(Length(register) % 2 == 0, "Length of register must be even.");
let u = Length(register) / 2;
let xs = register[0 .. u - 1];
let xs = register[0..u - 1];
let ys = register[u...];
for index in 0..u-1 {
CZ(xs[index], ys[index]);
Expand Down
4 changes: 2 additions & 2 deletions samples/algorithms/HiddenShiftNISQ.qs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace Sample {
/// - [*Martin Roetteler*,
/// Proc. SODA 2010, ACM, pp. 448-457, 2010]
/// (https://doi.org/10.1137/1.9781611973075.37)
operation FindHiddenShift (
operation FindHiddenShift(
Ufstar : (Qubit[] => Unit),
Ug : (Qubit[] => Unit),
n : Int)
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace Sample {
operation BentFunction(register : Qubit[]) : Unit {
Fact(Length(register) % 2 == 0, "Length of register must be even.");
let u = Length(register) / 2;
let xs = register[0 .. u - 1];
let xs = register[0..u - 1];
let ys = register[u...];
for index in 0..u-1 {
CZ(xs[index], ys[index]);
Expand Down
2 changes: 1 addition & 1 deletion samples/algorithms/Measurement.qs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Sample {
open Microsoft.Quantum.Measurement;

@EntryPoint()
operation Main () : (Result, Result[]) {
operation Main() : (Result, Result[]) {
// The `M` operation performs a measurement of a single qubit in the
// computational basis, also known as the Pauli Z basis.
use q = Qubit();
Expand Down
14 changes: 7 additions & 7 deletions samples/algorithms/PhaseFlipCode.qs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ namespace Sample {
// parity measurements.
let indexOfError =
if (parity01, parity12) == (One, Zero) {
0
} elif (parity01, parity12) == (One, One) {
1
} elif (parity01, parity12) == (Zero, One) {
2
} else {
0
} elif (parity01, parity12) == (One, One) {
1
} elif (parity01, parity12) == (Zero, One) {
2
} else {
-1
};
};

// If an error was detected, correct that qubit.
if indexOfError > -1 {
Expand Down
2 changes: 1 addition & 1 deletion samples/algorithms/QRNG.qs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Sample {
// Allocate a qubit.
use q = Qubit();

// Set the qubit into superposition of 0 and 1 using the Hadamard
// Set the qubit into superposition of 0 and 1 using the Hadamard
// operation `H`.
H(q);

Expand Down
2 changes: 1 addition & 1 deletion samples/algorithms/RandomBit.qs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Sample {
// Set the qubit in superposition by applying a Hadamard transformation.
H(qubit);

// Measure the qubit. There is a 50% probability of measuring either
// Measure the qubit. There is a 50% probability of measuring either
// `Zero` or `One`.
let result = M(qubit);

Expand Down
12 changes: 5 additions & 7 deletions samples/algorithms/Shor.qs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ namespace Sample {
set foundFactors = true;
set factors = (gcd, number / gcd);
}
set attempt = attempt+1;
set attempt = attempt + 1;
if (attempt > 100) {
fail "Failed to find factors: too many attempts!";
}
}
until foundFactors
} until foundFactors
fixup {
Message("The estimated period did not yield a valid factor. " +
"Trying again.");
Expand Down Expand Up @@ -235,8 +234,7 @@ namespace Sample {
if frequencyEstimate != 0 {
return PeriodFromFrequency(
modulus, frequencyEstimate, bitsPrecision, 1);
}
else {
} else {
Message("The estimated frequency was 0, trying again.");
return 1;
}
Expand All @@ -258,10 +256,10 @@ namespace Sample {
///
/// # Output
/// The numerator k of dyadic fraction k/2^bitsPrecision approximating s/r.
operation EstimateFrequency(generator : Int,modulus : Int, bitsize : Int)
operation EstimateFrequency(generator : Int, modulus : Int, bitsize : Int)
: Int {
mutable frequencyEstimate = 0;
let bitsPrecision = 2 * bitsize + 1;
let bitsPrecision = 2 * bitsize + 1;
Message($"Estimating frequency with bitsPrecision={bitsPrecision}.");

// Allocate qubits for the superposition of eigenstates of the oracle
Expand Down
2 changes: 1 addition & 1 deletion samples/algorithms/Superposition.qs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Sample {
// Set the qubit in superposition by applying a Hadamard transformation.
H(qubit);

// Measure the qubit. There is a 50% probability of measuring either
// Measure the qubit. There is a 50% probability of measuring either
// `Zero` or `One`.
let result = M(qubit);

Expand Down
2 changes: 1 addition & 1 deletion samples/algorithms/Teleportation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Sample {
open Microsoft.Quantum.Measurement;

@EntryPoint()
operation Main () : Result[] {
operation Main() : Result[] {
// Allocate the message and target qubits.
use (message, target) = (Qubit(), Qubit());

Expand Down
35 changes: 16 additions & 19 deletions samples/estimation/Dynamics.qs
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,36 @@ namespace QuantumDynamics {

let len1 = 3;
let len2 = 3;
let valLength = 2*len1+len2+1;
mutable values = [0.0, size=valLength];
let valLength = 2 * len1 + len2 + 1;
mutable values = [0.0, size = valLength];

let val1 = J*p*dt;
let val2 = -g*p*dt;
let val3 = J*(1.0 - 3.0*p)*dt/2.0;
let val4 = g*(1.0 - 4.0*p)*dt/2.0;
let val1 = J * p * dt;
let val2 = -g * p * dt;
let val3 = J * (1.0 - 3.0 * p) * dt / 2.0;
let val4 = g * (1.0 - 4.0 * p) * dt / 2.0;

for i in 0..len1 {

if (i % 2 == 0) {
set values w/= i <- val1;
}
else {
} else {
set values w/= i <- val2;
}

}

for i in len1+1..len1+len2 {
for i in len1 + 1..len1 + len2 {
if (i % 2 == 0) {
set values w/= i <- val3;
}
else {
} else {
set values w/= i <- val4;
}
}

for i in len1+len2+1..valLength-1 {
for i in len1 + len2 + 1..valLength-1 {
if (i % 2 == 0) {
set values w/= i <- val1;
}
else {
} else {
set values w/= i <- val2;
}
}
Expand Down Expand Up @@ -128,8 +125,8 @@ namespace QuantumDynamics {
for row in 0..r_end {
for col in start..2..c_end { // Iterate through even or odd columns based on `grp`

let row2 = dir ? row+1 | row;
let col2 = dir ? col | col+1;
let row2 = dir ? row + 1 | row;
let col2 = dir ? col | col + 1;

Exp(P_op, theta, [qArr[row][col], qArr[row2][col2]]);
}
Expand All @@ -151,18 +148,18 @@ namespace QuantumDynamics {
///
operation IsingModel2DSim(N1 : Int, N2 : Int, J : Double, g : Double, totTime : Double, dt : Double) : Unit {

use qs = Qubit[N1*N2];
use qs = Qubit[N1 * N2];
let qubitArray = Chunks(N2, qs); // qubits are re-arranged to be in an N1 x N2 array

let p = 1.0 / (4.0 - 4.0^(1.0 / 3.0));
let p = 1.0 / (4.0 - 4.0 ^ (1.0 / 3.0));
let t = Ceiling(totTime / dt);

let seqLen = 10 * t + 1;

let angSeq = SetAngleSequence(p, dt, J, g);

for i in 0..seqLen - 1 {
let theta = (i==0 or i==seqLen-1) ? J*p*dt/2.0 | angSeq[i%10];
let theta = (i == 0 or i == seqLen-1) ? J * p * dt / 2.0 | angSeq[i % 10];

// for even indexes
if i % 2 == 0 {
Expand Down
18 changes: 11 additions & 7 deletions samples/estimation/EkeraHastadFactoring.qs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ namespace Microsoft.Quantum.Applications.Cryptography {
// ------------------------------ //

/// Window size for exponentiation (c_exp)
internal function ExponentWindowLength_() : Int { 5 }
internal function ExponentWindowLength_() : Int {
5
}

/// Window size for multiplication (c_mul)
internal function MultiplicationWindowLength_() : Int { 5 }
internal function MultiplicationWindowLength_() : Int {
5
}

// ------------------------------- //
// Modular arithmetic (operations) //
Expand Down Expand Up @@ -184,11 +188,11 @@ namespace Microsoft.Quantum.Applications.Cryptography {
}

internal function LookupData(factor : BigInt, expLength : Int, mulLength : Int, base : BigInt, mod : BigInt, sign : Int, numBits : Int) : Bool[][] {
mutable data = [[false, size = numBits], size = 2^(expLength + mulLength)];
for b in 0..2^mulLength - 1 {
for a in 0..2^expLength - 1 {
let idx = b * 2^expLength + a;
let value = ModulusL(factor * IntAsBigInt(b) * IntAsBigInt(sign) * (base^a), mod);
mutable data = [[false, size = numBits], size = 2 ^ (expLength + mulLength)];
for b in 0..2 ^ mulLength - 1 {
for a in 0..2 ^ expLength - 1 {
let idx = b * 2 ^ expLength + a;
let value = ModulusL(factor * IntAsBigInt(b) * IntAsBigInt(sign) * (base ^ a), mod);
set data w/= idx <- BigIntAsBoolArray(value, numBits);
}
}
Expand Down
4 changes: 2 additions & 2 deletions samples/estimation/ShorRE.qs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Shors {

// When chooseing parameters for `EstimateFrequency`, make sure that
// generator and modules are not co-prime
let _ = EstimateFrequency(11, 2^bitsize - 1, bitsize);
let _ = EstimateFrequency(11, 2 ^ bitsize - 1, bitsize);
}

/// # Summary
Expand All @@ -50,7 +50,7 @@ namespace Shors {
)
: Int {
mutable frequencyEstimate = 0;
let bitsPrecision = 2 * bitsize + 1;
let bitsPrecision = 2 * bitsize + 1;

// Allocate qubits for the superposition of eigenstates of
// the oracle that is used in period finding.
Expand Down
Loading

0 comments on commit 80ba9e7

Please sign in to comment.