Skip to content

Commit 3a8d49c

Browse files
authored
IQP migration followup (#2269)
* new iqp followup * remove print * fix use_fractional_gates bug * add reno & fix tests * add tests * add test & update url logic * update fg test
1 parent dbb8a02 commit 3a8d49c

File tree

6 files changed

+75
-14
lines changed

6 files changed

+75
-14
lines changed

qiskit_ibm_runtime/qiskit_runtime_service.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,19 @@ def __init__(
187187
if self._channel in ["ibm_cloud", "ibm_quantum_platform"]:
188188
self._default_instance = False
189189
self._active_api_client = RuntimeClient(self._client_params)
190+
self._backend_instance_groups: List[Dict[str, Any]] = []
191+
self._region = region or self._account.region
192+
self._plans_preference = plans_preference or self._account.plans_preference
193+
self._cached_backend_objs: List[IBMBackend] = []
194+
if self._account.instance:
195+
self._default_instance = True
190196
if instance is not None:
191197
self._api_clients = {instance: RuntimeClient(self._client_params)}
192198
else:
193199
self._api_clients = {}
194-
self._cached_backend_objs: List[IBMBackend] = []
195-
if self._account.instance:
196-
self._default_instance = True
197-
self._backend_instance_groups: List[Dict[str, Any]] = []
198-
self._region = region or self._account.region
199-
self._plans_preference = plans_preference or self._account.plans_preference
200+
instance_backends = self._resolve_cloud_instances(instance)
201+
for inst, _ in instance_backends:
202+
self._get_or_create_cloud_client(inst)
200203

201204
else:
202205
warnings.warn(
@@ -790,6 +793,20 @@ def _create_backend_obj(
790793
"ibm_quantum_platform",
791794
]:
792795
config = self._backend_configs[backend_name]
796+
# if cached config does not match use_fractional_gates
797+
if (
798+
use_fractional_gates
799+
and "rzz" not in config.basis_gates
800+
or not use_fractional_gates
801+
and "rzz" in config.basis_gates
802+
):
803+
config = configuration_from_server_data(
804+
raw_config=self._active_api_client.backend_configuration(backend_name),
805+
instance=instance,
806+
use_fractional_gates=use_fractional_gates,
807+
)
808+
self._backend_configs[backend_name] = config
809+
793810
else:
794811
config = configuration_from_server_data(
795812
raw_config=self._active_api_client.backend_configuration(backend_name),
@@ -1251,7 +1268,6 @@ def jobs(
12511268
job_responses = [] # type: List[Dict[str, Any]]
12521269
current_page_limit = limit or 20
12531270
offset = skip
1254-
12551271
while True:
12561272
jobs_response = self._active_api_client.jobs_get(
12571273
limit=current_page_limit,

qiskit_ibm_runtime/utils/utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,10 @@ def default_runtime_url_resolver(
342342
elif channel == "ibm_quantum_platform":
343343
# ibm_quantum_platform url
344344
region = _location_from_crn(instance)
345-
if region == "eu-de":
346-
api_host = (
347-
f"{parsed_url.scheme}://{region}" f".quantum.{parsed_url.hostname}/api/v1"
348-
)
349-
else:
350-
api_host = f"{parsed_url.scheme}://" f"quantum.{parsed_url.hostname}/api/v1"
345+
region_prefix = "" if region == "us-east" else f"{region}."
346+
api_host = (
347+
f"{parsed_url.scheme}://{region_prefix}" f"quantum.{parsed_url.hostname}/api/v1"
348+
)
351349
else:
352350
# ibm_cloud url
353351
api_host = (

release-notes/unreleased/2269.bug.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixed an issue where if there was no instance saved nor passed in at initialization, the service could not make
2+
any api calls until :meth:`.QiskitRuntimeService.backend` or :meth:`.QiskitRuntimeService.backends` is called first.
3+
4+
Fixed a bug where if ``use_fractional_gates`` is set but the backend configuration was already cached,
5+
the incorrect configuration could be returned.

test/integration/test_account.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,35 @@ def test_instances(self):
143143
self.assertTrue(instances[0]["crn"])
144144
self.assertTrue(instances[0]["name"])
145145

146+
def test_jobs_before_backend(self):
147+
"""Test retrieving jobs before backends call."""
148+
service = QiskitRuntimeService(
149+
token=self.dependencies.token, channel="ibm_quantum_platform", url=self.dependencies.url
150+
)
151+
self.assertTrue(service._all_instances)
152+
jobs = service.jobs()
153+
self.assertTrue(jobs)
154+
job = jobs[0]
155+
self.assertTrue(job.result())
156+
157+
def test_jobs_different_instances(self):
158+
"""Test retrieving jobs from different instances."""
159+
service = QiskitRuntimeService(
160+
token=self.dependencies.token, channel="ibm_quantum_platform", url=self.dependencies.url
161+
)
162+
instances = service.instances()
163+
for instance in instances:
164+
instance_service = QiskitRuntimeService(
165+
token=self.dependencies.token,
166+
instance=instance["crn"],
167+
channel="ibm_quantum_platform",
168+
url=self.dependencies.url,
169+
)
170+
jobs = instance_service.jobs()
171+
if jobs:
172+
instance_job = jobs[0].job_id()
173+
self.assertTrue(service.job(instance_job))
174+
146175

147176
class TestIntegrationAccount(IBMIntegrationTestCase):
148177
"""Integration tests for account management."""

test/integration/test_backend.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,14 @@ def test_too_many_qubits_in_circuit(self):
321321
f"circuit has {num_qubits} qubits but the target system requires {num}",
322322
str(err.exception),
323323
)
324+
325+
def test_use_fractional_gates_flag(self):
326+
"""Test use_fractional_gates returns correct backend config."""
327+
try:
328+
real_device_name = "alt_fez"
329+
real_device_no_fg = self.service.backend(real_device_name, use_fractional_gates=False)
330+
real_device_fg = self.service.backend(real_device_name, use_fractional_gates=True)
331+
except QiskitBackendNotFoundError:
332+
self.skipTest("Real backend not available.")
333+
self.assertIn("rzz", real_device_fg.basis_gates)
334+
self.assertNotIn("rzz", real_device_no_fg.basis_gates)

test/unit/test_account.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import uuid
1919
from typing import Any
2020
from unittest import skipIf
21+
from unittest.mock import patch
2122
from ddt import ddt, data
2223

2324
from qiskit_ibm_runtime.proxies import ProxyConfiguration
@@ -730,7 +731,8 @@ def test_enable_cloud_account_by_channel_token_url(self):
730731
for url in urls:
731732
with self.subTest(url=url), no_envs(["QISKIT_IBM_TOKEN"]):
732733
token = uuid.uuid4().hex
733-
service = FakeRuntimeService(channel="ibm_cloud", token=token, url=url)
734+
with patch.object(FakeRuntimeService, "_resolve_cloud_instances", return_value=[]):
735+
service = FakeRuntimeService(channel="ibm_cloud", token=token, url=url)
734736
self.assertTrue(service)
735737

736738
def test_enable_ibm_quantum_account_by_channel_token_url(self):

0 commit comments

Comments
 (0)