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

Providerv1 deprication patch #182

Merged
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
33 changes: 31 additions & 2 deletions qiskit_braket_provider/providers/braket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from braket.device_schema.dwave import DwaveDeviceCapabilities
from braket.device_schema.quera import QueraDeviceCapabilities
from braket.device_schema.xanadu import XanaduDeviceCapabilities
from qiskit.providers import ProviderV1
from qiskit.providers.exceptions import QiskitBackendNotFoundError

from .braket_backend import BraketAwsBackend, BraketLocalBackend


class BraketProvider(ProviderV1):
class BraketProvider:
"""BraketProvider class for accessing Amazon Braket backends.

Example:
Expand All @@ -29,7 +29,36 @@ class BraketProvider(ProviderV1):
BraketBackend[dm1]]
"""

def get_backend(self, name=None, **kwargs):
"""Return a single backend matching the specififed filters.

Args:
name (str): name of the selected backend
**kwargs: dict with additional options for filitering and storing aws session
Returns:
BraketAwsBackend: a backend matching the filters.
Raises:
QiskitBackendNotFoundError: if no backend could be found or
more than one backend matches the filters.
"""
backends = self.backends(name=name, **kwargs)
if len(backends) > 1:
raise QiskitBackendNotFoundError(
"More than one backend matches the criteria"
)
if not backends:
raise QiskitBackendNotFoundError("No backend matches the criteria")
return backends[0]

def backends(self, name=None, **kwargs):
"""Return a list of backends matching the specififed filters.

Args:
name (str): name of the selected backend
**kwargs: dict with additional options for filitering and storing aws session
Returns:
BraketAwsBackend: a list of backends matching the filters.
"""
if kwargs.get("local"):
return [
BraketLocalBackend(name="braket_sv"),
Expand Down
25 changes: 25 additions & 0 deletions tests/providers/test_braket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from qiskit import QuantumCircuit
from qiskit import circuit as qiskit_circuit
from qiskit.compiler import transpile
from qiskit.providers.exceptions import QiskitBackendNotFoundError

from qiskit_braket_provider.providers import BraketProvider
from qiskit_braket_provider.providers.braket_backend import (
Expand Down Expand Up @@ -38,6 +39,30 @@ def setUp(self):
self.mock_session.boto_session.region_name = SIMULATOR_REGION
self.mock_session.search_devices.return_value = simulators

self.empty_mock_session = Mock()
self.empty_mock_session.get_device.side_effect = []
self.empty_mock_session.region = SIMULATOR_REGION
self.empty_mock_session.boto_session.region_name = SIMULATOR_REGION
self.empty_mock_session.search_devices.return_value = []

def test_provider_backend(self):
"""Test QiskitBackendNotFoundError is raised"""
provider = BraketProvider()

# Matches QiskitBackendNotFoundError where multiple backends are found
with self.assertRaises(QiskitBackendNotFoundError) as err1:
provider.get_backend(
aws_session=self.mock_session, types=[AwsDeviceType.SIMULATOR]
)
self.assertIsInstance(err1.exception, QiskitBackendNotFoundError)

# Matches QiskitBackendNotFoundError where no backends are found
with self.assertRaises(QiskitBackendNotFoundError) as err2:
provider.get_backend(
aws_session=self.empty_mock_session, types=[AwsDeviceType.SIMULATOR]
)
self.assertIsInstance(err2.exception, QiskitBackendNotFoundError)

def test_provider_backends(self):
"""Tests provider."""
provider = BraketProvider()
Expand Down
Loading