TrainingFuncs module

TrainingFuncs.import_circuit(circuit: str)[source]
TrainingFuncs.save_results_params(results_and_params, path: str)[source]

Saves a dictionary containing n_qubits, indices, scaler, weights, inputs, and targets to the specified path

Parameters:

results_and_params (dict): A dictionary containing n_qubits, indices, scaler, weights, inputs, and targets path (str): path to which the dictionary will be saved as “dict”

TrainingFuncs.scale_data(train, test, train_size: int, test_size: int, n_qubits: int, scaler_min: float = 0.2, scaler_max: float = 0.8)[source]

Scales the data for embedding in the circuit. By default, scales all values to be between 0.2 and 0.8.

Parameters:

train (numpy.ndarray): 2-D array containing the training data. The shape of the data is (train_size, n_qubits) test (numpy.ndarray): 2-D array containing the testing data. The shape of the data is (test_size, n_qubits) train_size (int): the length of the training set test_size (int): the length of the training set n_qubits (int): number of qubits in the circuit scaler_min (float)= the target minimum value for all values in the full dataset scaler_max (float)= the target maximum value for all values in the full dataset

Returns:

final_train (numpy.ndarray): 2-D array containing the scaled training data. The shape of the data is (train_size, n_qubits) final_test (numpy.ndarray): 2-D array containing the scaled testing data. The shape of the data is (test_size, n_qubits) scaler: The initialized and fitted scaler

TrainingFuncs.split_train_test(data, n_qubits: int, train_ratio: float = 0.6666666666666666, random: bool = False, seed: int = 0)[source]

Splits data into two sub-datsets: one for training and one for testing.

Parameters:

data (numpy.array): 1-D data to be split n_qubits (int): number of qubits in the circuit train_ratio (float): fraction of full data to be used for training. Must be less than 1. random (bool): if False (default), the data is split chronologically e.g., the first 2/3 of the data are used for training and the last 1/3 are used for testing. If True, the data is split randomly. seed (int): seed for np.random

Returns:

train (numpy.ndarray): 2-D array containing the training data. The shape of the data is (train_size//n_qubits, n_qubits) test (numpy.ndarray): 2-D array containing the testing data. The shape of the data is (test_size//n_qubits, n_qubits) train_size: the total number of values in train divided by n_qubits (i.e., the number of n_qubits-sized “groups” in train) test_size: the total number of values in test divided by n_qubits (i.e., the number of n_qubits-sized “groups” in test) train_ratio: fraction of full data to be used for training. Must be less than 1. indices: gives the index of each “group” in the original data. If random is False, indices is just the range of integers from 0 to the number of groups.

TrainingFuncs.train_model(train, test, weights, circuit, n_qubits: int, max_steps: int, epochs: int, loss_function='mean square error', learning_rate=0.1, bool_plot=False, save_plot: str | None = None)[source]

Trains the circuit using the “train” dataset and according to the specified hyperparameters.

Parameters:

train (numpy.ndarray): 2-D array containing the scaled training data. Used to train the model and update weights. test (numpy.ndarray): 2-D array containing the scaled testing data. Used to check progress of training. weights: parameters of the circuit updated by the optimizer to change output. circuit (pennylane.qnode.QNode): The circuit which encodes the training data and, according to its parameters, returns an output to be compared with “target” data n_qubits (int): The number of qubits in the quantum circuit max_steps (int): The number of times the weights should be updated before the entire dataset has been evaluated. max_epochs (int): The maximum number of times the full dataset should be evaluated. loss_function: the function to evaluate the performance of the model i.e., the function to be minimized optimizer: The algorithm that adjusts the weights in the circuit. learning_rate: the learning rate of the optimizer. bool_plot (bool): If True, will plot the loss as a function of epochs once training is completed save_plot (str): If None, the plot will not be saved, otherwise it will be saved as the string entered

Returns:

weights: the most-recently updated weights that were obtained when training is complete x_t: input values for testing target_y_t: target values for training