-
Notifications
You must be signed in to change notification settings - Fork 104
Generic Qiskit Backend #722
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
Merged
+329
−80
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f5597ae
Initial Generic Qiskit Backend
ScottCarda-MS 281e42b
Ensure metadata are strings
ScottCarda-MS e21e459
added _azure_config method to generic
ScottCarda-MS 19c08a2
Merge branch 'main' into sccarda/temp1
ScottCarda-MS 04cb3cc
undo formatting for job.py
ScottCarda-MS b3a471c
raise error if name is not an installed backend or valid target.
ScottCarda-MS 72cf1a4
set target_profile based on target in generic backend
ScottCarda-MS 359917e
Merge branch 'main' into sccarda/GenericQiskitBackend
ScottCarda-MS 422d7f9
Merge branch 'main' into sccarda/GenericQiskitBackend
ScottCarda-MS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| ## | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| ## | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any, Dict, Optional | ||
|
|
||
| from azure.quantum.version import __version__ | ||
|
|
||
| try: | ||
| from qiskit.providers import Options | ||
| from qsharp import TargetProfile | ||
| except ImportError as exc: | ||
| raise ImportError( | ||
| "Missing optional 'qiskit' dependencies. \ | ||
| To install run: pip install azure-quantum[qiskit]" | ||
| ) from exc | ||
|
|
||
| from .backend import AzureBackendConfig, AzureQirBackend | ||
|
|
||
| if TYPE_CHECKING: | ||
| from azure.quantum.qiskit import AzureQuantumProvider | ||
|
|
||
|
|
||
| _DEFAULT_SHOTS_COUNT = 500 | ||
|
|
||
|
|
||
| class AzureGenericQirBackend(AzureQirBackend): | ||
| """Fallback QIR backend for arbitrary Azure Quantum workspace targets. | ||
|
|
||
| This backend is created dynamically by :class:`~azure.quantum.qiskit.provider.AzureQuantumProvider` | ||
| for targets present in the workspace that do not have a dedicated Qiskit backend class. | ||
|
|
||
| It submits Qiskit circuits using QIR (`qir.v1`) payloads. | ||
| """ | ||
|
|
||
| _SHOTS_PARAM_NAME = "shots" | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| provider: "AzureQuantumProvider", | ||
| *, | ||
| provider_id: str, | ||
| target_profile: Optional[TargetProfile | str] = None, | ||
| num_qubits: Optional[int] = None, | ||
| description: Optional[str] = None, | ||
| **kwargs: Any, | ||
| ): | ||
| self._provider_id = provider_id | ||
|
|
||
| config = AzureBackendConfig.from_dict( | ||
| { | ||
| "backend_name": name, | ||
| "backend_version": __version__, | ||
| "simulator": False, | ||
| "local": False, | ||
| "coupling_map": None, | ||
| "description": description | ||
| or f"Azure Quantum target '{name}' (generic QIR backend)", | ||
| "basis_gates": self._basis_gates(), | ||
| "memory": False, | ||
| "n_qubits": num_qubits, | ||
| "conditional": False, | ||
| "max_shots": None, | ||
| "open_pulse": False, | ||
| "gates": [{"name": "TODO", "parameters": [], "qasm_def": "TODO"}], | ||
|
||
| "azure": self._azure_config(), | ||
| } | ||
| ) | ||
|
|
||
| super().__init__(config, provider, **kwargs) | ||
|
|
||
| # Prefer an instance-specific target profile discovered from the workspace target metadata. | ||
| default_target_profile = self._coerce_target_profile(target_profile) | ||
| if default_target_profile is not None: | ||
| self.set_options(target_profile=default_target_profile) | ||
|
|
||
| @staticmethod | ||
| def _coerce_target_profile( | ||
| value: Optional[TargetProfile | str], | ||
| ) -> Optional[TargetProfile]: | ||
| if value is None: | ||
| return None | ||
| if isinstance(value, TargetProfile): | ||
| return value | ||
| if not isinstance(value, str): | ||
| return None | ||
|
|
||
| raw = value.strip() | ||
| if not raw: | ||
| return None | ||
|
|
||
| # Prefer the qsharp helper when available. | ||
| from_str = getattr(TargetProfile, "from_str", None) | ||
| if callable(from_str): | ||
| try: | ||
| parsed = from_str(raw) | ||
| if isinstance(parsed, TargetProfile): | ||
| return parsed | ||
| except Exception: | ||
| pass | ||
|
|
||
| # Best-effort: try enum attribute lookup. | ||
| normalized = raw.replace("-", "_") | ||
| return getattr(TargetProfile, normalized, None) | ||
|
|
||
| @classmethod | ||
| def _default_options(cls) -> Options: | ||
| # Default to the most conservative QIR profile; users can override per-run via | ||
| # `target_profile=` in backend.run(...). | ||
ScottCarda-MS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Options( | ||
| **{cls._SHOTS_PARAM_NAME: _DEFAULT_SHOTS_COUNT}, | ||
| target_profile=TargetProfile.Base, | ||
| ) | ||
|
|
||
| def _azure_config(self) -> Dict[str, str]: | ||
| config = super()._azure_config() | ||
| config.update({"provider_id": self._provider_id, "is_default": False}) | ||
| return config | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.