forked from Qiskit/qiskit-ibm-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
241 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2024. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""TwirledSliceSpan""" | ||
|
||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from typing import Iterable | ||
|
||
import math | ||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
from .execution_span import ExecutionSpan, ShapeType | ||
|
||
|
||
class TwirledSliceSpan(ExecutionSpan): | ||
"""An :class:`~.ExecutionSpan` for data stored in a sliceable format when twirling. | ||
This type of execution span references pub result data that came from a twirled sampler | ||
experiment which was executed by either prepending or appending an axis to paramater values | ||
to account for twirling. The value format of ``data_slices`` begins with the shape of the | ||
parameter values accounting for twirling, and the second element indicates whether the | ||
twirling axis is the first or the last. ``shape_slice`` and ``shots_slice`` are the same as | ||
for :class:`~.DoubleSliceSpan`. | ||
Args: | ||
start: The start time of the span, in UTC. | ||
stop: The stop time of the span, in UTC. | ||
data_slices: A map from pub indices to | ||
``(twirled_shape, at_front, shape_slice, shots_slice)``. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
start: datetime, | ||
stop: datetime, | ||
data_slices: dict[int, tuple[ShapeType, bool, slice, slice]], | ||
): | ||
super().__init__(start, stop) | ||
self._data_slices = data_slices | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return isinstance(other, TwirledSliceSpan) and ( | ||
self.start == other.start | ||
and self.stop == other.stop | ||
and self._data_slices == other._data_slices | ||
) | ||
|
||
@property | ||
def pub_idxs(self) -> list[int]: | ||
return sorted(self._data_slices) | ||
|
||
@property | ||
def size(self) -> int: | ||
size = 0 | ||
for shape, _, shape_sl, shots_sl in self._data_slices.values(): | ||
size += len(range(math.prod(shape[:-1]))[shape_sl]) * len(range(shape[-1])[shots_sl]) | ||
return size | ||
|
||
def mask(self, pub_idx: int) -> npt.NDArray[np.bool_]: | ||
twirled_shape, at_front, shape_sl, shots_sl = self._data_slices[pub_idx] | ||
mask = np.zeros(twirled_shape, dtype=np.bool_) | ||
mask.reshape(np.prod(twirled_shape[:-1]), twirled_shape[-1])[(shape_sl, shots_sl)] = True | ||
|
||
if at_front: | ||
# if the first axis is over twirling samples, push them right before shots | ||
ndim = len(twirled_shape) | ||
mask = mask.transpose(*range(1, ndim - 1), 0, ndim - 1) | ||
twirled_shape = twirled_shape[1:-1] + twirled_shape[:1] + twirled_shape[-1:] | ||
|
||
# merge twirling axis and shots axis before returning | ||
return mask.reshape(*twirled_shape[:-2], math.prod(twirled_shape[-2:])) | ||
|
||
def filter_by_pub(self, pub_idx: int | Iterable[int]) -> "TwirledSliceSpan": | ||
pub_idx = {pub_idx} if isinstance(pub_idx, int) else set(pub_idx) | ||
slices = {idx: val for idx, val in self._data_slices.items() if idx in pub_idx} | ||
return TwirledSliceSpan(self.start, self.stop, slices) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters