squlearn.qnn.lowlevel_qnn.LowLevelQNN

class squlearn.qnn.lowlevel_qnn.LowLevelQNN(parameterized_quantum_circuit: EncodingCircuitBase, observable: ObservableBase | list, executor: Executor, num_features: int, post_processing: Callable = None, caching=True)

Low-level, framework-agnostic evaluation engine for a parameterized quantum circuit together with one or more observables.

Evaluates the expectation value (f) and its derivatives with respect to the circuit’s input features (x), its trainable parameters (p), and the observable’s trainable parameters (p_op), as well as the corresponding values of the squared observable (fcc/dfccdx/dfccdp/dfccdop) and the variance family derived from those (var/dvardx/dvardp/dvardop). For Qiskit and PennyLane, arbitrary chained higher-order derivatives (e.g. dfdxdx, laplace) are supported as well; for Qiskit, derivatives can additionally be requested directly by parameter identity (e.g. llqnn.parameters[0]). Qulacs supports the base key set without chained derivatives. Any unsupported key combination raises NotImplementedError.

Parameters:
  • pqc (EncodingCircuitBase) – The parameterized quantum circuit.

  • observable (Union[ObservableBase, list]) – The observable(s) to measure.

  • executor (Executor) – The executor for the quantum circuit.

  • num_features (int) – Dimension of the input features.

  • post_processing (Callable) – Optional post processing function operating on the result dict after evaluate.

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

evaluate(x: float | ndarray, param: float | ndarray, param_obs: float | ndarray, *values: str | Parameter | Parameters | 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

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.

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

Resets 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.

set_shots(num_shots: int) → None

Sets the number of shots for the next evaluations.