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 |
|
Connect to your Azure Quantum workspace |
microsoft.quantum.how-to.connect-workspace |
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.
-
An Azure account with an active subscription. If you don’t have an Azure account, register for free and sign up for a pay-as-you-go subscription.
-
An Azure Quantum workspace. See Create an Azure Quantum workspace.
-
The latest version of the Azure Quantum
azure-quantum
package.!pip install --upgrade azure-quantum
-
If you use Azure CLI, you must have the latest version. For the installation instructions, see:
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.
-
Log in to the Azure portal and select your Azure Quantum workspace.
-
On the left panel, navigate to Operations > Access keys.
-
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.
-
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.
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 callingfrom_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())
-
Open Visual Studio Code.
-
Select View -> Command Palette and type Q#: Connect to an Azure Quantum workspace. Press Enter.
-
Select Connection string.
-
Paste the connection string you copied from the Azure portal and press Enter.
-
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.
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.
-
Log in to your Azure account, https://portal.azure.com,
-
Select your Azure Quantum workspace, and navigate to Overview.
-
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.":::
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.
-
Log in to your Azure account using the Azure CLI.
az login
-
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
-
Set the workspace you want to use. Replace
WorkspaceLocation
,ResourceGroupName
, andWorkspaceName
with your workspace location, resource group name, and workspace name, respectively.az quantum workspace set --location WorkspaceLocation --resource-group ResourceGroupName --workspace-name WorkspaceName
- 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