Edit

Connect to your Azure Quantum workspace with the QDK or Azure CLI

If you have an Azure Quantum workspace, you can connect to your workspace and submit your code with the qdk.azure Python module. The qdk.azure module provides a Workspace class that represents an Azure Quantum workspace.

Prerequisites

To connect to your workspace with the qdk.azure module, you need the following:

Connect with a connection string

Use a connection string to specify the connection parameters to an Azure Quantum workspace. Connection strings are useful in the following scenarios:

  • You want to share access to your workspace with others who don't have an Azure account.
  • You want to share access to your workspace with others for a limited time.
  • You can't use Microsoft Entra ID because of company policies.

Tip

Every Azure Quantum workspace has a primary key and a secondary key, and each key has its own connection string. To allow others to access your workspace, share the secondary key and use the primary key only for your own services. You can replace the secondary key without causing downtime in your own services. For more information about sharing access to your workspace, see Share your workspace access.

Copy the connection string

  1. Sign in to the Azure portal.
  2. Go to your Azure Quantum workspace.
  3. In the workspace panel, expand the Operations dropdown and select Access Keys.
  4. Enable access keys for your workspace. If the Access Keys slider is set to Disabled, set the slider to Enabled.
  5. Select the Copy icon for the connection string that you want to copy. You can choose either the primary or secondary connection string.

Warning

It's a security risk to store your account access keys or connection strings in clear text. Store your account keys in an encrypted format, or migrate your applications to use Microsoft Entra authorization for access to your Azure Quantum workspace.

Use the connection string to access your Azure Quantum workspace

Use the connection string you copied to connect to your Azure Quantum workspace with the qdk.azure module or the QDK extension in Visual Studio Code.

To connect to your Azure Quantum workspace, choose one of the following methods.

  • Use the from_connection_string function to create a Workspace object.

    from qdk.azure import Workspace 
    
    connection_string = "" # Add your connection string
    workspace = Workspace.from_connection_string(connection_string) 
    
    print(workspace.get_targets())
    
  • Store your connection string in an environment variable.

    import os
    from qdk.azure import Workspace 
    
    connection_string = "" # Add your connection string  
    os.environ["AZURE_QUANTUM_CONNECTION_STRING"] = connection_string 
    
    workspace = Workspace() 
    print(workspace.get_targets()) 
    

For more information about how to work with keys, see Manage your Access Keys.

Important

If you disable access keys for your workspace, you can't use connection strings to connect to your workspace. Use workspace parameters to connect instead.

Connect to your workspace with workspace parameters

Every Azure Quantum workspace has a unique set of parameters that you can use to connect to the workspace.

Parameter Description Use when you connect with
subscription_id The Azure subscription ID. Azure CLI
resource_group The Azure resource group name. Azure CLI
name The name of your Azure Quantum workspace. Azure CLI
resource_id The Azure resource ID of the Azure Quantum workspace. Python

To find your workspace parameters, follow these steps:

  1. Sign in to the Azure portal.
  2. Go to your Azure Quantum workspace.
  3. From your workspace panel, choose Overview.
  4. Expand the Essentials dropdown.
  5. Copy the parameters in their corresponding fields.

Note

Ensure that you sign in to the correct tenant before you connect to your workspace. For more information about tenants, see How to manage Azure subscriptions with the Azure CLI.

Use workspace parameters to connect to your Azure Quantum workspace

To stay signed in when you run your Python scripts, open a terminal and run the following Azure CLI command. This command sets the subscription for your workspace. Replace <subscriptionId> with your subscription ID.

az account set --subscription <subscriptionId>

You can use your workspace parameters to connect to your Azure Quantum workspace through the qdk.azure module or through Azure CLI.

To connect to your Azure Quantum workspace, choose one of the following options.

  • Specify the resource ID (recommended):

    from qdk.azure import Workspace 
    
    workspace = Workspace(resource_id="") # Add the resource ID of your workspace
    
  • Specify the subscription ID, resource group, and workspace name:

    from qdk.azure import Workspace 
    
    workspace = Workspace(  
        subscription_id="", # Add the subscription ID of your workspace
        resource_group="", # Add the resource group of your workspace
        name="" # Add the name of your workspace
        )
    
  • Specify only the workspace name. This option might fail when you have multiple workspaces that have the same name in the same tenant.

    from qdk.azure import Workspace 
    workspace = Workspace(name="") # Add the name of your workspace