From 43756e633e599a363082302a8406e39d61f2b935 Mon Sep 17 00:00:00 2001 From: yoshi-qc <95134786+yoshi-qc@users.noreply.github.com> Date: Thu, 22 Aug 2024 01:42:33 +0900 Subject: [PATCH 1/2] Add feature "connection_string" --- pytket/extensions/azure/backends/azure.py | 45 +++++++++++++++++----- pytket/extensions/azure/backends/config.py | 21 +++++++--- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/pytket/extensions/azure/backends/azure.py b/pytket/extensions/azure/backends/azure.py index c5519e6..1e31c7f 100644 --- a/pytket/extensions/azure/backends/azure.py +++ b/pytket/extensions/azure/backends/azure.py @@ -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 = { @@ -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. @@ -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__, @@ -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__, diff --git a/pytket/extensions/azure/backends/config.py b/pytket/extensions/azure/backends/config.py index 8c5c5c6..52236f4 100644 --- a/pytket/extensions/azure/backends/config.py +++ b/pytket/extensions/azure/backends/config.py @@ -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 @@ -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( @@ -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() From a8904195ef7ba4befeac7bca39da8f99c2c26740 Mon Sep 17 00:00:00 2001 From: yoshi-qc <95134786+yoshi-qc@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:05:23 +0900 Subject: [PATCH 2/2] Update changelog.rst --- docs/changelog.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index f66ce52..da37250 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,11 @@ Changelog ~~~~~~~~~ +Unreleased +---------- + +* Add connection for Azure Quantum workspace using access key ``connection_string``. + 0.1.0 -----