paddle_quantum.qml.vsql

The Variational Shadow Quantum Learning (VSQL) model.

paddle_quantum.qml.vsql.image_preprocess(image, num_qubits)

Normalize the input image. Flatten them and make them to normalized vectors.

Parameters:
  • image (ndarray) – The input image.

  • num_qubits (int) – The number of the qubits, which decides the dimension of the vector.

Returns:

Return the preprocessed image which is a normalized vector.

Return type:

ndarray

class paddle_quantum.qml.vsql.ImageDataset(file_path, num_samples=0, transform=None)

Bases: Dataset

The class to implement the image dataset.

Parameters:
  • file_path (str) – The path of the dataset file, each line of which represents a piece of data. Each line contains the file path and label of the image, separated by tabs.

  • num_samples (int) – The number of the samples. It is the number of the data that the dataset contains. Defaults to 0 , which means contains all data in the dataset file.

  • transform (Callable | None) – The function to transform the image. Defaults to None , which means do nothing on the image.

paddle_quantum.qml.vsql.generate_observable(start_idx, num_shadow)

Generate the observable to measure the quantum states.

Parameters:
  • start_idx (int) – The start index of the qubits.

  • num_shadow (int) – The number of the qubits which the shadow circuit contains.

Returns:

Return the generated observable.

Return type:

Hamiltonian

class paddle_quantum.qml.vsql.VSQL(num_qubits, num_shadow, num_classes, depth=1)

Bases: Layer

The class of the variational shadow quantum learning (VSQL) model.

The details can be referred to https://ojs.aaai.org/index.php/AAAI/article/view/17016 .

Parameters:
  • num_qubits (int) – The number of the qubits which the quantum circuit contains.

  • num_shadow (int) – The number of the qubits which the shadow circuit contains.

  • num_classes (int) – The number of the classes which the model will classify.

  • depth (int) – The depth of the quantum circuit. Defaults to 1 .

forward(batch_input)

The forward function.

Parameters:

batch_input (List[Tensor]) – The input of the model. It’s shape is \((\text{batch_size}, 2^{\text{num_qubits}})\) .

Returns:

Return the output of the model. It’s shape is \((\text{batch_size}, \text{num_classes})\) .

Return type:

Tensor

paddle_quantum.qml.vsql.train(model_name, num_qubits, num_shadow, classes, batch_size, num_epochs, depth=1, dataset='MNIST', saved_dir='', learning_rate=0.01, using_validation=False, num_workers=0, early_stopping=1000, num_train=0, num_dev=0, num_test=0)

The function of training the VSQL model.

Parameters:
  • model_name (str) – The name of the model. It is the filename of the saved model.

  • num_qubits (int) – The number of the qubits which the quantum circuit contains.

  • num_shadow (int) – The number of the qubits which the shadow circuit contains.

  • classes (list) – The classes of handwrite digits to be predicted. Defaults to None , which means predict all the classes.

  • batch_size (int) – The size of the batch samplers.

  • num_epochs (int) – The number of the epochs to train the model.

  • depth (int) – The depth of the quantum circuit. Defaults to 1 .

  • dataset (str) – The dataset used to train the model, which should be a directory. Defaults to 'MNIST' , which means using the built-in MNIST dataset.

  • saved_dir (str) – The directory to saved the trained model and the training log. Defaults to use the current path.

  • learning_rate (float) – The learning rate used to update the parameters. Defaults to 0.01 .

  • using_validation (bool) – If the datasets contains the validation dataset. Defaults to False , which means the validation dataset is not included.

  • num_workers (int) – The number of the subprocess to load data, 0 for no subprocess used and loading data in main process. Defaults to 0 .

  • early_stopping (int) – Number of epochs with no improvement after which training will be stopped. Defaults to 1000 .

  • num_train (int) – The number of the data in the training dataset. Defaults to 0 , which will use all training data.

  • num_dev (int) – The number of the data in the test dataset. Defaults to 0 , which will use all validation data.

  • num_test (int) – The number of the data in the test dataset. Defaults to 0 , which will use all test data.

paddle_quantum.qml.vsql.evaluate(model, data_loader)

Evaluate the model.

Parameters:
  • model (Layer) – The trained model to be evaluated.

  • data_loader (DataLoader) – The dataloader of the data used to evaluate the model.

Returns:

Return the average loss and accuracy in the data of the input dataloader.

Return type:

Tuple[float, float]

paddle_quantum.qml.vsql.test(model, model_path, test_loader)

Use the test dataset to test the model.

Parameters:
  • model (Layer) – The model to be tested.

  • model_path (str) – The file path of the models’ file.

  • test_loader (DataLoader) – The dataloader of the test dataset.

paddle_quantum.qml.vsql.inference(image_path, is_dir, model_path, num_qubits, num_shadow, classes, depth=1)

The inference function. Using the trained model to predict new data.

Parameters:
  • image_path (str) – The path of the image to be predicted.

  • is_dir (bool) – Whether the input image_path is a directory. If it is a directory, the model will predict all images under it.

  • model_path (str) – The path of the model file.

  • num_qubits (int) – The number of the qubits which the quantum circuit contains.

  • num_shadow (int) – The number of the qubits which the shadow circuit contains.

  • classes (list) – The classes which the input image belongs to.

  • depth (int) – The depth of the quantum circuit. Defaults to 1 .

Returns:

Return the class the model predicted and the probability of each class.

Return type:

Tuple[int, list]