qiboml.interfaces#
Submodules#
qiboml.interfaces.pytorch#
qiboml.interfaces.keras#
qiboml.interfaces.circuit_tracer#
- class qiboml.interfaces.circuit_tracer.CircuitTracer(circuit_structure: List[Union[Circuit, QuantumEncoding, Callable]], derivation_mode: str = 'forward')[source]#
Bases:
ABCWrapper to the circuit structure that takes care of tracing the circuit construction. In particular, it computes both the Jacobian wrt the parameters and the inputs of the operations that construct the circuits.
- circuit_structure: List[Union[Circuit, QuantumEncoding, Callable]]#
- abstract property engine#
The calculation engine used.
- abstract static jacfwd(f: Callable, argnums: Union[int, Tuple[int]])[source]#
The jacobian functional in forward derivation mode.
- abstract static jacrev(f: Callable, argnums: Union[int, Tuple[int]])[source]#
The jacobian functional in reverse derivation mode.
- _compute_jacobian_functional(circuit: Union[QuantumEncoding, Callable]) Callable[source]#
Compute the jacobian functional for the input function.
- Parameters:
circuit (Callable) – the input functions that build the circuit.
- Returns:
(Callable) the jacobian functional
- property jacobian_functionals: dict[int, Callable]#
The dictionary containing the jacobian functionals of each element composing circuit structure.
- nonzero(array: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]#
The numpy-like np.nonzero function of the current engine.
- _build_parameters_map(jacobian: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) dict[int, Tuple[int]][source]#
Construct the mapping between independent and dependent parameters. In detail, the index of each independent parameter is mapped to the indices of all the dependent parameters that originated from it. This is particularly useful for the jacobian of encoders, as it provides a map from the inputs to the actual gates where they are encoded in the circuit.
- trace(f: Union[Callable, QuantumEncoding], params: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], dict[int, Tuple[int]]][source]#
Trace the construction of a circuit through the function f with given parameters params.
- Parameters:
f (Callable) – the function that builds the circuit, either a custom user-defined
- function or an encoder.
params (ndarray): the parameters assigned to the built circuit.
- Returns:
(Tuple(ndarray, dict)) the computed jacobian and the mapping between the independent
and dependent parameters of f.
- property is_encoding_differentiable: bool#
Check if all the encoders in the circuit structure are differentiable.
- abstract requires_gradient(x: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) bool[source]#
Check whether the input array needs gradients to be calculated for it.
- _build_from_encoding(encoding: QuantumEncoding, x: ndarray[tuple[int, ...], dtype[_ScalarType_co]], trace: bool = True) Circuit[source]#
Build the circuit starting from an encoder.
- Parameters:
encoding (QuantumEncoding) – the encoder.
x (ndarray) – the input data.
trace (bool) – whether to trace the construction and thus also
True. (calculate the jacobian. Defaults to) –
- Returns:
(Circuit | Tuple(ndarray, dict, Circuit)) the built circuit or the
tuple: jacobian wrt inputs, input to gate map and circuit.
- _build_from_circuit(circuit: Circuit, params: ndarray[tuple[int, ...], dtype[_ScalarType_co]], trace: bool = True) Circuit[source]#
Build the circuit starting from a circuit. In practice the given parameters params are set into the circuit only.
- Parameters:
circuit (Circuit) – the circuit.
params (ndarray) – the parameters to set.
trace (bool) – whether to trace the construction and thus also
True. (calculate the jacobian. Defaults to) –
- Returns:
(Circuit | Tuple(ndarray, dict, Circuit)) the built circuit or the
tuple: jacobian wrt parameters, input to gate map and circuit.
- _build_from_callable(f: Callable, params: ndarray[tuple[int, ...], dtype[_ScalarType_co]], trace: bool = True) Circuit[source]#
Build the circuit starting from a custom callable.
- Parameters:
f (Callable) – the custom callable which builds the circuit.
params (ndarray) – the parameters used for construction.
trace (bool) – whether to trace the construction and thus also
True. (calculate the jacobian. Defaults to) –
- Returns:
(Circuit | Tuple(ndarray, dict, Circuit)) the built circuit or the
tuple: jacobian wrt parameters, input to gate map and circuit.
- static _get_device(array: ndarray[tuple[int, ...], dtype[_ScalarType_co]])[source]#
Extract the device of the input array.
- static _get_dtype(array: ndarray[tuple[int, ...], dtype[_ScalarType_co]])[source]#
Extract the dtype of the input array.
- abstract identity(dim: int, dtype, device) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]#
The numpy-like np.eye function of the current engine.
- abstract zeros(shape: Union[int, Tuple[int]], dtype, device) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]#
The numpy-like np.zeros function of the current engine.
- fill_jacobian(jacobian: ndarray[tuple[int, ...], dtype[_ScalarType_co]], row_span: Tuple[int, int], col_span: Tuple[int, int], values: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]#
Fill the input jacobian in the span defined by (row_span, col_span) with the given values. This is mostly here to be overwritten by engines that do not allow for direct item assignment.
- build_circuit(params: ndarray[tuple[int, ...], dtype[_ScalarType_co]], x: Optional[ndarray[tuple[int, ...], dtype[_ScalarType_co]]] = None) Circuit[source]#
Construct the circuit defined by the internal
circuit_structurewithout worrying about tracing the operations.- Parameters:
params (ndarray) – the parameters to construct the circuit with.
x (ndarray, optional) – the inputs to construct the circuit with.
- Returns:
(Circuit) the constructed circuit.
- _abc_impl = <_abc._abc_data object>#