Edit

Submit jobs to Azure Quantum with the QDK Python package

Use the Microsoft Quantum Development Kit (QDK) Python package to submit Q#, OpenQASM, Qiskit, Cirq, and PennyLane programs to Azure Quantum.

This article provides job submission examples for each supported quantum language or framework. For an interactive Q# or OpenQASM workflow that doesn't require Python code, see Submit jobs with the QDK extension for VS Code.

Prerequisites

  • An Azure account with an active subscription.

  • An Azure Quantum workspace. To create one, see Create an Azure Quantum workspace.

  • Python 3.10 or later.

  • If you use a Jupyter notebook, a Jupyter environment such as the Jupyter extension for VS Code and the ipykernel package.

  • Install Azure CLI with the quantum extension.

    az extension add --upgrade --name quantum
    
  • Install the latest version of the qdk Python package with the azure and jupyter extras.

    pip install --upgrade "qdk[azure,jupyter]"
    

    If you want to submit a Qiskit or Cirq program, install the cirq and qiskit extras.

    pip install --upgrade "qdk[qiskit,cirq]"
    

Connect to your Azure Quantum workspace

Before you submit a job, you need to connect to an Azure Quantum workspace. The qdk.azure module provides the Workspace object to connect to Azure Quantum workspaces. To connect to your workspace, follow these steps.

Sign in with Azure CLI

If you didn't already, sign in to Azure from a terminal before you open a Jupyter notebook.

az login

If you don't sign in from the terminal, you need to authenticate every time you connect to a Quantum workspace through Python. If your account has access to multiple subscriptions, set the subscription that contains your Azure Quantum workspace.

az account set --subscription <subscription-id>

Run az login again when the session expires or when you want to use a different Azure account.

Get the resource ID of your workspace

  1. Sign in to the Azure portal.
  2. Go to the Quantum workspace where you want to submit your job.
  3. In the Overview page, find and copy Resource ID.

Connect to the workspace

  1. To connect to your Quantum workspace, create a Workspace object with the resource ID that you copied.

    from qdk.azure import Workspace
    
    workspace = Workspace(resource_id="") # Add your resource ID
    

    Note

    Y

  2. Verify the connection and view the targets available in the workspace.

    for target in workspace.get_targets():
        print(target.name)
    

For other connection and authentication options, see Connect to your Azure Quantum workspace.

Note

The following examples submit jobs to simulator targets. The targets available in your workspace depend on your configured providers.

Prepare and submit your program

Choose the tab for your quantum language or framework.

Use the qdk.qsharp module to define or load Q# code, compile an entry point to QIR, and submit the QIR to an Azure Quantum target.

Define and compile the Q# program

  1. Initialize Q# with a QIR target profile that the Azure Quantum target supports.

    from qdk import qsharp, TargetProfile
    
    qsharp.init(target_profile=TargetProfile.Base)
    

    For more information about QIR target profiles, see Azure Quantum QIR target profiles in the QDK.

  2. Define a Q# operation. For example, use the following RandomBit operation.

    qsharp.eval("""
        operation RandomBit() : Result {
            use q = Qubit();
            H(q);
            return MResetZ(q);
        }
    """)
    
  3. Test the operation on the local simulator.

    print(qsharp.run("RandomBit()", shots=10))
    
  4. Compile the operation to QIR.

    from qdk.qsharp import compile
    
    program = compile("RandomBit()")
    

Submit the Q# job

  1. Select a compatible target from your workspace.

    target = workspace.get_targets("rigetti.sim.qvm")
    
  2. Submit the compiled program.

    job = target.submit(program, "qsharp-job", shots=100)
    print("Job ID:", job.id)
    
  3. Wait for the job to complete and retrieve its results.

    job.wait_until_completed()
    print("Status:", job.details.status)
    
    results = job.get_results()
    print(results)