Skip to content

Commit

Permalink
more plots
Browse files Browse the repository at this point in the history
  • Loading branch information
natestemen committed Jan 15, 2025
1 parent 39f4ed2 commit 8471ab3
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
Binary file added benchmarks/average_relative_error_over_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions benchmarks/scripts/plot_expval_benchmarks_over_time.py
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 benchmarks/scripts/plot_latest_expval_benchmarks_per_circuit.py
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)

0 comments on commit 8471ab3

Please sign in to comment.