-
-
Notifications
You must be signed in to change notification settings - Fork 6
Add Error Budget Optimization Approach #155
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
e46f778
Add Error Budget Optimization
tobi-forster 3a7e7f8
add data and models
tobi-forster 861b245
Merge remote-tracking branch 'origin/main' into Error_Budget_Optimiza…
tobi-forster 4b5b285
Added README.md
tobi-forster 296709f
fixing file path
tobi-forster f6fc47c
Merge remote-tracking branch 'origin/main' into Error_Budget_Optimiza…
tobi-forster 9164121
Improved structure
tobi-forster 80081b1
Adding the error budget optimization approach to the README.
tobi-forster 2accbd0
Improve code structure.
tobi-forster 4582674
ZIP MQTBench Folder for improved structure.
tobi-forster f689c1a
Added comment on dependencies
tobi-forster 9d0f87a
Added comment on dependencies
tobi-forster d730e47
🎨 pre-commit fixes
pre-commit-ci[bot] 79a711c
Improve code structure.
tobi-forster be19809
excluded some capitalization checks.
tobi-forster fcdfd7a
Removing additional README.
tobi-forster 1514fe7
removed unused variables
tobi-forster 0932c46
Update resource_estimation/Error_Budget_Optimization/generate_data.py
tobi-forster 03a683c
Update resource_estimation/Error_Budget_Optimization/generate_data.py
tobi-forster e484d92
Improve code structure
tobi-forster 2ab9ebd
Corrected type annotations and improved code structure.
tobi-forster ee0710e
Add function to generate benchmarks.
tobi-forster 5572866
Improved code structure and adjusted pre-commit settings.
tobi-forster 918b6da
🚚 proper naming for folder
burgholzer 6034fe3
🩹 fix mypy linter configuration in pre-commit
burgholzer 9ab0d02
♻️ adjust for MQT Bench v2
burgholzer 15f61d3
🎨 use list comprehension for benchmark generation
burgholzer 5df4233
🎨 improve code quality in data generation
burgholzer 563dd39
🔧 simplify ruff configuration
burgholzer e9d72be
🎨 improve code quality for evaluation code
burgholzer 087b1ff
🔒 update lockfile
burgholzer 133e33b
♻️ stream files and circuits instead of loading everything in memory
burgholzer 322de25
Support newer versions of qsharp qiskit and mqt.bench and adding the …
tobi-forster 738b9d5
Potential fix for code scanning alert no. 2: Potentially uninitialize…
tobi-forster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
tobi-forster marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
Binary file not shown.
116 changes: 116 additions & 0 deletions
116
resource_estimation/Error_Budget_Optimization/evaluate.py
This file contains hidden or 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,116 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| from qsharp.estimator import ErrorBudgetPartition, EstimatorParams, LogicalCounts | ||
|
|
||
|
|
||
| def evaluate(X, Y, total_budget): | ||
| """ | ||
| Evaluates the impact of different error budget partitions on quantum resource estimates. | ||
| Args: | ||
| X: A 2D array where each row contains quantum circuit logical counts (e.g., numQubits, tCount, rotationCount, etc.). | ||
| Y: A 2D array where each row contains error budgetds for logical, t_states, and rotations. | ||
| total_budget: The total error budget to be distributed among logical, t_states, and rotations. | ||
| Returns: | ||
| qubits_diffs: List of relative differences in physical qubits compared to the default budget distribution. | ||
| runtime_diffs: List of relative differences in runtime compared to the default budget distribution. | ||
| product_diffs: List of relative differences in the product of qubits and runtime compared to the default budget distribution. | ||
| qubits_list: List of estimated physical qubits for each parameter set. | ||
| runtime_list: List of estimated runtimes for each parameter set. | ||
| default_qubits_list: List of physical qubits using the default budget for each parameter set. | ||
| default_runtime_list: List of runtimes using the default budget for each parameter set. | ||
| """ | ||
|
|
||
| product_diffs = [] | ||
| for i, params in enumerate(Y): | ||
| c = {} | ||
| c["numQubits"] = int(X[i, 0]) | ||
| c["tCount"] = int(X[i, 1]) | ||
| c["rotationCount"] = int(X[i, 2]) | ||
| c["rotationDepth"] = int(X[i, 3]) | ||
| c["cczCount"] = int(X[i, 4]) | ||
| c["ccixCount"] = int(X[i, 5]) | ||
| c["measurementCount"] = int(X[i, 6]) | ||
| logical_counts = LogicalCounts(c) | ||
| params_sum = params[0] + params[1] + params[2] | ||
| params_normalized = [ | ||
| params[0] / params_sum * total_budget, | ||
| params[1] / params_sum * total_budget, | ||
| params[2] / params_sum * total_budget, | ||
| ] | ||
|
|
||
| parameters = EstimatorParams() | ||
| parameters.error_budget = ErrorBudgetPartition() | ||
| parameters.error_budget.logical = params_normalized[0] | ||
| parameters.error_budget.t_states = params_normalized[1] | ||
| parameters.error_budget.rotations = params_normalized[2] | ||
|
|
||
| default_parameters = EstimatorParams() | ||
| default_parameters.error_budget = total_budget | ||
|
|
||
| result = logical_counts.estimate(parameters) | ||
| default_result = logical_counts.estimate(default_parameters) | ||
| qubits = result["physicalCounts"]["physicalQubits"] | ||
| runtime = result["physicalCounts"]["runtime"] | ||
| default_qubits = default_result["physicalCounts"]["physicalQubits"] | ||
| default_runtime = default_result["physicalCounts"]["runtime"] | ||
|
|
||
| product_diff = ((qubits * runtime) - (default_qubits * default_runtime)) / (default_qubits * default_runtime) | ||
| if product_diff > 0: | ||
| product_diff = 0 | ||
|
|
||
| product_diffs.append(product_diff) | ||
|
|
||
| return product_diffs | ||
|
|
||
|
|
||
| def plot_results(product_diffs, product_diffs_optimal, legend=False, bin_width=4): | ||
| """ | ||
| Plots histograms comparing predicted and optimal space-time differences. | ||
| This function visualizes the distribution of space-time differences (in percent) | ||
| for predicted and optimal product distributions. It overlays two histograms for | ||
| comparison and customizes axis ticks, labels, and legend. | ||
| Args: | ||
| product_diffs: List of space-time differences for predicted distributions. | ||
| product_diffs_optimal: List of space-time differences for best found distributions. | ||
| legend: Whether to display the legend on the plot. Defaults to False. | ||
| bin_width: Width of histogram bins. Defaults to 4. | ||
| Returns: | ||
| None. Displays the plot. | ||
| """ | ||
|
|
||
| product_diffs = [100 * i for i in product_diffs] | ||
| product_diffs_optimal = [100 * i for i in product_diffs_optimal] | ||
|
|
||
| all_data = product_diffs + product_diffs_optimal | ||
| data_min = min(all_data) | ||
| data_max = max(all_data) | ||
|
|
||
| data_min = min(0, data_min) | ||
| data_max += bin_width | ||
|
|
||
| bin_edges = np.arange(data_min, data_max + bin_width, bin_width) | ||
|
|
||
| np.arange(-100, 1, 20) | ||
| _fig, ax = plt.subplots(figsize=(5, 2.5)) | ||
| ax.hist( | ||
| product_diffs_optimal, | ||
| bins=bin_edges, | ||
| color="steelblue", | ||
| edgecolor="black", | ||
| alpha=0.5, | ||
| label="Best Distributions Determined", | ||
| ) | ||
| ax.hist( | ||
| product_diffs, bins=bin_edges, color="orange", edgecolor="black", alpha=0.5, label="Predicted Distributions" | ||
| ) | ||
| ax.set_xlim(data_min, data_max) | ||
| ax.set_xticks([-100, -80, -60, -40, -20, 0]) | ||
| ax.set_yticks([0, 40, 80, 120]) | ||
| ax.set_xlabel("Space-Time Difference [%]", fontsize=15) | ||
| ax.tick_params(axis="both", which="major", labelsize=15) | ||
| if legend: | ||
| ax.legend(loc="upper left", fontsize=12) | ||
| plt.tight_layout() | ||
| plt.show() |
108 changes: 108 additions & 0 deletions
108
resource_estimation/Error_Budget_Optimization/example_use.ipynb
This file contains hidden or 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,108 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "bb93cfcc", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "### Currently, please use the following versions for the approach:\n", | ||
| "### - qiskit==1.2.0\n", | ||
| "### - qsharp==1.9.0\n", | ||
tobi-forster marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "\n", | ||
| "\n", | ||
| "import zipfile\n", | ||
| "\n", | ||
| "from evaluate import evaluate, plot_results\n", | ||
| "from generate_data import generate_data\n", | ||
| "from training import train" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "f3e4a6d5", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Data Generation" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "2116443e", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "total_error_budget = 0.1\n", | ||
| "number_of_randomly_generated_distributions = 1000\n", | ||
| "path = \"MQTBench\"\n", | ||
| "\n", | ||
| "with zipfile.ZipFile(f\"{path}.zip\", \"r\") as zip_ref:\n", | ||
| " zip_ref.extractall(path)\n", | ||
| "\n", | ||
| "data = generate_data(total_error_budget, number_of_randomly_generated_distributions, path)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "7af169cb", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Training" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "675f4541", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "model, X_test, Y_test = train(data)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "6d4021ca", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Evaluation" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "18ac8807", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "Y_pred = model.predict(X_test)\n", | ||
| "product_diffs = evaluate(X_test, Y_pred, total_error_budget)\n", | ||
| "product_diffs_dataset = evaluate(X_test, Y_test, total_error_budget)\n", | ||
| "\n", | ||
| "plot_results(product_diffs, product_diffs_dataset, legend=True, bin_width=4)" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": ".venv", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.