isqtools.neural_networks package¶
Submodules¶
isqtools.neural_networks.neural_network module¶
A Neural Network abstract class for all. This part code is inspired by IBM qiskit. For more information, check out:https://qiskit.org/
- class NeuralNetwork(num_inputs, num_weights, sparse, output_shape, input_gradients=True)¶
Bases:
ABC
Abstract Neural Network class providing forward and backward pass and handling batched inputs. This is to be implemented by other (quantum) neural networks.
- Parameters:
num_inputs (
int
)num_weights (
int
)sparse (
bool
)output_shape (
int
|tuple
[int
,...
])input_gradients (
bool
)
Initialize quantum neural network.
- Parameters:
num_inputs (
int
) – The number of input features.num_weights (
int
) – The number of trainable weights.sparse (
bool
) – Determines whether the output is a sparse array or not.output_shape (
int
|tuple
[int
,...
]) – The shape of the output.input_gradients (
bool
) – Determines whether to compute gradients with respect to input data.
- Raises:
NeuralNetworkError – Invalid parameter values.
- backward(input_data, weights)¶
- Parameters:
input_data (
float
|list
[float
] |ndarray
|None
)weights (
float
|list
[float
] |ndarray
|None
)
- Return type:
tuple
[ndarray
|SparseArray
|None
,ndarray
|SparseArray
|None
]
- forward(input_data, weights)¶
Forward pass of the network.
- Parameters:
input_data (
float
|list
[float
] |ndarray
|None
) – input data of the shape (num_inputs). In case of a single scalar input it is directly cast to and interpreted like a one-element array.weights (
float
|list
[float
] |ndarray
|None
) – trainable weights of the shape (num_weights). In case of a single scalar weightit is directly cast to and interpreted like a one-element array.
- Return type:
ndarray
|SparseArray
- Returns:
The result of the neural network of the shape (output_shape).
- property input_gradients: bool¶
Returns whether gradients with respect to input data are computed by this neural network in the
backward
method or not. By default such gradients are not computed.
- property num_inputs: int¶
Returns the number of input features.
- property num_weights: int¶
Returns the number of trainable weights.
- property output_shape: tuple[int, ...]¶
Returns the output shape.
- property sparse: bool¶
Returns whether the output is sparse or not.
- exception NeuralNetworkError¶
Bases:
Exception
Neural network error.
- class SparseArray¶
Bases:
object
Empty SparseArray class Replacement if sparse.SparseArray is not present.
isqtools.neural_networks.qnn_autograd module¶
A Quantum Neural Network implemented by autograd.
- class QNNAutograd(circuit, num_inputs, num_weights, output_shape, input_gradients=True, sparse=False)¶
Bases:
NeuralNetwork
Use autograd’s automatic differentiation (autodiff) to implement the
forward
and ``backward``of the neural network.- Parameters:
circuit (
Callable
[[Any
,Any
],Any
])num_inputs (
int
)num_weights (
int
)output_shape (
int
|tuple
[int
,...
])input_gradients (
bool
)sparse (
bool
)
Initialize quantum neural network.
- Parameters:
num_inputs (
int
) – The number of input features.num_weights (
int
) – The number of trainable weights.sparse (
bool
) – Determines whether the output is a sparse array or not.output_shape (
int
|tuple
[int
,...
]) – The shape of the output.input_gradients (
bool
) – Determines whether to compute gradients with respect to input data.circuit (
Callable
[[Any
,Any
],Any
])
- Raises:
NeuralNetworkError – Invalid parameter values.
isqtools.neural_networks.qnn_param_shift module¶
A Quantum Neural Network using parameter shift to get gradients, which is useful for sampling measurement.
- class QNNParamShift(circuit, num_inputs, num_weights, output_shape, input_gradients=True, sparse=False)¶
Bases:
NeuralNetwork
Initialize quantum neural network.
- Parameters:
num_inputs (
int
) – The number of input features.num_weights (
int
) – The number of trainable weights.sparse (
bool
) – Determines whether the output is a sparse array or not.output_shape (
int
|tuple
[int
,...
]) – The shape of the output.input_gradients (
bool
) – Determines whether to compute gradients with respect to input data.circuit (
Callable
[[Any
,Any
],Any
])
- Raises:
NeuralNetworkError – Invalid parameter values.
- parameter_shift_input(input, weights)¶
- parameter_shift_input_element(input, weights, i)¶
- parameter_shift_weights(input, weights)¶
- parameter_shift_weights_element(input, weights, i)¶
isqtools.neural_networks.torch_layer module¶
A connector to directly use circuits as PyTorch modules. This part code is inspired by tensorcircuit.
- class TorchLayer(circuit, num_weights, *, is_vmap=True, in_dims=(0, None), initial_weights=None)¶
Bases:
object
Apply a transformation of quantum circuits to the incoming data
- Parameters:
circuit (
Callable
[...
,Any
])num_weights (
int
)is_vmap (
bool
)in_dims (
tuple
[int
|None
,int
|None
])initial_weights (
Any
|None
)
- extra_repr()¶
Nice print this layer.
- Return type:
str
- forward(inputs)¶
- Parameters:
inputs (
Any
)- Return type:
Any
-
num_weights:
int
¶
- exception TorchLayerError¶
Bases:
Exception
Torch layer exception.
isqtools.neural_networks.torch_wrapper module¶
A connector to use Neural Networks as PyTorch modules. This part code is inspired by IBM qiskit. For more information, check out:https://qiskit.org/
- class TorchWrapper(neural_network, initial_weights=None, sparse=None)¶
Bases:
object
Connects a Quantum Neural Network to PyTorch.
- Parameters:
neural_network (
NeuralNetwork
)initial_weights (
ndarray
|Any
|None
)sparse (
bool
|None
)neural_network – The neural network to be connected to PyTorch. Remember that
input_gradients
must be set toTrue
in the neural network initialization before passing it to theTorchConnector
for the gradient computations to work properly during training.initial_weights – The initial weights to start training the network. If this is None, the initial weights are chosen uniformly at random from [-1, 1].
sparse – Whether this connector should return sparse output or not. If sparse is set to None, then the setting from the given neural network is used. Note that sparse output is only returned if the underlying neural network also returns sparse output, otherwise it will be dense independent of the setting. Also note that PyTorch currently does not support sparse back propagation, i.e., if sparse is set to True, the backward pass of this module will return None.
- extra_repr()¶
- Return type:
str
- forward(input_data=None)¶
Forward pass.
- Parameters:
input_data (
Any
|None
) – data to be evaluated.- Return type:
Any
- Returns:
Result of forward pass of this model.
- property neural_network: NeuralNetwork¶
Returns the underlying neural network.
- property sparse: bool | None¶
Returns whether this connector returns sparse output or not.
- property weight: Any¶
Returns the weights of the underlying network.
Module contents¶
Quantum neural network package.