Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature "connection_string" #15

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Changelog
~~~~~~~~~

Unreleased
----------

* Add connection for Azure Quantum workspace using access key ``connection_string``.

0.2.0 (October 2024)
---------------------

Expand Down
45 changes: 35 additions & 10 deletions pytket/extensions/azure/backends/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,24 @@


def _get_workspace(
resource_id: Optional[str] = None, location: Optional[str] = None
resource_id: Optional[str] = None,
location: Optional[str] = None,
connection_string: Optional[str] = None,
) -> Workspace:
if os.getenv("AZURE_QUANTUM_CONNECTION_STRING") is not None:
return Workspace()
else:
config = AzureConfig.from_default_config_file()
if resource_id is None:
resource_id = config.resource_id
if location is None:
location = config.location
return Workspace(resource_id=resource_id, location=location)
if config.use_string:
if connection_string is None:
connection_string = config.connection_string
return Workspace.from_connection_string(connection_string)
else:
if resource_id is None:
resource_id = config.resource_id
if location is None:
location = config.location
return Workspace(resource_id=resource_id, location=location)


_GATE_SET = {
Expand Down Expand Up @@ -75,6 +82,8 @@ def __init__(
name: str,
resource_id: Optional[str] = None,
location: Optional[str] = None,
connection_string: Optional[str] = None,
use_string: bool = False,
):
"""Construct an Azure backend for a device.

Expand All @@ -93,9 +102,19 @@ def __init__(
:param location: Azure Quantum `location`. If omitted this is read from
config (see `set_azure_config()`), unless the environment variable
`AZURE_QUANTUM_CONNECTION_STRING` is set in which case this is used.
:param connection_string: Azure Quantum `connection_string`.
The connection_string can be set on Azure Quantum.
See https://learn.microsoft.com/en-us/azure/quantum/how-to-connect-workspace
If omitted this is read from config (see `set_azure_config()`), unless
the environment variable `AZURE_QUANTUM_CONNECTION_STRING` is set in which
case this is used.
:param use_string: Use the `connection_string`. Defaults to False.
"""
super().__init__()
self._workspace = _get_workspace(resource_id, location)
if use_string:
self._workspace = _get_workspace(connection_string=connection_string)
else:
self._workspace = _get_workspace(resource_id, location)
self._target = self._workspace.get_targets(name=name)
self._backendinfo = BackendInfo(
name=type(self).__name__,
Expand Down Expand Up @@ -258,13 +277,19 @@ def available_devices(cls, **kwargs: Any) -> List[BackendInfo]:

- resource_id (str)
- location (str)
- connection_string (str)
- use_string (bool) = False

If omitted these are read from config, unless the environment variable
`AZURE_QUANTUM_CONNECTION_STRING` is set in which case it is used.
"""
resource_id = kwargs.get("resource_id")
location = kwargs.get("location")
workspace = _get_workspace(resource_id, location)
if kwargs.get("use_string"):
connection_string = kwargs.get("connection_string")
workspace = _get_workspace(connection_string=connection_string)
else:
resource_id = kwargs.get("resource_id")
location = kwargs.get("location")
workspace = _get_workspace(resource_id, location)
return [
BackendInfo(
name=cls.__name__,
Expand Down
21 changes: 16 additions & 5 deletions pytket/extensions/azure/backends/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""IQM config."""
"""Azure config."""

from typing import Any, Dict, Optional, Type, ClassVar
from dataclasses import dataclass
Expand All @@ -27,6 +27,8 @@ class AzureConfig(PytketExtConfig):

resource_id: Optional[str]
location: Optional[str]
connection_string: Optional[str]
use_string: bool = False

@classmethod
def from_extension_dict(
Expand All @@ -35,17 +37,26 @@ def from_extension_dict(
return cls(
ext_dict.get("resource_id"),
ext_dict.get("location"),
ext_dict.get("connection_string"),
ext_dict.get("use_string"),
)


def set_azure_config(
resource_id: Optional[str] = None,
location: Optional[str] = None,
connection_string: Optional[str] = None,
use_string: bool = False,
) -> None:
"""Save Azure confuguration."""
config = AzureConfig.from_default_config_file()
if resource_id is not None:
config.resource_id = resource_id
if location is not None:
config.location = location
if use_string:
config.use_string = use_string
if connection_string is not None:
config.connection_string = connection_string
else:
if resource_id is not None:
config.resource_id = resource_id
if location is not None:
config.location = location
config.update_default_config_file()
Loading