Liking cljdoc? Tell your friends :D

org.soulspace.qclojure.ml.application.qnn

Quantum Neural Networks (QNN) implementation.

QNNs are parameterized quantum circuits organized in layers that can approximate arbitrary functions for both classification and regression tasks. This implementation follows Clojure principles of simplicity and data orientation.

Key Features:

  • Layer-based architecture using simple data maps
  • Explicit parameter management per layer
  • Sequential network composition using vectors
  • Integration with existing variational algorithm infrastructure
  • Hardware-compatible quantum operations
  • Support for multiple activation patterns

Design Principles:

  • Layers as data: Each layer is a simple map describing its configuration
  • Explicit parameters: Clear parameter ownership and allocation per layer
  • Functional composition: Networks as vectors of layers processed sequentially
  • Data orientation: All network state represented as plain Clojure data structures

Architecture:

  1. Input encoding layer (feature map)
  2. Hidden quantum layers (parameterized unitaries)
  3. Quantum activation functions (non-linear transformations)
  4. Output measurement layer (classical readout)
Quantum Neural Networks (QNN) implementation.

QNNs are parameterized quantum circuits organized in layers that can approximate
arbitrary functions for both classification and regression tasks. This implementation
follows Clojure principles of simplicity and data orientation.

Key Features:
- Layer-based architecture using simple data maps
- Explicit parameter management per layer
- Sequential network composition using vectors
- Integration with existing variational algorithm infrastructure
- Hardware-compatible quantum operations
- Support for multiple activation patterns

Design Principles:
- Layers as data: Each layer is a simple map describing its configuration
- Explicit parameters: Clear parameter ownership and allocation per layer
- Functional composition: Networks as vectors of layers processed sequentially
- Data orientation: All network state represented as plain Clojure data structures

Architecture:
1. Input encoding layer (feature map)
2. Hidden quantum layers (parameterized unitaries)
3. Quantum activation functions (non-linear transformations)
4. Output measurement layer (classical readout)
raw docstring

allocate-parametersclj

(allocate-parameters network)

Allocate parameter indices for each layer in a QNN network.

Parameters:

  • network: Vector of layer configuration maps

Returns: Map with :network (updated with parameter indices) and :total-parameters

Allocate parameter indices for each layer in a QNN network.

Parameters:
- network: Vector of layer configuration maps

Returns:
Map with :network (updated with parameter indices) and :total-parameters
sourceraw docstring

analyze-qnn-networkclj

(analyze-qnn-network network)

Analyze a QNN network configuration and provide insights.

Parameters:

  • network: QNN network configuration

Returns: Analysis map with network statistics and insights

Analyze a QNN network configuration and provide insights.

Parameters:
- network: QNN network configuration

Returns:
Analysis map with network statistics and insights
sourceraw docstring

apply-activation-layerclj

(apply-activation-layer circuit layer _feature-data parameters)

Apply quantum activation function to circuit qubits.

Parameters:

  • circuit: Input quantum circuit
  • layer: Activation layer configuration
  • feature-data: Not used for activation layers
  • parameters: Parameter vector for this layer

Returns: Quantum circuit with applied activation function

Apply quantum activation function to circuit qubits.

Parameters:
- circuit: Input quantum circuit
- layer: Activation layer configuration
- feature-data: Not used for activation layers
- parameters: Parameter vector for this layer

Returns:
Quantum circuit with applied activation function
sourceraw docstring

apply-dense-layerclj

(apply-dense-layer circuit layer _feature-data parameters)

Apply dense quantum layer with parameterized rotations.

Parameters:

  • circuit: Input quantum circuit
  • layer: Dense layer configuration
  • feature-data: Not used for dense layers
  • parameters: Parameter vector for this layer

Returns: Quantum circuit with applied dense transformations

Apply dense quantum layer with parameterized rotations.

Parameters:
- circuit: Input quantum circuit
- layer: Dense layer configuration
- feature-data: Not used for dense layers
- parameters: Parameter vector for this layer

Returns:
Quantum circuit with applied dense transformations
sourceraw docstring

apply-entangling-layerclj

