4
$\begingroup$

In Q#'s type documentation, it is mentioned that you can create signatures like this:

function ConjugateInvertibleWith : (inner: ((Qubit[] => Unit) : Adjoint),
                                    outer : ((Qubit[] => Unit) : Adjoint))
                                 : ((Qubit[] => Unit) : Adjoint)

My question is: how is this function actually implemented.

Presumably a function with this name will return an operation that, when invoked, calls outer, then inner, then adjoint outer. However, I have no idea how to actually write a function like this. In particular, it's not clear how to write the equivalent of a lambda with a closure. For example, if I try to declare an operation inside a function (similar to how you can def inside a def in python), I get a syntax error.

Does this have to be done in a non-Q# library, like in C#, then imported into Q#? If so, how?

$\endgroup$
5
$\begingroup$

For this example, one obtains a function with that signature by partial application of an operation that is defined outside the body, instead of as a lambda in the function. As a concrete example, consider this non-generic version of the WithA operation, modified from Q# canon.

operation WithA(
    outer : (Qubit[] => Unit : Adjoint), 
    inner : (Qubit[] => Unit : Adjoint), 
    target : Qubit[]) 
    : Unit
{
    body (...)
    {
        outer(target);
        inner(target);
        Adjoint outer(target);
    }
    adjoint invert;
}

This applies the sequence $|\textrm{target}\rangle\rightarrow\textrm{outer}^\dagger\cdot\textrm{inner}\cdot\textrm{outer}|\textrm{target}\rangle$.

We can then partially apply the target, by using the underscore character in place of an argument, to create the desired signature as follows.

function WithAFunction(
    outer : (Qubit[] => Unit : Adjoint), 
    inner : (Qubit[] => Unit : Adjoint)) 
    : ((Qubit[] => Unit) : Adjoint)
{
    return WithA(outer, inner, _);
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.