Skip to content

ProgramResults

Bases: Displayable

A container for the results of a quantum program execution.

This class stores the shot histories and provides methods for collecting and analyzing data from the executed shots. It replaces the direct storage of shot histories in QuantumProgram.

ProgramResults

ProgramResults(shot_histories: dict[int, History] | None = None, shot_wall_clock_times: dict[int, float] | None = None, name: str = '(Unnamed program results)', parent_program: 'QuantumProgram | str | Path | None' = None, checkpoint_enabled: bool = False, checkpoint_dir: str | Path | None = None, max_memory_shots: int = 100, num_shots: int | None = None, max_frame_limit: int | None = None, results_filename: str = 'results.h5') -> None

Parameters:

Name Type Description Default
shot_histories dict[int, History] | None

A dictionary mapping shot indices to History objects. Defaults to None, which initializes an empty dict.

None
shot_wall_clock_times dict[int, float] | None

A dictionary mapping shot indices to each shot's wall-clock execution duration in seconds. Defaults to None, which initializes an empty dict.

None
name str

Name for logging

'(Unnamed program results)'
parent_program 'QuantumProgram | str | Path | None'

Reference to the parent QuantumProgram that generated these results. Can be a QuantumProgram object, a filepath string, or None.

None
checkpoint_enabled bool

Whether checkpointing is enabled for this ProgramResults.

False
checkpoint_dir str | Path | None

Directory where checkpoint files (including the parent program file, if written) are stored. If None, a default of ./checkpoints is used when a write is actually needed.

None
num_shots int | None

The total number of shots for this run (for resume detection).

None
max_frame_limit int | None

The maximum frame limit used in this run (for resume detection). None means no limit, or not set.

None
results_filename str

Filename to use for the canonical results checkpoint file. Defaults to "results.h5".

'results.h5'
max_memory_shots int

How many lazily read shots stay in memory at once. A value below 1 raises a ValueError.

100

Attributes

Name Type Value Doc
max_frame_limit 'int | None' property Maximum frame limit for this run (for resume detection). Resolves
name str property Name for logging. Resolves lazily from the nested source on
num_shots Any property Total number of shots for this run (for resume detection).
parent_program 'QuantumProgram | str | pathlib.Path | None' property Reference to the parent QuantumProgram that generated these

add_shot

add_shot(shot_index: int, history: HistoryLike, wall_clock_time: float | None = None) -> None

Add a shot history to the results.

Parameters:

Name Type Description Default
shot_index int

The index of the shot to add.

required
history HistoryLike

The History object for the shot.

required
wall_clock_time float | None

The shot's wall-clock execution duration in seconds. If None (the default), no entry is recorded for this shot in shot_wall_clock_times.

None

checkpoint

checkpoint(checkpoint_dir: str | Path | None = None, worker_id: str | None = None) -> BatchReceipt

Write every currently-unwritten shot to the checkpoint files.

Shots are marked done in the writer's ledger afterwards.

Parameters:

Name Type Description Default
checkpoint_dir str | Path | None

Directory for checkpoint files. If None, uses ./checkpoints.

None
worker_id str | None

Identifies the writer. With None (one writer per directory), shots are appended to the canonical results.h5. With an id, they go to that writer's own payload file, which consolidate_checkpoints() later merges into results.h5. An id can be reused after its writer was killed, but not while that writer is alive.

None

Returns:

Type Description
BatchReceipt

The keys written and the file holding them; keys are empty when nothing was unwritten.


collect_shot_data

collect_shot_data(key: str, indices: HistoryCollectDataIndexTypes, strip_none_entries: bool = False, return_counter: bool = False, frame_filter: Mapping[str, object] | None = None) -> list | Counter

Collate frame data over executed shots.

Parameters:

Name Type Description Default
key str

See key in collect_data()

required
indices HistoryCollectDataIndexTypes

See indices in collect_data()

required
strip_none_entries bool

See strip_none_entries in collect_data()

False
return_counter bool

Whether to return using a collections.Counter or not (default).

False
frame_filter Mapping[str, object] | None

See frame_filter in collect_data()

None

Returns:

Type Description
list

List of collect_data() outputs per shot


consolidate_checkpoints

consolidate_checkpoints(checkpoint_dir: str | Path | None = None) -> Path

Merge all payloads in checkpoint_dir into the canonical file.

The merged payloads and ledgers are then removed. An unreadable payload raises CheckpointReadError and nothing is deleted. Run only when no writer is live.

Parameters:

Name Type Description Default
checkpoint_dir str | Path | None

Directory containing checkpoint files to consolidate.

None

Returns:

Type Description
Path

Path to the canonical checkpoint file.


decode

Inherited static method from Serializable.decode().


display

Inherited method from Displayable.display().


dump

Inherited method from Serializable.dump().


encode

Inherited static method from Serializable.encode().


get_shot_history

get_shot_history(shot_index: int) -> History | None

Get a shot history, reading it from the checkpoint if it isn't in memory.

Raises CheckpointReadError if the store's file for the shot is unreadable.

Parameters:

Name Type Description Default
shot_index int

Index of the shot to retrieve.

required

Returns:

Type Description
History | None

The requested History object, or None if not found.


get_unwritten_shots

get_unwritten_shots() -> list[int]

Get a list of shot indices that have not been written to checkpoint files.

Returns:

Type Description
list

List of unwritten shot indices.


load

Inherited class method from Serializable.load().


load_checkpoint

load_checkpoint(checkpoint_dir: str | Path | None = None) -> None

Load checkpoint data from disk through the checkpoint store.

Metadata still at defaults is filled from the canonical file, and shots and wall-clock times not already in memory are loaded from it and any payloads; loaded shots leave the unwritten set. A missing directory or canonical file loads nothing; an unreadable file raises CheckpointReadError.

Parameters:

Name Type Description Default
checkpoint_dir str | Path | None

Directory containing checkpoint files. If None, uses the default checkpoint directory.

None

mark_shots_as_written

mark_shots_as_written(shot_indices: list[int]) -> None

Mark shots as having been written to checkpoint files.

Parameters:

Name Type Description Default
shot_indices list[int]

List of shot indices to mark as written.

required

mark_shots_checkpointed

mark_shots_checkpointed(shot_indices: list[int]) -> None

Record that the given shots are already durably checkpointed somewhere else (e.g. by the worker that computed them), without writing anything here. Marks the shots written and drops any of them held in memory.

Parameters:

Name Type Description Default
shot_indices list[int]

Shot indices to mark as already-checkpointed.

required

read

Inherited class method from Serializable.read().


serialize_function

Inherited static method from Serializable.serialize_function().


write

Inherited method from Serializable.write().