def create_workspace():
    workspace = Workspace(subscription_id=config._SUBSCRIPTION_ID,
                          resource_group=config._RESOURCE_GROUP,
                          name=config._NAME,
                          location=config._LOCATION)
    workspace.login()
    return workspace
    def create_workspace(self) -> Workspace:
        """Create workspace using credentials stored in config file

        :return: Workspace
        :rtype: Workspace
        """

        workspace = Workspace(
            subscription_id=self.subscription_id,
            resource_group=self.resource_group,
            name=self.workspace_name,
        )

        if self.is_live or self.in_recording:
            workspace.credentials = ServicePrincipalCredentials(
                tenant=self.tenant_id,
                client_id=self.client_id,
                secret=self.client_secret,
                resource="https://quantum.microsoft.com")
            workspace.login(False)
        else:
            workspace.credentials = BasicTokenAuthentication(
                token=self.dummy_auth_token)

        return workspace
示例#3
0
def create_workspace_mock_login(**kwds) -> Workspace:
    """Create a mock Workspace object by patching the Azure authentication and uses a dummy authentication token."""
    workspace = Workspace(**kwds)

    dummy_auth_token = {
        'access_token':
        'eyJ1eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJidWQiOiJodHRwczovL3F1YW50dW0ubWljcm9zb2Z0LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI0Ny8iLCJpYXQiOjE2MTE2ODMwNjAsIm5iZiI6MTYxMTY4MzA2MCwiZXhwIjoxNjExNjg2OTYwLCJfY2xhaW1fbmFtZXMiOnsiZ3JvdXBzIjoic3JjMSJ9LCJfY2xhaW1fc291cmNlcyI6eyJzcmMxIjp7ImVuZHBvaW50IjoiaHR0cHM6Ly9ncmFwaC53aW5kb3dzLm5ldC83MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDcvdXNlcnMvZTlhNGE5ZTEtODcxNS00Yjc1LTk2NWQtYzB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VmOS00Njg5LTgxNDMtNzZiMTk0ZTg1NTA5Il19.PCWEtCdso3_jehm3Ppg9lCSy_VgwY96IG0_Lqji5tN3yEmBmsP4Du-6MA2IHlz7pbKfQ8Qdw4aeobWZkuDW71Zo9PCkBSLQewng5EMbDvZO3jPJfCOd0IepaPVdtvtaCL2KnPEZicEM4kIO_9f8hCC4Ik8MAem788HuutNhN_YExJDWtM-aNoXIBLtDm39u3bCr2WFk4he3xpISLD3ZqAk2UPKagMwuwO-tArtcoQvA1_n_owv-I5P8vEk1wOmUh6LTB6pUAIS4wFIMgINUE1dBSuQmyimEfc7rRuWl-YJrMH0WRdbgFutwbBv_5dKs6VcYGgrvA3nIGU_Xz5vuJMA',
        'token_type': 'Bearer',
        'expires_in': 485
    }
    with unittest.mock.patch.object(MsalWrapper,
                                    'acquire_auth_token',
                                    return_value=dummy_auth_token):
        workspace.login(True)

    return workspace
示例#4
0
def create_workspace() -> Workspace:
    """Create workspace using credentials stored in config file

    :return: Workspace
    :rtype: Workspace
    """
    config = get_config()
    workspace = Workspace(
        subscription_id=config["azure.quantum"]["subscription_id"],
        resource_group=config["azure.quantum"]["resource_group"],
        name=config["azure.quantum"]["workspace_name"],
        storage="")

    # try to login - this should trigger the device flow
    workspace.login(False)
    return workspace
