Search code examples
pythonazureq#azure-quantum

Python calling q# file on IONQ QPU results in error about a System.Text.Json, Version=5.0.0.0 file not being found


Attempting to learn how to call q# from Python code and have it run for real on the IONQ QPU as it does (or appears to do) using VS and >dotnet run of the raw q# code. I followed the guides and workshop.

Python code:

     import qsharp
     import qsharp.azure
     qsharp.projects.add("****path to *******/TestIONQ.csproj")
     from TestIONQ import GetRandomResult
     print(f"Simulated Result: {GetRandomResult.simulate()}")
     print("------------------------------------------------")
        
     qsharp.azure.connect(
       subscription = "****************************",
       resourceGroup = "**************",
       workspace = "************",
       location = "******* US")
     qsharp.azure.target("ionq.qpu")
     result = qsharp.azure.execute(GetRandomResult, jobName="Generate random bit")
     print(f" Final result from IONQ - QPU:   {result}")

q# code:

         namespace TestIONQ {
            
             open Microsoft.Quantum.Canon;
             open Microsoft.Quantum.Intrinsic;
                
             //@EntryPoint()
             operation GetRandomResult() : Result {
                 use q = Qubit();
                 H(q);
                 return M(q);
             }  
     }

and the .csproj file:

     <Project Sdk="Microsoft.Quantum.Sdk/0.16.2104138035">
        
       <PropertyGroup>
         <OutputType>Exe</OutputType>
         <TargetFramework>netcoreapp3.1</TargetFramework>
         <ExecutionTarget>ionq.qpu</ExecutionTarget>
         <IQSharpLoadAutomatically>true</IQSharpLoadAutomatically>
       </PropertyGroup>
        
     </Project>

The results of running the above Python code in Anaconda qsharp-env environment (Python 3.7.10) are as follows:

Simulated Result: 0
------------------------------------------------
Connected to Azure Quantum workspace ####### in location #####us.
Loading package Microsoft.Quantum.Providers.IonQ and dependencies...
Active target is now ionq.qpu
Submitting TestIONQ.GetRandomResult to target ionq.qpu...
Failed to submit Q# operation TestIONQ.GetRandomResult for execution.
Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=####token#####'. The system cannot find the file specified.

Obviously, no problem connecting to Azure and the Workspace. In fact I can run the container-ship optimization example no problem from Python. This also works fine for the first half of the Python code when .simulate() is invoked.

Next, when I try to bypass the IONQ QPU and use its own simulator by changing this one line:

qsharp.azure.target("ionq.simulator")

The resulting error is the same and the results are as follows:

Simulated Result: 1
------------------------------------------------
Connected to Azure Quantum workspace ######## in location #######.
Loading package Microsoft.Quantum.Providers.IonQ and dependencies...
Active target is now ionq.simulator
Submitting TestIONQ.GetRandomResult to target ionq.simulator...
Failed to submit Q# operation TestIONQ.GetRandomResult for execution.
Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken='....token......'. The system cannot find the file specified.
Traceback (most recent call last):
  File "ionq_sim_remote.py", line 18, in <module>
    result = qsharp.azure.execute(GetRandomResult, jobName="Generate random bit")
  File "F:\Python38\miniconda\envs\qsharp-env\lib\site-packages\qsharp\azure.py", line 137, in execute
    if "error_code" in result: raise AzureError(result)
qsharp.azure.AzureError: {'error_code': 1010, 'error_name': 'JobSubmissionFailed', 'error_description': 'Failed to submit the job to the Azure Quantum workspace.'}

This runs very easily on Azure using the q# code snippet within Visual Studio at the command line using a variant of what was shown during the workshop

az quantum execute --target-id ionq.qpu --job-name IONQ_test --resource-group ***rg name*** --workspace-name ***ws name*** --location **** -o table   

and indeed this appears to have run on the actual QPU hardware as compared to the simulator (which gives the exact 0.5/0.5 result).

Result    Frequency
--------  ----------
0         0.49800000
1         0.50200000

But then calling that same q# code from Python - including the same .csproj file seems to throw this JSON file error - even with the qsharp-env loaded into Anaconda. I apologize if it is something silly that I have done- trying to learn here.

By the way, this works great as a way around the problem with no Anaconda environment required or anything special:

Python:

import os
os.system(f'powershell.exe az quantum execute --target-id ionq.qpu --job-name Pytest --resource-group **** --workspace-name **** --location **** -o table ')

And the result was definitely run on the actual hardware (took a good while):

Result    Frequency
--------  -----------  -------------------------
0         0.53200000   ▐███████████            |
1         0.46800000   ▐█████████              |

Solution

  • @Joab.Ai, thank you for posting this issue! We've identified this to be specific to the latest version of qsharp (0.16.2104.138035). While we are looking into a fix, a workaround will be to downgrade your qsharp package version:

    Edit: we have fixed this issue in our latest release! Update to the latest version with this command:

    conda install -c quantum-engineering qsharp=0.16.2105.140472
    

    or simply run:

    conda update -c quantum-engineering qsharp