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!
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.