Plotting functions

Module name: qmlt.numerical.plot

Code author: Josh Izaac <josh@xanadu.ai>

This module contains the functions required to plot the parameters of the numeric learner.

These are auxillary functions, it is recommended you instead use the plotting method available in the numeric learner, which will provide live plots of the training progress and monitored parameters. This can be turned on by passing the plot key to the hyperparameters dictionary.

For example,

>>> hyperparams = {'circuit': circuit,
               'log_every': 10,
               'plot': True
               }

Here, the integer value of log_every specifies at how many global steps the live plots should be updated. When the training is complete, the terminal will show the message

Training complete. Close the live plot window to exit.

To use auxillary plotting functions on a logfile:

>>> from qmlt.numerical import plot
>>> plot.plot_parameter(numerical, y='loss')

You can also chain together plots by passing through the returned axes, to display multiple parameters on one plot:

>>> ax = plot.plot_parameter(numerical, y='loss')
>>> ax = plot.plot_parameter(numerical, y='cost', ax=ax)
>>> ax = plot.plot_parameter(numerical, y='regul', ax=ax,
...                          legend=True, save_filename="test.png")

Finally, you can also automatically plot all parameters against the global step, on one figure as multiple subplots:

>>> plot.plot_all("numerical/logsNUM/log.csv")

Plotting functions

plot_parameter(logfile[, x, y, …]) Produces a line plot visualizing settings, training progress, or monitored parameters.
plot_all(logfile[, x, y, save_filename, …]) Produces a figure containing line plots visualizing settings, training progress, or monitored parameters.

Auxillary functions

_squareish(n) Factors an integer to two integers that closesly approximates a square
_plot(x, y[, ax, xlabel, ylabel]) Produces a line plot visualizing settings, training progress, or monitored parameters.

Code details

qmlt.numerical.plot._squareish(n)[source]

Factors an integer to two integers that closesly approximates a square

Parameters:n (int) – integer to factor.
Returns:the squareish integers.
Return type:tuple(int, int)
qmlt.numerical.plot._plot(x, y, ax=None, xlabel=None, ylabel=None, **plot_kw)[source]

Produces a line plot visualizing settings, training progress, or monitored parameters.

Parameters:
  • x (array) – the data to plot on the x-axis.
  • y (array) – the data to plot on the y-axis.
  • xlabel (str) – the x-axis label.
  • ylabel (str) – the y-axis label.
  • **plot_kw – additional keyword arguments to be passed to plt.plot.
Returns:

returns a tuple containing the figure and axes.

Return type:

axes

qmlt.numerical.plot.plot_parameter(logfile, x='global_step', y='loss', save_filename=None, ax=None, legend=False, style='ggplot', legend_kw=None, fig_kw=None, savefig_kw=None, **plot_kw)[source]

Produces a line plot visualizing settings, training progress, or monitored parameters.

Parameters:
  • logfile (str) – the location of the logfile containing the training progress and parameter values for each global step.
  • x (str) – the parameter to plot on the x-axis. By default the global step.
  • y (str) – the parameter to plot on the y-axis. By default the loss.
  • save_filename (str) – string containing the output image filename, including all relevant paths and file extensions. All image filetypes supported by matplotlib are supported here. By default, the plot is not saved.
  • ax (matplotlib.axes.Axes) – a matplotlib axes object. If none is provided, this is created automatically.
  • legend (bool) – If True, a legend is added containing the y parameter names.
  • style (str) – a supported matplotlib style sheet. To see the available styles on your system, please refer to the output of matplotlib.pyplot.style.available.
  • legend_kw (dict) – dictionary of additional matplotlib keyword arguments to apss to matplotlib.pyplot.legend.
  • fig_kw (dict) – dictionary of additional matplotlib keyword arguments to apss to matplotlib.figure.Figure.
  • savefig_kw (dict) – dictionary of additional matplotlib keyword arguments to apss to matplotlib.figure.Figure.savefig.
  • **plot_kw – additional keyword arguments to be passed to matplotlib.pyplot.plot.
Returns:

returns the plotting axes.

Return type:

matplotlib.axes.Axes

qmlt.numerical.plot.plot_all(logfile, x='global_step', y=None, save_filename=None, figax=None, style='ggplot', fig_kw=None, savefig_kw=None, **plot_kw)[source]

Produces a figure containing line plots visualizing settings, training progress, or monitored parameters.

Parameters:
  • filename (str) – string containing the output image filename, including all relevant paths and file extensions. All image filetypes supported by matplotlib are supported here.
  • logfile (str) – the location of the logfile containing the training progress and parameter values for each global step.
  • x (str) – the parameter to plot on the x-axes. By default the global step.
  • Sequence[str] (y) – the parameters to plot on the figure. By default, all will be plotted.
  • save_filename (str) – string containing the output image filename, including all relevant paths and file extensions. All image filetypes supported by matplotlib are supported here. By default, the plot is not saved.
  • figax (tuple) – a tuple containing the figure and the plotting axes. Created by default if not provided.
  • style (str) – a supported matplotlib style sheet. To see the available styles on your system, please refer to the output of matplotlib.pyplot.style.available.
  • fig_kw (dict) – dictionary of additional matplotlib keyword arguments to apss to matplotlib.figure.Figure.
  • savefig_kw (dict) – dictionary of additional matplotlib keyword arguments to apss to matplotlib.figure.Figure.savefig.
  • **plot_kw – additional keyword arguments to be passed to matplotlib.pyplot.plot.
Returns:

returns a tuple containing the figure and the plotting axes.

Return type:

tuple