UNPKG

@astermind/astermind-premium

Version:

Astermind Premium - Premium ML Toolkit

190 lines 6.92 kB
// quantum-inspired-elm.ts — Quantum-Inspired ELM // Quantum computing principles for feature maps and optimization import { ELM } from '@astermind/astermind-elm'; import { requireLicense } from '../core/license.js'; /** * Quantum-Inspired ELM * Features: * - Quantum feature maps * - Quantum superposition * - Quantum entanglement * - Quantum kernel methods */ export class QuantumInspiredELM { constructor(options) { this.trained = false; this.quantumStates = []; requireLicense(); // Premium feature - requires valid license this.categories = options.categories; this.options = { categories: options.categories, hiddenUnits: options.hiddenUnits ?? 256, quantumLayers: options.quantumLayers ?? 2, entanglement: options.entanglement ?? true, superposition: options.superposition ?? true, activation: options.activation ?? 'relu', maxLen: options.maxLen ?? 100, useTokenizer: options.useTokenizer ?? true, }; this.elm = new ELM({ useTokenizer: this.options.useTokenizer ? true : undefined, hiddenUnits: this.options.hiddenUnits, categories: this.options.categories, maxLen: this.options.maxLen, activation: this.options.activation, }); } /** * Train with quantum-inspired features */ train(X, y) { // Prepare labels const labelIndices = y.map(label => typeof label === 'number' ? label : this.options.categories.indexOf(label)); // Apply quantum feature maps const quantumFeatures = this._applyQuantumFeatureMap(X); // Train ELM this.elm.setCategories?.(this.options.categories); this.elm.trainFromData?.(quantumFeatures, labelIndices); this.trained = true; } /** * Apply quantum feature map */ _applyQuantumFeatureMap(X) { const features = []; for (const x of X) { let quantumState = this._encodeToQuantumState(x); // Apply quantum layers for (let layer = 0; layer < this.options.quantumLayers; layer++) { quantumState = this._applyQuantumLayer(quantumState, layer); } // Measure quantum state (convert to classical features) const measured = this._measureQuantumState(quantumState); features.push(measured); } return features; } /** * Encode classical data to quantum state */ _encodeToQuantumState(x) { // Quantum state encoding (amplitude encoding) const state = new Array(Math.pow(2, Math.ceil(Math.log2(x.length)))).fill(0); // Normalize input const norm = Math.sqrt(x.reduce((sum, v) => sum + v * v, 0)); if (norm > 0) { for (let i = 0; i < x.length; i++) { state[i] = x[i] / norm; } } return state; } /** * Apply quantum layer (quantum gates simulation) */ _applyQuantumLayer(state, layer) { let newState = [...state]; // Apply quantum gates (simplified simulation) if (this.options.superposition) { // Hadamard-like transformation (superposition) newState = this._applySuperposition(newState); } if (this.options.entanglement) { // Entanglement (CNOT-like) newState = this._applyEntanglement(newState); } // Rotation gates newState = this._applyRotation(newState, layer); return newState; } /** * Apply superposition (Hadamard-like) */ _applySuperposition(state) { const newState = new Array(state.length).fill(0); const factor = 1 / Math.sqrt(2); for (let i = 0; i < state.length; i++) { for (let j = 0; j < state.length; j++) { // Simplified Hadamard transformation const phase = (i === j) ? factor : factor * Math.cos(Math.PI * i * j / state.length); newState[i] += state[j] * phase; } } return newState; } /** * Apply entanglement (CNOT-like) */ _applyEntanglement(state) { const newState = [...state]; // Entangle pairs of qubits for (let i = 0; i < state.length - 1; i += 2) { const temp = newState[i]; newState[i] = newState[i + 1]; newState[i + 1] = temp; } return newState; } /** * Apply rotation gates */ _applyRotation(state, layer) { const newState = new Array(state.length).fill(0); const angle = Math.PI / (2 * (layer + 1)); for (let i = 0; i < state.length; i++) { const cos = Math.cos(angle); const sin = Math.sin(angle); newState[i] = state[i] * cos - state[(i + 1) % state.length] * sin; } return newState; } /** * Measure quantum state (convert to classical) */ _measureQuantumState(state) { // Measure by computing probabilities (amplitudes squared) const probabilities = state.map(amp => amp * amp); // Project to hidden units dimension const hiddenDim = this.options.hiddenUnits; const features = new Array(hiddenDim).fill(0); for (let i = 0; i < hiddenDim; i++) { const idx = i % probabilities.length; features[i] = probabilities[idx]; } return features; } /** * Predict with quantum-inspired model */ predict(X, topK = 3) { if (!this.trained) { throw new Error('Model must be trained before prediction'); } const XArray = Array.isArray(X[0]) ? X : [X]; const results = []; for (const x of XArray) { // Apply quantum feature map const quantumFeatures = this._applyQuantumFeatureMap([x])[0]; // Predict const preds = this.elm.predictFromVector?.([quantumFeatures], topK) || []; // Get quantum state for this input let quantumState = this._encodeToQuantumState(x); for (let layer = 0; layer < this.options.quantumLayers; layer++) { quantumState = this._applyQuantumLayer(quantumState, layer); } const amplitude = Math.sqrt(quantumState.reduce((sum, v) => sum + v * v, 0)); for (const pred of preds.slice(0, topK)) { results.push({ label: pred.label || this.options.categories[pred.index || 0], prob: pred.prob || 0, quantumState: [...quantumState], amplitude, }); } } return results; } } //# sourceMappingURL=quantum-inspired-elm.js.map