Class QuantumLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents a neural network layer that uses quantum computing principles for processing inputs.
public class QuantumLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
LayerBase<T>QuantumLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
The QuantumLayer implements a simulated quantum circuit that processes input data using quantum rotations and measurements. It transforms classical inputs into quantum states, applies quantum operations, and converts the results back to classical outputs. This approach can potentially capture complex patterns that traditional neural network layers might miss.
For Beginners: This layer uses concepts from quantum computing to process data in a unique way.
Think of it like a special filter that:
- Transforms regular data into a quantum-like format (similar to how light can be both a wave and a particle)
- Performs calculations that explore multiple possibilities simultaneously
- Converts the results back into standard values that other layers can work with
While traditional neural networks work with definite values, quantum layers work with probabilities and superpositions (being in multiple states at once). This can help the network find patterns that might be missed with traditional approaches.
You don't need to understand quantum physics to use this layer - just know that it offers a different way of processing information that can be powerful for certain problems.
Constructors
QuantumLayer(int, int, int)
Initializes a new instance of the QuantumLayer<T> class with specified dimensions.
public QuantumLayer(int inputSize, int outputSize, int numQubits)
Parameters
inputSizeintThe size of the input to the layer.
outputSizeintThe size of the output from the layer.
numQubitsintThe number of qubits to use in the quantum circuit.
Remarks
This constructor creates a new QuantumLayer with the specified dimensions. The number of qubits determines the complexity of the quantum circuit. The quantum circuit is initialized with random rotation angles, which are the trainable parameters of the layer.
For Beginners: This creates a new quantum layer for your neural network.
When you create this layer, you specify:
- inputSize: How many numbers come into the layer
- outputSize: How many numbers come out of the layer
- numQubits: How complex the quantum calculations should be
More qubits (quantum bits) mean more complex calculations but also require more computational resources. The layer starts with random settings that will be refined during training.
For example, a layer with 3 qubits can process 8 (2³) different states simultaneously, which is what gives quantum computing its potential power.
Properties
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
truebecause QuantumLayer uses complex matrix multiplication which is supported in TensorOperations via ComplexMatMul. The quantum circuit can be compiled to a static computation graph.
SupportsTraining
Gets a value indicating whether this layer supports training.
public override bool SupportsTraining { get; }
Property Value
- bool
trueindicating that this layer can be trained through backpropagation.
Remarks
This property indicates that the QuantumLayer has parameters (rotation angles) that can be optimized during the training process using backpropagation. The gradients of these parameters are calculated during the backward pass and used to update the parameters.
For Beginners: This property tells you if the layer can learn from data.
A value of true means:
- The layer has values (rotation angles) that can be adjusted during training
- It will improve its performance as it sees more data
- It participates in the learning process of the neural network
When you train a neural network containing this layer, the rotation angles will automatically adjust to better process your specific data.
Methods
Backward(Tensor<T>)
Performs the backward pass of the quantum layer.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to the layer's output.
Returns
- Tensor<T>
The gradient of the loss with respect to the layer's input.
Remarks
This method implements the backward pass of the quantum layer, which is used during training to propagate error gradients back through the network. It calculates the gradient of the loss with respect to the input and updates the gradients of the rotation angles. The quantum circuit adjoint (conjugate transpose) is used for backpropagation.
For Beginners: This method is used during training to calculate how the layer's input should change to reduce errors.
During the backward pass:
- The error gradient from the next layer is received
- This gradient is passed backward through the quantum circuit
- Gradients for the rotation angles are calculated and stored
- The gradient for the input is calculated and returned
This process allows the neural network to learn by adjusting both the input to this layer and the rotation angles within the quantum circuit. It's part of the "backpropagation" algorithm that helps neural networks improve over time.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the layer's computation graph for JIT compilation.
public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to populate with input computation nodes.
Returns
- ComputationNode<T>
The output computation node representing the layer's operation.
Remarks
This method constructs a computation graph representation of the layer's forward pass that can be JIT compiled for faster inference. All layers MUST implement this method to support JIT compilation.
For Beginners: JIT (Just-In-Time) compilation converts the layer's operations into optimized native code for 5-10x faster inference.
To support JIT compilation, a layer must:
- Implement this method to export its computation graph
- Set SupportsJitCompilation to true
- Use ComputationNode and TensorOperations to build the graph
All layers are required to implement this method, even if they set SupportsJitCompilation = false.
Forward(Tensor<T>)
Performs the forward pass of the quantum layer.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to process.
Returns
- Tensor<T>
The output tensor after quantum processing.
Remarks
This method implements the forward pass of the quantum layer. It converts the input tensor to a quantum state, applies the quantum circuit, and then measures the state to produce the output. The quantum state is normalized to ensure valid probabilities, and the output represents the probability distribution of the quantum state after measurement.
For Beginners: This method processes your data through the quantum circuit.
During the forward pass:
- Your regular data is converted to a quantum state
- The quantum circuit (with its rotation angles) processes this state
- The resulting quantum state is measured to get probabilities
- These probabilities form the output of the layer
This is like running an experiment where quantum particles can exist in multiple states, and then checking which states they actually end up in when measured. The layer saves the input for later use during training.
ForwardGpu(params IGpuTensor<T>[])
Performs the GPU-accelerated forward pass for the quantum layer.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]The GPU tensor inputs. First element is the input activation.
Returns
- IGpuTensor<T>
A GPU tensor containing the quantum probability distribution output.
Remarks
The quantum layer processes data through a simulated quantum circuit with complex-valued operations. This method uses specialized CUDA kernels to perform quantum operations entirely on GPU.
GetParameters()
Gets all trainable parameters of the quantum layer as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
A vector containing all trainable parameters (rotation angles).
Remarks
This method retrieves all trainable parameters (rotation angles) of the quantum layer as a single vector. This is useful for optimization algorithms that operate on all parameters at once, or for saving and loading model weights.
For Beginners: This method collects all the learnable values from the quantum layer.
The parameters:
- Are the rotation angles that the quantum layer learns during training
- Control how the quantum circuit processes information
- Are returned as a single list (vector)
This is useful for:
- Saving the model to disk
- Loading parameters from a previously trained model
- Advanced optimization techniques that need access to all parameters
ResetState()
Resets the internal state of the quantum layer.
public override void ResetState()
Remarks
This method resets the internal state of the quantum layer, including the cached input from the forward pass and the angle gradients. This is useful when starting to process a new sequence or when implementing stateful networks.
For Beginners: This method clears the layer's memory to start fresh.
When resetting the state:
- Stored inputs from previous calculations are cleared
- Angle gradients are reset to zero
- The layer forgets any information from previous batches
This is important for:
- Processing a new, unrelated batch of data
- Preventing information from one batch affecting another
- Starting a new training episode
The quantum circuit itself (with its learned rotation angles) is not reset, only the temporary state information.
SetParameters(Vector<T>)
Sets the trainable parameters of the quantum layer.
public override void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>A vector containing all parameters (rotation angles) to set.
Remarks
This method sets the trainable parameters (rotation angles) of the quantum layer from a single vector. The quantum circuit is reset and reconstructed with the new rotation angles. This is useful for loading saved model weights or for implementing optimization algorithms that operate on all parameters at once.
For Beginners: This method updates all the rotation angles in the quantum layer.
When setting parameters:
- The input must be a vector with the correct length (equal to the number of qubits)
- The quantum circuit is reset to its starting state
- New rotation angles are applied to rebuild the circuit
This is useful for:
- Loading a previously saved model
- Transferring parameters from another model
- Testing different parameter values
An error is thrown if the input vector doesn't have the expected number of parameters.
Exceptions
- ArgumentException
Thrown when the parameters vector has incorrect length.
UpdateParameters(T)
Updates the parameters of the quantum layer using the calculated gradients.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate to use for the parameter updates.
Remarks
This method updates the rotation angles of the quantum circuit based on the gradients calculated during the backward pass. The learning rate controls the size of the parameter updates. After updating the angles, the quantum circuit is reconstructed with the new angles.
For Beginners: This method updates the layer's internal values during training.
When updating parameters:
- The rotation angles are adjusted based on their gradients
- The learning rate controls how big each update step is
- Angles are kept within a valid range (0 to 2p)
- The quantum circuit is updated with the new angles
This is how the quantum layer "learns" from data over time. Smaller learning rates mean slower but more stable learning, while larger learning rates mean faster but potentially unstable learning.