(apply-entangling-layer circuit layer _feature-data _parameters)

Apply entangling layer with specified connectivity pattern.

Parameters:

  • circuit: Input quantum circuit
  • layer: Entangling layer configuration
  • feature-data: Not used for entangling layers
  • parameters: Parameter vector for this layer (for parameterized entangling)

Returns: Quantum circuit with applied entangling operations

Apply entangling layer with specified connectivity pattern.

Parameters:
- circuit: Input quantum circuit
- layer: Entangling layer configuration
- feature-data: Not used for entangling layers
- parameters: Parameter vector for this layer (for parameterized entangling)

Returns:
Quantum circuit with applied entangling operations
sourceraw docstring

apply-input-layerclj

(apply-input-layer circuit layer feature-data _parameters)

Apply input layer (feature encoding) to a quantum circuit.

Parameters:

  • circuit: Input quantum circuit
  • layer: Input layer configuration
  • feature-data: Classical feature vector to encode
  • parameters: Not used for input layers

Returns: Quantum circuit with encoded features

Apply input layer (feature encoding) to a quantum circuit.

Parameters:
- circuit: Input quantum circuit
- layer: Input layer configuration
- feature-data: Classical feature vector to encode
- parameters: Not used for input layers

Returns:
Quantum circuit with encoded features
sourceraw docstring

apply-layerclj

(apply-layer circuit layer feature-data layer-parameters)

Apply a single QNN layer to a quantum circuit.

Parameters:

  • circuit: Input quantum circuit
  • layer: Layer configuration map
  • feature-data: Classical feature data (used only by input layers)
  • layer-parameters: Parameters specific to this layer

Returns: Quantum circuit with layer operations applied

Apply a single QNN layer to a quantum circuit.

Parameters:
- circuit: Input quantum circuit
- layer: Layer configuration map
- feature-data: Classical feature data (used only by input layers)
- layer-parameters: Parameters specific to this layer

Returns:
Quantum circuit with layer operations applied
sourceraw docstring

apply-measurement-layerclj

(apply-measurement-layer circuit layer _feature-data _parameters)

Apply measurement operations to circuit (for output layers).

Parameters:

  • circuit: Input quantum circuit
  • layer: Measurement layer configuration
  • feature-data: Not used for measurement layers
  • parameters: Not used for measurement layers

Returns: Quantum circuit with measurement operations

Apply measurement operations to circuit (for output layers).

Parameters:
- circuit: Input quantum circuit
- layer: Measurement layer configuration
- feature-data: Not used for measurement layers
- parameters: Not used for measurement layers

Returns:
Quantum circuit with measurement operations
sourceraw docstring

apply-qnn-networkclj

(apply-qnn-network network feature-data parameters)

Apply a complete QNN network to input data, creating a quantum circuit.

This function implements the forward pass of a QNN by sequentially applying each layer to the quantum circuit. It handles parameter extraction and layer-specific data passing.

Parameters:

  • network: Vector of layer configuration maps (with allocated parameters)
  • feature-data: Classical input feature vector
  • parameters: Complete parameter vector for the entire network

Returns: Quantum circuit representing the forward pass of the QNN

Apply a complete QNN network to input data, creating a quantum circuit.

This function implements the forward pass of a QNN by sequentially applying
each layer to the quantum circuit. It handles parameter extraction and
layer-specific data passing.

Parameters:
- network: Vector of layer configuration maps (with allocated parameters)
- feature-data: Classical input feature vector
- parameters: Complete parameter vector for the entire network

Returns:
Quantum circuit representing the forward pass of the QNN
sourceraw docstring

compute-lossclj

(compute-loss prediction label loss-type)

Compute loss between prediction and true label.

Parameters:

  • prediction: Model prediction (expectation value, probability vector, or bitstring)
  • label: True label (number for regression, integer for classification)
  • loss-type: Type of loss function

Returns: Loss value as a real number

Compute loss between prediction and true label.

Parameters:
- prediction: Model prediction (expectation value, probability vector, or bitstring)
- label: True label (number for regression, integer for classification)
- loss-type: Type of loss function

Returns:
Loss value as a real number
sourceraw docstring

