paddle_quantum.qml.qsann

The Quantum Self-Attention Neural Network (QSANN) model.

paddle_quantum.qml.qsann.generate_observable(num_qubits, num_terms)

Generate the observables to observe the quantum state.

Parameters:
  • num_qubits (int) – The number of the qubits.

  • num_terms (int) – The number of the generated observables.

Returns:

Return the generated observables.

Return type:

List[list]

class paddle_quantum.qml.qsann.QSANN(num_qubits, len_vocab, num_layers, depth_ebd, depth_query, depth_key, depth_value)

Bases: Layer

The class of the quantum self-attention neural network (QSANN) model.

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

  • len_vocab (int) – The length of the vocabulary.

  • num_layers (int) – The number of the self-attention layers.

  • depth_ebd (int) – The depth of the embedding circuit.

  • depth_query (int) – The depth of the query circuit.

  • depth_key (int) – The depth of the key circuit.

  • depth_value (int) – The depth of the value circuit.

forward(batch_text)

The forward function to execute the model.

Parameters:

batch_text (List[List[int]]) – The batch of input texts. Each of them is a list of int.

Returns:

Return a list which contains the predictions of the input texts.

Return type:

List[Tensor]

paddle_quantum.qml.qsann.deal_vocab(vocab_path)

Get the map from the word to the index by the input vocabulary file.

Parameters:

vocab_path (str) – The path of the vocabulary file.

Returns:

Return the map from the word to the corresponding index.

Return type:

Dict[str, int]

class paddle_quantum.qml.qsann.TextDataset(file_path, word2idx, pad_size=0)

Bases: Dataset

The class to implement the text dataset.

Parameters:
  • file_path (str) – The dataset file.

  • word2idx (dict) – The map from the word to the corresponding index.

  • pad_size (int) – The size pad the text sequence to. Defaults to 0, which means no padding.

paddle_quantum.qml.qsann.build_iter(dataset, batch_size, shuffle=False)

Build the iteration of the batch data.

Parameters:
  • dataset (TextDataset) – The dataset to be built.

  • batch_size (int) – The number of the data in a batch.

  • shuffle (bool) – Whether to randomly shuffle the order of the data. Defaults to False.

Returns:

The built iteration which contains the batches of the data.

Return type:

list

paddle_quantum.qml.qsann.train(model_name, dataset, num_qubits, num_layers, depth_ebd, depth_query, depth_key, depth_value, batch_size, num_epochs, learning_rate=0.01, saved_dir='', using_validation=False, early_stopping=1000)

The function of training the QSANN model.

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

  • dataset (str) – The dataset used to train the model, which should be a directory.

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

  • num_layers (int) – The number of the self-attention layers.

  • depth_ebd (int) – The depth of the embedding circuit.

  • depth_query (int) – The depth of the query circuit.

  • depth_key (int) – The depth of the key circuit.

  • depth_value (int) – The depth of the value circuit.

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

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

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

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

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

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

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

Evaluate the model.

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

  • data_loader (list) – 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.qsann.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 (list) – The dataloader of the test dataset.

paddle_quantum.qml.qsann.inference(text, model_path, vocab_path, classes, num_qubits, num_layers, depth_ebd, depth_query, depth_key, depth_value)

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

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

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

  • vocab_path (str) – The path of the vocabulary file.

  • classes (List[str]) – The classes of all the labels.

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

  • num_layers (int) – The number of the self-attention layers.

  • depth_ebd (int) – The depth of the embedding circuit.

  • depth_query (int) – The depth of the query circuit.

  • depth_key (int) – The depth of the key circuit.

  • depth_value (int) – The depth of the value circuit.

Returns:

Return the class which the model predicted.

Return type:

str