Skip to content

Commit

Permalink
return the figure and axis in the energy spectrum plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
JDTeske committed May 18, 2022
1 parent b742293 commit d61d6b1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions qopt/energy_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def vector_color_map(vectors: np.array):

def plot_energy_spectrum(hamiltonian: List[OperatorMatrix],
x_val: np.array,
x_label: str):
x_label: str,
ax=None):
"""
Calculates and plots the energy spectra of hamilton operators.
Expand All @@ -94,6 +95,9 @@ def plot_energy_spectrum(hamiltonian: List[OperatorMatrix],
x_label: str
Label of the x-axis.
ax: matplotlib pyplot axes
Instance of axes to plot the data in. Defaults to None.
"""
d = hamiltonian[0].shape[0]
eigenvalues = np.empty((len(hamiltonian), d))
Expand All @@ -103,9 +107,10 @@ def plot_energy_spectrum(hamiltonian: List[OperatorMatrix],
eigenvalues[i, :] = eig_val
eigenvectors[i, :, :] = np.abs(eig_vec)

fig, ax = plt.subplots()
if ax is None:
_, ax = plt.subplots()
for i in range(d):
ax.scatter(x=x_val, y=eigenvalues[:, i],
c=vector_color_map(eigenvectors[:, :, i]))
ax.set_xlabel(x_label)
return fig, ax
return ax

0 comments on commit d61d6b1

Please sign in to comment.