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

Commit

Permalink
[Superposition] Update code to use recent Q# features (#303)
Browse files Browse the repository at this point in the history
Per #190
  • Loading branch information
delbert authored Mar 18, 2020
1 parent ee3c9ff commit 6c0ba03
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
20 changes: 10 additions & 10 deletions Superposition/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ namespace Quantum.Kata.Superposition {
// Goal: create a GHZ state (|0...0⟩ + |1...1⟩) / sqrt(2) on these qubits.
operation GHZ_State_Reference (qs : Qubit[]) : Unit is Adj {

H(qs[0]);
H(Head(qs));

for (q in Rest(qs)) {
CNOT(qs[0], q);
CNOT(Head(qs), q);
}
}

Expand All @@ -159,13 +159,13 @@ namespace Quantum.Kata.Superposition {
// Goal: create a superposition of all even numbers on N qubits if isEven is true,
// or a superposition of all odd numbers on N qubits if isEven is false.
operation EvenOddNumbersSuperposition_Reference (qs : Qubit[], isEven : Bool) : Unit is Adj {
let N = Length(qs);
for (i in 0 .. N-2) {
H(qs[i]);

for (q in Most(qs)) {
H(q);
}
// for odd numbers, flip the last bit to 1
if (not isEven) {
X(qs[N-1]);
X(Tail(qs));
}
}

Expand Down Expand Up @@ -231,16 +231,16 @@ namespace Quantum.Kata.Superposition {
// Example: for bit string = [true, false] the qubit state required is (|00⟩ + |10⟩) / sqrt(2).
operation ZeroAndBitstringSuperposition_Reference (qs : Qubit[], bits : Bool[]) : Unit is Adj {

EqualityFactI(Length(bits), Length(qs), "Arrays should have the same length");
EqualityFactB(bits[0], true, "First bit of the input bit string should be set to true");
Fact(Length(bits) == Length(qs), "Arrays should have the same length");
Fact(Head(bits), "First bit of the input bit string should be set to true");

// Hadamard first qubit
H(qs[0]);
H(Head(qs));

// iterate through the bit string and CNOT to qubits corresponding to true bits
for (i in 1 .. Length(qs) - 1) {
if (bits[i]) {
CNOT(qs[0], qs[i]);
CNOT(Head(qs), qs[i]);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions Superposition/Tasks.qs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Quantum.Kata.Superposition {

open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
Expand Down Expand Up @@ -69,7 +70,7 @@ namespace Quantum.Kata.Superposition {
operation AllBasisVectors_TwoQubits (qs : Qubit[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
EqualityFactI(Length(qs), 2, "The array should have exactly 2 qubits.");
Fact(Length(qs) == 2, "The array should have exactly 2 qubits.");

// ...
}
Expand All @@ -81,7 +82,7 @@ namespace Quantum.Kata.Superposition {
operation AllBasisVectorsWithPhases_TwoQubits (qs : Qubit[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
EqualityFactI(Length(qs), 2, "The array should have exactly 2 qubits.");
Fact(Length(qs) == 2, "The array should have exactly 2 qubits.");

// Hint: Is this state separable?
// ...
Expand Down Expand Up @@ -174,8 +175,8 @@ namespace Quantum.Kata.Superposition {
operation ZeroAndBitstringSuperposition (qs : Qubit[], bits : Bool[]) : Unit {
// The following lines enforce the constraints on the input that you are given.
// You don't need to modify them. Feel free to remove them, this won't cause your code to fail.
EqualityFactI(Length(bits), Length(qs), "Arrays should have the same length");
EqualityFactB(bits[0], true, "First bit of the input bit string should be set to true");
Fact(Length(bits) == Length(qs), "Arrays should have the same length");
Fact(Head(bits), "First bit of the input bit string should be set to true");

// ...
}
Expand Down
3 changes: 1 addition & 2 deletions Superposition/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ namespace Quantum.Kata.Superposition {
}
}
}
until (ok)
fixup { }
until (ok);

// convert numbers to bit strings
for (i in 0 .. 3) {
Expand Down

0 comments on commit 6c0ba03

Please sign in to comment.