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

Adding Call, ToOperation #3

Merged
merged 4 commits into from Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 52 additions & 1 deletion Canon/src/Utils/TypeConversion.qs
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.


namespace Microsoft.Quantum.Canon
{

Expand Down Expand Up @@ -178,6 +177,58 @@ namespace Microsoft.Quantum.Canon
return arr;
}

/// # Summary
/// Calls a function with a given input.
///
/// # Description
/// Given a function and an input to that function, calls the function
/// and returns its output.
///
/// # Input
/// ## fn
/// A function to be called.
/// ## input
/// The input to be passed to the function.
///
/// # Output
/// The result of calling `fn`.
///
/// # Remarks
/// This operation is mainly useful for forcing a function to be called
/// at a specific place within an operation, or for calling a function
/// where an operation is expected.
operation Call<'Input, 'Output>(fn : ('Input -> 'Output), input : 'Input) : 'Output {
return fn(input);
}

/// # Summary
/// Converts functions to operations.
///
/// # Description
/// Given a function, returns an operation which calls that function,
/// and which does nothing else.
///
/// # Input
/// ## fn
/// A function to be converted to an operation.
///
/// # Output
/// An operation `op` such that `op(input)` is identical to `fn(input)`
/// for all `input`.
///
/// # Type Parameters
/// ## 'Input
/// Input type of the function to be converted.
/// ## 'Output
/// Output type of the function to be converted.
///
/// # Remarks
/// This is mainly useful for passing functions to functions or operations
/// which expect an operation as input.
function ToOperation<'Input, 'Output>(fn : ('Input -> 'Output)) : ('Input => 'Output) {
return Call(fn, _);
}

}


2 changes: 1 addition & 1 deletion Canon/tests/QubitizationTests.qs
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Quantum.Tests {
// BlockEncoding.qs tests

// The returned operations encode the Hamiltonian (cos^2(angle) I+sin^2(angle) X)/2.
function LCUTestHelper() : (Double[], Double, Double, (Qubit[] => () : Adjoint, Controlled), ((Qubit[], Qubit[]) => Unit : Adjoint, Controlled)){
function LCUTestHelper() : (Double[], Double, Double, (Qubit[] => Unit : Adjoint, Controlled), ((Qubit[], Qubit[]) => Unit : Adjoint, Controlled)){
let angle = 1.789;
let eigenvalues = [0.5, 0.5 * Cos(angle * 2.0)];
let prob = PowD(Cos(angle),4.0)+PowD(Sin(angle),4.0);
Expand Down
26 changes: 26 additions & 0 deletions Canon/tests/TypeConversionTests.qs
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;

function Square(x : Int) : Int {
return x * x;
}

operation ApplyOp<'T, 'U>(op : ('T => 'U), input : 'T) : 'U {
return op(input);
}

operation CallTest() : Unit {
AssertIntEqual(Call(Square, 4), 16, "Call failed with Square.");
}

operation ToOperationTest() : Unit {
let op = ToOperation(Square);
AssertIntEqual(ApplyOp(op, 3), 9, "ToOperation failed with Square.");
}

}