Skip to content

Commit

Permalink
Don't cache input params (#1783)
Browse files Browse the repository at this point in the history
* Don't cache input params

* Add release note

* fix unit tests

* fix unit tests

* Update integration test
  • Loading branch information
kt474 authored Jul 9, 2024
1 parent 44022c9 commit c7494aa
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 33 deletions.
10 changes: 3 additions & 7 deletions qiskit_ibm_runtime/base_runtime_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def __init__(
job_id: str,
program_id: str,
service: "qiskit_runtime_service.QiskitRuntimeService",
params: Optional[Dict] = None,
creation_date: Optional[str] = None,
user_callback: Optional[Callable] = None,
result_decoder: Optional[Union[Type[ResultDecoder], Sequence[Type[ResultDecoder]]]] = None,
Expand All @@ -83,7 +82,6 @@ def __init__(
client_params: Parameters used for server connection.
job_id: Job ID.
program_id: ID of the program this job is for.
params: Job parameters.
creation_date: Job creation date, in UTC.
user_callback: User callback function.
result_decoder: A :class:`ResultDecoder` subclass used to decode job results.
Expand All @@ -97,7 +95,6 @@ def __init__(
self._job_id = job_id
self._api_client = api_client
self._interim_results: Optional[Any] = None
self._params = params or {}
self._creation_date = creation_date
self._program_id = program_id
self._reason: Optional[str] = None
Expand Down Expand Up @@ -386,10 +383,9 @@ def inputs(self) -> Dict:
Returns:
Input parameters used in this job.
"""
if not self._params:
response = self._api_client.job_get(job_id=self.job_id(), exclude_params=False)
self._params = response.get("params", {})
return self._params

response = self._api_client.job_get(job_id=self.job_id(), exclude_params=False)
return response.get("params", {})

@property
def primitive_id(self) -> str:
Expand Down
6 changes: 1 addition & 5 deletions qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from .utils.result_decoder import ResultDecoder
from .runtime_job import RuntimeJob
from .runtime_job_v2 import RuntimeJobV2
from .utils import RuntimeDecoder, RuntimeEncoder, validate_job_tags
from .utils import validate_job_tags
from .api.client_parameters import ClientParameters
from .runtime_options import RuntimeOptions
from .ibm_backend import IBMBackend
Expand Down Expand Up @@ -1101,9 +1101,7 @@ def _decode_job(self, raw_data: Dict) -> Union[RuntimeJob, RuntimeJobV2]:
if not isinstance(params, str):
if params:
version = params.get("version", 1)
params = json.dumps(params, cls=RuntimeEncoder)

decoded = json.loads(params, cls=RuntimeDecoder)
if version == 2:
return RuntimeJobV2(
backend=backend,
Expand All @@ -1112,7 +1110,6 @@ def _decode_job(self, raw_data: Dict) -> Union[RuntimeJob, RuntimeJobV2]:
service=self,
job_id=raw_data["id"],
program_id=raw_data.get("program", {}).get("id", ""),
params=decoded,
creation_date=raw_data.get("created", None),
session_id=raw_data.get("session_id"),
tags=raw_data.get("tags"),
Expand All @@ -1124,7 +1121,6 @@ def _decode_job(self, raw_data: Dict) -> Union[RuntimeJob, RuntimeJobV2]:
service=self,
job_id=raw_data["id"],
program_id=raw_data.get("program", {}).get("id", ""),
params=decoded,
creation_date=raw_data.get("created", None),
session_id=raw_data.get("session_id"),
tags=raw_data.get("tags"),
Expand Down
3 changes: 0 additions & 3 deletions qiskit_ibm_runtime/runtime_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def __init__(
job_id: str,
program_id: str,
service: "qiskit_runtime_service.QiskitRuntimeService",
params: Optional[Dict] = None,
creation_date: Optional[str] = None,
user_callback: Optional[Callable] = None,
result_decoder: Optional[Union[Type[ResultDecoder], Sequence[Type[ResultDecoder]]]] = None,
Expand All @@ -104,7 +103,6 @@ def __init__(
client_params: Parameters used for server connection.
job_id: Job ID.
program_id: ID of the program this job is for.
params: Job parameters.
creation_date: Job creation date, in UTC.
user_callback: User callback function.
result_decoder: A :class:`ResultDecoder` subclass used to decode job results.
Expand All @@ -123,7 +121,6 @@ def __init__(
job_id=job_id,
program_id=program_id,
service=service,
params=params,
creation_date=creation_date,
user_callback=user_callback,
result_decoder=result_decoder,
Expand Down
3 changes: 0 additions & 3 deletions qiskit_ibm_runtime/runtime_job_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def __init__(
job_id: str,
program_id: str,
service: "qiskit_runtime_service.QiskitRuntimeService",
params: Optional[Dict] = None,
creation_date: Optional[str] = None,
user_callback: Optional[Callable] = None,
result_decoder: Optional[Union[Type[ResultDecoder], Sequence[Type[ResultDecoder]]]] = None,
Expand All @@ -81,7 +80,6 @@ def __init__(
client_params: Parameters used for server connection.
job_id: Job ID.
program_id: ID of the program this job is for.
params: Job parameters.
creation_date: Job creation date, in UTC.
user_callback: User callback function.
result_decoder: A :class:`ResultDecoder` subclass used to decode job results.
Expand All @@ -100,7 +98,6 @@ def __init__(
job_id=job_id,
program_id=program_id,
service=service,
params=params,
creation_date=creation_date,
user_callback=user_callback,
result_decoder=result_decoder,
Expand Down
2 changes: 2 additions & 0 deletions release-notes/unreleased/1783.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The input parameters for jobs will no longer be cached. These parameters can include large circuits
and should not be automatically kept in memory.
2 changes: 1 addition & 1 deletion test/integration/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def test_circuit_params_not_stored(self, service):
"""Test that circuits are not automatically stored in the job params."""
job = self._run_program(service)
job.wait_for_final_state()
self.assertFalse(job._params)
self.assertFalse(hasattr(job, "_params"))
self.assertTrue(job.inputs)

def _assert_complex_types_equal(self, expected, received):
Expand Down
8 changes: 3 additions & 5 deletions test/unit/mock/fake_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def __init__(
project,
backend_name,
final_status,
params,
image,
job_tags=None,
log_level=None,
Expand All @@ -118,7 +117,6 @@ def __init__(
self._group = group
self._project = project
self._backend_name = backend_name
self._params = params
self._image = image
self._interim_results = json.dumps({"quasi_dists": [{0: 0.5, 3: 0.5}], "metadata": []})
self._job_tags = job_tags
Expand Down Expand Up @@ -157,7 +155,6 @@ def to_dict(self):
"reason": self._reason,
"reasonCode": self._reason_code,
},
"params": self._params,
"program": {"id": self._program_id},
"image": self._image,
}
Expand Down Expand Up @@ -277,6 +274,7 @@ def __init__(
self._channel = channel
self.session_time = 0
self._sessions = set()
self._params = {}

# Setup the available backends
if not backend_specs:
Expand All @@ -303,7 +301,7 @@ def program_run(
self,
program_id: str,
backend_name: Optional[str],
params: Dict,
params: dict,
image: str,
hgp: Optional[str],
log_level: Optional[str],
Expand Down Expand Up @@ -335,7 +333,6 @@ def program_run(
group=group,
project=project,
backend_name=backend_name,
params=params,
final_status=self._final_status,
image=image,
log_level=log_level,
Expand All @@ -347,6 +344,7 @@ def program_run(
**self._job_kwargs,
)
self.session_time = session_time
self._params = params
self._jobs[job_id] = job
if start_session:
self._sessions.add(job_id)
Expand Down
9 changes: 0 additions & 9 deletions test/unit/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def test_run_program(self, service):
self.assertTrue(job.job_id())
self.assertIsInstance(job, RuntimeJob)
self.assertIsInstance(job.status(), JobStatus)
self.assertEqual(job.inputs, params)
with mock_wait_for_final_state(service, job):
job.wait_for_final_state()
self.assertEqual(job.status(), JobStatus.DONE)
Expand Down Expand Up @@ -127,7 +126,6 @@ def test_run_program_with_custom_runtime_image(self, service):
self.assertTrue(job.job_id())
self.assertIsInstance(job, RuntimeJob)
self.assertIsInstance(job.status(), JobStatus)
self.assertEqual(job.inputs, params)
with mock_wait_for_final_state(service, job):
job.wait_for_final_state()
self.assertTrue(job.result())
Expand Down Expand Up @@ -209,13 +207,6 @@ def test_job_status(self, service):
time.sleep(random.randint(1, 5))
self.assertTrue(job.status())

@run_quantum_and_cloud_fake
def test_job_inputs(self, service):
"""Test job inputs."""
inputs = {"param1": "foo", "param2": "bar"}
job = run_program(service, inputs=inputs)
self.assertEqual(inputs, job.inputs)

@run_quantum_and_cloud_fake
def test_wait_for_final_state(self, service):
"""Test wait for final state."""
Expand Down

0 comments on commit c7494aa

Please sign in to comment.