Skip to content

Commit

Permalink
Allow IBMServerlessProvider users to save/load their credentials to/f…
Browse files Browse the repository at this point in the history
…rom disk (#1048)

* Allow for account to be saved/loaded. Not working but first pass at code complete.

* Fix bug causing credential file to be overwritten. Code works now.

* Allow IBMServerlessProvider to be instantiated by name

* Add default name arg to Provider

* Move the filepath and default account name to class variables in IBMServerlessProvider

* pylint

* Missing if statement. Bug.

* Use os.environ.get so that None is returned if varname does not exist. Minor bug.

* Use the class variables, rather than instance vars in constructor.

* Use dict.get so script doesnt crash if config does not contain token keyword

* Minor code style cleanup

* peer review

* cleanup

* Update IBMServerlessprovider init docstring

* classmethod-->staticmethod, no longer need class vars
  • Loading branch information
caleb-johnson committed Oct 20, 2023
1 parent 013346e commit 4d97552
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions client/quantum_serverless/core/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import ray
import requests
from ray.dashboard.modules.job.sdk import JobSubmissionClient

from opentelemetry import trace
from qiskit_ibm_provider import IBMProvider

from quantum_serverless.core.constants import (
REQUESTS_TIMEOUT,
Expand Down Expand Up @@ -455,19 +455,67 @@ class IBMServerlessProvider(ServerlessProvider):
"""
A provider for connecting to the IBM serverless host.
Example:
>>> provider = IBMServerlessProvider(token="<TOKEN>")
Credentials can be saved to disk by calling the `save_account()` method::
from quantum_serverless import IBMServerlessProvider
IBMServerlessProvider.save_account(token=<INSERT_IBM_QUANTUM_TOKEN>)
Once the credentials are saved, you can simply instantiate the provider with no
constructor args, as shown below.
from quantum_serverless import IBMServerlessProvider
provider = IBMServerlessProvider()
Instead of saving credentials to disk, you can also set the environment variable
ENV_GATEWAY_PROVIDER_TOKEN and then instantiate the provider as below::
from quantum_serverless import IBMServerlessProvider
provider = IBMServerlessProvider()
You can also enable an account just for the current session by instantiating the
provider with the API token::
from quantum_serverless import IBMServerlessProvider
provider = IBMServerlessProvider(token=<INSERT_IBM_QUANTUM_TOKEN>)
"""

def __init__(self, token: str):
def __init__(self, token: Optional[str] = None, name: Optional[str] = None):
"""
Initialize a provider with access to an IBMQ-provided remote cluster.
If a ``token`` is used to initialize an instance, the ``name`` argument
will be ignored.
If only a ``name`` is provided, the token for the named account will
be retrieved from the user's local IBM Quantum account config file.
If neither argument is provided, the token will be searched for in the
environment variables and also in the local IBM Quantum account config
file using the default account name.
Args:
token: IBM quantum token
name: Name of the account to load
"""
token = token or IBMProvider(name=name).active_account().get("token")
super().__init__(token=token, host=IBM_SERVERLESS_HOST_URL)

@staticmethod
def save_account(
token: Optional[str] = None,
name: Optional[str] = None,
overwrite: Optional[bool] = False,
) -> None:
"""
Save the account to disk for future use.
Args:
token: IBM Quantum API token
name: Name of the account to save
overwrite: ``True`` if the existing account is to be overwritten
"""
IBMProvider.save_account(token=token, name=name, overwrite=overwrite)

def get_compute_resources(self) -> List[ComputeResource]:
raise NotImplementedError("GatewayProvider does not support resources api yet.")

Expand Down

0 comments on commit 4d97552

Please sign in to comment.