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

Update to 0.15 syntax, batch 2 #591

Merged
merged 29 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b26c14c
[ExploringGroversAlgorithm] Fix dead link
vivanwin Oct 25, 2020
d6be7cd
Updating linkings to link directly to workbook
vivanwin Oct 25, 2020
c7ca8b9
Merge remote-tracking branch 'upstream/main' into main
vivanwin Dec 8, 2020
1f797b5
Add %config and %trace where it makes sense
vivanwin Dec 9, 2020
bebf0b2
Merge remote-tracking branch 'upstream/main' into main
vivanwin Dec 11, 2020
b7d7b93
Use config files
vivanwin Dec 11, 2020
96b73cb
Remove little endian kets from output
tcNickolas Dec 12, 2020
d999f06
Merge remote-tracking branch 'upstream/main' into main
vivanwin Dec 17, 2020
9768ef7
Merge remote-tracking branch 'upstream/main' into main
vivanwin Dec 21, 2020
598b078
Merge remote-tracking branch 'upstream/main' into main
vivanwin Dec 29, 2020
357683a
Merge remote-tracking branch 'upstream/main' into main
vivanwin Jan 3, 2021
1a8a51b
Merge remote-tracking branch 'upstream/main' into main
vivanwin Jan 29, 2021
b2ec052
update to SDK 0.15 CHSHGame
vivanwin Jan 29, 2021
f15743b
Update to 0.15 syntax DeutschJozsaAlgorithm
vivanwin Jan 29, 2021
76b81b3
Update to 0.15 syntax DistinguishUnitaries
vivanwin Jan 29, 2021
b279c14
Update to 0.15 syntax GHZGame
vivanwin Jan 29, 2021
8974bdb
Update to 0.15 syntax GraphColoring
vivanwin Jan 29, 2021
4364f98
Update to 0.15 syntax GroverAlgorithm
vivanwin Jan 29, 2021
d97ef59
Update to 0.15 syntax JointMeasurements
vivanwin Jan 29, 2021
d0e3e73
Update to 0.15 syntax KeyDistribution_BB84
vivanwin Jan 29, 2021
e0b1cbb
Update to 0.15 syntax MagicSquareGame
vivanwin Jan 29, 2021
1705530
Update to 0.15 syntax PhaseEstimation
vivanwin Jan 29, 2021
515071b
Update to 0.15 syntax QEC_BitFlipCode
vivanwin Jan 29, 2021
9dd9b13
Update to 0.15 syntax QFT
vivanwin Jan 29, 2021
f250db4
Update to 0.15 syntax RippleCarryAdder
vivanwin Jan 29, 2021
5577143
Update to 0.15 syntax SimonsAlgorithm
vivanwin Jan 29, 2021
82e1963
Fix a couple indentations
tcNickolas Jan 30, 2021
a58b127
Merge branch 'main' into main
tcNickolas Jan 30, 2021
d1b1b9d
Return scoped "use" to PhaseEstimation task 2.2
tcNickolas Jan 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update to 0.15 syntax QFT
  • Loading branch information
