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

Fixing Deprecation Warning #217

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"matplotlib>=3.4.3",
"scipy>=1.8",
"qiskit>=0.36.1",
"qiskit-ibm-provider",
"pyquil>=3.1.0",
"docplex>=2.23.1",
"autograd>=1.4",
Expand Down
24 changes: 16 additions & 8 deletions src/openqaoa-qiskit/backends/devices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from qiskit import IBMQ
from qiskit_ibm_provider import IBMProvider
from qiskit_aer import AerSimulator
from typing import List
from openqaoa.backends.devices_core import DeviceBase
Expand Down Expand Up @@ -81,7 +81,7 @@ def check_connection(self) -> bool:
if self.provider_connected == False:
return self.provider_connected

self.available_qpus = [backend.name() for backend in self.provider.backends()]
self.available_qpus = [backend.name for backend in self.provider.backends()]

if self.device_name == "":
return self.provider_connected
Expand Down Expand Up @@ -112,18 +112,26 @@ def _check_provider_connection(self) -> bool:
"""

try:
self.provider = IBMQ.load_account()
if any([self.hub, self.group, self.project]):
self.provider = IBMQ.get_provider(
hub=self.hub, group=self.group, project=self.project
)
#Use default
self.provider = IBMProvider()
#Unless exact instance is specified
if all([self.hub, self.group, self.project]):
instance_name = self.hub + '/' + self.group + '/' + self.project
assert instance_name in self.provider.instances()
self.provider = IBMProvider(instance=instance_name)
elif any([self.hub, self.group, self.project]):
#if only partially specified, print warning.
raise Exception("You've only partially specified the instance name. Either"
"the hub, group or project is missing. hub: {}, group: {}, project: {}.\n"
"The default instance will be used instead. (This default can "
"be specified when doing `IBMProvider.save_account`)")
return True
except Exception as e:
print(
"An Exception has occured when trying to connect with the provider."
"Please note that you are required to set up your IBMQ account locally first."
"See: https://quantum-computing.ibm.com/lab/docs/iql/manage/account/ibmq"
"for how to save your IBMQ account locally: {}".format(e)
"for how to save your IBMQ account locally. \n {}".format(e)
)
return False

Expand Down
14 changes: 7 additions & 7 deletions src/openqaoa-qiskit/backends/qaoa_qiskit_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

# IBM Qiskit imports
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile
from qiskit.providers.ibmq.job import (
IBMQJobApiError,
IBMQJobInvalidStateError,
IBMQJobFailureError,
IBMQJobTimeoutError,
from qiskit_ibm_provider.job.exceptions import (
IBMJobApiError,
IBMJobInvalidStateError,
IBMJobFailureError,
IBMJobTimeoutError,
)
from qiskit.circuit import Parameter

Expand Down Expand Up @@ -239,12 +239,12 @@ def get_counts(self, params: QAOAVariationalBaseParams, n_shots=None) -> dict:
counts = job.result().get_counts()
api_contact = True
job_state = True
except (IBMQJobApiError, IBMQJobTimeoutError):
except (IBMJobApiError, IBMJobTimeoutError):
print("There was an error when trying to contact the IBMQ API.")
job_state = True
no_of_api_retries += 1
time.sleep(5)
except (IBMQJobFailureError, IBMQJobInvalidStateError):
except (IBMJobFailureError, IBMJobInvalidStateError):
print("There was an error with the state of the Job in IBMQ.")
no_of_job_retries += 1
break
Expand Down
24 changes: 16 additions & 8 deletions tests/test_qpu_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self):
self.HUB = "ibm-q"
self.GROUP = "open"
self.PROJECT = "main"
self.INSTANCE = "ibm-q/open/main"

@pytest.mark.api
def test_changing_provider(self):
Expand All @@ -42,21 +43,28 @@ def test_changing_provider(self):

device_obj = DeviceQiskit(device_name="ibmq_manila")
device_obj.check_connection()

provider_instances = device_obj.provider.instances()

if len(provider_instances) >= 2:

for each_item in provider_instances[:2]:

[hub, group, project] = each_item.split('/')
device_obj2 = DeviceQiskit(device_name="ibmq_manila", hub=hub, group=group, project=project)
device_obj2.check_connection()

self.assertEqual(device_obj2.provider._account.instance, each_item)

self.assertEqual(device_obj.provider.credentials.hub, self.HUB)
self.assertEqual(device_obj.provider.credentials.group, self.GROUP)
self.assertEqual(device_obj.provider.credentials.project, self.PROJECT)

device_obj2 = DeviceQiskit(device_name="ibmq_manila", hub="ibm-q-startup")
device_obj2.check_connection()
self.assertEqual(device_obj2.provider.credentials.hub, "ibm-q-startup")

@pytest.mark.api
def test_check_connection_provider_no_backend_wrong_hub_group_project(self):

"""
If the wrong hub, group or project is specified, check_connection should
Hub, group and project must always be specified together.
If either the hub, group or project is wrongly specified, check_connection should
return False.
If not all 3 are specified, check_connection should return False.
The provider_connected attribute should be updated to False.
Since the API Token is loaded from save_account, the api token will be
checked by Qiskit.
Expand Down