Skip to content

Commit

Permalink
Updates to new array creation syntax, part 2 (microsoft#739)
Browse files Browse the repository at this point in the history
This change covers cases not handled by the Q# Formatter.
  • Loading branch information
tcNickolas authored Jan 27, 2022
1 parent fe270bf commit 98baf2f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion BoundedKnapsack/Tasks.qs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace Quantum.Kata.BoundedKnapsack
// so you'll return an array of qubit arrays [|101⟩, |1110⟩, |0100⟩].
function RegisterAsJaggedArray<'T> (array : 'T[], b : Int[]) : 'T[][] {
// ...
return new 'T[][0];
return [];
}


Expand Down
14 changes: 6 additions & 8 deletions MagicSquareGame/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -133,37 +133,35 @@ namespace Quantum.Kata.MagicSquareGame {
// are equivalent. Alice and Bob can choose to use either method without affecting their
// strategy.
operation AliceQuantum_Reference(rowIndex : Int, qs : Qubit[]) : Int[] {
mutable cells = new Int[3];
mutable cells = [];
for column in 0..2 {
// Alice uses joint measurement to measure the qubits in the observable's Pauli bases.
let obs = GetMagicObservables_Reference(rowIndex, column);
let result = MeasureObservable_Reference(obs, qs);
set cells w/= column <- IsResultZero(result) ? 1 | -1;
set cells += [IsResultZero(result) ? 1 | -1];
}
return cells;
}

operation BobQuantum_Reference(columnIndex : Int, qs : Qubit[]) : Int[] {
mutable cells = new Int[3];
mutable cells = [];
for row in 0..2 {
// Bob converts the observable into an operator before measuring it.
let obs = GetMagicObservables_Reference(row, columnIndex);
let op = ApplyMagicObservables_Reference(obs, _);
let result = MeasureOperator_Reference(op, qs);
set cells w/= row <- IsResultZero(result) ? 1 | -1;
set cells += [IsResultZero(result) ? 1 | -1];
}
return cells;
}


// Task 2.7. Play the magic square game using the quantum strategy
operation PlayQuantumMagicSquare_Reference (askAlice : (Qubit[] => Int[]), askBob : (Qubit[] => Int[])) : (Int[], Int[]) {
mutable alice = new Int[3];
mutable bob = new Int[3];
use qs = Qubit[4];
CreateEntangledState_Reference(qs);
set alice = askAlice(qs[0..1]);
set bob = askBob(qs[2..3]);
let alice = askAlice(qs[0..1]);
let bob = askBob(qs[2..3]);
ResetAll(qs);
return (alice, bob);
}
Expand Down
7 changes: 2 additions & 5 deletions MagicSquareGame/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,10 @@ namespace Quantum.Kata.MagicSquareGame {
// ------------------------------------------------------
function Pairs<'T> (array : 'T[]) : ('T, 'T)[] {
let N = Length(array);
let length = N * (N - 1) / 2;
mutable pairs = new ('T, 'T)[length];
mutable i = 0;
mutable pairs = [];
for j in 0..N - 1 {
for k in j + 1..N - 1 {
set pairs w/= i <- (array[j], array[k]);
set i += 1;
set pairs += [(array[j], array[k])];
}
}
return pairs;
Expand Down
11 changes: 5 additions & 6 deletions SolveSATWithGrover/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,13 @@ namespace Quantum.Kata.GroversAlgorithm {

// ------------------------------------------------------
function GetClauseQubits (queryRegister : Qubit[], clause : (Int, Bool)[]) : (Qubit[], Bool[]) {
mutable clauseQubits = new Qubit[Length(clause)];
mutable flip = [false, size = Length(clause)];
for varIndex in 0 .. Length(clause) - 1 {
let (index, isTrue) = clause[varIndex];
mutable clauseQubits = [];
mutable flip = [];
for (index, isTrue) in clause {
// Add the variable used in the clause to the list of variables which we'll need to call the OR oracle
set clauseQubits w/= varIndex <- queryRegister[index];
set clauseQubits += [queryRegister[index]];
// If the negation of the variable is present in the formula, mark the qubit as needing a flip
set flip w/= varIndex <- not isTrue;
set flip += [not isTrue];
}

return (clauseQubits, flip);
Expand Down
11 changes: 5 additions & 6 deletions tutorials/ExploringGroversAlgorithm/Code.qs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ namespace Quantum.Kata.ExploringGroversAlgorithm

// Helper function to get the list of qubits used in the clause and the bitmask of whether they need to be flipped
function GetClauseQubits (queryRegister : Qubit[], clause : (Int, Bool)[]) : (Qubit[], Bool[]) {
mutable clauseQubits = new Qubit[Length(clause)];
mutable flip = [false, size = Length(clause)];
for varIndex in 0 .. Length(clause) - 1 {
let (index, isTrue) = clause[varIndex];
mutable clauseQubits = [];
mutable flip = [];
for (index, isTrue) in clause {
// Add the variable used in the clause to the list of variables which we'll need to call the OR oracle
let qt = queryRegister[index];
set clauseQubits w/= varIndex <- queryRegister[index];
set clauseQubits += [queryRegister[index]];
// If the negation of the variable is present in the formula, mark the qubit as needing a flip
set flip w/= varIndex <- not isTrue;
set flip += [not isTrue];
}

return (clauseQubits, flip);
Expand Down
2 changes: 1 addition & 1 deletion tutorials/SingleQubitSystemMeasurements/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Quantum.Kata.SingleQubitSystemMeasurements {
operation DistinguishTwoStates (statePrep : ((Qubit, Int) => Unit is Adj), testImpl : (Qubit => Bool), stateName : String[], checkFinalState : Bool) : Unit {
let nTotal = 100;
let nStates = 2;
mutable misclassifications = new Int[nStates];
mutable misclassifications = [0, size = nStates];

use q = Qubit();
for i in 1 .. nTotal {
Expand Down

0 comments on commit 98baf2f

Please sign in to comment.