0

The code below to solve a simple problem on azure quantum never works. Microsoft, please help me with authentication.

from azure.quantum.optimization import Problem, ProblemType, Term
from typing import List
from azure.quantum.optimization import Term
from azure.quantum import Workspace
from azure.identity import EnvironmentCredential
from azure.quantum.optimization import ParallelTempering

problem = Problem(name="My First Problem", problem_type=ProblemType.ising)

workspace = Workspace (
    subscription_id = "my-subscription-id",  # Add your subscription_id
    resource_group = "AzureQuantum",   # Add your resource_group
    name = "my-workspace-name",             # Add your workspace name
    location = "my-workspace-location"  ,        # Add your workspace location (for example, "westus")
    credential = EnvironmentCredential(AZURE_USERNAME="my-email-id", AZURE_PASSWORD="my-microsoft-password")
    # credential = ManagedIdentityCredential()
    )

terms = [
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0]),
    Term(c=9, indices=[2,1]),
    Term(c=2, indices=[3,0]),
    Term(c=-4, indices=[3,1]),
    Term(c=4, indices=[3,2])
]

problem.add_terms(terms=terms)



solver = ParallelTempering(workspace, timeout=100)

result = solver.optimize(problem)
print(result)

The above code throws the error:

EnvironmentCredential.get_token failed: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot.this issue.
---------------------------------------------------------------------------
CredentialUnavailableError                Traceback (most recent call last)
[<ipython-input-19-90cd448f8194>](https://localhost:8080/#) in <module>()
      3 solver = ParallelTempering(workspace, timeout=100)
      4 
----> 5 result = solver.optimize(problem)
      6 print(result)

19 frames
[/usr/local/lib/python3.7/dist-packages/azure/identity/_credentials/environment.py](https://localhost:8080/#) in get_token(self, *scopes, **kwargs)
    141                 "this issue."
    142             )
--> 143             raise CredentialUnavailableError(message=message)
    144         return self._credential.get_token(*scopes, **kwargs)

CredentialUnavailableError: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.

Details

The above code works perfectly fine when I do not pass any credentials to the workspace, but this pops up a window for authentication. I do not want to click manually on the browser every time I run something to autheticate. I just want to pass the credentials in code with ease without having to deal with all the complicated things defined for the authentication in the docs.

Note: I'm passing my email and password in the EnvironmentCredential (I'm obviously not writing confidential info here like subscription id passed in workspace)

1 Answers1

1

The EnvironmentCredential takes its parameters from environment variables -not from arguments in the constructor-, so in order to avoid getting prompted for credentials you would need to set the corresponding environment variables with the correct values, then run your program. Something like:

set AZURE_USERNAME="my-email-id"
set AZURE_PASSWORD="my-microsoft-password"
python myprogram.py

in your code you would then do something like:

workspace = Workspace (
    subscription_id = "my-subscription-id",  # Add your subscription_id
    resource_group = "AzureQuantum",   # Add your resource_group
    name = "my-workspace-name",             # Add your workspace name
    location = "my-workspace-location"  ,        # Add your workspace location (for example, "westus")
    credential = EnvironmentCredential()
    )

That being said, an easier way to not get prompted for credentials is to install the Azure CLI and login using az login; that will prompt once for credentials and then persist them locally in your machine so you don't have to login again.

Optionally, if you are using VS Code you can install the Azure Account extension.

For all these options, you don't need to provide any credential in the AzureQuantum constructor. It will automatically try to discover if you've set the environment variables, used the CLI or the extension to log in and used that. It only defaults to prompt on the browser if it can't find anything else.

El capi
  • 451
  • 2
  • 3
  • Thanks for your answer. Very helpful. Although I'm running my program on AWS ec2 instance, I don't see any reason Azure CLI wouldn't work there; however, I'm skeptical that azure CLI wouldn't work there. I will check and let people know in the comments. – Sagar Dollin Aug 11 '22 at 12:25
  • And yes I'm using VS code. So I'm betting my hopes on the VS Code extension. – Sagar Dollin Aug 11 '22 at 12:29