count-layer-parametersclj

(count-layer-parameters layer)

Count the number of parameters needed for a specific layer type.

Parameters:

  • layer: Layer configuration map

Returns: Number of parameters required for this layer

Count the number of parameters needed for a specific layer type.

Parameters:
- layer: Layer configuration map

Returns:
Number of parameters required for this layer
sourceraw docstring

create-feedforward-qnnclj

(create-feedforward-qnn num-qubits
                        hidden-layers
                        &
                        {:keys [feature-map-type activation-type]
                         :or {feature-map-type :angle
                              activation-type :quantum-tanh}})

Create a standard feedforward QNN configuration.

Parameters:

  • num-qubits: Number of qubits in the network
  • hidden-layers: Number of hidden layers
  • feature-map-type: Input encoding type (:angle, :amplitude, etc.)
  • activation-type: Activation function type

Returns: Complete QNN network configuration with allocated parameters

Create a standard feedforward QNN configuration.

Parameters:
- num-qubits: Number of qubits in the network
- hidden-layers: Number of hidden layers
- feature-map-type: Input encoding type (:angle, :amplitude, etc.)
- activation-type: Activation function type

Returns:
Complete QNN network configuration with allocated parameters
sourceraw docstring

create-qnn-circuit-constructorclj

(create-qnn-circuit-constructor network)

Create a circuit constructor function for QNN integration with variational algorithms.

This function creates a circuit constructor compatible with the variational algorithm template. It returns a function that takes parameters and feature data, returning a complete QNN circuit.

Parameters:

  • network: QNN network configuration (with allocated parameters)

Returns: Function (parameters, feature-data) -> quantum circuit

Create a circuit constructor function for QNN integration with variational algorithms.

This function creates a circuit constructor compatible with the variational algorithm
template. It returns a function that takes parameters and feature data, returning
a complete QNN circuit.

Parameters:
- network: QNN network configuration (with allocated parameters)

Returns:
Function (parameters, feature-data) -> quantum circuit
sourceraw docstring

create-qnn-cost-functionclj

(create-qnn-cost-function network
                          training-data
                          backend
                          loss-type
                          &
                          {:keys [options] :or {options {}}})

Create a cost function for QNN training compatible with variational algorithms.

This function creates a cost function that takes parameters and returns a loss value. It integrates with the QClojure variational algorithm template for optimization.

Parameters:

  • network: QNN network configuration
  • training-data: Vector of {:features [...] :label ...} maps
  • backend: Quantum backend for circuit execution
  • loss-type: Loss function type (:mse, :cross-entropy, :hinge, etc.)
  • options: Training options (shots, measurement strategy, etc.)

Returns: Cost function (parameters) -> loss value

Create a cost function for QNN training compatible with variational algorithms.

This function creates a cost function that takes parameters and returns a loss value.
It integrates with the QClojure variational algorithm template for optimization.

Parameters:
- network: QNN network configuration
- training-data: Vector of {:features [...] :label ...} maps
- backend: Quantum backend for circuit execution
- loss-type: Loss function type (:mse, :cross-entropy, :hinge, etc.)
- options: Training options (shots, measurement strategy, etc.)

Returns:
Cost function (parameters) -> loss value
sourceraw docstring

create-qnn-modelclj

(create-qnn-model config)

Create a complete QNN model with training and inference capabilities.

This function provides a high-level interface for QNN creation, training, and inference, abstracting away the low-level details.

Parameters:

  • config: QNN configuration map

Configuration options:

  • :num-qubits - Number of qubits
  • :hidden-layers - Number of hidden layers
  • :feature-map-type - Input encoding type
  • :activation-type - Activation function type

Returns: QNN model map with train and predict functions

Create a complete QNN model with training and inference capabilities.

This function provides a high-level interface for QNN creation, training,
and inference, abstracting away the low-level details.

Parameters:
- config: QNN configuration map

Configuration options:
- :num-qubits - Number of qubits
- :hidden-layers - Number of hidden layers
- :feature-map-type - Input encoding type
- :activation-type - Activation function type

