Skip to content

Commit

Permalink
Merge branch 'main' into melf/add-ruff-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
cqc-melf authored Oct 24, 2024
2 parents 7337086 + 76e8f79 commit d101ee0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
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 @@ -39,17 +39,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 @@ -79,6 +86,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 @@ -97,9 +106,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 @@ -262,13 +281,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 dataclasses import dataclass
from typing import Any, ClassVar, Optional
Expand All @@ -28,6 +28,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 @@ -36,17 +38,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()

0 comments on commit d101ee0

Please sign in to comment.