0

I would like to get a list of a mathematical sequence in Q#, but I have an error that I can't solve.
I use Azure Quantum to deploy the code in a Notebook.

Here is what I do:

First I connect to Azure Quantum:

import qsharp.azure

targets = qsharp.azure.connect(
   resourceId="/subscriptions/MY_ID/resourceGroups/AzureQuantum/providers/Microsoft.Quantum/Workspaces/maths",
   location="westeurope")

Then I log all the workspace's target:

import qsharp

print("This workspace's targets:")
for target in targets:
    print("-", target.id)

I declare GetSequence to use it and call it in Python.

GetSequence: any = None

Here is my Q# code. I would like to be able to return in an Array or in a String sequence its content, to be able to read it with Python.
This is where the execution gets stuck and generates an error.

%%qsharp

open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Convert;

operation GetSequence() : Int
{
    // mutable sequence = [];

    int[] sequence = new int[6];
    int count = 0;
    int start = 1;
    int end = 5;

    repeat {
        count++;

        sequence[count] = 2^(count + 1) - 1;
    } until count > end;

    return sequence;
}

I get the content of the previously generated sequence and display it in the console.

def main():
    qsharp.azure.target("ionq.simulator")

    result = qsharp.azure.execute(GetSequence)
    print(result)

main()

Here is my error:

/snippet_.qs(21,12): error QS5022: No identifier with the name "sequence" exists.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_12196/3192535580.py in <module>
----> 1 get_ipython().run_cell_magic('qsharp', '', '\nopen Microsoft.Quantum.Canon;\nopen Microsoft.Quantum.Intrinsic;\nopen Microsoft.Quantum.Convert;\n\noperation GetSequence() : Int\n{\n    // mutable sequence = [];\n\n    int[] sequence = new int[6];\n    int count = 0;\n    int start = 1;\n    int end = 5;\n\n    repeat {\n        count++;\n\n        sequence[count] = 2^(count + 1) - 1;\n    } until count > end;\n\n    return sequence;\n}\n')

/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2470             with self.builtin_trap:
   2471                 args = (magic_arg_s, cell)
-> 2472                 result = fn(*args, **kwargs)
   2473             return result
   2474 

/usr/local/lib/python3.7/site-packages/qsharp/ipython_magic.py in qsharp(magic_args, cell, local_ns)
     26             local_ns[callables._name] = callables
     27         else:
---> 28             for qs_callable in callables:
     29                 local_ns[qs_callable._name] = qs_callable
     30 

TypeError: 'NoneType' object is not iterablefail: 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]
      QS3102 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): Expecting expression.
fail: Microsoft.Quantum.IQSharp.Snippets[0]
      QS3033 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): The symbol is reserved for internal use only.
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]
      QS3033 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): The symbol is reserved for internal use only.
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]
      QS3033 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): The symbol is reserved for internal use only.
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]
      QS3033 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): The symbol is reserved for internal use only.
fail: Microsoft.Quantum.IQSharp.Snippets[0]
      QS3001 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): Syntax does not match any known patterns.
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]
      QS5022 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): No identifier with the name "count" exists.
fail: Microsoft.Quantum.IQSharp.Snippets[0]
      QS5022 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): No identifier with the name "end" exists.
fail: Microsoft.Quantum.IQSharp.Snippets[0]
      QS5022 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): No identifier with the name "sequence" exists.

Can you please help me?
Thanks for advance for your response!

cypolo
  • 17
  • 6

1 Answers1

1

Your Q# code doesn't compile - the fail lines in the output give you compilation errors.

For example, your code uses C-style syntax of defining and updating variables rather than Q# style. You can learn more about working with variables in Q# in Q# documentation. Also, your return type is declared as Int but you return sequence that is Int[].

Additionally, I would recommend you to try and run local simulation of your program before you submit it to Azure - this will make debugging a lot easier, since you'll have a lot fewer potential causes of failure.

Mariia Mykhailova
  • 1,987
  • 1
  • 9
  • 18
  • Thanks for your answer @Mariia Mykhailova! I have read that you sent and I have always the same errors. Here's that I do: https://sourceb.in/zV1LVmhaHx, and here's the errors: https://sourceb.in/GzmPCE4Jea – cypolo Oct 11 '22 at 07:17
  • Now you need to fix the syntax for updating the variables, both integer and array – Mariia Mykhailova Oct 11 '22 at 08:16
  • Thanks a lot! Now the Q# works. What can I do to convert the array of Int to read it with Python? This type is not supported. I don’t know if it’s a good idea to parse the result into a String or if there are a way to change the type of the array. @ Mariia Mykhailova – cypolo Oct 11 '22 at 11:01
  • Int arrays correspond to lists of ints in Python, so you don't need to do any specific conversion. If you're trying to submit this code as a job to IonQ simulator, you're likely running into another issue - IonQ targets only support Result type for job returns, since they need to build histograms of them (https://learn.microsoft.com/en-us/azure/quantum/provider-ionq#input-format). Purely classical Q# jobs are not supposed to be executed on IonQ targets – Mariia Mykhailova Oct 12 '22 at 06:24
  • Thanks again for your response @Marria Mykhailova! I just read about the different quantum providers available in Azure, and I was wondering if the problem would be solved if I switched from an IonQ machine to Quantinuum. How can I get the result of a simple Q# job, without having to manipulate the Qubits? – cypolo Oct 12 '22 at 10:54
  • I have not tried running this kind of code on Quantinuum so I'm not sure if the limitations are the same. Azure Quantum targets are either quantum hardware or partner simulators that follow the same patterns as the hardware. Why do you want to run a Q# job on Azure Quantum if you don't need qubits in it? Local simulation should be sufficient for purely classical Q#, and possibly faster – Mariia Mykhailova Oct 14 '22 at 17:30