vivanwin committed Jan 29, 2021
commit 9dd9b130517262b0a97c50e4b3d45761a74cc82d
33 changes: 16 additions & 17 deletions QFT/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Quantum.Kata.QFT {
// where 0.j₁j₂...jₙ is a binary fraction, similar to decimal fractions:
// 0.j₁j₂...jₙ = j₁ / 2¹ + j₂ / 2² + ... + jₙ / 2ⁿ.
operation BinaryFractionClassical_Reference (q : Qubit, j : Int[]) : Unit is Adj+Ctl {
for (ind in 0 .. Length(j) - 1) {
for ind in 0 .. Length(j) - 1 {
if (j[ind] == 1) {
Rotation_Reference(q, ind + 1);
}
Expand All @@ -53,7 +53,7 @@ namespace Quantum.Kata.QFT {

function IntArrayAsInt (j : Int[]) : Int {
mutable n = 0;
for (ind in 0 .. Length(j) - 1) {
for ind in 0 .. Length(j) - 1 {
set n = n * 2 + j[ind];
}
return n;
Expand All @@ -75,7 +75,7 @@ namespace Quantum.Kata.QFT {
// to (α |0⟩ + β · exp(2πi · 0.j₁j₂...jₙ) |1⟩) ⊗ |j₁j₂...jₙ⟩,
// where 0.j₁j₂...jₙ is a binary fraction corresponding to the basis state j₁j₂...jₙ of the register.
operation BinaryFractionQuantum_Reference (q : Qubit, jRegister : Qubit[]) : Unit is Adj+Ctl {
for (ind in 0 .. Length(jRegister) - 1) {
for ind in 0 .. Length(jRegister) - 1 {
Controlled Rotation_Reference([jRegister[ind]], (q, ind + 1));
}
}
Expand All @@ -88,7 +88,7 @@ namespace Quantum.Kata.QFT {
// to 1/sqrt(2) (|0⟩ + exp(2πi · 0.j₁j₂...jₙ) |1⟩) ⊗ |j₂...jₙ⟩.
operation BinaryFractionQuantumInPlace_Reference (register : Qubit[]) : Unit is Adj+Ctl {
OneQubitQFT_Reference(register[0]);
for (ind in 1 .. Length(register) - 1) {
for ind in 1 .. Length(register) - 1 {
Controlled Rotation_Reference([register[ind]], (register[0], ind + 1));
}
}
Expand All @@ -99,7 +99,7 @@ namespace Quantum.Kata.QFT {
// Goal: Reverse the order of qubits, i.e., convert the state of the register to |xₙ...x₂x₁⟩.
operation ReverseRegister_Reference (register : Qubit[]) : Unit is Adj+Ctl {
let N = Length(register);
for (ind in 0 .. N / 2 - 1) {
for ind in 0 .. N / 2 - 1 {
SWAP(register[ind], register[N - 1 - ind]);
}
}
Expand All @@ -114,7 +114,7 @@ namespace Quantum.Kata.QFT {
// 1/sqrt(2) (|0⟩ + exp(2πi · 0.j₁j₂...jₙ₋₁jₙ) |1⟩) ⊗
operation QuantumFourierTransform_Reference (register : Qubit[]) : Unit is Adj+Ctl {
let n = Length(register);
for (i in 0 .. n - 1) {
for i in 0 .. n - 1 {
BinaryFractionQuantumInPlace_Reference(register[i ...]);
}
ReverseRegister_Reference(register);
Expand Down Expand Up @@ -207,7 +207,7 @@ namespace Quantum.Kata.QFT {
// Task 3.1. Implement powers of the QFT
operation QFTPower_Reference (P : Int, inputRegister : Qubit[]) : Unit is Adj+Ctl {
// Use the fact that QFT⁴ = I
for (_ in 1 .. (P % 4)) {
for _ in 1 .. (P % 4) {
QuantumFourierTransform_Reference(inputRegister);
}
}
Expand All @@ -228,16 +228,15 @@ namespace Quantum.Kata.QFT {


operation QFTRoot_Reference (P : Int, inputRegister : Qubit[]) : Unit is Adj+Ctl {
using (aux = Qubit[2]) {
let Q = QuantumFourierTransform_Reference;
let Q2 = OperationPowCA(Q, 2);
within {
ApplyToEachCA(H, aux);
Controlled Adjoint Q([aux[0]], inputRegister);
Controlled Adjoint Q2([aux[1]], inputRegister);
} apply {
Circ(LittleEndian(aux), PI() / (2.0 * IntAsDouble(P)));
}
use aux = Qubit[2];
let Q = QuantumFourierTransform_Reference;
let Q2 = OperationPowCA(Q, 2);
within {
ApplyToEachCA(H, aux);
Controlled Adjoint Q([aux[0]], inputRegister);
Controlled Adjoint Q2([aux[1]], inputRegister);
} apply {
Circ(LittleEndian(aux), PI() / (2.0 * IntAsDouble(P)));
}
}

Expand Down
76 changes: 37 additions & 39 deletions QFT/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Quantum.Kata.QFT {
ArrayWrapperControlledOperation(T, _));

// general case
for (k in 4 .. 10) {
for k in 4 .. 10 {
AssertOperationsEqualReferenced(2, ArrayWrapperControlledOperation(Rotation(_, k), _),
ArrayWrapperControlledOperation(Rotation_Reference(_, k), _));
}
Expand All @@ -65,16 +65,16 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
function IntAsIntArray (j : Int, nBits : Int) : Int[] {
mutable bits = new Int[nBits];
for (ind in 0 .. nBits - 1) {
for ind in 0 .. nBits - 1 {
set bits w/= ind <- ((j &&& (1 <<< (nBits - 1 - ind))) > 0 ? 1 | 0);
}
return bits;
}

@Test("QuantumSimulator")
operation T13_BinaryFractionClassical () : Unit {
for (n in 1 .. 5) {
for (exponent in 0 .. (1 <<< n) - 1) {
for n in 1 .. 5 {
for exponent in 0 .. (1 <<< n) - 1 {
let bits = IntAsIntArray(exponent, n);
Message($"{n}-bit {exponent} = {bits}");
AssertOperationsEqualReferenced(2, ArrayWrapperControlledOperation(BinaryFractionClassical(_, bits), _),
Expand All @@ -95,7 +95,7 @@ namespace Quantum.Kata.QFT {

@Test("QuantumSimulator")
operation T14_BinaryFractionQuantum () : Unit {
for (n in 1 .. 5) {
for n in 1 .. 5 {
AssertOperationsEqualReferenced(n + 2, Task14InputWrapper(BinaryFractionQuantum, _),
Task14InputWrapper(BinaryFractionQuantum_Reference, _));
}
Expand All @@ -105,7 +105,7 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T15_BinaryFractionQuantumInPlace () : Unit {
for (n in 1 .. 6) {
for n in 1 .. 6 {
AssertOperationsEqualReferenced(n, BinaryFractionQuantumInPlace,
BinaryFractionQuantumInPlace_Reference);
}
Expand All @@ -115,7 +115,7 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T16_ReverseRegister () : Unit {
for (n in 1 .. 6) {
for n in 1 .. 6 {
AssertOperationsEqualReferenced(n, ReverseRegister,
ReverseRegister_Reference);
AssertOperationsEqualReferenced(n, ReverseRegister,
Expand All @@ -137,7 +137,7 @@ namespace Quantum.Kata.QFT {
operation T17_QuantumFourierTransform () : Unit {
AssertOperationsEqualReferenced(1, QuantumFourierTransform, HWrapper);

for (n in 1 .. 5) {
for n in 1 .. 5 {
AssertOperationsEqualReferenced(n, QuantumFourierTransform,
QuantumFourierTransform_Reference);
AssertOperationsEqualReferenced(n, QuantumFourierTransform,
Expand All @@ -151,7 +151,7 @@ namespace Quantum.Kata.QFT {
operation T18_InverseQFT () : Unit {
AssertOperationsEqualReferenced(1, InverseQFT, HWrapper);

for (n in 1 .. 5) {
for n in 1 .. 5 {
AssertOperationsEqualReferenced(n, InverseQFT,
InverseQFT_Reference);
AssertOperationsEqualReferenced(n, InverseQFT,
Expand All @@ -167,21 +167,20 @@ namespace Quantum.Kata.QFT {
operation AssertEqualOnZeroState (N : Int,
testImpl : (Qubit[] => Unit),
refImpl : (Qubit[] => Unit is Adj)) : Unit {
using (qs = Qubit[N]) {
// apply operation that needs to be tested
testImpl(qs);
use qs = Qubit[N];
// apply operation that needs to be tested
testImpl(qs);

// apply adjoint reference operation and check that the result is |0⟩
Adjoint refImpl(qs);
// apply adjoint reference operation and check that the result is |0⟩
Adjoint refImpl(qs);

// assert that all qubits end up in |0⟩ state
AssertAllZero(qs);
}
// assert that all qubits end up in |0⟩ state
AssertAllZero(qs);
}

@Test("QuantumSimulator")
operation T21_PrepareEqualSuperposition () : Unit {
for (N in 1 .. 5) {
for N in 1 .. 5 {
AssertEqualOnZeroState(N, PrepareEqualSuperposition, PrepareEqualSuperposition_Reference);
}
}
Expand All @@ -190,11 +189,11 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T22_PreparePeriodicState () : Unit {
for (N in 1 .. 5) {
for N in 1 .. 5 {
// cross-test: for F = 0 it's the same as equal superposition of states
AssertEqualOnZeroState(N, PreparePeriodicState(_, 0), PrepareEqualSuperposition_Reference);

for (F in 1 .. (1 <<< N - 1)) {
for F in 1 .. (1 <<< N - 1) {
AssertEqualOnZeroState(N, PreparePeriodicState(_, F), PreparePeriodicState_Reference(_, F));
}
}
Expand All @@ -204,7 +203,7 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T23_PrepareAlternatingState () : Unit {
for (N in 1 .. 5) {
for N in 1 .. 5 {
AssertEqualOnZeroState(N, PrepareAlternatingState, PrepareAlternatingState_Reference);
}
}
Expand All @@ -217,7 +216,7 @@ namespace Quantum.Kata.QFT {

@Test("QuantumSimulator")
operation T24_PrepareEqualSuperpositionOfEvenStates () : Unit {
for (N in 1 .. 5) {
for N in 1 .. 5 {
// cross-test: we already know how to prepare a superposition of even states
AssertEqualOnZeroState(N, ApplyHToMostWrapper, PrepareEqualSuperpositionOfEvenStates_Reference);
AssertEqualOnZeroState(N, PrepareEqualSuperpositionOfEvenStates, PrepareEqualSuperpositionOfEvenStates_Reference);
Expand All @@ -228,7 +227,7 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T25_PrepareSquareWaveSignal () : Unit {
for (N in 2 .. 5) {
for N in 2 .. 5 {
AssertEqualOnZeroState(N, PrepareSquareWaveSignal, PrepareSquareWaveSignal_Reference);
}
}
Expand All @@ -237,18 +236,17 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T26_Frequency () : Unit {
for (N in 2 .. 5) {
using (register = Qubit[N]) {
for (F in 0 .. (1 <<< N - 1)) {
// Prepare input state
PreparePeriodicState_Reference(register, F);
// Feed it to the solution
let FRet = Frequency(register);
if (FRet != F) {
fail $"Expected frequency {F}, returned frequency {FRet} (n = {N})";
}
ResetAll(register);
for N in 2 .. 5 {
use register = Qubit[N];
for F in 0 .. (1 <<< N - 1) {
// Prepare input state
PreparePeriodicState_Reference(register, F);
// Feed it to the solution
let FRet = Frequency(register);
if (FRet != F) {
fail $"Expected frequency {F}, returned frequency {FRet} (n = {N})";
}
ResetAll(register);
}
}
}
Expand All @@ -260,22 +258,22 @@ namespace Quantum.Kata.QFT {

// slow brute-force implementation of QFT integer power to test on small cases
internal operation QFTPower_Slow (P : Int, inputRegister : Qubit[]) : Unit is Adj {
for (_ in 1 .. P) {
for _ in 1 .. P {
QFT(BigEndian(inputRegister));
}
}

@Test("QuantumSimulator")
operation T31_QFTPower () : Unit {
// small tests: check correctness of our approach on small-ish powers on 4-qubit register
for (p in 0 .. 20) {
for p in 0 .. 20 {
let testOp = QFTPower_Reference(p, _);
let refOp = QFTPower_Slow(p, _);
AssertOperationsEqualReferenced(4, testOp, refOp);
}

// large tests: check speed and correctness both
for (n in 1 .. 9) {
for n in 1 .. 9 {
let power = (2 <<< (n + 10)) - 1;
let testOp = QFTPower(power, _);
let refOp = QFTPower_Reference(power, _);
Expand All @@ -288,8 +286,8 @@ namespace Quantum.Kata.QFT {
// ------------------------------------------------------
@Test("QuantumSimulator")
operation T32_QFTRoot () : Unit {
for (n in 2 .. 8) {
for (p in 2 .. 8) {
for n in 2 .. 8 {
for p in 2 .. 8 {
let testOp = QFTRoot(p, _);

// we only compare the solution's powers to the QFT (big endian),
Expand Down