示例#5
0
def create_workspace() -> Workspace:
    """Create workspace using credentials stored in config file

    :return: Workspace
    :rtype: Workspace
    """

    client_id = os.environ.get("AZURE_CLIENT_ID", "")
    client_secret = os.environ.get("AZURE_CLIENT_SECRET", "")
    tenant_id = os.environ.get("AZURE_TENANT_ID", "")
    resource_group = os.environ.get("RESOURCE_GROUP", "")
    subscription_id = os.environ.get("SUBSCRIPTION_ID", "")
    workspace_name = os.environ.get("WORKSPACE_NAME", "")

    assert len(
        client_id) > 0, "AZURE_CLIENT_ID not found in environment variables."
    assert len(client_secret
               ) > 0, "AZURE_CLIENT_SECRET not found in environment variables."
    assert len(
        tenant_id) > 0, "AZURE_TENANT_ID not found in environment variables."
    assert len(resource_group
               ) > 0, "RESOURCE_GROUP not found in environment variables."
    assert len(subscription_id
               ) > 0, "SUBSCRIPTION_ID not found in environment variables."
    assert len(workspace_name
               ) > 0, "WORKSPACE_NAME not found in environment variables."

    if len(client_secret) > 0:
        workspace = Workspace(
            subscription_id=subscription_id,
            resource_group=resource_group,
            name=workspace_name,
        )
        workspace.credentials = ServicePrincipalCredentials(
            tenant=tenant_id,
            client_id=client_id,
            secret=client_secret,
            resource="https://quantum.microsoft.com")

    workspace.login()
    return workspace
示例#6
0
def SPLogin():
    quantumWorkspace = {}
    try:
        quantumWorkspace = Workspace(
            subscription_id=os.environ.get(
                'subscriptionId'),  # Add your subscription_id
            resource_group=os.environ.get(
                'resourceGroup'),  # Add your resource_group
            name=os.environ.get(
                'quantumWorkspaceName'),  # Add your workspace name
            location=os.environ.get(
                'quantumLocation'
            )  # Add the workspace location, for example, westus
        )
        if (os.environ.get('loginMethod') == 'servicePrincipal'):
            quantumWorkspace.credentials = ServicePrincipalCredentials(
                tenant=os.environ.get(
                    'directoryId'
                ),  # From service principal creation, your Directory (tenant) ID
                client_id=os.environ.get(
                    'appId'
                ),  # From service principal creation, your Application (client) ID
                secret=os.environ.get(
                    'appsecret'
                ),  # From service principal creation, your secret
                resource=
                "https://quantum.microsoft.com"  # Do not change! This is the resource you want to authenticate against - the Azure Quantum service
            )
        else:
            quantumWorkspace.login(
            )  #If you do not want to use service prinipal login you can use the interactive browser login
    except:
        KeyError('Failed to login with service principal')

    quantumWorkspace.login()

    return quantumWorkspace
示例#7
0
def create_workspace() -> Workspace:
    """Create workspace using credentials stored in config file

    :return: Workspace
    :rtype: Workspace
    """
    config = get_config()
    if config:
        workspace = Workspace(
            subscription_id=config["azure.quantum"]["subscription_id"],
            resource_group=config["azure.quantum"]["resource_group"],
            name=config["azure.quantum"]["workspace_name"],
        )

        # try to login - this should trigger the device flow
        workspace.login(False)
    else:
        workspace = create_workspace_mock_login(
            subscription_id=RecordingUpdater.dummy_uid,
            resource_group=RecordingUpdater.dummy_rg,
            name=RecordingUpdater.dummy_ws,
        )

    return workspace
示例#8
0
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# Instantiate Workspace object which allows you to connect to the Workspace you've previously deployed in Azure.
# Be sure to fill in the settings below which can be retrieved by running 'az quantum workspace show' in the terminal.
from azure.quantum import Workspace

# Copy the settings for your workspace below
workspace = Workspace(
    subscription_id="",  # Add your subscription_id
    resource_group="",  # Add your resource_group
    name="",  # Add your workspace name
    location=""  # Add your workspace location (for example, "westus")
)

workspace.login()

# Take an array of container weights and return a Problem object that represents the cost function
from typing import List
from azure.quantum.optimization import Problem, ProblemType, Term


def create_problem_for_container_weights(
        container_weights: List[int]) -> Problem:
    terms: List[Term] = []

    # Expand the squared summation
    for i in range(len(container_weights)):
        for j in range(len(container_weights)):
            if i == j:
                # Skip the terms where i == j as they form constant terms in an Ising problem and can be disregarded: