modules.applications.qml.generative_modeling.training.qgan.QGAN

class QGAN

Bases: TrainingGenerative

Class for QGAN

__init__()

Constructor method.

Methods

__init__()

Constructor method.

get_available_submodule_options()

Gets the list of available options.

get_available_submodules(option)

If the module has submodules depending on certain options, this method should adjust the submodule_options accordingly.

get_default_submodule(option)

Raises ValueError as this module has no submodules.

get_depending_parameters(option, config)

If the module has parameters depending on certain options, this method should return the parameters for the given option.

get_parameter_options()

Returns the configurable settings for this circuit.

get_requirements()

Returns requirements of this module.

get_submodule(option)

Submodule is instantiated according to the information given in self.sub_options.

kl_divergence(pmf_model, pmf_target)

This function calculates the Kullback-Leibler divergence, that is used as a loss function.

mmd(pmf_model, pmf_target)

This function calculates the maximum mean discrepancy, that is used as a loss function.

nll(pmf_model, pmf_target)

This function calculates th negative log likelihood, that is used as a loss function.

postprocess(input_data, config, **kwargs)

Perform the actual training of the machine learning model.

preprocess(input_data, config, **kwargs)

Essential method for the benchmarking process.

sample_from_pmf(pmf, n_shots)

This function samples from the probability mass function generated by the quantum circuit.

setup_training(input_data, config)

Sets up the training configuration.

start_training(input_data, config, **kwargs)

This function starts the training of the QGAN.

class Config

Bases: TypedDict

Attributes of a valid config.

epochs: int
batch_size: int
learning_rate_generator: int
learning_rate_discriminator: int
device: str
loss: str
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
class Timing

Bases: object

This module is an abstraction of time measurement for both CPU and GPU processes.

start_recording_cpu() None

This is a function to start time measurement on the CPU.

start_recording_gpu() None

This is a function to start time measurement on the GPU.

stop_recording_cpu() float

This is a function to stop time measurement on the CPU.

.return: Elapsed time in milliseconds

stop_recording_gpu() float

This is a function to stop time measurement on the GPU.

Returns:

Elapsed time in milliseconds

get_available_submodule_options() list

Gets the list of available options.

Returns:

List of module options

get_available_submodules(option: list) list

If the module has submodules depending on certain options, this method should adjust the submodule_options accordingly.

Parameters:

option -- List of chosen options

Returns:

List of available submodules

get_default_submodule(option: str) Core

Raises ValueError as this module has no submodules.

Parameters:

option -- Option name

Raises:

ValueError -- If called, since this module has no submodules

get_depending_parameters(option: str, config: dict) dict

If the module has parameters depending on certain options, this method should return the parameters for the given option.

Parameters:
  • option -- The chosen option

  • config -- Current config dictionary

Returns:

The parameters for the given option

get_parameter_options() dict

Returns the configurable settings for this circuit.

Returns:

Configuration settings for QGAN


return {
"epochs": {

"values": [2, 100, 200, 10000], "description": "How many epochs do you want?"

}, "batch_size": {

"values": [10, 20, 100, 2000], "description": "What batch size do you want?"

}, "learning_rate_generator": {

"values": [0.1, 0.2], "description": "What learning rate do you want to set for the generator?"

}, "learning_rate_discriminator": {

"values": [0.1, 0.05], "description": "What learning rate do you want to set for the discriminator?"

}, "device": {

"values": ["cpu", "gpu"], "description": "Where do you want to run the discriminator?"

}, "pretrained": {

"values": [True, False], "description": "Do you want to use parameters of a pretrained model?"

}, "loss": {

"values": ["KL", "NLL"], "description": "Which loss function do you want to use?"

}

}

static get_requirements() list[dict]

Returns requirements of this module.

Returns:

List of dict with requirements of this module

get_submodule(option: str) Core

Submodule is instantiated according to the information given in self.sub_options. If self.sub_options is None, get_default_submodule is called as a fallback.

Parameters:

option -- String with the options

Returns:

Instance of a module

kl_divergence(pmf_model: ndarray, pmf_target: ndarray) ndarray

This function calculates the Kullback-Leibler divergence, that is used as a loss function.

Parameters:
  • pmf_model -- Probability mass function generated by the quantum circuit

  • pmf_target -- Probability mass function of the target distribution

Returns:

Kullback-Leibler divergence

mmd(pmf_model: ndarray, pmf_target: ndarray) ndarray

This function calculates the maximum mean discrepancy, that is used as a loss function.

Parameters:
  • pmf_model -- Probability mass function generated by the quantum circuit

  • pmf_target -- Probability mass function of the target distribution

Returns:

Maximum mean discrepancy

nll(pmf_model: ndarray, pmf_target: ndarray) ndarray

This function calculates th negative log likelihood, that is used as a loss function.

Parameters:
  • pmf_model -- Probability mass function generated by the quantum circuit

  • pmf_target -- Probability mass function of the target distribution

Returns:

Negative log likelihood

postprocess(input_data: dict, config: dict, **kwargs) tuple[dict, float]

Perform the actual training of the machine learning model.

Parameters:
  • input_data -- Collected information of the benchmarking process

  • config -- Training settings

  • kwargs -- Optional additional arguments

Returns:

Training results and the postprocessing time

preprocess(input_data: any, config: dict, **kwargs) tuple[any, float]

Essential method for the benchmarking process. This is always executed before traversing down to the next module, passing the data returned by this function.

Parameters:
  • input_data -- Data for the module, comes from the parent module if that exists

  • config -- Config for the module

  • kwargs -- Optional keyword arguments

Returns:

The output of the preprocessing and the time it took to preprocess

sample_from_pmf(pmf: ndarray, n_shots: int) ndarray

This function samples from the probability mass function generated by the quantum circuit.

Parameters:
  • pmf -- Probability mass function generated by the quantum circuit

  • n_shots -- Number of shots

Returns:

Number of counts in the 2**n_qubits bins

setup_training(input_data: dict, config: dict) None

Sets up the training configuration.

Parameters:
  • input_data -- dictionary with the variables from the circuit needed to start the training

  • config -- Configurations for the QGAN training.

start_training(input_data: dict, config: dict, **kwargs: dict) dict

This function starts the training of the QGAN.

Parameters:
  • input_data -- Dictionary with the variables from the circuit needed to start the training

  • config -- Training settings

  • kwargs -- Optional additional arguments

Returns:

Dictionary including the solution