squlearn.qnn.lowlevel_qnn_qiskit.LowLevelQNNQiskit

class squlearn.qnn.lowlevel_qnn_qiskit.LowLevelQNNQiskit(parameterized_quantum_circuit: EncodingCircuitBase, operator: ObservableBase | list, executor: Executor, caching=True, primitive: str | None = None)

Low level implementation of QNNs and its derivatives based on Qiskit.

Parameters:
  • pqc (EncodingCircuitBase) – parameterized quantum circuit in encoding circuit format

  • operator (Union[ObservableBase,list]) – Operator that is used in the expectation value of the QNN. Can be a list for multiple outputs.

  • executor (Executor) – Executor that is used for the evaluation of the QNN

  • caching – Caching of the result for each x, param, param_op combination (default = True)

  • primitive (str) – Primitive that is used for the evaluation of the QNN. Possible values are "estimator" or "sampler". If None, the primitive is set according to the executor. (default = None)

Attributes:

num_qubits

Number of qubits of the QNN

Type:

int

num_features

Dimension of features of the PQC

Type:

int

num_parameters

Number of trainable parameters of the PQC

Type:

int

num_operator

Number of outputs

Type:

int

num_parameters_observable

Number of trainable parameters of the expectation value operator

Type:

int

multiple_output

True if multiple outputs are used

Type:

bool

parameters

Parameter vector of the PQC

Type:

ParameterVector

features

Feature vector of the PQC

Type:

ParameterVector

parameters_operator

Parameter vector of the cost operator

Type:

ParameterVector

Methods:

evaluate(x: float | ndarray, param: float | ndarray, param_op: float | ndarray, *values: str | Expec | ParameterVector | ParameterVectorElement | tuple) dict

General function for evaluating the output of derivatives of the QNN.

Evaluation works for given combination of input features x and parameters param and param_op. The function includes caching of results

If x, param, and/or param_op are given as a nested list (for example multiple sets of parameters), the values are returned in a nested list.

Parameters:
  • x (np.ndarray) – Values of the input feature data.

  • param (np.ndarray) – Parameter values of the PQC parameters

  • param_op (np.ndarray) – Parameter values of the operator parameters

  • values – Derivatives (or values) of the QNN that are evaluated. Higher order derivatives are given as tuples of parameters or vectors.

Results:

Returns a dictionary with the computed values. The keys of the dictionary are given by the entries in the values tuple

evaluate_dfdop(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the derivative of the QNN with respect to the operator’s parameters.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivative of the the QNN with respect to the operator’s parameters.

evaluate_dfdopdx(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the derivative of the QNN with respect to the operator’s parameters and x.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivative of the QNN with respect to the operator’s parameters and x

evaluate_dfdp(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the derivative of the QNN with respect to the PQC’s parameters.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivative of the the QNN with respect to the PQC’s parameters.

evaluate_dfdpdx(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the derivative of the QNN with respect to the PQC’s parameters and x.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivative of the QNN with respect to the PQC’s parameters and x

evaluate_dfdx(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates derivatives of the QNN with respect to x.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivatives of the the QNN with respect to x

evaluate_dfdxdx(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates second order derivatives of the QNN with respect to x.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated second order derivatives of the the QNN with respect to x

evaluate_diff_tuple(x: float | ndarray, param: float | ndarray, param_op: float | ndarray, diff_tuple) float | ndarray

Evaluate the given tuple of derivatives of the PQC.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

  • diff_tuple – Tuple with parameters used in the differentiation

Returns:

Differentiated values of the QNN

evaluate_f(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the QNN

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Values from the QNN

evaluate_from_string(x: float | ndarray, param: float | ndarray, param_op: float | ndarray, input_string: str) float | ndarray

Evaluate the given PQC from an input string

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

  • input_string (str) – Input string that determines the evaluated value(s)

Returns:

Values from the QNN defined by the string

evaluate_laplace(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates Laplacian of the QNN for x.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated Laplacian of the the QNN for x

evaluate_laplace_dop(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the derivative of the Laplacian with respect to the operator’s parameters.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivative of the Laplacian with respect to the operator’s parameters

evaluate_laplace_dp(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the derivative of the Laplacian with respect to the PQC’s parameters.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated derivative of the Laplacian with respect to the PQC’s parameters

evaluate_probabilities(x: float | ndarray, param: float | ndarray)

Evaluate the probabilities of the encoding circuit / PQC.

The function only works with the QuantumInstance executer.

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

Returns:

List of probabilities stored in the SparseVectorStateFn format. (dictionary can be obtained by .to_dict_fn() or to_dict_fn().primitive)

evaluate_var(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the variance (<OO>-<O>^2) of the QNN

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated variance of the the QNN

evaluate_variance(x: float | ndarray, param: float | ndarray, param_op: float | ndarray) float | ndarray

Evaluates the variance (<OO>-<O>^2) of the QNN

Parameters:
  • x (Union[float,np.ndarray]) – Input data values

  • param (Union[float,np.ndarray]) – Parameter values of the PQC

  • param_op (Union[float,np.ndarray]) – Parameter values of the operator

Returns:

Evaluated variance of the the QNN

get_optree_from_expec(input_expec: Expec)

Returns the OpTree expression for the given Expec object.

Parameters:

input_expec (Expec) – Expec object from which the OpTree is obtained

Returns:

OpTree structure created from the Expec object.

get_optree_from_string(input_string: str)

Return the OpTree expression of the given PQC

Parameters:

input_string (str) – String from which the OpTree is obtained.

Returns:

OpTree structure created from the string.

get_params(deep: bool = True) dict

Returns the dictionary of the hyper-parameters of the QNN.

In case of multiple outputs, the hyper-parameters of the operator are prefixed with op0__, op1__, etc.

get_shots() int

Getter for the number of shots.

Returns:

Returns the number of shots that are used for the current evaluation.

gradient(x: float | ndarray, param: float | ndarray, param_obs: float | ndarray)

Return the gradient wrt. trainable parameters of the QNN.

Parameters:
  • x (Union[float, np.ndarray]) – Input data.

  • param (Union[float, np.ndarray]) – Parameters of the PQC.

  • param_obs (Union[float, np.ndarray]) – Parameters of the observable.

Returns:

Gradient of the QNN.

Return type:

np.ndarray

reset_shots() None

Function for resetting the number of shots to the initial ones

set_params(**params) None

Sets the hyper-parameters of the QNN

In case of multiple outputs, the hyper-parameters of the operator are prefixed with op0__, op1__, etc.

Parameters:

params – Hyper-parameters that are adjusted, e.g. num_qubits=4

set_shots(num_shots: int) None

Sets the number shots for the next evaluations.

Parameters:

num_shots (int) – Number of shots that are set