Skip to content

Latest commit

 

History

History
195 lines (130 loc) · 9.38 KB

how-to-connect-workspace.md

File metadata and controls

195 lines (130 loc) · 9.38 KB
author description ms.author ms.date ms.service ms.subservice ms.topic no-loc title uid
SoniaLopezBravo
Learn how to connect and access to your Azure Quantum workspace using connection string and workspace parameters.
sonialopez
03/04/2024
azure-quantum
qdk
how-to
Q#
$$v
Connect to your Azure Quantum workspace
microsoft.quantum.how-to.connect-workspace

Connect to your Azure Quantum workspace with the azure-quantum Python package

Once you have created an Azure Quantum workspace, you can connect to it and submit your code using the azure-quantum Python package. The azure-quantum package provides a Workspace class that represents an Azure Quantum workspace.

Prerequisites

Connect using a connection string

You can use a connection string to specify the connection parameters to an Azure Quantum Workspace. You might use a connection string in the following scenarios:

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

Tip

Every Azure Quantum workspace has primary and secondary keys, and their corresponding connection strings. If you want to allow access to your workspace to others, you can share your secondary key and you use your primary for your own services. This way, you can replace the secondary key as needed without having downtime in your own services. For more information about sharing your workspace access, see Share your workspace access.

Copy the connection string

  1. Log in to the Azure portal and select your Azure Quantum workspace.

  2. On the left panel, navigate to Operations > Access keys.

  3. Access Keys have to be enabled. If Access Keys are disabled, you need to enable them first. See how to do it in Manage your Access Keys.

  4. Click on the Copy icon to copy the connection string. You can select either the primary or secondary connection string.

    :::image type="content" source="media/connection-string-copy.png" alt-text="Screenshot of Azure portal showing how to copy the connection strings.":::

Warning

Storing your account access keys or connection string in clear text presents a security risk and is not recommended. 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

Once you copied the connection string, you can use it to connect to your Azure Quantum workspace.

If you're working with a Python environment, you can create a Workspace object to connect to your Azure Quantum workspace. When creating a Workspace object, you have two options for identifying your Azure Quantum workspace.

  • You can create a Workspace object by calling from_connection_string.

    # Creating a new Workspace object from a connection string 
    from azure.quantum import Workspace 
    
    connection_string = "[Copy connection string]" 
    workspace = Workspace.from_connection_string(connection_string) 
    
    print(workspace.get_targets()) 
  • If you don't want to copy your connection string in the code, you can also store your connection string in an environment variable and use Workspace().

    # Using environment variable to connect with  connection string
    
    connection_string = "[Copy connection string]" 
    
    import os 
    
    os.environ["AZURE_QUANTUM_CONNECTION_STRING"] = connection_string 
    
    from azure.quantum import Workspace 
    
    workspace = Workspace() 
    print(workspace.get_targets()) 
  1. Open Visual Studio Code.

  2. Select View -> Command Palette and type Q#: Connect to an Azure Quantum workspace. Press Enter.

  3. Select Connection string.

  4. Paste the connection string you copied from the Azure portal and press Enter.

  5. Your Azure Quantum workspace appears in the Explorer pane, under Quantum Workspaces. You can expand the workspace to see the targets available in your workspace and the list of jobs.

    :::image type="content" source="media/quantum-workspace-explorer-vscode.png" alt-text="Screenshot of Visual Studio Code showing how to expand the Quantum Workspace pane.":::


For more information about how to enable/disable and regenerate your keys, see Manage your Access Keys.

Important

When Access Keys are disabled, all request using connection strings or access keys are unauthorized. You can still use the workspace parameters to connect to your workspace.

Connect using the workspace parameters

Every Azure Quantum workspace has a unique set of parameters that you can use to connect to it. You can use the following parameters to connect to your Azure Quantum workspace:

Parameter Description
subscription_id The Azure subscription ID.
resource_group The Azure resource group name.
name The name of your Azure Quantum workspace.
location The Azure region where the Azure Quantum workspace is provisioned. This may be specified as a region name such as "East US" or a location name such as "eastus".
resource_id The Azure resource ID of the Azure Quantum workspace.

You can find the workspace parameters in the overview of your Azure Quantum workspace in Azure portal.

  1. Log in to your Azure account, https://portal.azure.com,

  2. Select your Azure Quantum workspace, and navigate to Overview.

  3. Copy the parameters in the fields.

    :::image type="content" source="media/azure-portal-workspace-overview.png" alt-text="Screenshot of Visual Studio Code showing how to expand the overview pane of your Quantum Workspace.":::

Use the workspace parameters to connect to your Azure Quantum workspace

Create a Workspace object to connect to your Azure Quantum workspace. When creating a Workspace object, you have two options for identifying your Azure Quantum workspace.

  • You can specify the location and resource ID (recommended):

    from azure.quantum import Workspace 
    
    workspace = Workspace(  
        resource_id = "", # Add the resource ID of your workspace
        location = "" # Add the location of your workspace (for example "westus")
        )
  • You can specify the location, subscription ID, resource group, and workspace name:

    from azure.quantum import Workspace 
    
    workspace = Workspace(  
        subscription_id = "", # Add the subscription ID of your workspace
        resource_group = "", # Add the resource group of your workspace
        workspace_name = "", # Add the name of your workspace
        location = "" # Add the location of your workspace (for example "westus")
        )

You can use the Azure Command-Line Interface (Azure CLI) to connect t your workspace. For more information, see Manage quantum workspaces with the Azure CLI.

  1. Log in to your Azure account using the Azure CLI.

    az login
    
  2. Select the subscription you want to use. Replace SubscriptionName with your subscription name. You can also use the subscription ID instead of the subscription name.

    az account set --subscription SubscriptionName
    
  3. Set the workspace you want to use. Replace WorkspaceLocation, ResourceGroupName, and WorkspaceName with your workspace location, resource group name, and workspace name, respectively.

    az quantum workspace set --location WorkspaceLocation --resource-group ResourceGroupName --workspace-name WorkspaceName
    

Related content

  • Manage quantum workspaces with Azure CLI
  • Share access to your Azure Quantum workspace
  • Manage quantum workspaces with the Azure Resource Manager
  • Authenticate using a service principal
  • Authenticate using a managed identity