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 {
|
|
|
|
|
2019-05-04 02:46:04 +03:00
|
|
|
open Microsoft.Quantum.Intrinsic;
|
|
|
|
open Microsoft.Quantum.Diagnostics;
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
|
2018-09-20 19:58:36 +03:00
|
|
|
// ------------------------------------------------------
|
2019-06-24 07:11:12 +03:00
|
|
|
|
|
|
|
operation T1_CreateEntangledPair_Test () : Unit {
|
|
|
|
using ((q1, q2) = (Qubit(), Qubit())) {
|
|
|
|
|
2018-10-30 18:48:56 +03:00
|
|
|
// apply operation that needs to be tested
|
2019-06-24 07:11:12 +03:00
|
|
|
CreateEntangledPair(q1, q2);
|
|
|
|
|
2018-10-30 18:48:56 +03:00
|
|
|
// apply adjoint reference operation and check that the result is |0^N⟩
|
2019-06-24 07:11:12 +03:00
|
|
|
Adjoint CreateEntangledPair_Reference(q1, q2);
|
|
|
|
|
2018-10-30 18:48:56 +03:00
|
|
|
// assert that all qubits end up in |0⟩ state
|
2019-06-24 07:11:12 +03:00
|
|
|
AssertAllZero([q1, q2]);
|
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.
|
2019-06-24 07:11:12 +03:00
|
|
|
operation ComposeProtocol (
|
|
|
|
encodeOp : ((Qubit, ProtocolMessage) => Unit),
|
|
|
|
decodeOp : ((Qubit, Qubit) => ProtocolMessage),
|
|
|
|
message : ProtocolMessage
|
|
|
|
) : ProtocolMessage {
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
using (qs = Qubit[2]) {
|
2019-06-24 07:11:12 +03:00
|
|
|
CreateEntangledPair_Reference(qs[0], qs[1]);
|
2018-10-30 18:48:56 +03:00
|
|
|
encodeOp(qs[0], message);
|
2019-06-24 07:11:12 +03:00
|
|
|
return decodeOp(qs[0], qs[1]);
|
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
|
2019-06-24 07:11:12 +03:00
|
|
|
operation TestProtocol (protocolOp : (ProtocolMessage => ProtocolMessage)) : Unit {
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
// Loop over the 4 possible combinations of two bits
|
|
|
|
for (n in 0 .. 3) {
|
2019-06-24 07:11:12 +03:00
|
|
|
let data = ProtocolMessage(1 == n / 2, 1 == n % 2);
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
for (iter in 1 .. 100) {
|
|
|
|
let result = protocolOp(data);
|
|
|
|
|
|
|
|
// Now test if the bits were transfered correctly.
|
2019-06-24 07:11:12 +03:00
|
|
|
Fact(result::Bit1 == data::Bit1 and result::Bit2 == data::Bit2,
|
|
|
|
$"{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
|
|
|
|
|
|
|
}
|