-
Notifications
You must be signed in to change notification settings - Fork 0
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
Plot expectation value benchmarks #168
Open
natestemen
wants to merge
20
commits into
main
Choose a base branch
from
plot-expectation-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,151
−1,596
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9f62ec5
remove unused QASM generation functions
jordandsullivan 47632ad
Bug fixes for save
jordandsullivan c7dcd18
Add N=10 qubits QCNN file
jordandsullivan 1ed386c
refactor expval benchmark to run single iteration
natestemen fc9a3e5
Merge branch 'plot-expectation-value' of https://github.com/unitaryfu…
jordandsullivan 019a232
Copy benchpress QASM 2 files for N=10 and N=9 qubits
jordandsullivan c32ee7c
Rename qasm file to include basis
jordandsullivan dcb4322
Add filenames to expal run
jordandsullivan 1c28fd3
Fix filenames
jordandsullivan 22a35a7
ensure observable matches circuit size
natestemen 5353c4b
add qisit aer to requirements
natestemen 683310f
remove old expval data
natestemen 1fb692f
first results
natestemen 39f4ed2
first plot
natestemen 8471ab3
more plots
natestemen 782c920
uncomment first commands to run
natestemen 011b185
s/relative/absolute for errors
natestemen e334d0c
add new data and remove relative errors
natestemen f6b6417
remove old plot
natestemen 496731a
Merge branch 'main' into plot-expectation-value
jordandsullivan 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
natestemen marked this conversation as resolved.
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.
natestemen marked this conversation as resolved.
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.
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,73 @@ | ||
import os | ||
import glob | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
|
||
# Step 1: Get the directory of the current script | ||
directory_of_this_file = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
# Step 2: Construct the correct path to the results folder | ||
results_folder = os.path.join(directory_of_this_file, "../results") | ||
|
||
# Step 3: Use glob to find all CSV files in the results folder | ||
csv_files = glob.glob(os.path.join(results_folder, "expval_*.csv")) | ||
|
||
# Step 4: Iterate through all CSV files and load them into dataframes with date column | ||
dfs = [] # List to store dataframes | ||
for file in csv_files: | ||
# Extract the date from the filename (assuming the date is after 'expval_' and before '.csv') | ||
date_label = str(file).split('_')[1].split('.')[0] | ||
|
||
# Load the CSV file into a DataFrame | ||
df = pd.read_csv(file, comment="#") | ||
|
||
# Add the extracted date as a new column in the dataframe | ||
df['date'] = date_label | ||
|
||
# Append the dataframe to the list | ||
dfs.append(df) | ||
|
||
# Step 5: Concatenate all dataframes into one large dataframe | ||
df_all = pd.concat(dfs, ignore_index=True) | ||
|
||
# Convert the 'date' column to datetime | ||
df_all['date'] = pd.to_datetime(df_all['date']) | ||
|
||
# Step 6: Group by date and compiler, and calculate the average relative error | ||
summary = df_all.groupby(['date', 'compiler']).agg( | ||
avg_relative_error=('relative_error', 'mean') | ||
).reset_index() | ||
|
||
# Step 7: Set up the figure | ||
fig, ax = plt.subplots(figsize=(12, 6)) | ||
|
||
# Set color map for different compilers | ||
unique_compilers = sorted(summary['compiler'].unique()) | ||
colormap = plt.get_cmap("tab10", len(unique_compilers)) | ||
color_map = {compiler: colormap(i) for i, compiler in enumerate(unique_compilers)} | ||
|
||
# Step 8: Plot average relative error over time | ||
for compiler in unique_compilers: | ||
compiler_data = summary[summary['compiler'] == compiler] | ||
|
||
# Plot average relative error | ||
ax.plot( | ||
compiler_data['date'], | ||
compiler_data['avg_relative_error'], | ||
label=compiler, | ||
color=color_map[compiler], | ||
marker='o' | ||
) | ||
|
||
# Step 9: Customize plot | ||
ax.set_title("Average Relative Error Over Time") | ||
ax.set_xlabel("Date") | ||
ax.set_ylabel("Average Relative Error") | ||
ax.grid(True) | ||
ax.legend(title="Compiler") | ||
|
||
# Adjust layout and save the figure | ||
plt.tight_layout() | ||
filename = os.path.join(directory_of_this_file, "../average_relative_error_over_time.png") | ||
print(f"\n Saving plot to {filename}") | ||
fig.savefig(filename) |
105 changes: 105 additions & 0 deletions
105
benchmarks/scripts/plot_latest_expval_benchmarks_per_circuit.py
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,105 @@ | ||
import os | ||
import glob | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
|
||
# Step 1: Get the directory of the current script | ||
directory_of_this_file = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
# Step 2: Construct the correct path to the results folder | ||
results_folder = os.path.join(directory_of_this_file, "../results") | ||
|
||
# Step 3: Use glob to find all CSV files in the results folder | ||
csv_files = glob.glob(os.path.join(results_folder, "expval_*.csv")) | ||
|
||
# Step 4: Iterate through all CSV files and load them into dataframes with date column | ||
dfs = [] # List to store dataframes | ||
for file in csv_files: | ||
# Extract the date from the filename (assuming the date is after 'expval_' and before '.csv') | ||
date_label = str(file).split('_')[1].split('.')[0] | ||
|
||
# Load the CSV file into a DataFrame | ||
df = pd.read_csv(file, comment="#") | ||
|
||
# Add the extracted date as a new column in the dataframe | ||
df['date'] = date_label | ||
|
||
# Append the dataframe to the list | ||
dfs.append(df) | ||
|
||
# Step 5: Concatenate all dataframes into one large dataframe | ||
df_all = pd.concat(dfs, ignore_index=True) | ||
|
||
# Find the most recent date in the 'date' column | ||
latest_date = df_all['date'].max() | ||
print("latest date is", latest_date) | ||
|
||
# Filter the dataframe to only include rows from the most recent date | ||
df_latest = df_all[df_all['date'] == latest_date] | ||
|
||
# Step 6: Define the bar width and create x-axis positions for the circuits | ||
bar_width = 0.2 | ||
circuit_names = df_latest['circuit_name'].unique() | ||
x_positions = range(len(circuit_names)) # X positions for each circuit | ||
|
||
# Create a dictionary to map circuit names to indices | ||
circuit_name_to_index = {name: i for i, name in enumerate(circuit_names)} | ||
|
||
# Step 7: Set up the figure and axes | ||
fig, ax = plt.subplots(1, 2, figsize=(14, 7)) | ||
|
||
# Set color map for different compilers | ||
# Get unique compilers and sort them alphabetically | ||
unique_compilers = sorted(df_latest['compiler'].unique()) | ||
|
||
# Set color map for different compilers | ||
colormap = plt.get_cmap("tab10", len(unique_compilers)) | ||
color_map = {compiler: colormap(i) for i, compiler in enumerate(unique_compilers)} | ||
|
||
# Step 8: Plot relative error and absolute error for each compiler | ||
for i, (key, grp) in enumerate(df_latest.groupby("compiler")): | ||
# Get indices for each circuit in the current compiler group | ||
grp_indices = grp['circuit_name'].map(circuit_name_to_index) | ||
|
||
# Plot relative error | ||
ax[0].bar( | ||
[grp_indices + i * bar_width for grp_indices in grp_indices], # Shift bars for each compiler | ||
grp['relative_error'], # Relative error data | ||
width=bar_width, | ||
label=key, | ||
color=color_map[key] | ||
) | ||
|
||
# Plot absolute error | ||
ax[1].bar( | ||
[grp_indices + i * bar_width for grp_indices in grp_indices], # Shift bars for each compiler | ||
grp['absoluate_error'], # Absolute error data | ||
width=bar_width, | ||
label=key, | ||
color=color_map[key] | ||
) | ||
|
||
# Step 9: Customize plots | ||
ax[0].set_title(f"Relative Error by Compiler on Circuits (Date: {latest_date})") | ||
ax[0].set_xlabel("Circuit Name") | ||
ax[0].set_ylabel("Relative Error") | ||
ax[0].set_xticks(x_positions) | ||
ax[0].set_xticklabels(circuit_names, rotation=75) | ||
ax[0].set_yscale("log") | ||
|
||
ax[1].set_title(f"Absolute Error by Compiler on Circuits (Date: {latest_date})") | ||
ax[1].set_xlabel("Circuit Name") | ||
ax[1].set_ylabel("Absolute Error") | ||
ax[1].set_xticks(x_positions) | ||
ax[1].set_xticklabels(circuit_names, rotation=75) | ||
ax[1].set_yscale("log") | ||
|
||
# Step 10: Add legend | ||
ax[0].legend(title="Compiler") | ||
ax[1].legend(title="Compiler") | ||
|
||
# Adjust layout and save the figure | ||
plt.tight_layout() | ||
filename = os.path.join(directory_of_this_file, "../latest_relative_absolute_errors_by_circuit.png") | ||
print(f"\n Saving plot to {filename}") | ||
fig.savefig(filename) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to understand why the relative errors are in the hundreds? What are the actual expectation values we're getting? Are they what we'd expect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we shouldn't use the same observable on all circuits if it is giving answers that don't seem meaningful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per @willzeng , we want to split off the customization of specific observables with the different benchmarks into a separate issue. Can complete this issue with the current all Z observable.