paddle_quantum.ansatz.vans

The source file of the variable ansatz.

paddle_quantum.ansatz.vans.cir_decompose(cir, trainable=False)

Decompose all layers of circuit into gates, and make all parameterized gates trainable if needed

Parameters:
  • cir (Circuit) – Target quantum circuit.

  • trainable (bool | None) – whether the decomposed parameterized gates are trainable

Returns:

A quantum circuit with same structure and parameters but all layers are decomposed into Gates.

Return type:

Circuit

Note

This function does not support customized gates, such as oracle and control-oracle.

class paddle_quantum.ansatz.vans.Inserter

Bases: object

Class for block insertion for the circuit.

classmethod insert_identities(cir, insert_rate, epsilon)

Insert identity blocks to the current circuit, according to the insert rate.

Parameters:
  • cir (Circuit) – Quantum circuit to be simplified.

  • insert_rate (float) – Rate of number of inserted blocks.

  • epsilon (float) – Range of random initialization of parameters.

Returns:

Inserted circuit.

Return type:

Circuit

class paddle_quantum.ansatz.vans.Simplifier

Bases: object

Class for circuit simplification.

classmethod simplify_circuit(cir, zero_init_state=True)

Combine all simplifications together.

Parameters:
  • cir (Circuit) – Quantum circuit.

  • zero_init_state (bool | None) – Whether the initial state is \(|0\rangle\). Defaults to True.

Returns:

Simplified circuit.

Return type:

Circuit

class paddle_quantum.ansatz.vans.VAns(n, loss_func, *loss_func_args, epsilon=0.1, insert_rate=2, iter=100, iter_out=10, LR=0.1, threshold=0.002, accept_wall=100, zero_init_state=True)

Bases: object

Class of Variable Ansatz.

User can initialize this class to find variational circuit to construct QML model.

Note

The loss function passed in must have the quantum circuit as its first parameter.

Parameters:
  • n (int) – Number of qubits.

  • loss_func (Callable[[Circuit, Any], Tensor]) – Loss function that evaluate the loss of circuit.

  • loss_func_args (Any) – Parameters of loss function other than circuit.

  • epsilon (float) – Range of random initialization of parameters. Defaults to 0.1.

  • insert_rate (float) – Rate of number of inserted blocks. Defaults to 2.

  • iter (int) – Number of iterations of optimizer. Defaults to 100.

  • iter_out (int) – Number of insert-simplify cycles. Defaults to 10.

  • LR (float) – Learning rate. Defaults to 0.1.

  • threshold (float) – Tolerance of incread loss after deleting one qubit gate. Defaults to 0.002.

  • accpet_wall – Percentage of probability of accepting the circuit in current insert-simplify cycle. Defaults to 100.

  • zero_init_state (bool) – Whether the initial state is \(|0\rangle\). Defaults to True.

train()

Train the quantum circuit.

Returns:

The quantum circuit with the lowest loss.

Return type:

Circuit

optimization(cir)

Optimize circuit parameters with loss function.

Parameters:

cir (Circuit) – Quantum circuit.

Returns:

Optimized loss.

Return type:

float

delete_gates(cir, loss)

Remove single qubit gates if the loss decreases or increases within a threshold.

Parameters:
  • cir (Circuit) – Quantum circuit.

  • loss (float) – Current loss.

Returns:

Circuit after deleting unnecessary gates.

Return type:

Circuit