Returns:
QNN model map with train and predict functions
sourceraw docstring

evaluate-qnn-accuracyclj

(evaluate-qnn-accuracy network test-data parameters backend)

Evaluate QNN accuracy on a dataset.

Parameters:

  • network: QNN network configuration
  • test-data: Test dataset
  • parameters: Optimized QNN parameters
  • backend: Quantum backend

Returns: Accuracy as a number between 0 and 1

Evaluate QNN accuracy on a dataset.

Parameters:
- network: QNN network configuration
- test-data: Test dataset
- parameters: Optimized QNN parameters
- backend: Quantum backend

Returns:
Accuracy as a number between 0 and 1
sourceraw docstring

extract-expectation-valueclj

(extract-expectation-value result & {:keys [options] :or {options {}}})

Extract expectation value from quantum measurement results.

Production-ready implementation that properly handles result-specs format from backend protocol execution. Works with both measurement-based expectations and explicit expectation value results.

For classification, computes <Z> expectation on output qubits. For regression, uses explicit expectation values from backend.

Parameters:

  • result: Quantum execution result from backend with :results map
  • options: Optional configuration map
    • :observable-index - Index of observable to extract (default: 0)
    • :use-measurements - Compute from measurements vs use explicit expectations

Returns: Expectation value as a real number

Extract expectation value from quantum measurement results.

Production-ready implementation that properly handles result-specs format
from backend protocol execution. Works with both measurement-based expectations
and explicit expectation value results.

For classification, computes <Z> expectation on output qubits.
For regression, uses explicit expectation values from backend.

Parameters:
- result: Quantum execution result from backend with :results map
- options: Optional configuration map
  - :observable-index - Index of observable to extract (default: 0)
  - :use-measurements - Compute from measurements vs use explicit expectations

Returns:
Expectation value as a real number
sourceraw docstring

extract-layer-parametersclj

(extract-layer-parameters layer full-parameters)

Extract parameters for a specific layer from the full parameter vector.

Parameters:

  • layer: Layer configuration with parameter indices
  • full-parameters: Complete parameter vector for the network

Returns: Parameter vector for this specific layer

Extract parameters for a specific layer from the full parameter vector.

Parameters:
- layer: Layer configuration with parameter indices
- full-parameters: Complete parameter vector for the network

Returns:
Parameter vector for this specific layer
sourceraw docstring

extract-most-probable-stateclj

(extract-most-probable-state result & {:keys [options] :or {options {}}})

Extract the most probable computational basis state.

Production-ready implementation that properly handles result-specs format from backend protocol execution. Finds the bitstring with the highest measurement count.

Parameters:

  • result: Quantum execution result from backend with :results map
  • options: Optional configuration map
    • :return-count - Also return the count (default: false)

Returns: String representing the most probable bitstring, or [bitstring count] if :return-count is true

Extract the most probable computational basis state.

Production-ready implementation that properly handles result-specs format
from backend protocol execution. Finds the bitstring with the highest
measurement count.

Parameters:
- result: Quantum execution result from backend with :results map
- options: Optional configuration map
  - :return-count - Also return the count (default: false)

Returns:
String representing the most probable bitstring, or
[bitstring count] if :return-count is true
sourceraw docstring

extract-probability-distributionclj

(extract-probability-distribution result & {:keys [options] :or {options {}}})

Extract probability distribution from quantum measurement results.

Production-ready implementation that properly handles result-specs format from backend protocol execution. Converts measurement counts to normalized probabilities.

Parameters:

  • result: Quantum execution result from backend with :results map
  • options: Optional configuration map
    • :num-states - Expected number of states (default: infer from outcomes)
    • :normalize - Whether to normalize probabilities (default: true)

Returns: Vector of probabilities for each computational basis state, sorted by bitstring value (e.g., [p("00"), p("01"), p("10"), p("11")])

Extract probability distribution from quantum measurement results.

Production-ready implementation that properly handles result-specs format
from backend protocol execution. Converts measurement counts to normalized
probabilities.

Parameters:
- result: Quantum execution result from backend with :results map
- options: Optional configuration map
  - :num-states - Expected number of states (default: infer from outcomes)
  - :normalize - Whether to normalize probabilities (default: true)

