### Classical Neural Network Benchmark Setup (Python) Source: https://context7.com/bb511/dimred4qsvm/llms.txt Defines a classical feed-forward neural network configuration that mirrors the architecture of the QSVM's autoencoder encoder. This allows for a direct comparison of classical versus quantum classification performance. ```python from classical_nn.neural_network import NeuralNetwork from classical_nn.train import main as train_nn # Classical NN configuration matching autoencoder architecture nn_config = { "layers": [67, 64, 52, 44, 32, 24, 16], # Same as AE encoder "lr": 0.01, "batch_size": 1024, "adam_betas": (0.9, 0.999), "out_activ": "nn.Sigmoid()", } ``` -------------------------------- ### Launch QSVM with Noisy Simulation via CLI Source: https://context7.com/bb511/dimred4qsvm/llms.txt Launches a Quantum Support Vector Machine (QSVM) with a noisy simulation. Requires data, normalization, event count, a pre-trained model path, backend name, run type, C parameter, training/testing counts, and an output folder. ```bash ./bin/qsvm_launch \ --data_folder ./data/ae_input/ \ --norm minmax \ --nevents 7.20e+05 \ --model_path ./trained_aes/sinkclass_best/best_model.pt \ --backend_name ibmq_guadalupe \ --run_type noisy \ --c_param 1.0 \ --ntrain 500 \ --ntest 100 \ --output_folder qsvm_production ``` -------------------------------- ### Train Vanilla Autoencoder via CLI Source: https://context7.com/bb511/dimred4qsvm/llms.txt Trains a vanilla autoencoder using the command-line interface. Specifies autoencoder type, data folder, normalization, number of events, batch size, epochs, learning rate, and output directory. ```bash ./bin/ae_train \ --aetype vanilla \ --data_folder ./data/ae_input/ \ --norm minmax \ --nevents 7.20e+05 \ --batch 128 \ --epochs 85 \ --lr 0.002 \ --outdir vanilla_experiment ``` -------------------------------- ### Quantum Instance Configuration for QSVM (Python) Source: https://context7.com/bb511/dimred4qsvm/llms.txt Configures Qiskit's QuantumInstance for different QSVM execution modes: ideal simulation, noisy simulation using hardware noise models, and direct execution on real quantum hardware. Includes options for backend selection, shots, and optimization levels. ```python from qsvm.util import configure_quantum_instance # 1. Ideal statevector simulation (fastest, no noise) quantum_instance_ideal, backend = configure_quantum_instance( ibmq_api_config=None, run_type="ideal", backend_name=None, ) # Output: Initialising ideal (statevector) simulation. # 2. Noisy simulation with real hardware noise model ibmq_credentials = { "token": "YOUR_IBMQ_API_TOKEN", "hub": "ibm-q", "group": "open", "project": "main", } quantum_instance_noisy, backend = configure_quantum_instance( ibmq_api_config=ibmq_credentials, run_type="noisy", backend_name="ibmq_guadalupe", shots=1024, optimization_level=3, ) # Output: Initialising noisy simulation. # Loaded IBMQ backend: ibmq_guadalupe. # 3. Real quantum hardware execution quantum_instance_hw, backend = configure_quantum_instance( ibmq_api_config=ibmq_credentials, run_type="hardware", backend_name="ibmq_guadalupe", shots=8192, ) # Output: Initialising run on a quantum computer. # Save backend calibration data for reproducibility from qsvm.util import save_backend_properties if backend is not None: save_backend_properties(backend, "./models/backend_properties") ``` -------------------------------- ### Initialize and Train Neural Network Model Source: https://context7.com/bb511/dimred4qsvm/llms.txt Initializes a NeuralNetwork model with specified device and hyperparameters, then trains it using provided data loaders. Training involves multiple epochs with early stopping and saves the model to a specified output directory. ```python model = NeuralNetwork(device="cuda", hpars=nn_config) model.train_model( train_loader=train_loader, valid_loader=valid_loader, epochs=85, early_stopping_limit=15, outdir="./trained_nns/classical_benchmark/", ) ``` -------------------------------- ### Train Autoencoder Model with Python Source: https://context7.com/bb511/dimred4qsvm/llms.txt Handles autoencoder model initialization, batch training with early stopping, validation loss tracking, and automatic model checkpointing. Outputs include loss plots, architecture diagrams, and hyperparameter JSON files. Requires the 'autoencoders' library. ```python from autoencoders.train import main as train_main # Configuration dictionary for training train_config = { "data_folder": "./data/ae_input/", "norm": "minmax", "nevents": "7.20e+05", "train_events": 100000, "valid_events": 20000, "aetype": "sinkclass", # vanilla, classifier, variational, sinkhorn, sinkclass "ae_layers": [64, 44, 32, 24, 16], # Layers after input (67 prepended automatically) "class_layers": [32, 64, 128, 64, 32, 16, 8, 1], # Classifier architecture "batch": 128, "lr": 0.002, "epochs": 85, "enc_activ": "nn.Tanh()", "dec_activ": "nn.Tanh()", "varia_weight": 1.0, # Variational loss weight "class_weight": 0.5, # Classification loss weight "sinkh_weight": 1.0, # Sinkhorn loss weight "adam_betas": (0.9, 0.999), "outdir": "sinkclass_experiment_01", } # Run training (outputs to ./trained_aes/sinkclass_experiment_01/) train_main(train_config) # Output files generated: # - best_model.pt: PyTorch model weights # - hyperparameters.json: Model configuration # - model_architecture.txt: Network structure # - loss_epochs.pdf: Training/validation loss plot ``` -------------------------------- ### QSVM Configuration for Ideal and Noisy Simulations Source: https://context7.com/bb511/dimred4qsvm/llms.txt Defines configuration dictionaries for QSVM, specifying data paths, model parameters, and simulation types (ideal, noisy, hardware). The noisy configuration includes specific backend and API details for IBM Quantum. ```python qsvm_config = { "data_folder": "./data/ae_input/", "norm": "minmax", "nevents": "7.20e+05", "model_path": "./trained_aes/sinkclass_final/best_model.pt", "ntrain": 200, # Training events (quantum is expensive) "nvalid": 50, "ntest": 100, "seed": 12345, "feature_dim": 16, # Latent space dimension "c_param": 1.0, # SVM regularization parameter "run_type": "ideal", # "ideal", "noisy", or "hardware" "backend_name": None, # Required for "noisy" or "hardware" "ibmq_api_config": None, # IBMQ credentials dict "config": {}, # QuantumInstance kwargs "output_folder": "qsvm_ideal_run", } # Run QSVM training qsvm_main(qsvm_config) # For noisy simulation based on real quantum hardware: noisy_config = { **qsvm_config, "run_type": "noisy", "backend_name": "ibmq_guadalupe", "ibmq_api_config": { "token": "YOUR_IBMQ_TOKEN", "hub": "ibm-q", "group": "open", "project": "main", }, "config": {"shots": 1024}, "output_folder": "qsvm_noisy_guadalupe", } ``` -------------------------------- ### Train Sinkclass Autoencoder via CLI Source: https://context7.com/bb511/dimred4qsvm/llms.txt Trains a Sinkclass autoencoder, noted as the best performer, via the command-line interface. Includes parameters for autoencoder type, data, normalization, events, batch size, epochs, and specific Sinkclass weights. ```bash ./bin/ae_train \ --aetype sinkclass \ --data_folder ./data/ae_input/ \ --norm minmax \ --nevents 7.20e+05 \ --batch 128 \ --epochs 85 \ --sinkh_weight 1.0 \ --class_weight 0.5 \ --outdir sinkclass_best ``` -------------------------------- ### Train QSVM with Quantum Kernels using Python Source: https://context7.com/bb511/dimred4qsvm/llms.txt Implements quantum kernel-based classification using Qiskit's QuantumKernel with custom feature map circuits. Supports ideal simulation, noisy simulation with real hardware noise models, and execution on IBM quantum computers. Requires the 'qsvm' and 'qiskit' libraries. ```python from qsvm.main import main as qsvm_main # Example usage (specific arguments would depend on the desired configuration) # qsvm_main(config_params) ``` -------------------------------- ### Instantiate and Use Autoencoder Models with choose_ae_model Source: https://context7.com/bb511/dimred4qsvm/llms.txt The choose_ae_model utility function instantiates one of five autoencoder architectures, each inheriting from AE_vanilla and offering specialized loss functions. It supports configurable hyperparameters and loading pre-trained models. The forward pass returns latent space representation, classifier output, and reconstruction. ```python from autoencoders.util import choose_ae_model, import_hyperparams # Available autoencoder types: # - "vanilla": Standard MSE reconstruction loss # - "classifier": Reconstruction + classification BCE loss # - "variational": VAE with KL divergence regularization # - "sinkhorn": Reconstruction + Wasserstein distance on latent space # - "sinkclass": Reconstruction + Sinkhorn + Classification (best performer) hyperparams = { "ae_layers": [67, 64, 52, 44, 32, 24, 16], # Encoder architecture (67 -> 16) "lr": 0.002, # Learning rate "batch_size": 128, "enc_activ": "nn.Tanh()", # Encoder output activation "dec_activ": "nn.Tanh()", # Decoder output activation "sinkh_weight": 0.5, # Sinkhorn loss weight "class_weight": 0.5, # Classification loss weight } device = "cuda:0" # or "cpu" # Instantiate Sinkclass autoencoder (recommended) model = choose_ae_model("sinkclass", device, hyperparams) # Load a pre-trained model hp = import_hyperparams("./trained_aes/my_model/hyperparameters.json") model = choose_ae_model(hp["ae_type"], device, hp) model.load_model("./trained_aes/my_model/best_model.pt") # Forward pass returns (latent_space, classifier_output, reconstruction) import torch x_input = torch.randn(100, 67).to(device) # 100 events, 67 features latent, classif, recon = model.forward(x_input.float()) print(f"Latent shape: {latent.shape}") # torch.Size([100, 16]) print(f"Classifier output shape: {classif.shape}") # torch.Size([100, 1]) ``` -------------------------------- ### Load Quantum Data with Autoencoder Reduction in Python Source: https://context7.com/bb511/dimred4qsvm/llms.txt Integrates autoencoder-based feature reduction with quantum machine learning data preparation. Loads data, passes it through a trained autoencoder, and provides the reduced latent space for QSVM training with optional k-fold cross-validation support. Requires the 'qsvm' and 'autoencoders' libraries. ```python from qsvm.qdata import qdata # Initialize quantum data loader with pre-trained autoencoder quantum_data = qdata( data_folder="./data/ae_input/", norm_name="minmax", nevents="7.20e+05", model_path="./trained_aes/sinkclass_final/best_model.pt", train_events=500, # QSVM training events (keep small for quantum) valid_events=100, test_events=200, kfolds=5, # K-fold cross-validation splits seed=12345, ) # Get latent space representations for QSVM train_features = quantum_data.get_latent_space("train") # Shape: (500, 16) train_labels = quantum_data.ae_data.trtarget test_features = quantum_data.get_latent_space("test") # Shape: (200, 16) test_labels = quantum_data.ae_data.tetarget print(f"Training samples: {quantum_data.ntrain}") print(f"Latent dimensions: {train_features.shape[1]}") # Get k-folded data for robust evaluation kfold_test_data, kfold_test_labels = quantum_data.get_kfolded_data("test") print(f"K-folds: {len(kfold_test_data)}, Events per fold: {kfold_test_data[0].shape[0]}") ``` -------------------------------- ### Load and Preprocess Data with AE_data Class Source: https://context7.com/bb511/dimred4qsvm/llms.txt The AE_data class loads and preprocesses datasets from normalized numpy arrays. It handles configurable event counts, signal/background splitting, and generates PyTorch data loaders with GPU optimization. Supports various normalization types and random seeds for reproducibility. ```python from autoencoders.data import AE_data import torch # Initialize data loader with normalized data ae_data = AE_data( data_folder="./data/ae_input/", norm_name="minmax", # Normalization type: minmax, maxabs, etc. nevents="7.20e+05", # Total signal events in dataset train_events=50000, # Number of training events (-1 for all) valid_events=10000, # Number of validation events test_events=10000, # Number of test events seed=42 # Random seed for reproducibility ) # Output: # ---------------- # AE data loading complete: # Training data size: 5.00e+04 # Validation data size: 1.00e+04 # Test data size: 1.00e+04 # ---------------- # Access loaded numpy arrays print(f"Features per event: {ae_data.nfeats}") # 67 features print(f"Training data shape: {ae_data.trdata.shape}") print(f"Training targets shape: {ae_data.trtarget.shape}") # Create PyTorch data loaders for GPU training device = torch.device("cuda" if torch.cuda.is_available() else "cpu") train_loader = ae_data.get_loader("train", device, batch_size=128, shuffle=True) valid_loader = ae_data.get_loader("valid", device, batch_size=None, shuffle=False) # Split data into signal and background test_sig, test_bkg = ae_data.split_sig_bkg(ae_data.tedata, ae_data.tetarget) print(f"Signal events: {test_sig.shape[0]}, Background events: {test_bkg.shape[0]}") ``` -------------------------------- ### Evaluate Autoencoder Model with Python Source: https://context7.com/bb511/dimred4qsvm/llms.txt Evaluates trained autoencoders by computing validation/test losses, generating latent space visualizations, and producing ROC curves for each latent variable. Requires the 'autoencoders' library and a pre-trained model. ```python from autoencoders.test import main as test_main # Test configuration test_config = { "data_folder": "./data/ae_input/", "norm": "minmax", "nevents": "7.20e+05", "model_path": "./trained_aes/sinkclass_final/best_model.pt", } # Run evaluation (generates plots in model directory) test_main(test_config) # Output: # ---------------------------------- # VALID LOSS: 0.0234 # TEST LOSS: 0.0241 # ---------------------------------- # Latent plots were saved to ./trained_aes/sinkclass_final/latent_plots/ # Latent roc plots were saved to ./trained_aes/sinkclass_final/latent_roc/ # Input vs reco plots were saved to ./trained_aes/sinkclass_final/input_vs_reco/ # Manual prediction and analysis from autoencoders.util import choose_ae_model, import_hyperparams from autoencoders.data import AE_data import numpy as np hp = import_hyperparams("./trained_aes/sinkclass_final/hyperparameters.json") model = choose_ae_model(hp["ae_type"], "cpu", hp) model.load_model("./trained_aes/sinkclass_final/best_model.pt") ae_data = AE_data("./data/ae_input/", "minmax", "7.20e+05") test_sig, test_bkg = ae_data.split_sig_bkg(ae_data.tedata, ae_data.tetarget) # Get latent representations sig_latent, sig_recon, sig_classif = model.predict(test_sig) bkg_latent, bkg_recon, bkg_classif = model.predict(test_bkg) print(f"Latent space dimension: {sig_latent.shape[1]}") # 16 ``` -------------------------------- ### Quantum Feature Map Circuit Generation (Python) Source: https://context7.com/bb511/dimred4qsvm/llms.txt Constructs a parameterized quantum circuit for encoding classical data using U2 gates with data re-uploading. This circuit is suitable for use with Qiskit's QuantumKernel for QSVM. ```python from qsvm.feature_map_circuits import u2Reuploading from qiskit.visualization import circuit_drawer # Create feature map for 8 qubits encoding 16 features feature_map = u2Reuploading(nqubits=8, nfeatures=16) # Visualize the circuit print(feature_map.draw(output='text')) # ┌─────────────────┐ ┌─────────────────┐┌─────────────────┐ # q_0: ┤ U(π/2,x[0],x[1])├──■────────────┤ U(π/2,x[16],x[17])├┤ U(x[0],x[1],0) ├ # ├─────────────────┤┌─┴─┐ ├─────────────────┤├─────────────────┤ # q_1: ┤ U(π/2,x[2],x[3])├┤ X ├──■───────┤ U(π/2,x[18],x[19])├┤ U(x[2],x[3],0) ├ # └─────────────────┘└───┘┌─┴─┐ └─────────────────┘└─────────────────┘ # ... # Use with Qiskit's QuantumKernel from qiskit_machine_learning.kernels import QuantumKernel from qiskit.utils import QuantumInstance from qiskit import Aer quantum_instance = QuantumInstance( backend=Aer.get_backend("aer_simulator_statevector") ) kernel = QuantumKernel(feature_map=feature_map, quantum_instance=quantum_instance) # Compute kernel matrix import numpy as np train_data = np.random.randn(10, 16) # 10 samples, 16 features kernel_matrix = kernel.evaluate(x_vec=train_data) print(f"Kernel matrix shape: {kernel_matrix.shape}") # (10, 10) ``` -------------------------------- ### Grid Search for Hyperparameter Optimization Source: https://github.com/bb511/dimred4qsvm/blob/main/classical_nn/README.md Conducts a linear grid search to optimize the neural network's hyperparameters, specifically learning rate and batch size. It scans a defined hyperparameter space to find the best combination. Requires similar parameters as training, plus ranges for batch size and learning rate. ```python python grid_search.py --data_folder path/to/data/ --norm minmax --nevents 7.20e+05 --ae_model_path /path/to/saved_AE/best_model.pt --ntrain 12000 --nvalid 1500 --epochs 85 --outdir test_grid_search --batch_size 1024 --lr 0.01 --ntest 3600 --batch_size_space 512 1024 --learning_rate_space 0.005 0.01 ``` -------------------------------- ### Perform Inference with Trained Model Source: https://context7.com/bb511/dimred4qsvm/llms.txt Generates random test data and uses a trained model to perform inference, obtaining latent representations and predictions. Prints the shapes of the resulting latent and prediction arrays. ```python import numpy as np test_data = np.random.randn(100, 67).astype(np.float32) latent, predictions = model.predict(test_data) print(f"Latent shape: {latent.shape}") # (100, 16) print(f"Predictions shape: {predictions.shape}") # (100, 1) ``` -------------------------------- ### Train Neural Network Classifier Source: https://github.com/bb511/dimred4qsvm/blob/main/classical_nn/README.md Performs the training of the NN classifier. The saved model corresponds to the one that achieved the minimum validation loss during training. It requires data folder, normalization type, number of events, path to an AE model, training/validation counts, epochs, output directory, batch size, and learning rate. ```python python train.py --data_folder path/to/data/ --norm minmax --nevents 7.20e+05 --ae_model_path /path/to/saved_AE/best_model.pt --ntrain 12000 --nvalid 1500 --epochs 85 --outdir test_train --batch_size 1024 --lr 0.01 ``` -------------------------------- ### Test Neural Network Model Performance Source: https://github.com/bb511/dimred4qsvm/blob/main/classical_nn/README.md Evaluates the trained NN model on a k-folded test dataset. It computes ROC curves and AUC for each fold, then calculates the mean and standard deviation to assess model performance. Requires data folder, normalization, event counts, AE model path, validation/test counts, trained NN model path, and number of folds. ```python python test.py --data_folder path/to/data/ --norm minmax --nevents 7.20e+05 --ae_model_path /path/to/saved_AE/best_model.pt --nevents 7.20e+05 --nvalid 1500 --ntest 3600 --nn_model_path trained_nns/test_train/best_model.pt --kfolds 5 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.