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
|
|
|
// ------------------------------------------------------
|
2020-10-08 03:32:36 +03:00
|
|
|
@Test("QuantumSimulator")
|
2020-10-15 14:36:23 +03:00
|
|
|
operation T1_CreateEntangledPair () : Unit {
|
2021-01-30 22:36:00 +03:00
|
|
|
use (q1, q2) = (Qubit(), Qubit());
|
2019-06-24 07:11:12 +03:00
|
|
|
|
2021-01-30 22:36:00 +03:00
|
|
|
// apply operation that needs to be tested
|
|
|
|
CreateEntangledPair(q1, q2);
|
2019-06-24 07:11:12 +03:00
|
|
|
|
2021-01-30 22:36:00 +03:00
|
|
|
// apply adjoint reference operation and check that the result is |0^N⟩
|
|
|
|
Adjoint CreateEntangledPair_Reference(q1, q2);
|
2019-06-24 07:11:12 +03:00
|
|
|
|
2021-01-30 22:36:00 +03:00
|
|
|
// assert that all qubits end up in |0⟩ state
|
|
|
|
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
|
|
|
|
2021-01-30 22:36:00 +03:00
|
|
|
use qs = Qubit[2];
|
|
|
|
CreateEntangledPair_Reference(qs[0], qs[1]);
|
|
|
|
encodeOp(qs[0], message);
|
|
|
|
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
|
2021-01-30 22:36:00 +03:00
|
|
|
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
|
|
|
|
2021-01-30 22:36:00 +03:00
|
|
|
for iter in 1 .. 100 {
|
2018-10-30 18:48:56 +03:00
|
|
|
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
|
|
|
|
2020-10-08 03:32:36 +03:00
|
|
|
@Test("QuantumSimulator")
|
2020-10-15 14:36:23 +03:00
|
|
|
operation T2_EncodeMessageInQubit () : Unit {
|
2018-10-30 18:48:56 +03:00
|
|
|
TestProtocol(ComposeProtocol(EncodeMessageInQubit, DecodeMessageFromQubits_Reference, _));
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
2020-10-08 03:32:36 +03:00
|
|
|
@Test("QuantumSimulator")
|
2020-10-15 14:36:23 +03:00
|
|
|
operation T3_DecodeMessageFromQubits () : Unit {
|
2018-10-30 18:48:56 +03:00
|
|
|
TestProtocol(ComposeProtocol(EncodeMessageInQubit_Reference, DecodeMessageFromQubits, _));
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
2020-10-08 03:32:36 +03:00
|
|
|
@Test("QuantumSimulator")
|
2020-10-15 14:36:23 +03:00
|
|
|
operation T4_SuperdenseCodingProtocol () : Unit {
|
2018-10-30 18:48:56 +03:00
|
|
|
TestProtocol(SuperdenseCodingProtocol);
|
2018-09-20 19:58:36 +03:00
|
|
|
}
|
2018-10-30 18:48:56 +03:00
|
|
|
|
|
|
|
}
|