2018-10-30 18:48:56 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2018-09-20 19:58:36 +03:00
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2018-10-30 18:48:56 +03:00
|
|
|
// This file contains testing harness for all tasks.
|
2018-09-20 19:58:36 +03:00
|
|
|
// You should not modify anything in this file.
|
|
|
|
// The tasks themselves can be found in Tasks.qs file.
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-10-30 18:48:56 +03:00
|
|
|
namespace Quantum.Kata.SuperdenseCoding {
|
|
|
|
|
2018-09-20 19:58:36 +03:00
|
|
|
open Microsoft.Quantum.Primitive;
|
|
|
|
open Microsoft.Quantum.Canon;
|
|
|
|
open Microsoft.Quantum.Extensions.Testing;
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
2018-09-20 19:58:36 +03:00
|
|
|
// ------------------------------------------------------
|
2018-10-30 18:48:56 +03:00
|
|
|
operation AssertEqualOnZeroState (N : Int, taskImpl : (Qubit[] => Unit), refImpl : (Qubit[] => Unit : Adjoint)) : Unit {
|
|
|
|
using (qs = Qubit[N]) {
|
|
|
|
// apply operation that needs to be tested
|
|
|
|
taskImpl(qs);
|
|
|
|
|
|
|
|
// apply adjoint reference operation and check that the result is |0^N⟩
|
|
|
|
Adjoint refImpl(qs);
|
|
|
|
|
|
|
|
// assert that all qubits end up in |0⟩ state
|
|
|
|
AssertAllZero(qs);
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
operation T1_CreateEntangledPair_Test () : Unit {
|
|
|
|
// We only check for 2 qubits.
|
|
|
|
AssertEqualOnZeroState(2, CreateEntangledPair, CreateEntangledPair_Reference);
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
2018-09-20 19:58:36 +03:00
|
|
|
// ------------------------------------------------------
|
|
|
|
// Helper operation that runs superdense coding protocol using two building blocks
|
|
|
|
// specified as first two parameters.
|
2018-10-30 18:48:56 +03:00
|
|
|
operation ComposeProtocol (encodeOp : ((Qubit, Bool[]) => Unit), decodeOp : ((Qubit, Qubit) => Bool[]), message : Bool[]) : Bool[] {
|
|
|
|
mutable result = new Bool[2];
|
|
|
|
|
|
|
|
using (qs = Qubit[2]) {
|
|
|
|
CreateEntangledPair_Reference(qs);
|
|
|
|
encodeOp(qs[0], message);
|
|
|
|
set result = decodeOp(qs[1], qs[0]);
|
|
|
|
|
|
|
|
// Make sure that we return qubits back in 0 state.
|
|
|
|
ResetAll(qs);
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
return result;
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
2018-09-20 19:58:36 +03:00
|
|
|
// ------------------------------------------------------
|
|
|
|
// Helper operation that runs superdense coding protocol (specified by protocolOp)
|
|
|
|
// on all possible input values and verifies that decoding result matches the inputs
|
2018-10-30 18:48:56 +03:00
|
|
|
operation TestProtocol (protocolOp : (Bool[] => Bool[])) : Unit {
|
|
|
|
mutable data = new Bool[2];
|
|
|
|
|
|
|
|
// Loop over the 4 possible combinations of two bits
|
|
|
|
for (n in 0 .. 3) {
|
|
|
|
set data[0] = 1 == n / 2;
|
|
|
|
set data[1] = 1 == n % 2;
|
|
|
|
|
|
|
|
for (iter in 1 .. 100) {
|
|
|
|
let result = protocolOp(data);
|
|
|
|
|
|
|
|
// Now test if the bits were transfered correctly.
|
|
|
|
AssertBoolArrayEqual(result, data, $"The message {data} was transfered incorrectly as {result}");
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
operation T2_EncodeMessageInQubit_Test () : Unit {
|
|
|
|
TestProtocol(ComposeProtocol(EncodeMessageInQubit, DecodeMessageFromQubits_Reference, _));
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
operation T3_DecodeMessageFromQubits_Test () : Unit {
|
|
|
|
TestProtocol(ComposeProtocol(EncodeMessageInQubit_Reference, DecodeMessageFromQubits, _));
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
operation T4_SuperdenseCodingProtocol_Test () : Unit {
|
|
|
|
TestProtocol(SuperdenseCodingProtocol);
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
}
|