Returns:
Vector of probabilities for each computational basis state, sorted by
bitstring value (e.g., [p("00"), p("01"), p("10"), p("11")])
sourceraw docstring

layer-type-dispatchcljmultimethod

source

optimize-parametersclj

(optimize-parameters cost-fn initial-params options)

Simple parameter optimization for demonstration.

In a full implementation, this would delegate to QClojure's optimization algorithms like Nelder-Mead, COBYLA, or gradient-based methods.

Parameters:

  • cost-fn: Cost function to minimize
  • initial-params: Initial parameter values
  • options: Optimization options

Returns: Optimization result map

Simple parameter optimization for demonstration.

In a full implementation, this would delegate to QClojure's optimization
algorithms like Nelder-Mead, COBYLA, or gradient-based methods.

Parameters:
- cost-fn: Cost function to minimize
- initial-params: Initial parameter values
- options: Optimization options

Returns:
Optimization result map
sourceraw docstring

qnn-circuit-constructor-adapterclj

(qnn-circuit-constructor-adapter options)

Create circuit constructor function for QNN from options.

This function creates a circuit constructor that works with the enhanced variational template. The resulting function takes parameters and a feature vector, returning a complete QNN circuit.

Parameters:

  • options: QNN training options containing :network

Returns: Function that takes (parameters, feature-vector) and returns a quantum circuit

Create circuit constructor function for QNN from options.

This function creates a circuit constructor that works with the enhanced
variational template. The resulting function takes parameters and a feature
vector, returning a complete QNN circuit.

Parameters:
- options: QNN training options containing :network

Returns:
Function that takes (parameters, feature-vector) and returns a quantum circuit
sourceraw docstring

qnn-dataset-constructorclj

(qnn-dataset-constructor options)

Extract training data from QNN options for the variational algorithm template.

This function adapts the QNN data format to work with the variational algorithm template, which expects a dataset source.

Parameters:

  • options: QNN training options containing :training-data

Returns: Training data vector

Extract training data from QNN options for the variational algorithm template.

This function adapts the QNN data format to work with the variational algorithm template,
which expects a dataset source.

Parameters:
- options: QNN training options containing :training-data

Returns:
Training data vector
sourceraw docstring

qnn-forward-passclj

(qnn-forward-pass network
                  feature-data
                  parameters
                  backend
                  &
                  {:keys [options] :or {options {}}})

Execute the forward pass of a QNN on a quantum backend.

This function executes the complete QNN circuit on a quantum backend using the proper backend protocol with result specifications. It returns measurement results suitable for both classification and regression tasks.

Production-ready implementation:

  • Uses backend protocol's execute-circuit with result-specs
  • Requests appropriate measurements for the task type
  • Compatible with real quantum hardware and cloud services

Parameters:

  • network: QNN network configuration
  • feature-data: Input feature vector
  • parameters: Network parameter vector
  • backend: Quantum backend implementing QuantumBackend protocol
  • options: Execution options map

Options:

  • :shots - Number of measurement shots (default: 1024)
  • :task-type - :classification or :regression (default: :classification)
  • :measurement-qubits - Qubits to measure (default: all output qubits)
  • :observables - Observables for expectation value measurements (default: Pauli-Z)

Returns: Execution result map from backend containing:

  • :results - Result map with measurements, expectations, or probabilities
  • :final-state - Final quantum state (if state-vector available)
  • :execution-metadata - Backend execution metadata
Execute the forward pass of a QNN on a quantum backend.

This function executes the complete QNN circuit on a quantum backend using
the proper backend protocol with result specifications. It returns measurement
results suitable for both classification and regression tasks.

Production-ready implementation:
- Uses backend protocol's execute-circuit with result-specs
- Requests appropriate measurements for the task type
- Compatible with real quantum hardware and cloud services

Parameters:
- network: QNN network configuration
- feature-data: Input feature vector
- parameters: Network parameter vector
- backend: Quantum backend implementing QuantumBackend protocol
- options: Execution options map

