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

Don't cache in target_history() #1957

Open
wants to merge 5 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
33 changes: 18 additions & 15 deletions qiskit_ibm_runtime/ibm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def __getattr__(self, name: str) -> Any:
"'{}' object has no attribute '{}'".format(self.__class__.__name__, name)
)
# Lazy load properties and pulse defaults and construct the target object.
self._get_properties()
self._properties = self.properties()
self._get_defaults()
self._convert_to_target()
# Check if the attribute now is available on IBMBackend class due to above steps
Expand All @@ -238,16 +238,6 @@ def __getattr__(self, name: str) -> Any:
"'{}' object has no attribute '{}'".format(self.__class__.__name__, name)
)

def _get_properties(self, datetime: Optional[python_datetime] = None) -> None:
"""Gets backend properties and decodes it"""
if datetime:
datetime = local_to_utc(datetime)
if datetime or not self._properties:
api_properties = self._api_client.backend_properties(self.name, datetime=datetime)
if api_properties:
backend_properties = properties_from_server_data(api_properties)
self._properties = backend_properties

def _get_defaults(self) -> None:
"""Gets defaults if pulse backend and decodes it"""
if not self._defaults and isinstance(self._configuration, PulseBackendConfiguration):
Expand Down Expand Up @@ -341,7 +331,7 @@ def target(self) -> Target:
Returns:
Target
"""
self._get_properties()
self._properties = self.properties()
self._get_defaults()
self._convert_to_target()
return self._target
Expand All @@ -352,10 +342,23 @@ def target_history(self, datetime: Optional[python_datetime] = None) -> Target:
Returns:
Target with properties found on `datetime`
"""
self._get_properties(datetime=datetime)
self._get_defaults()
self._convert_to_target(refresh=True)
return self._target
if self.options.use_fractional_gates is None:
include_control_flow = True
include_fractional_gates = True
else:
# In IBM backend architecture as of today
# these features can be only exclusively supported.
include_control_flow = not self.options.use_fractional_gates
include_fractional_gates = self.options.use_fractional_gates

return convert_to_target(
configuration=self._configuration, # type: ignore[arg-type]
properties=self.properties(datetime=datetime), # pylint: disable=unexpected-keyword-arg
defaults=self._defaults,
include_control_flow=include_control_flow,
include_fractional_gates=include_fractional_gates,
)

def properties(
self, refresh: bool = False, datetime: Optional[python_datetime] = None
Expand Down
9 changes: 9 additions & 0 deletions test/integration/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ def test_backend_target_history(self):
self.assertIsNotNone(backend.target_history())
self.assertIsNotNone(backend.target_history(datetime=datetime.now() - timedelta(30)))

@production_only
def test_properties_not_cached_target_history(self):
"""Check backend properties is not cached in target_history()."""
backend = self.backend
with self.subTest(backend=backend.name):
properties = backend.properties()
backend.target_history(datetime=datetime.now() - timedelta(60))
self.assertEqual(properties, backend.properties())

def test_backend_max_circuits(self):
"""Check if the max_circuits property is set."""
backend = self.backend
Expand Down
Loading