paddle_quantum.fisher
The source file of the class for the fisher information.
- class paddle_quantum.fisher.QuantumFisher(cir)
Bases:
object
Quantum fisher information (QFI) & related calculators.
- Parameters:
cir (Circuit) – Parameterized quantum circuits requiring calculation of quantum Fisher information.
Note
This class does not fit the situation when parameters among gates are correlated, such as control-gates.
- get_qfisher_matrix()
Use parameter shift rule of order 2 to calculate the matrix of QFI.
- Returns:
Matrix of QFI.
- Return type:
ndarray
import paddle from paddle_quantum.ansatz import Circuit from paddle_quantum.fisher import QuantumFisher cir = Circuit(1) zero = paddle.zeros([1], dtype="float64") cir.ry(0, param=zero) cir.rz(0, param=zero) qf = QuantumFisher(cir) qfim = qf.get_qfisher_matrix() print(f'The QFIM at {cir.param.tolist()} is \n {qfim}.')
The QFIM at [0.0, 0.0] is [[1. 0.] [0. 0.]].
- get_qfisher_norm(direction, step_size=0.01)
Use finite difference rule to calculate the projection norm of QFI along particular direction.
- Parameters:
direction (ndarray) – A direction represented by a vector.
step_size (float | None) – Step size of the finite difference rule. Defaults to
0.01
。
- Returns:
Projection norm.
- Return type:
float
import paddle from paddle_quantum.ansatz import Circuit from paddle_quantum.fisher import QuantumFisher cir = Circuit(2) zero = paddle.zeros([1], dtype="float64") cir.ry(0, param=zero) cir.ry(1, param=zero) cir.cnot(qubits_idx=[0, 1]) cir.ry(0, param=zero) cir.ry(1, param=zero) qf = QuantumFisher(cir) v = [1,1,1,1] qfi_norm = qf.get_qfisher_norm(direction=v) print(f'The QFI norm along {v} at {cir.param.tolist()} is {qfi_norm:.7f}')
The QFI norm along [1, 1, 1, 1] at [0.0, 0.0, 0.0, 0.0] is 6.0031546
- get_eff_qdim(num_param_samples=4, tol=None)
Calculate the effective quantum dimension, i.e. the maximum rank of QFI matrix in the whole parameter space.
- Parameters:
num_param_samples (int | None) – Number of samples to estimate the dimension. Defaults to
4
.tol (float | None) – Minimum tolerance of the singular values to be 0. Defaults to
None
, with the same meaning as innumpy.linalg.matrix_rank()
.
- Returns:
Effective quantum dimension of the quantum circuit.
- Return type:
int
import paddle from paddle_quantum.ansatz import Circuit from paddle_quantum.fisher import QuantumFisher cir = Circuit(1) zero = paddle.zeros([1], dtype="float64") cir.rz(0, param=zero) cir.ry(0, param=zero) qf = QuantumFisher(cir) print(cir) print(f'The number of parameters of -Rz-Ry- is {len(cir.param.tolist())}') print(f'The effective quantum dimension of -Rz-Ry- is {qf.get_eff_qdim()}')
--Rz(0.000)----Ry(0.000)-- The number of parameters of -Rz-Ry- is 2 The effective quantum dimension of -Rz-Ry- is 1
- get_qfisher_rank(tol=None)
Calculate the rank of the QFI matrix.
- Parameters:
tol (float | None) – Minimum tolerance of the singular values to be 0. Defaults to
None
, with the same meaning as innumpy.linalg.matrix_rank()
.- Returns:
Rank of the QFI matrix.
- Return type:
int
- class paddle_quantum.fisher.ClassicalFisher(model, num_thetas, num_inputs, model_type='quantum', **kwargs)
Bases:
object
Classical fisher information (CFI) & related calculators.
- Parameters:
model (Layer) – Instance of the classical or quantum neural network model.
num_thetas (int) – Number of the parameter sets.
num_inputs (int) – Number of the input samples.
model_type (str) – Model type is
'classical'
or'quantum'
. Defaults to'quantum'
.**kwargs (List[int] | int | str) –
including
size: list of sizes of classical NN units
num_qubits: number of qubits of quantum NN
depth: depth of quantum NN
encoding:
IQP
orre-uploading
encoding of quantum NN
- Raises:
ValueError – Unsupported encoding.
ValueError – Unsupported model type.
- get_gradient(x)
Calculate the gradients with respect to the variational parameters of the output layer.
- Parameters:
x (ndarray | Tensor) – Input samples.
- Returns:
Gradient with respect to the variational parameters of the output layer with shape [num_inputs, dimension of the output layer, num_thetas].
- Return type:
ndarray
- get_cfisher(gradients)
Use the Jacobian matrix to calculate the CFI matrix.
- Parameters:
gradients (ndarray) – Gradients with respect to the variational parameter of the output layer.
- Returns:
CFI matrix with shape [num_inputs, dimension of the output layer, num_theta].
- Return type:
ndarray
- get_normalized_cfisher()
Calculate the normalized CFI matrix.
- Returns:
contains elements
CFI matrix with shape [num_inputs, num_theta, num_theta]
its trace
- Return type:
Tuple[ndarray, float]
- get_eff_dim(normalized_cfisher, list_num_samples, gamma=1)
Calculate the classical effective dimension.
- Parameters:
normalized_cfisher (ndarray) – Normalized CFI matrix.
list_num_samples (List[int]) – List of different numbers of samples.
gamma (int | None) – A parameter in the effective dimension. Defaults to
1
.
- Returns:
Classical effective dimensions for different numbers of samples.
- Return type:
List[int]