Options:
- :shots - Number of measurement shots (default: 1024)
- :task-type - :classification or :regression (default: :classification)
- :measurement-qubits - Qubits to measure (default: all output qubits)
- :observables - Observables for expectation value measurements (default: Pauli-Z)

Returns:
Execution result map from backend containing:
- :results - Result map with measurements, expectations, or probabilities
- :final-state - Final quantum state (if state-vector available)
- :execution-metadata - Backend execution metadata
sourceraw docstring

qnn-loss-functionclj

(qnn-loss-function loss-type task-type)

Create a loss function for QNN that works with the enhanced variational template.

This function creates loss functions suitable for both classification and regression tasks in quantum neural networks. It computes loss based on predictions and labels/targets.

Parameters:

  • loss-type: Type of loss function (:mse, :cross-entropy, :hinge, :accuracy)
  • task-type: Type of ML task (:classification or :regression)

Returns: Function that takes (predictions, labels) and returns loss value

Create a loss function for QNN that works with the enhanced variational template.

This function creates loss functions suitable for both classification and regression
tasks in quantum neural networks. It computes loss based on predictions and labels/targets.

Parameters:
- loss-type: Type of loss function (:mse, :cross-entropy, :hinge, :accuracy)
- task-type: Type of ML task (:classification or :regression)

Returns:
Function that takes (predictions, labels) and returns loss value
sourceraw docstring

qnn-parameter-countclj

(qnn-parameter-count options)

Calculate required parameter count for QNN network.

This function determines how many parameters are needed for the complete QNN network by summing parameters across all layers.

Parameters:

  • options: QNN training options containing :network

Returns: Total number of parameters required

Calculate required parameter count for QNN network.

This function determines how many parameters are needed for the complete
QNN network by summing parameters across all layers.

Parameters:
- options: QNN training options containing :network

Returns:
Total number of parameters required
sourceraw docstring

qnn-prediction-extractorclj

(qnn-prediction-extractor measurement-result
                          task-type
                          measurement-strategy
                          shots)

Extract prediction from QNN measurement result for enhanced template.

This function is designed to work with the enhanced-variational-algorithm template's prediction extraction infrastructure. It handles both classification and regression.

Parameters:

  • measurement-result: Measurement result map
  • task-type: Type of ML task (:classification or :regression)
  • measurement-strategy: How to extract predictions (:expectation, :probability, :bitstring)
  • shots: Number of measurement shots

Returns: Predicted value (integer for classification, float for regression)

Extract prediction from QNN measurement result for enhanced template.

This function is designed to work with the enhanced-variational-algorithm template's
prediction extraction infrastructure. It handles both classification and regression.

Parameters:
- measurement-result: Measurement result map
- task-type: Type of ML task (:classification or :regression)
- measurement-strategy: How to extract predictions (:expectation, :probability, :bitstring)
- shots: Number of measurement shots

Returns:
Predicted value (integer for classification, float for regression)
sourceraw docstring

qnn-result-processorclj

(qnn-result-processor optimization-result options)

Process QNN training results with ML-specific metrics.

This processor extracts ML performance metrics from the optimization results and adds QNN-specific analysis.

Parameters:

  • optimization-result: Result from variational optimization
  • options: QNN training options

Returns: Enhanced result map with QNN metrics

Process QNN training results with ML-specific metrics.

This processor extracts ML performance metrics from the optimization results
and adds QNN-specific analysis.

Parameters:
- optimization-result: Result from variational optimization
- options: QNN training options

Returns:
Enhanced result map with QNN metrics
sourceraw docstring

train-qnnclj

(train-qnn backend options)

Train a QNN using the enhanced variational algorithm template.

This function configures and executes quantum neural network training with support for both classification and regression tasks. The QNN is trained using the enhanced variational algorithm template with ML-specific objective handling.

The training process:

  1. Encodes training features into quantum states via input layers
  2. Applies parameterized quantum layers (dense, entangling, activation)
  3. Measures results and extracts predictions
  4. Optimizes network parameters to minimize task-specific loss

Parameters:

  • backend: Quantum backend for circuit execution
  • options: Configuration map with required and optional keys

