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

Devikamehra/improve error join measurement #453

Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4a2f5df
[GraphColoring] Update code to use recent Q# features (#423)
tcNickolas Jul 27, 2020
a257161
[DeutschJoszaAlgorithm] Update code to use recent Q# features (#424)
arkabhat Jul 27, 2020
b5a9187
[UnitaryPatterns] Update code to use recent Q# features (#429)
arkabhat Jul 29, 2020
3e5a465
[KeyDistribution_BB84] Update whitespace and remove unnecessary reset…
pwndr3 Jul 29, 2020
da7be99
[PhaseEstimation] Update code to use recent Q# features (#435)
devikamehra Jul 29, 2020
2c615b5
[CHSHGame] Update code style and error messages (#441)
katherinel09 Jul 29, 2020
943ca5d
[SolveSATWithGrover] Update code to use recent Q# features (#442)
arkabhat Jul 29, 2020
aacdd06
[JointMeasurement] Improved Error Statement
devikamehra Aug 5, 2020
8a232ec
[JointMeasurement] Fixed the errors with more information.
devikamehra Sep 2, 2020
75ff28e
Merge branch 'OneWeekHackathon' into devikamehra/ImproveErrorJoinMeas…
devikamehra Sep 2, 2020
70ce1f6
[JointMeasurement] Replaced based states with superposition.
devikamehra Sep 2, 2020
c2cac95
Merge branch 'devikamehra/ImproveErrorJoinMeasurement' of https://git…
devikamehra Sep 2, 2020
4fe0b74
Merge branch 'master' into devikamehra/ImproveErrorJoinMeasurement
devikamehra Sep 2, 2020
59b9c3f
Merge branch 'master' into devikamehra/ImproveErrorJoinMeasurement
devikamehra Sep 2, 2020
838b337
[JointMeasurement] Test Code updated as per review comments.
devikamehra Sep 3, 2020
184df1f
[JointMeasurement] measurementsPerRun removed
devikamehra Sep 3, 2020
1acb7a7
Undo introduction of deprecated functions
tcNickolas Sep 5, 2020
2fc3beb
Merge branch 'master' into devikamehra/ImproveErrorJoinMeasurement
tcNickolas Sep 5, 2020
39860db
Remove a stray tab
tcNickolas Sep 5, 2020
894c682
Undo some more whitespace changes
tcNickolas Sep 5, 2020
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
71 changes: 50 additions & 21 deletions JointMeasurements/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,46 @@ namespace Quantum.Kata.JointMeasurements {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Random;

open Quantum.Kata.Utils;

// "Framework" operation for testing multi-qubit tasks for distinguishing states of an array of qubits
// with Int return
operation DistinguishStates_MultiQubit (nQubit : Int, nState : Int, statePrep : ((Qubit[], Int, Double) => Unit is Adj), testImpl : (Qubit[] => Int), preserveState : Bool) : Unit {
operation DistinguishStates_MultiQubit (nQubits : Int,
nStates : Int,
statePrep : ((Qubit[], Int, Double) => Unit is Adj),
testImpl : (Qubit[] => Int),
preserveState : Bool,
stateNames : String[]) : Unit {
let nTotal = 100;
mutable nOk = 0;

using (qs = Qubit[nQubit]) {
// misclassifications will store the number of times state i has been classified as state j (dimension nStates^2)
mutable misclassifications = new Int[nStates * nStates];
// unknownClassifications will store the number of times state i has been classified as some invalid state (index < 0 or >= nStates)
mutable unknownClassifications = new Int[nStates];

using (qs = Qubit[nQubits]) {
for (i in 1 .. nTotal) {
// get a random integer to define the state of the qubits
let state = DrawRandomInt(0, nState - 1);

let state = DrawRandomInt(0, nStates - 1);
// get a random rotation angle to define the exact state of the qubits
let alpha = DrawRandomDouble(0.0, 1.0) * PI();

// do state prep: convert |0...0⟩ to outcome with return equal to state
statePrep(qs, state, alpha);
// get the solution's answer and verify that it's a match

// get the solution's answer and verify that it's a match, if not, increase the exact mismatch count
let ans = testImpl(qs);
if (ans == state) {
set nOk += 1;
if ((ans >= 0) and (ans < nStates)) {
// classification result is a valid state index - check if is it correct
if (ans != state) {
set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1);
}
}

else {
// classification result is an invalid state index - file it separately
set unknownClassifications w/= state <- (unknownClassifications[state] + 1);
}

if (preserveState) {
// check that the state of the qubit after the operation is unchanged
Adjoint statePrep(qs, state, alpha);
Expand All @@ -52,10 +66,24 @@ namespace Quantum.Kata.JointMeasurements {
}
}

EqualityFactI(nOk, nTotal, $"{nTotal - nOk} test runs out of {nTotal} returned incorrect state.");
mutable totalMisclassifications = 0;
for (i in 0 .. nStates - 1) {
for (j in 0 .. nStates - 1) {
if (misclassifications[(i * nStates) + j] != 0) {
set totalMisclassifications += misclassifications[i * nStates + j];
Message($"Misclassified {stateNames[i]} as {stateNames[j]} in {misclassifications[(i * nStates) + j]} test runs.");
}
}
if (unknownClassifications[i] != 0) {
set totalMisclassifications += unknownClassifications[i];
Message($"Misclassified {stateNames[i]} as Unknown State in {unknownClassifications[i]} test runs.");
}
}
// This check will tell the total number of failed classifications
Fact(totalMisclassifications == 0, $"{totalMisclassifications} test runs out of {nTotal} returned incorrect state (see output for details).");
}


// ------------------------------------------------------
operation StatePrep_ParityMeasurement (qs : Qubit[], state : Int, alpha : Double) : Unit is Adj {

Expand All @@ -76,19 +104,19 @@ namespace Quantum.Kata.JointMeasurements {

// ------------------------------------------------------
operation T01_SingleQubitMeasurement_Test () : Unit {
DistinguishStates_MultiQubit(2, 2, StatePrep_ParityMeasurement, SingleQubitMeasurement, false);
DistinguishStates_MultiQubit(2, 2, StatePrep_ParityMeasurement, SingleQubitMeasurement, false, ["α|00⟩ + β|11⟩", "α|01⟩ + β|10⟩"]);
}


// ------------------------------------------------------
operation T02_ParityMeasurement_Test () : Unit {
DistinguishStates_MultiQubit(2, 2, StatePrep_ParityMeasurement, ParityMeasurement, true);
DistinguishStates_MultiQubit(2, 2, StatePrep_ParityMeasurement, ParityMeasurement, true, ["α|00⟩ + β|11⟩", "α|01⟩ + β|10⟩"]);
}


// ------------------------------------------------------
operation T03_GHZOrGHZWithX_Test () : Unit {
DistinguishStates_MultiQubit(4, 2, StatePrep_ParityMeasurement, GHZOrGHZWithX, true);
DistinguishStates_MultiQubit(4, 2, StatePrep_ParityMeasurement, GHZOrGHZWithX, true, ["α|0000⟩ + β|1111⟩", "α|0011⟩ + β|1100⟩"]);
}


Expand Down Expand Up @@ -123,10 +151,10 @@ namespace Quantum.Kata.JointMeasurements {
}
}


operation T04_GHZOrWState_Test () : Unit {
for (i in 1 .. 5) {
DistinguishStates_MultiQubit(2 * i, 2, StatePrep_GHZOrWState, GHZOrWState, true);
DistinguishStates_MultiQubit(2 * i, 2, StatePrep_GHZOrWState, GHZOrWState, true, ["GHZ State", "W State"]);
}
}

Expand All @@ -148,7 +176,8 @@ namespace Quantum.Kata.JointMeasurements {


operation T05_DifferentBasis_Test () : Unit {
DistinguishStates_MultiQubit(2, 2, StatePrep_DifferentBasis, DifferentBasis, true);
DistinguishStates_MultiQubit(2, 2, StatePrep_DifferentBasis, DifferentBasis, true,
["α|00⟩ + β|01⟩ + β|10⟩ + α|11⟩", "α|00⟩ - β|01⟩ + β|10⟩ - α|11⟩"]);
}


Expand Down