iqm-benchmarks 2.53__0-py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- iqm/benchmarks/__init__.py +75 -0
- iqm/benchmarks/benchmark_definition.py +369 -0
- iqm/benchmarks/circuit_containers.py +233 -0
- iqm/benchmarks/coherence/coherence.py +563 -0
- iqm/benchmarks/compressive_gst/__init__.py +21 -0
- iqm/benchmarks/compressive_gst/compressive_gst.py +611 -0
- iqm/benchmarks/compressive_gst/gst_analysis.py +1078 -0
- iqm/benchmarks/compressive_gst/mgst/__init__.py +90 -0
- iqm/benchmarks/compressive_gst/mgst/additional_fns.py +766 -0
- iqm/benchmarks/compressive_gst/mgst/algorithm.py +818 -0
- iqm/benchmarks/compressive_gst/mgst/low_level_jit.py +730 -0
- iqm/benchmarks/compressive_gst/mgst/optimization.py +485 -0
- iqm/benchmarks/compressive_gst/mgst/qiskit_interface.py +243 -0
- iqm/benchmarks/compressive_gst/mgst/reporting/__init__.py +1 -0
- iqm/benchmarks/compressive_gst/mgst/reporting/figure_gen.py +725 -0
- iqm/benchmarks/compressive_gst/mgst/reporting/reporting.py +846 -0
- iqm/benchmarks/compressive_gst/mgst/utils_gst.py +122 -0
- iqm/benchmarks/entanglement/__init__.py +19 -0
- iqm/benchmarks/entanglement/ghz.py +1079 -0
- iqm/benchmarks/entanglement/graph_states.py +1424 -0
- iqm/benchmarks/logging_config.py +28 -0
- iqm/benchmarks/optimization/__init__.py +17 -0
- iqm/benchmarks/optimization/qscore.py +1222 -0
- iqm/benchmarks/quantum_volume/__init__.py +20 -0
- iqm/benchmarks/quantum_volume/clops.py +785 -0
- iqm/benchmarks/quantum_volume/quantum_volume.py +833 -0
- iqm/benchmarks/randomized_benchmarking/__init__.py +16 -0
- iqm/benchmarks/randomized_benchmarking/clifford_generation.py +271 -0
- iqm/benchmarks/randomized_benchmarking/clifford_rb/__init__.py +17 -0
- iqm/benchmarks/randomized_benchmarking/clifford_rb/clifford_rb.py +462 -0
- iqm/benchmarks/randomized_benchmarking/direct_rb/__init__.py +17 -0
- iqm/benchmarks/randomized_benchmarking/direct_rb/direct_rb.py +1079 -0
- iqm/benchmarks/randomized_benchmarking/eplg/__init__.py +17 -0
- iqm/benchmarks/randomized_benchmarking/eplg/eplg.py +418 -0
- iqm/benchmarks/randomized_benchmarking/interleaved_rb/__init__.py +17 -0
- iqm/benchmarks/randomized_benchmarking/interleaved_rb/interleaved_rb.py +644 -0
- iqm/benchmarks/randomized_benchmarking/mirror_rb/__init__.py +17 -0
- iqm/benchmarks/randomized_benchmarking/mirror_rb/mirror_rb.py +802 -0
- iqm/benchmarks/randomized_benchmarking/multi_lmfit.py +140 -0
- iqm/benchmarks/randomized_benchmarking/randomized_benchmarking_common.py +1320 -0
- iqm/benchmarks/readout_mitigation.py +356 -0
- iqm/benchmarks/utils.py +1337 -0
- iqm/benchmarks/utils_plots.py +814 -0
- iqm/benchmarks/utils_shadows.py +253 -0
- iqm_benchmarks-2.53.0.dist-info/METADATA +461 -0
- iqm_benchmarks-2.53.0.dist-info/RECORD +49 -0
- iqm_benchmarks-2.53.0.dist-info/WHEEL +4 -0
- iqm_benchmarks-2.53.0.dist-info/licenses/AUTHORS.rst +18 -0
- iqm_benchmarks-2.53.0.dist-info/licenses/LICENSE.txt +226 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Copyright 2024 IQM Benchmarks developers
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""IQM's Python Library Benchmarking Suite QCVV."""
|
|
16
|
+
|
|
17
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
18
|
+
|
|
19
|
+
from .benchmark_definition import (
|
|
20
|
+
Benchmark,
|
|
21
|
+
BenchmarkAnalysisResult,
|
|
22
|
+
BenchmarkObservation,
|
|
23
|
+
BenchmarkObservationIdentifier,
|
|
24
|
+
BenchmarkRunResult,
|
|
25
|
+
)
|
|
26
|
+
from .circuit_containers import BenchmarkCircuit, CircuitGroup, Circuits
|
|
27
|
+
from .compressive_gst.compressive_gst import CompressiveGST, GSTConfiguration
|
|
28
|
+
from .entanglement.ghz import GHZBenchmark, GHZConfiguration
|
|
29
|
+
from .entanglement.graph_states import GraphStateBenchmark, GraphStateConfiguration
|
|
30
|
+
from .optimization.qscore import QScoreBenchmark, QScoreConfiguration
|
|
31
|
+
from .quantum_volume.clops import CLOPSBenchmark, CLOPSConfiguration
|
|
32
|
+
from .quantum_volume.quantum_volume import (
|
|
33
|
+
QuantumVolumeBenchmark,
|
|
34
|
+
QuantumVolumeConfiguration,
|
|
35
|
+
)
|
|
36
|
+
from .randomized_benchmarking.clifford_rb.clifford_rb import (
|
|
37
|
+
CliffordRandomizedBenchmarking,
|
|
38
|
+
CliffordRBConfiguration,
|
|
39
|
+
)
|
|
40
|
+
from .randomized_benchmarking.direct_rb.direct_rb import (
|
|
41
|
+
DirectRandomizedBenchmarking,
|
|
42
|
+
DirectRBConfiguration,
|
|
43
|
+
)
|
|
44
|
+
from .randomized_benchmarking.eplg.eplg import EPLGBenchmark, EPLGConfiguration
|
|
45
|
+
from .randomized_benchmarking.interleaved_rb.interleaved_rb import (
|
|
46
|
+
InterleavedRandomizedBenchmarking,
|
|
47
|
+
InterleavedRBConfiguration,
|
|
48
|
+
)
|
|
49
|
+
from .randomized_benchmarking.mirror_rb.mirror_rb import (
|
|
50
|
+
MirrorRandomizedBenchmarking,
|
|
51
|
+
MirrorRBConfiguration,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
AVAILABLE_BENCHMARKS = {
|
|
55
|
+
GHZBenchmark.name: GHZBenchmark,
|
|
56
|
+
CLOPSBenchmark.name: CLOPSBenchmark,
|
|
57
|
+
QuantumVolumeBenchmark.name: QuantumVolumeBenchmark,
|
|
58
|
+
CliffordRandomizedBenchmarking.name: CliffordRandomizedBenchmarking,
|
|
59
|
+
InterleavedRandomizedBenchmarking.name: InterleavedRandomizedBenchmarking,
|
|
60
|
+
MirrorRandomizedBenchmarking.name: MirrorRandomizedBenchmarking,
|
|
61
|
+
DirectRandomizedBenchmarking.name: DirectRandomizedBenchmarking,
|
|
62
|
+
EPLGBenchmark.name: EPLGBenchmark,
|
|
63
|
+
QScoreBenchmark.name: QScoreBenchmark,
|
|
64
|
+
GraphStateBenchmark.name: GraphStateBenchmark,
|
|
65
|
+
CompressiveGST.name: CompressiveGST,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
# Change here if project is renamed and does not equal the package name
|
|
70
|
+
dist_name = "iqm-benchmarks"
|
|
71
|
+
__version__ = version(dist_name)
|
|
72
|
+
except PackageNotFoundError: # pragma: no cover
|
|
73
|
+
__version__ = "unknown"
|
|
74
|
+
finally:
|
|
75
|
+
del version, PackageNotFoundError
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
# Copyright 2024 IQM Benchmarks developers
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Implementation of the base class for defining a benchmark."""
|
|
16
|
+
|
|
17
|
+
from abc import ABC, abstractmethod
|
|
18
|
+
from collections.abc import Callable
|
|
19
|
+
import copy
|
|
20
|
+
from copy import deepcopy
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
import functools
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
from iqm.benchmarks.circuit_containers import BenchmarkCircuit, Circuits
|
|
26
|
+
from iqm.benchmarks.logging_config import qcvv_logger
|
|
27
|
+
from iqm.benchmarks.utils import PhysicalLayout, RoutingMethod, get_iqm_backend, process_backend, timeit
|
|
28
|
+
from iqm.iqm_client.models import CircuitCompilationOptions, DDMode, DDStrategy
|
|
29
|
+
from iqm.qiskit_iqm.iqm_backend import IQMBackendBase
|
|
30
|
+
from iqm.qiskit_iqm.iqm_provider import IQMBackend, IQMFacadeBackend
|
|
31
|
+
from matplotlib.figure import Figure
|
|
32
|
+
import matplotlib.pyplot as plt
|
|
33
|
+
from pydantic import BaseModel
|
|
34
|
+
import xarray as xr
|
|
35
|
+
|
|
36
|
+
BACKEND_TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S.%f%z"
|
|
37
|
+
BENCHMARK_TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class BenchmarkObservationIdentifier:
|
|
42
|
+
"""Identifier for observations for ease of use.
|
|
43
|
+
|
|
44
|
+
Attributes:
|
|
45
|
+
qubit_indices: list containing the indices of the qubits the observation was executed on.
|
|
46
|
+
custom_identifier: optional user-defined string identifier.
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
qubit_indices: list[int] | None = None
|
|
51
|
+
custom_identifier: str | None = None
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def string_identifier(self) -> str:
|
|
55
|
+
"""String version of the qubit indices for ease of use, or custom identifier if given.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
A string of the qubit indices
|
|
59
|
+
|
|
60
|
+
"""
|
|
61
|
+
if self.custom_identifier is not None:
|
|
62
|
+
identifier = self.custom_identifier
|
|
63
|
+
elif self.qubit_indices:
|
|
64
|
+
identifier = str(self.qubit_indices)
|
|
65
|
+
else:
|
|
66
|
+
raise ValueError("Either qubit_indices or custom_identifier must be provided.")
|
|
67
|
+
return identifier
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass
|
|
71
|
+
class BenchmarkObservation:
|
|
72
|
+
"""Dataclass to store the main results of a single run of a Benchmark.
|
|
73
|
+
|
|
74
|
+
Attributes:
|
|
75
|
+
name: name of the observation
|
|
76
|
+
value: value of the observation
|
|
77
|
+
identifier: identifier, which should be a string of the qubit layout
|
|
78
|
+
uncertainty: uncertainty of the observation
|
|
79
|
+
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
name: str
|
|
83
|
+
value: Any
|
|
84
|
+
identifier: BenchmarkObservationIdentifier
|
|
85
|
+
uncertainty: Any | None = None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass
|
|
89
|
+
class BenchmarkRunResult:
|
|
90
|
+
"""A dataclass that stores the results of a single run of a Benchmark.
|
|
91
|
+
|
|
92
|
+
RunResult should contain enough information that the Benchmark can be analyzed based on those
|
|
93
|
+
results.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
dataset: xr.Dataset
|
|
97
|
+
circuits: Circuits
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass
|
|
101
|
+
class BenchmarkAnalysisResult:
|
|
102
|
+
"""A dataclass storing the results of the analysis.
|
|
103
|
+
|
|
104
|
+
The result consists of a dataset, plots, and observations. Plots are defined as a dictionary
|
|
105
|
+
that maps a plot name to a figure. Observations are key-value pairs of data that contain the
|
|
106
|
+
main results of the benchmark.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
dataset: xr.Dataset
|
|
110
|
+
circuits: Circuits | None = field(default=None)
|
|
111
|
+
plots: dict[str, Figure] = field(default_factory=lambda: ({}))
|
|
112
|
+
observations: list[BenchmarkObservation] = field(default_factory=list)
|
|
113
|
+
|
|
114
|
+
def plot(self, plot_name: str) -> Figure:
|
|
115
|
+
"""Plots the given figure.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
plot_name: Name of the figure to be plotted.
|
|
119
|
+
|
|
120
|
+
"""
|
|
121
|
+
return self.plots[plot_name]
|
|
122
|
+
|
|
123
|
+
def plot_all(self) -> None:
|
|
124
|
+
"""Plots all the figures defined for the analysis."""
|
|
125
|
+
for fig in self.plots.values():
|
|
126
|
+
show_figure(fig)
|
|
127
|
+
plt.show()
|
|
128
|
+
|
|
129
|
+
@classmethod
|
|
130
|
+
def from_run_result(cls, run: BenchmarkRunResult) -> "BenchmarkAnalysisResult":
|
|
131
|
+
"""Creates a new ``AnalysisResult`` from a ``RunResult``.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
run: A run for which analysis result is created.
|
|
135
|
+
|
|
136
|
+
"""
|
|
137
|
+
return cls(dataset=run.dataset, circuits=run.circuits)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def default_analysis_function(result: BenchmarkRunResult) -> BenchmarkAnalysisResult:
|
|
141
|
+
"""The default analysis that only pass the result through."""
|
|
142
|
+
return BenchmarkAnalysisResult.from_run_result(result)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def merge_datasets_dac(datasets: list[xr.Dataset]) -> xr.Dataset:
|
|
146
|
+
"""Merges a list of datasets recursively to minimize dataset sizes during merge.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
datasets: A list of xarray datasets
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
A list containing a single merged dataset
|
|
153
|
+
|
|
154
|
+
"""
|
|
155
|
+
if len(datasets) == 1:
|
|
156
|
+
return datasets[0]
|
|
157
|
+
datasets_new = []
|
|
158
|
+
for i in range(0, len(datasets), 2):
|
|
159
|
+
if i == (len(datasets) - 1):
|
|
160
|
+
datasets_new.append(datasets[i])
|
|
161
|
+
else:
|
|
162
|
+
datasets_new.append(xr.merge(datasets[i : i + 2]))
|
|
163
|
+
return merge_datasets_dac(datasets_new)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@timeit
|
|
167
|
+
def add_counts_to_dataset(counts: list[dict[str, int]], identifier: str, dataset: xr.Dataset) -> xr.Dataset:
|
|
168
|
+
"""Adds the counts from a cortex job result to the given dataset.
|
|
169
|
+
|
|
170
|
+
If counts with the same identifier are already present in the old dataset, then both counts are added together.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
counts: A list of dictionaries with counts of bitstrings.
|
|
174
|
+
identifier: A string to identify the current data, for instance the qubit layout.
|
|
175
|
+
dataset: Dataset to add results to.
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
A merged dataset where the new counts are added the input dataset
|
|
179
|
+
|
|
180
|
+
"""
|
|
181
|
+
qcvv_logger.info("Adding counts to dataset")
|
|
182
|
+
if not isinstance(counts, list):
|
|
183
|
+
counts = [counts]
|
|
184
|
+
datasets = []
|
|
185
|
+
for ii, _ in enumerate(counts):
|
|
186
|
+
ds_temp = xr.Dataset()
|
|
187
|
+
counts_dict = dict(counts[ii])
|
|
188
|
+
counts_array = xr.DataArray(
|
|
189
|
+
list(counts_dict.values()),
|
|
190
|
+
{f"{identifier}_state_{ii}": list(counts_dict.keys())},
|
|
191
|
+
)
|
|
192
|
+
if f"{identifier}_counts_{ii}" in dataset.variables:
|
|
193
|
+
counts_array = counts_array + dataset[f"{identifier}_counts_{ii}"]
|
|
194
|
+
counts_array = counts_array.sortby(counts_array[f"{identifier}_state_{ii}"])
|
|
195
|
+
ds_temp.update({f"{identifier}_counts_{ii}": counts_array})
|
|
196
|
+
datasets.append(ds_temp)
|
|
197
|
+
dataset_new = merge_datasets_dac(datasets)
|
|
198
|
+
dataset_merged = dataset.merge(dataset_new, compat="override")
|
|
199
|
+
return dataset_merged
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def show_figure(fig: Figure) -> None:
|
|
203
|
+
"""Shows a closed figure.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
fig: Figure to show.
|
|
207
|
+
|
|
208
|
+
"""
|
|
209
|
+
dummy = plt.figure()
|
|
210
|
+
new_manager = dummy.canvas.manager
|
|
211
|
+
if new_manager is not None:
|
|
212
|
+
fig_canvas = new_manager.canvas
|
|
213
|
+
if fig_canvas is not None:
|
|
214
|
+
fig_canvas = new_manager.canvas
|
|
215
|
+
fig_canvas.figure = fig
|
|
216
|
+
fig.set_canvas(fig_canvas)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
class Benchmark(ABC):
|
|
220
|
+
"""A base class for running cortex-based Benchmark experiments.
|
|
221
|
+
|
|
222
|
+
In order to write a new benchmark, it is recommended to derive a class
|
|
223
|
+
from this baseclass. A new benchmark is defined by deriving from this base
|
|
224
|
+
class and implementing the execute method. Additionally, a custom analysis
|
|
225
|
+
for the benchmark can be implemented by giving the pointer to the analysis
|
|
226
|
+
method in ``analysis_function`` field. The given analysis_function should
|
|
227
|
+
accept ``AnalysisResult`` as its input and return the final result.
|
|
228
|
+
"""
|
|
229
|
+
|
|
230
|
+
analysis_function: Callable[[BenchmarkRunResult], BenchmarkAnalysisResult] = staticmethod(default_analysis_function)
|
|
231
|
+
default_options: dict[str, Any] | None = None
|
|
232
|
+
options: dict[str, Any] | None = None
|
|
233
|
+
|
|
234
|
+
def __init__(
|
|
235
|
+
self,
|
|
236
|
+
backend: str | IQMBackendBase,
|
|
237
|
+
configuration: "BenchmarkConfigurationBase",
|
|
238
|
+
**kwargs: dict[str, Any],
|
|
239
|
+
) -> None:
|
|
240
|
+
self.configuration = configuration
|
|
241
|
+
self.serializable_configuration = deepcopy(self.configuration)
|
|
242
|
+
self.serializable_configuration.benchmark = self.name()
|
|
243
|
+
|
|
244
|
+
self.circuits = Circuits()
|
|
245
|
+
|
|
246
|
+
if isinstance(backend, str):
|
|
247
|
+
self.backend = get_iqm_backend(backend)
|
|
248
|
+
else:
|
|
249
|
+
self.backend = backend
|
|
250
|
+
self.backend, self.backend_name = process_backend(backend)
|
|
251
|
+
|
|
252
|
+
self.shots = self.configuration.shots
|
|
253
|
+
self.max_gates_per_batch = self.configuration.max_gates_per_batch
|
|
254
|
+
self.max_circuits_per_batch = self.configuration.max_circuits_per_batch
|
|
255
|
+
|
|
256
|
+
self.routing_method = self.configuration.routing_method
|
|
257
|
+
self.physical_layout = self.configuration.physical_layout
|
|
258
|
+
|
|
259
|
+
self.transpiled_circuits: BenchmarkCircuit
|
|
260
|
+
self.untranspiled_circuits: BenchmarkCircuit
|
|
261
|
+
|
|
262
|
+
self.options = copy.copy(self.default_options) if self.default_options else {}
|
|
263
|
+
self.options.update(kwargs)
|
|
264
|
+
self.runs: list[BenchmarkRunResult] = []
|
|
265
|
+
|
|
266
|
+
# Circuit compilation options
|
|
267
|
+
if self.configuration.use_dd:
|
|
268
|
+
if self.name in [
|
|
269
|
+
"compressive_GST",
|
|
270
|
+
"mirror_rb",
|
|
271
|
+
"interleaved_clifford_rb",
|
|
272
|
+
"clifford_rb",
|
|
273
|
+
]:
|
|
274
|
+
qcvv_logger.warning(
|
|
275
|
+
"Beware that activating dynamical decoupling will change fidelities, "
|
|
276
|
+
"error models and their interpretation."
|
|
277
|
+
)
|
|
278
|
+
self.circuit_compilation_options = CircuitCompilationOptions(
|
|
279
|
+
dd_mode=DDMode.ENABLED, dd_strategy=self.configuration.dd_strategy
|
|
280
|
+
)
|
|
281
|
+
else:
|
|
282
|
+
self.circuit_compilation_options = CircuitCompilationOptions(dd_mode=DDMode.DISABLED)
|
|
283
|
+
|
|
284
|
+
@classmethod
|
|
285
|
+
@abstractmethod
|
|
286
|
+
def name(cls) -> str:
|
|
287
|
+
"""Return the name of the benchmark."""
|
|
288
|
+
|
|
289
|
+
@abstractmethod
|
|
290
|
+
def execute(self, backend: IQMBackend | IQMFacadeBackend) -> xr.Dataset:
|
|
291
|
+
"""Executes the benchmark.
|
|
292
|
+
|
|
293
|
+
This method should be overridden by deriving classes.
|
|
294
|
+
|
|
295
|
+
Args:
|
|
296
|
+
backend: Qiskit backend used to execute benchmarks.
|
|
297
|
+
|
|
298
|
+
Returns:
|
|
299
|
+
An xarray dataset which contains to results of the benchmark execution.
|
|
300
|
+
The dataset should contain all the information necessary for analysing
|
|
301
|
+
the benchmark results.
|
|
302
|
+
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
def run(self) -> BenchmarkRunResult:
|
|
306
|
+
"""Runs the benchmark using the given backend.
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
RunResult: The result of the benchmark run.
|
|
310
|
+
|
|
311
|
+
"""
|
|
312
|
+
backend_for_execute = copy.copy(self.backend)
|
|
313
|
+
backend_for_execute.run = functools.partial(self.backend.run)
|
|
314
|
+
dataset = self.execute(backend_for_execute)
|
|
315
|
+
run = BenchmarkRunResult(dataset, self.circuits)
|
|
316
|
+
self.runs.append(run)
|
|
317
|
+
return run
|
|
318
|
+
|
|
319
|
+
def analyze(self, run_index: int = -1) -> BenchmarkAnalysisResult:
|
|
320
|
+
"""The default analysis for the benchmark.
|
|
321
|
+
|
|
322
|
+
Internally uses the function defined by the attribute ``analysis_function``
|
|
323
|
+
to perform the analysis. This function makes a shallow copy of the dataset produced by
|
|
324
|
+
run. Therefore, it is recommended to not make changes to the data of the dataset but
|
|
325
|
+
just the structure of the array.
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
run_index: Index for the run to analyze.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
An analysis result constructed from the run and updated by the analysis method defined by
|
|
332
|
+
the ``analysis_function`` field.
|
|
333
|
+
|
|
334
|
+
"""
|
|
335
|
+
run = self.runs[run_index]
|
|
336
|
+
updated_result = self.analysis_function(run)
|
|
337
|
+
return updated_result
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
class BenchmarkConfigurationBase(BaseModel):
|
|
341
|
+
r"""Benchmark configuration base.
|
|
342
|
+
|
|
343
|
+
Attributes:
|
|
344
|
+
benchmark: Benchmark configuration.
|
|
345
|
+
shots: Number of shots to use in circuit execution. The default for all benchmarks is 2**8.
|
|
346
|
+
max_gates_per_batch: Maximum number of gates per circuit batch. The default for all benchmarks is None.
|
|
347
|
+
max_circuits_per_batch: Maximum number of circuits per batch. The default for all benchmarks is None.
|
|
348
|
+
routing_method: Qiskit routing method to use in transpilation. The default for all benchmarks is "sabre".
|
|
349
|
+
physical_layout: Whether physical layout is constrained during transpilation to selected physical qubits.
|
|
350
|
+
"fixed": physical layout is constrained during transpilation to the selected initial physical qubits.
|
|
351
|
+
"batching": physical layout is allowed to use any other physical qubits, and circuits are batched according
|
|
352
|
+
to final measured qubits. The default for all benchmarks is "fixed".
|
|
353
|
+
use_dd: Boolean flag determining whether to enable dynamical decoupling during circuit execution. The default
|
|
354
|
+
for all benchmarks is False.
|
|
355
|
+
dd_strategy: Dynamical decoupling strategy.
|
|
356
|
+
active_reset_cycles: Number of active reset cycles to apply after each circuit execution.
|
|
357
|
+
If None, passive reset is used.
|
|
358
|
+
|
|
359
|
+
"""
|
|
360
|
+
|
|
361
|
+
benchmark: type[Benchmark] | str
|
|
362
|
+
shots: int = 2**8
|
|
363
|
+
max_gates_per_batch: int | None = None
|
|
364
|
+
max_circuits_per_batch: int | None = None
|
|
365
|
+
routing_method: RoutingMethod = RoutingMethod.SABRE
|
|
366
|
+
physical_layout: PhysicalLayout = PhysicalLayout.FIXED
|
|
367
|
+
use_dd: bool | None = False
|
|
368
|
+
dd_strategy: DDStrategy | None = None
|
|
369
|
+
active_reset_cycles: int | None = None
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# Copyright 2024 IQM Benchmarks developers
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Module containing classes to easily interact with quantum circuits."""
|
|
16
|
+
|
|
17
|
+
from dataclasses import dataclass, field
|
|
18
|
+
from typing import TypeAlias
|
|
19
|
+
|
|
20
|
+
from iqm.qiskit_iqm.iqm_circuit import IQMCircuit
|
|
21
|
+
from qiskit.circuit import Qubit
|
|
22
|
+
|
|
23
|
+
QubitLayout: TypeAlias = tuple[Qubit, ...]
|
|
24
|
+
QubitLayouts: TypeAlias = tuple[QubitLayout, ...]
|
|
25
|
+
QubitLayoutIndices: TypeAlias = tuple[tuple[int, ...], ...]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# pylint: disable=protected-access
|
|
29
|
+
@dataclass
|
|
30
|
+
class CircuitGroup:
|
|
31
|
+
"""Group of `IQMCircuits`. It represents a list of circuits with a common purpose, typically executed in a batch.
|
|
32
|
+
|
|
33
|
+
Attributes:
|
|
34
|
+
circuits: List of `IQMCircuit`.
|
|
35
|
+
name: Name of the group.
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
circuits: list[IQMCircuit] = field(default_factory=list)
|
|
40
|
+
name: str | None = field(default="")
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def qubit_layouts_by_index(self) -> QubitLayoutIndices:
|
|
44
|
+
"""The qubit layouts contained in all the circuits, where the qubits are represented by an integer.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
All qubit layouts.
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
return tuple(tuple(int(qubit._index) for qubit in layout) for layout in self.qubit_layouts)
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def qubit_layouts(self) -> QubitLayouts:
|
|
54
|
+
"""The qubit layouts contained in all the circuits, where the qubits are represented by an instance of `Qubit`.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
All qubit layouts.
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
qubit_layouts = tuple(tuple(x._data.active_bits()[0]) for x in self.circuits)
|
|
61
|
+
return qubit_layouts
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def qubits(self) -> set[Qubit]:
|
|
65
|
+
"""The set of active qubits in all of the circuits.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
A set of `Qubit`
|
|
69
|
+
|
|
70
|
+
"""
|
|
71
|
+
qubit_set = set()
|
|
72
|
+
for circuit in self.circuits:
|
|
73
|
+
for qubit in circuit._data.active_bits()[0]:
|
|
74
|
+
qubit_set.add(qubit)
|
|
75
|
+
return qubit_set
|
|
76
|
+
|
|
77
|
+
def __setitem__(self, key: str, value: IQMCircuit) -> None:
|
|
78
|
+
"""Set a circuit with the given key as its name."""
|
|
79
|
+
value.name = key
|
|
80
|
+
return self.add_circuit(value)
|
|
81
|
+
|
|
82
|
+
def add_circuit(self, circuit: IQMCircuit) -> None:
|
|
83
|
+
"""Adds a circuit to the internal list.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
circuit: Circuit to add.
|
|
87
|
+
|
|
88
|
+
"""
|
|
89
|
+
self.circuits.append(circuit)
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def circuit_names(self) -> list[str]:
|
|
93
|
+
"""Name of all the circuits contained in the group as a string.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
List of strings
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
benchmark_circuit_names = [x.name for x in self.circuits]
|
|
100
|
+
return benchmark_circuit_names
|
|
101
|
+
|
|
102
|
+
def get_circuits_by_name(self, name: str) -> IQMCircuit | None:
|
|
103
|
+
"""Returns a list of the internal circuits that share the same name.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
name: Name of the circuit to return as a string
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
The circuit with the desired name as a string, or None if they are not found.
|
|
110
|
+
|
|
111
|
+
"""
|
|
112
|
+
benchmark_circuit_names = filter(lambda x: x.name == name, self.circuits)
|
|
113
|
+
return next(benchmark_circuit_names, None)
|
|
114
|
+
|
|
115
|
+
def __getitem__(self, key: str) -> IQMCircuit | None:
|
|
116
|
+
"""Get a circuit by name."""
|
|
117
|
+
return self.get_circuits_by_name(key)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@dataclass
|
|
121
|
+
class BenchmarkCircuit:
|
|
122
|
+
"""A class grouping a list of `CircuitGroup` into a single purpose.
|
|
123
|
+
|
|
124
|
+
This can typically represent, for example, all of the transpiled circuits that are executed.
|
|
125
|
+
|
|
126
|
+
Attributes:
|
|
127
|
+
name: Name of the `BenchmarkCircuit`.
|
|
128
|
+
circuit_groups: List of `CircuitGroup` contained inside
|
|
129
|
+
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
name: str
|
|
133
|
+
circuit_groups: list[CircuitGroup] = field(default_factory=list)
|
|
134
|
+
|
|
135
|
+
def get_circuit_group_by_name(self, name: str) -> CircuitGroup | None:
|
|
136
|
+
"""Gets a `CircuitGroup` by name.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
name: name of the group.
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
The desired `CircuitGroup`, or None if it does not exist.
|
|
143
|
+
|
|
144
|
+
"""
|
|
145
|
+
benchmark_circuit_names = filter(lambda x: x.name == name, self.circuit_groups)
|
|
146
|
+
next_value = next(benchmark_circuit_names, None)
|
|
147
|
+
return next_value
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def groups(self) -> list[CircuitGroup]:
|
|
151
|
+
"""The circuit groups inside."""
|
|
152
|
+
return self.circuit_groups
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def group_names(self) -> list[str | None]:
|
|
156
|
+
"""Names of all the contained `CircuitGroup`."""
|
|
157
|
+
return [x.name for x in self.circuit_groups] if len(self.circuit_groups) > 0 else []
|
|
158
|
+
|
|
159
|
+
@property
|
|
160
|
+
def qubit_indices(self) -> set[int]:
|
|
161
|
+
"""Set of all the qubits used in all the `CircuitGroup`, represented as an integer."""
|
|
162
|
+
qubit_set = set()
|
|
163
|
+
for circuit in self.circuit_groups:
|
|
164
|
+
for qubit_index in [x._index for x in circuit.qubits]:
|
|
165
|
+
qubit_set.add(qubit_index)
|
|
166
|
+
return qubit_set
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def qubits(self) -> set[Qubit]:
|
|
170
|
+
"""Set of all the qubits used in all the `CircuitGroup`, represented as an instance of `Qubit`."""
|
|
171
|
+
qubit_set: set[Qubit] = set()
|
|
172
|
+
for qubit in [x.qubits for x in self.circuit_groups]:
|
|
173
|
+
qubit_set = qubit_set.union(qubit)
|
|
174
|
+
return qubit_set
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def qubit_layouts_by_index(self) -> set[QubitLayoutIndices]:
|
|
178
|
+
"""Set of all the qubit layouts used, where the qubits are represented as an integer."""
|
|
179
|
+
layout_set = set()
|
|
180
|
+
for layout in [x.qubit_layouts_by_index for x in self.circuit_groups]:
|
|
181
|
+
layout_set.add(layout)
|
|
182
|
+
return layout_set
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def qubit_layouts(self) -> set[QubitLayouts]:
|
|
186
|
+
"""Set of all the qubit layouts used, where the qubits are represented as an instance of `Qubit`."""
|
|
187
|
+
layout_set = set()
|
|
188
|
+
for layout in [x.qubit_layouts for x in self.circuit_groups]:
|
|
189
|
+
layout_set.add(layout)
|
|
190
|
+
return layout_set
|
|
191
|
+
|
|
192
|
+
def __setitem__(self, key: str, value: CircuitGroup) -> None:
|
|
193
|
+
"""Set a circuit group with the given key as its name."""
|
|
194
|
+
value.name = key
|
|
195
|
+
return self.circuit_groups.append(value)
|
|
196
|
+
|
|
197
|
+
def __getitem__(self, key: str) -> CircuitGroup | None:
|
|
198
|
+
"""Get a circuit group by name."""
|
|
199
|
+
return self.get_circuit_group_by_name(key)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
@dataclass
|
|
203
|
+
class Circuits:
|
|
204
|
+
"""Container for all the `BenchmarkCircuit` that are generated in a single benchmark execution.
|
|
205
|
+
|
|
206
|
+
Attributes:
|
|
207
|
+
benchmark_circuits: List of `BenchmarkCircuit` contained.
|
|
208
|
+
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
benchmark_circuits: list[BenchmarkCircuit] = field(default_factory=list)
|
|
212
|
+
|
|
213
|
+
def __setitem__(self, key: str, value: BenchmarkCircuit) -> None:
|
|
214
|
+
"""Set a benchmark circuit with the given key as its name."""
|
|
215
|
+
value.name = key
|
|
216
|
+
return self.benchmark_circuits.append(value)
|
|
217
|
+
|
|
218
|
+
def __getitem__(self, key: str) -> BenchmarkCircuit | None:
|
|
219
|
+
"""Get a benchmark circuit by name."""
|
|
220
|
+
return self.get_benchmark_circuits_by_name(key)
|
|
221
|
+
|
|
222
|
+
def get_benchmark_circuits_by_name(self, name: str) -> BenchmarkCircuit | None:
|
|
223
|
+
"""Returned the `BenchmarkCircuit` by name.
|
|
224
|
+
|
|
225
|
+
Args:
|
|
226
|
+
name: Name of the requested `BenchmarkCircuit` as a string
|
|
227
|
+
|
|
228
|
+
Returns:
|
|
229
|
+
The requested `BenchmarkCircuit` if it exists, None otherwise
|
|
230
|
+
|
|
231
|
+
"""
|
|
232
|
+
benchmark_circuit_names = filter(lambda x: x.name == name, self.benchmark_circuits)
|
|
233
|
+
return next(benchmark_circuit_names, None)
|