Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
NVIDIA
GitHub Repository: NVIDIA/cuda-q-academic
Path: blob/main/hybrid-workflows/auxiliary_files/labs_plotting.py
1127 views
1
import matplotlib.pyplot as plt
2
import numpy as np
3
4
def plot_energy_distributions(results_1, results_2=None, labels=("Experiment 1", "Experiment 2"), n_val=0):
5
"""
6
Plots histograms of final energy distributions.
7
8
Args:
9
results_1 (array-like): List of energy values from first experiment.
10
results_2 (array-like, optional): List of energy values from second experiment.
11
labels (tuple): Labels for the legend.
12
n_val (int): Sequence length (for title).
13
"""
14
plt.figure(figsize=(10, 6))
15
16
# Determine common bin edges for consistent alignment
17
all_data = results_1
18
if results_2 is not None:
19
all_data = np.concatenate([results_1, results_2])
20
21
# Calculate 15 bins based on the full range of data
22
bins = np.histogram_bin_edges(all_data, bins=15)
23
24
# Plot first set
25
plt.hist(results_1, bins=bins, alpha=0.6, label=labels[0], color='blue', edgecolor='black')
26
27
# Plot second set if provided
28
if results_2 is not None:
29
plt.hist(results_2, bins=bins, alpha=0.6, label=labels[1], color='green', edgecolor='black')
30
31
plt.title(f"LABS Memetic Optimization Results (N={n_val})\nFinal Population Cost Distribution")
32
plt.xlabel("Energy Cost (Lower is Better)")
33
plt.ylabel("Frequency in Population")
34
plt.legend()
35
plt.grid(True, alpha=0.3)
36
plt.tight_layout()
37
plt.show()
38