From 5fb120edcf9166a91e27f52f8ef10fb3618899bd Mon Sep 17 00:00:00 2001 From: "shahidee44@hotmail.com" Date: Mon, 27 Mar 2023 09:35:52 +0000 Subject: [PATCH 1/3] Fixed qiskit deprecation warning, updated unittests and DeviceQiskit --- setup.py | 1 + src/openqaoa-qiskit/backends/devices.py | 24 ++++++++++++------- .../backends/qaoa_qiskit_qpu.py | 14 +++++------ tests/test_qpu_devices.py | 24 ++++++++++++------- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/setup.py b/setup.py index 8d580aaa0..3bb48b41b 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/src/openqaoa-qiskit/backends/devices.py b/src/openqaoa-qiskit/backends/devices.py index 996c7242c..b75f23154 100644 --- a/src/openqaoa-qiskit/backends/devices.py +++ b/src/openqaoa-qiskit/backends/devices.py @@ -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 @@ -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 @@ -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 diff --git a/src/openqaoa-qiskit/backends/qaoa_qiskit_qpu.py b/src/openqaoa-qiskit/backends/qaoa_qiskit_qpu.py index 67b2a6a35..49eb278f5 100644 --- a/src/openqaoa-qiskit/backends/qaoa_qiskit_qpu.py +++ b/src/openqaoa-qiskit/backends/qaoa_qiskit_qpu.py @@ -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 @@ -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 diff --git a/tests/test_qpu_devices.py b/tests/test_qpu_devices.py index 81e25736f..1b8d213ed 100644 --- a/tests/test_qpu_devices.py +++ b/tests/test_qpu_devices.py @@ -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): @@ -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. From 5796b84d459ecd95a3088b80f658b4ee55f1990a Mon Sep 17 00:00:00 2001 From: Vishal Sharma Date: Mon, 27 Mar 2023 15:56:49 +0000 Subject: [PATCH 2/3] issue with github actions, changing the location of draft conditional --- .github/workflows/test_dev.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_dev.yml b/.github/workflows/test_dev.yml index 91c665abf..a64d89d9f 100644 --- a/.github/workflows/test_dev.yml +++ b/.github/workflows/test_dev.yml @@ -12,7 +12,6 @@ on: jobs: # This workflow contains a single job called "build" build: - if : github.event.pull_request.draft == false strategy: fail-fast: false matrix: @@ -20,6 +19,7 @@ jobs: python: [3.8] # The type of runner that the job will run on runs-on: ${{ matrix.os }} + if : github.event.pull_request.draft == false # Steps represent a sequence of tasks that will be executed as part of the job steps: @@ -79,8 +79,8 @@ jobs: files: ./coverage.xml docs: - if : github.event.pull_request.draft == false runs-on: ubuntu-latest + if : github.event.pull_request.draft == false steps: - name: Check out openqaoa uses: actions/checkout@v3 From 3278e64d07ecbf332606b3980ae8d9ac9ca42c24 Mon Sep 17 00:00:00 2001 From: shahidee44 <36878360+shahidee44@users.noreply.github.com> Date: Tue, 28 Mar 2023 10:26:59 +0800 Subject: [PATCH 3/3] Update test_dev.yml --- .github/workflows/test_dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_dev.yml b/.github/workflows/test_dev.yml index a64d89d9f..6317f8c98 100644 --- a/.github/workflows/test_dev.yml +++ b/.github/workflows/test_dev.yml @@ -49,7 +49,7 @@ jobs: run: | IBMQ_TOKEN=$IBMQ_TOKEN source env/bin/activate - python -c'from qiskit import IBMQ; import os; IBMQ.save_account(os.environ.get("IBMQ_TOKEN"))' + python -c'from qiskit_ibm_provider import IBMProvider; import os; IBMProvider.save_account(os.environ.get("IBMQ_TOKEN"))' - name: Setup AWS env: ACCESS_KEY: ${{ secrets.ACCESS_KEY }}