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

Added channel and instance for initialization #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ authors = []
dependencies = [
"certifi>=2021.5.30",
"importlib_metadata>=4.8.1",
"qiskit_serverless>=0.20.0, <0.21.0",
"qiskit_serverless @ git+https://github.com/Qiskit/qiskit-serverless.git@ibm-cloud-integration#subdirectory=client"
]

[project.optional-dependencies]
Expand Down
36 changes: 33 additions & 3 deletions qiskit_ibm_catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_serverless import IBMServerlessClient
from qiskit_serverless.core import Job, QiskitFunction
from qiskit_serverless.core.enums import Channel
from qiskit_serverless.core.function import RunnableQiskitFunction
from qiskit_serverless.exception import QiskitServerlessException


class QiskitFunctionsCatalog:
Expand All @@ -53,7 +55,13 @@ class QiskitFunctionsCatalog:

PRE_FILTER_KEYWORD: str = "catalog"

def __init__(self, token: Optional[str] = None, name: Optional[str] = None) -> None:
def __init__(
self,
token: Optional[str] = None,
channel: str = Channel.IBM_QUANTUM.value,
instance: Optional[str] = None,
name: Optional[str] = None,
) -> None:
"""
Initialize qiskit functions catalog.

Expand All @@ -64,10 +72,14 @@ def __init__(self, token: Optional[str] = None, name: Optional[str] = None) -> N
be retrieved from the user's local IBM Quantum account config file.

Args:
channel: identifies the method to use to authenticate the user
token: IBM quantum token
instance: IBM Cloud CRN
name: Name of the account to load
"""
self._client = IBMServerlessClient(token=token, name=name)
self._client = IBMServerlessClient(
channel=channel, token=token, instance=instance, name=name
)

def load(
self, title: str, provider: Optional[str] = None
Expand Down Expand Up @@ -201,6 +213,8 @@ def __repr__(self) -> str:
@staticmethod
def save_account(
token: Optional[str] = None,
channel: str = Channel.IBM_QUANTUM.value,
instance: Optional[str] = None,
name: Optional[str] = None,
overwrite: Optional[bool] = False,
) -> None:
Expand All @@ -209,7 +223,23 @@ def save_account(

Args:
token: IBM Quantum API token
channel: identifies the method to use to authenticate the user
instance: IBM Cloud CRN
name: Name of the account to save
overwrite: ``True`` if the existing account is to be overwritten
"""
QiskitRuntimeService.save_account(token=token, name=name, overwrite=overwrite)
try:
channel_enum = Channel(channel)
except ValueError as error:
raise QiskitServerlessException(
"Your channel value is not correct. Use one of the available channels: "
f"{Channel.LOCAL.value}, {Channel.IBM_QUANTUM.value}, {Channel.IBM_CLOUD.value}"
) from error

QiskitRuntimeService.save_account(
channel=channel_enum.value,
token=token,
instance=instance,
name=name,
overwrite=overwrite,
)
39 changes: 35 additions & 4 deletions qiskit_ibm_catalog/serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_serverless import IBMServerlessClient
from qiskit_serverless.core import Job, QiskitFunction
from qiskit_serverless.core.enums import Channel
from qiskit_serverless.core.function import RunnableQiskitFunction
from qiskit_serverless.exception import QiskitServerlessException


class QiskitServerless:
Expand All @@ -54,7 +56,13 @@ class QiskitServerless:

PRE_FILTER_KEYWORD: str = "serverless"

def __init__(self, token: Optional[str] = None, name: Optional[str] = None) -> None:
def __init__(
self,
token: Optional[str] = None,
channel: str = Channel.IBM_QUANTUM.value,
instance: Optional[str] = None,
name: Optional[str] = None,
) -> None:
"""
Initialize qiskit serverless client.

Expand All @@ -65,10 +73,14 @@ def __init__(self, token: Optional[str] = None, name: Optional[str] = None) -> N
be retrieved from the user's local IBM Quantum account config file.

Args:
token: IBM quantum token
channel: identifies the method to use to authenticate the user
token: IBM quantum token or IBM Cloud Api Key
instance: IBM Cloud CRN
name: Name of the account to load
"""
self._client = IBMServerlessClient(token=token, name=name)
self._client = IBMServerlessClient(
channel=channel, token=token, instance=instance, name=name
)

def upload(self, function: QiskitFunction) -> RunnableQiskitFunction:
"""Uploads qiskit function.
Expand Down Expand Up @@ -213,6 +225,8 @@ def __repr__(self) -> str:
@staticmethod
def save_account(
token: Optional[str] = None,
channel: str = Channel.IBM_QUANTUM.value,
instance: Optional[str] = None,
name: Optional[str] = None,
overwrite: Optional[bool] = False,
) -> None:
Expand All @@ -221,7 +235,24 @@ def save_account(

Args:
token: IBM Quantum API token
channel: identifies the method to use to authenticate the user
instance: IBM Cloud CRN
name: Name of the account to save
overwrite: ``True`` if the existing account is to be overwritten
"""
QiskitRuntimeService.save_account(token=token, name=name, overwrite=overwrite)

try:
channel_enum = Channel(channel)
except ValueError as error:
raise QiskitServerlessException(
"Your channel value is not correct. Use one of the available channels: "
f"{Channel.LOCAL.value}, {Channel.IBM_QUANTUM.value}, {Channel.IBM_CLOUD.value}"
) from error

QiskitRuntimeService.save_account(
channel=channel_enum.value,
token=token,
instance=instance,
name=name,
overwrite=overwrite,
)
4 changes: 2 additions & 2 deletions tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestCatalog(TestCase):
IBMServerlessClient, "jobs", return_value=[Job("42", mock.MagicMock())]
)
@mock.patch(
"qiskit_serverless.core.clients.serverless_client.ServerlessClient._verify_token"
"qiskit_serverless.core.clients.serverless_client.ServerlessClient._verify_credentials"
)
def test_basic_functions(self, _token_mock, jobs_mock, functions_list_mock):
"""Tests basic function of catalog."""
Expand All @@ -58,7 +58,7 @@ class TestServerless(TestCase):
IBMServerlessClient, "jobs", return_value=[Job("42", mock.MagicMock())]
)
@mock.patch(
"qiskit_serverless.core.clients.serverless_client.ServerlessClient._verify_token"
"qiskit_serverless.core.clients.serverless_client.ServerlessClient._verify_credentials"
)
def test_basic_functions(self, _token_mock, jobs_mock, functions_list_mock):
"""Tests basic function of serverless client."""
Expand Down