1
$\begingroup$

I would like to make a resource estimate based on Azure resource estimator.

However, the circuits I am interested in depend on some classical parameters: for this reason, I would like to make a modular script with sub-portions of the quantum circuits in separated parts.

I tried to do the following code (which does not work).

import qsharp.azure
targets = qsharp.azure.connect(
            resourceId = "/subscriptions/MYSUBSCRIPTION",
            location = "westeurope")
qsharp.azure.target("microsoft.estimator")
qsharp.azure.target("microsoft.estimator")

%%qsharp

open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Arrays;

operation subCircuit(q: Qubit[], x: Array[], n: Int) {
    for index in 0 .. n
    {
       H(q[index]); // Apply Hadamard on qubit "index"
       Rx(2.0*x[index],q[index]); // After the Hadamard, apply a Rx rotation on the qubit "index" of angle 2.0*x[index]
    }
}

operation AlgoTest() : Result[] {
    let n=3; // Number of qubits
    let x=[0.,1.,2.]; // Vector x
    mutable resultArray = [Zero, size = 3];
    use q = Qubit[n];
    subCircuit(q,x,n);

    for i in IndexRange(q) {
                set resultArray w/= i <- M(q[i]);
            }
    return resultArray;
}

result = qsharp.azure.execute(AlgoTest)

My questions:

As you see, I wanted to separate a sub-portion of my example algorithm subCircuit inside another function AlgoTest. However, the script returns several errors.

I know that I am probably not using good syntax, in particular, to define operation subCircuit, but this is precisely why I am asking this question. I looked at the documentation of operations and functions but the explanations are very obscure. Having an example of a working script doing what I want would be helpful for me to understand the syntax.

The errors returned are below (but overall I am not surprized there are errors because I am not using the good syntax, my question is precisely to understand which syntax is expected by the program, which is not very clear from the documentations -- perhaps I am not looking at the good place --):

fail: Microsoft.Quantum.IQSharp.Snippets[0] QS3212 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): Expecting return type annotation. The argument tuple needs to be followed by a colon and the return type of the callable. fail: Microsoft.Quantum.IQSharp.Snippets[0] QS3035 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): Unexpected code fragment. fail: Microsoft.Quantum.IQSharp.Snippets[0] QS3036 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): An expression used as a statement must be a call expression. fail: Microsoft.Quantum.IQSharp.Snippets[0] QS6005 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): No type with the name "Array" exists in any of the open namespaces. /snippet_.qs(7,53): error QS3212: Expecting return type annotation. The argument tuple needs to be followed by a colon and the return type of the callable. /snippet_.qs(28,8): error QS3035: Unexpected code fragment. /snippet_.qs(28,1): error QS3036: An expression used as a statement must be a call expression. /snippet_.qs(7,37): error QS6005: No type with the name "Array" exists in any of the open namespaces.

$\endgroup$
2
  • $\begingroup$ What are the errors? $\endgroup$
    – Sam Jaques
    Nov 27, 2023 at 17:43
  • $\begingroup$ @SamJaques I added the errors returned (but I am not sure it would be very helpful). $\endgroup$ Nov 27, 2023 at 18:26

1 Answer 1

2
$\begingroup$

(The errors you get are always helpful, so do include them whenever you can - for example, they can tell us the environment you're using to run the code, and that you're not putting Python and Q# code in the same code cell of a Jupyter Notebook.)

Expecting return type annotation. The argument tuple needs to be followed by a colon and the return type of the callable. 

When you define an operation or a function, it needs to have a return type. If it doesn't return anything, like in your case, the return type is Unit, so your operation signature should have : Unit after the closing bracket after the list of arguments.

No type with the name "Array" exists in any of the open namespaces.

There is indeed no Array type in Q#; in this case the second argument of your operation is Double[].

Combining these two, the signature of your operation should be

operation subCircuit(q: Qubit[], x: Double[], n: Int) : Unit {

At this point your code will compile. (It will run into a runtime error because within the operation the for loop goes from 0 to n, inclusive, so it will try to access array elements out of array bounds; the iteration range should be 0 .. n - 1.)

$\endgroup$
2

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.