Required options:

  • :network - QNN network configuration (vector of layer maps)
  • :training-data - Vector of {:features [...] :label ...} maps

Optional options (with defaults):

  • :task-type - ML task type (:classification or :regression) [default: :classification]
  • :loss-type - Loss function (:accuracy, :cross-entropy, :hinge, :mse, :mae) [default: :accuracy for classification, :mse for regression]
  • :measurement-strategy - How to extract predictions (:expectation, :probability, :bitstring) [default: :bitstring for classification, :expectation for regression]
  • :optimization-method - Optimizer to use (:cmaes, :nelder-mead, :powell, :adam) [default: :cmaes]
  • :max-iterations - Maximum training iterations [default: 100]
  • :tolerance - Convergence tolerance [default: 1e-6]
  • :shots - Number of measurement shots [default: 1024]
  • :initial-parameters - Initial parameter values (random if not provided)

Returns: Map containing trained model and analysis results:

  • :success - Training completion status
  • :optimal-parameters - Trained network parameters
  • :final-loss - Final loss value
  • :final-accuracy - Final accuracy (for classification tasks)
  • :iterations - Number of iterations performed
  • :convergence-history - Loss values over iterations
  • :total-runtime-ms - Total training time
  • :network-info - Network architecture details
  • :training-info - Training configuration summary

Example:

(train-qnn 
  backend
  {:network qnn-network
   :training-data [{:features [0.1 0.2] :label 0}
                   {:features [0.8 0.9] :label 1}]
   :task-type :classification
   :loss-type :accuracy
   :optimization-method :cmaes
   :max-iterations 100
   :shots 1024})
Train a QNN using the enhanced variational algorithm template.

This function configures and executes quantum neural network training with support
for both classification and regression tasks. The QNN is trained using the enhanced
variational algorithm template with ML-specific objective handling.

The training process:
1. Encodes training features into quantum states via input layers
2. Applies parameterized quantum layers (dense, entangling, activation)
3. Measures results and extracts predictions
4. Optimizes network parameters to minimize task-specific loss

Parameters:
- backend: Quantum backend for circuit execution
- options: Configuration map with required and optional keys

Required options:
- :network - QNN network configuration (vector of layer maps)
- :training-data - Vector of {:features [...] :label ...} maps

Optional options (with defaults):
- :task-type - ML task type (:classification or :regression) [default: :classification]
- :loss-type - Loss function (:accuracy, :cross-entropy, :hinge, :mse, :mae) 
               [default: :accuracy for classification, :mse for regression]
- :measurement-strategy - How to extract predictions (:expectation, :probability, :bitstring)
                         [default: :bitstring for classification, :expectation for regression]
- :optimization-method - Optimizer to use (:cmaes, :nelder-mead, :powell, :adam)
                        [default: :cmaes]
- :max-iterations - Maximum training iterations [default: 100]
- :tolerance - Convergence tolerance [default: 1e-6]
- :shots - Number of measurement shots [default: 1024]
- :initial-parameters - Initial parameter values (random if not provided)

Returns:
Map containing trained model and analysis results:
- :success - Training completion status
- :optimal-parameters - Trained network parameters
- :final-loss - Final loss value
- :final-accuracy - Final accuracy (for classification tasks)
- :iterations - Number of iterations performed
- :convergence-history - Loss values over iterations
- :total-runtime-ms - Total training time
- :network-info - Network architecture details
- :training-info - Training configuration summary

Example:
```clojure
(train-qnn 
  backend
  {:network qnn-network
   :training-data [{:features [0.1 0.2] :label 0}
                   {:features [0.8 0.9] :label 1}]
   :task-type :classification
   :loss-type :accuracy
   :optimization-method :cmaes
   :max-iterations 100
   :shots 1024})
sourceraw docstring

visualize-qnn-networkclj

(visualize-qnn-network network)

Create a simple ASCII visualization of the QNN network structure.

Parameters:

  • network: QNN network configuration

Returns: String containing ASCII art representation

Create a simple ASCII visualization of the QNN network structure.

Parameters:
- network: QNN network configuration

Returns:
String containing ASCII art representation
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close