1
$\begingroup$

I am working with multiple operations and used partial application in different cases in Q#. I have 2 questions :

  • Can a partial applicated operation be passed as an argument ?
  • Lets say you have an operation with 3 parameters. Can you first fill one argument, store it, fill another argument, store it, and then finally call the operation by filling the last argument ?

Of course, I you need clarifications, I will comment and edit appropriately. Thanks for reading !

$\endgroup$

1 Answer 1

2
$\begingroup$

Can a partial applicated operation be passed as an argument ?

Yes.

For example, let's say you want to pass an argument of type (Qubit => Unit) (an operation applied to a single qubit, say, a gate), and you want to get it by using Ry gate with a fixed rotation angle parameter. The signature of Ry operation is operation Ry (theta : Double, qubit : Qubit) : Unit, so its type is ((Double, Qubit) => Unit). If you use partial application to define a new operation

let RyHalfPi = Ry(PI() / 2.0, _);

the signature of the new operation will be (Qubit => Unit) (since you already defined the first parameter), and it will behave as an operation that takes a single qubit parameter for all purposes, including passing it as an argument.

Can you first fill one argument, store it, fill another argument, store it, and then finally call the operation by filling the last argument ?

Yes, in exactly the same way as in the first case!

If, for example, you want to use R1Frac gate with signature (Int, Int, Qubit) : Unit, you can first fill in the numerator:

let R1Frac10 = R1Frac(10, _, _);

to get operation with signature (Int, Qubit) : Unit, and then fill in the power to get a single-qubit gate:

for (power in 0 .. 1 << 10) {
    let R1Frac10Power = R1Frac10 (power, _);
    // ...
}

You can also fill in parameters out of order (not necessarily left to right like I did in the example), following the same principle: you'll get an operation which takes all leftover (unsupplied) arguments in the order in which they are left.

  • You can check the testing harnesses of the Quantum Katas for more examples - they use partial application quite extensively to use unified testing harness for tasks with slightly different operation signatures.
$\endgroup$
4
  • 1
    $\begingroup$ Thanks for the answer, I find this function very powerful, I just think a couple of sentences should be added to the docs so that people don't have to search for this question. $\endgroup$ Aug 6, 2020 at 13:04
  • $\begingroup$ @Jonathcraft If I may ask to help us make diagnostics more actionable: are you working within an IDE (VS or VS Code, with the QDK extension installed), or are you working in a Jupyter notebook? $\endgroup$ Aug 6, 2020 at 21:14
  • $\begingroup$ I am working on VS code with the QKD extension and a python host file $\endgroup$ Aug 7, 2020 at 7:49
  • 1
    $\begingroup$ @Jonathcraft Cool. You should soon get more IntelliSense information (if your extension updates automatically) that will make it easier to spot these things right away. $\endgroup$ Aug 19, 2020 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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