UNPKG

@iota-big3/sdk-quantum

Version:

Quantum-ready architecture with post-quantum cryptography

469 lines 16.6 kB
"use strict"; /** * Clean Quantum SDK Implementation * @iota-big3/sdk-quantum */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuantumCircuitTemplates = exports.QuantumCircuitBuilder = exports.QuantumAlgorithmTemplates = exports.QuantumUtils = exports.QuantumSDK = exports.QuantumManager = void 0; const events_1 = require("events"); const quantum_manager_clean_1 = require("./core/quantum-manager-clean"); const types_clean_1 = require("./types-clean"); // Re-export all types __exportStar(require("./types-clean"), exports); var quantum_manager_clean_2 = require("./core/quantum-manager-clean"); Object.defineProperty(exports, "QuantumManager", { enumerable: true, get: function () { return quantum_manager_clean_2.QuantumManager; } }); /** * Main Quantum SDK Class */ class QuantumSDK extends events_1.EventEmitter { constructor(config = {}) { super(); this.config = config; this.manager = new quantum_manager_clean_1.QuantumManager(config); // Forward events from manager this.manager.on('quantum-event', (event) => { this.emit('event', event); this.emit(event.type, event); }); } // Post-Quantum Cryptography Methods async generateKeyPair(algorithm) { return this.manager.generateKeyPair(algorithm); } async encrypt(data, keyId) { return this.manager.encrypt(data, keyId); } async decrypt(encryptionResult) { return this.manager.decrypt(encryptionResult); } async sign(data, keyId) { return this.manager.sign(data, keyId); } // Quantum Simulation Methods async createCircuit(name, qubits) { return this.manager.createCircuit(name, qubits); } async executeCircuit(circuitId, shots) { return this.manager.executeCircuit(circuitId, shots); } // Quantum Storage Methods async storeData(key, data) { return this.manager.storeData(key, data); } // Health and Control async healthCheck() { return this.manager.healthCheck(); } isEnabled() { return this.manager.isEnabled(); } enable() { this.manager.enable(); } disable() { this.manager.disable(); } // Data Access getKeyPairs() { return this.manager.getKeyPairs(); } getCircuits() { return this.manager.getCircuits(); } getDataEntries() { return this.manager.getDataEntries(); } } exports.QuantumSDK = QuantumSDK; /** * Utility Functions for Quantum Operations */ class QuantumUtils { /** * Generate secure random bytes using quantum-safe methods */ static generateSecureRandom(length) { const bytes = new Uint8Array(length); if (typeof crypto !== 'undefined' && crypto.getRandomValues) { crypto.getRandomValues(bytes); } else { // Fallback for environments without crypto API for (let i = 0; i < length; i++) { bytes[i] = Math.floor(Math.random() * 256); } } return bytes; } /** * Calculate quantum-safe hash */ static calculateQuantumHash(data) { // Simple hash implementation for demonstration let hash = 0; for (let i = 0; i < data.length; i++) { hash = ((hash << 5) - hash + data[i]) & 0xffffffff; } return hash.toString(16).padStart(8, '0'); } /** * Validate post-quantum algorithm security level */ static validateSecurityLevel(algorithm, level) { const algorithmLevels = { [types_clean_1.PostQuantumAlgorithm.KYBER]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_3, types_clean_1.SecurityLevel.LEVEL_5], [types_clean_1.PostQuantumAlgorithm.DILITHIUM]: [types_clean_1.SecurityLevel.LEVEL_2, types_clean_1.SecurityLevel.LEVEL_3, types_clean_1.SecurityLevel.LEVEL_5], [types_clean_1.PostQuantumAlgorithm.FALCON]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_5], [types_clean_1.PostQuantumAlgorithm.SPHINCS_PLUS]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_3, types_clean_1.SecurityLevel.LEVEL_5], [types_clean_1.PostQuantumAlgorithm.NTRU]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_3], [types_clean_1.PostQuantumAlgorithm.SABER]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_3, types_clean_1.SecurityLevel.LEVEL_5], [types_clean_1.PostQuantumAlgorithm.FRODO_KEM]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_3, types_clean_1.SecurityLevel.LEVEL_5], [types_clean_1.PostQuantumAlgorithm.CLASSIC_MCELIECE]: [types_clean_1.SecurityLevel.LEVEL_1, types_clean_1.SecurityLevel.LEVEL_3, types_clean_1.SecurityLevel.LEVEL_5] }; return algorithmLevels[algorithm]?.includes(level) || false; } /** * Estimate quantum advantage for an algorithm */ static estimateQuantumAdvantage(classicalComplexity, quantumComplexity) { if (classicalComplexity.includes('2^n') && quantumComplexity.includes('n^')) { return types_clean_1.QuantumAdvantage.EXPONENTIAL; } if (classicalComplexity.includes('n^2') && quantumComplexity.includes('n')) { return types_clean_1.QuantumAdvantage.QUADRATIC; } if (classicalComplexity.includes('n') && quantumComplexity.includes('log')) { return types_clean_1.QuantumAdvantage.POLYNOMIAL; } return types_clean_1.QuantumAdvantage.UNKNOWN; } /** * Convert binary string to quantum state amplitudes */ static binaryToAmplitudes(binaryString) { const n = binaryString.length; const numStates = Math.pow(2, n); const amplitudes = Array(numStates).fill(0).map(() => ({ real: 0, imaginary: 0 })); const stateIndex = parseInt(binaryString, 2); amplitudes[stateIndex] = { real: 1, imaginary: 0 }; return amplitudes; } /** * Calculate quantum circuit depth */ static calculateCircuitDepth(gates) { const qubitLayers = {}; let maxDepth = 0; for (const gate of gates) { const maxQubitLayer = Math.max(...gate.qubits.map(q => qubitLayers[q] || 0)); const newLayer = maxQubitLayer + 1; for (const qubit of gate.qubits) { qubitLayers[qubit] = newLayer; } maxDepth = Math.max(maxDepth, newLayer); } return maxDepth; } } exports.QuantumUtils = QuantumUtils; /** * Quantum Algorithm Templates */ class QuantumAlgorithmTemplates { /** * Create Grover's search algorithm template */ static createGroverAlgorithm(databaseSize) { const qubits = Math.ceil(Math.log2(databaseSize)); const iterations = Math.floor(Math.PI / 4 * Math.sqrt(databaseSize)); return { id: 'grover-search', name: "Grover's Search Algorithm", description: 'Quantum search algorithm with quadratic speedup', type: types_clean_1.AlgorithmType.SEARCH, complexity: { timeComplexity: 'O(√N)', spaceComplexity: 'O(log N)', quantumAdvantage: types_clean_1.QuantumAdvantage.QUADRATIC, requiredQubits: qubits, requiredDepth: iterations * 4 // Approximate depth per iteration }, parameters: [ { name: 'database_size', type: 'integer', description: 'Size of the search database', required: true, defaultValue: databaseSize, constraints: { min: 1, max: Math.pow(2, 20) } }, { name: 'target_item', type: 'string', description: 'Item to search for', required: true } ] }; } /** * Create Shor's factoring algorithm template */ static createShorAlgorithm(numberToFactor) { const qubits = Math.ceil(Math.log2(numberToFactor)) * 2; return { id: 'shor-factoring', name: "Shor's Factoring Algorithm", description: 'Quantum algorithm for integer factorization', type: types_clean_1.AlgorithmType.FACTORING, complexity: { timeComplexity: 'O((log N)³)', spaceComplexity: 'O(log N)', quantumAdvantage: types_clean_1.QuantumAdvantage.EXPONENTIAL, requiredQubits: qubits, requiredDepth: qubits * 100 // Approximate depth }, parameters: [ { name: 'number', type: 'integer', description: 'Number to factor', required: true, defaultValue: numberToFactor, constraints: { min: 4, max: Math.pow(2, 30) } } ] }; } /** * Create Quantum Fourier Transform template */ static createQFTAlgorithm(qubits) { return { id: 'quantum-fourier-transform', name: 'Quantum Fourier Transform', description: 'Quantum version of the discrete Fourier transform', type: types_clean_1.AlgorithmType.SIMULATION, complexity: { timeComplexity: 'O(n²)', spaceComplexity: 'O(n)', quantumAdvantage: types_clean_1.QuantumAdvantage.EXPONENTIAL, requiredQubits: qubits, requiredDepth: qubits * (qubits + 1) / 2 }, parameters: [ { name: 'qubits', type: 'integer', description: 'Number of qubits', required: true, defaultValue: qubits, constraints: { min: 1, max: 50 } } ] }; } /** * Create Variational Quantum Eigensolver template */ static createVQEAlgorithm(moleculeSize) { const qubits = moleculeSize * 2; // Approximate qubit requirement return { id: 'variational-quantum-eigensolver', name: 'Variational Quantum Eigensolver', description: 'Hybrid quantum-classical algorithm for finding ground state energies', type: types_clean_1.AlgorithmType.CHEMISTRY, complexity: { timeComplexity: 'O(poly(n))', spaceComplexity: 'O(n)', quantumAdvantage: types_clean_1.QuantumAdvantage.POLYNOMIAL, requiredQubits: qubits, requiredDepth: 100 // Variable depth based on ansatz }, parameters: [ { name: 'molecule_size', type: 'integer', description: 'Number of atoms in molecule', required: true, defaultValue: moleculeSize, constraints: { min: 1, max: 20 } }, { name: 'ansatz_type', type: 'string', description: 'Type of quantum ansatz', required: false, defaultValue: 'UCCSD', constraints: { allowedValues: ['UCCSD', 'Hardware-efficient', 'ADAPT-VQE'] } } ] }; } } exports.QuantumAlgorithmTemplates = QuantumAlgorithmTemplates; /** * Quantum Circuit Builder */ class QuantumCircuitBuilder { constructor(name, qubits) { this.gates = []; this.name = name; this.qubits = qubits; } /** * Add Hadamard gate */ h(qubit) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.H, qubits: [qubit] }); return this; } /** * Add Pauli-X gate */ x(qubit) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.X, qubits: [qubit] }); return this; } /** * Add Pauli-Y gate */ y(qubit) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.Y, qubits: [qubit] }); return this; } /** * Add Pauli-Z gate */ z(qubit) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.Z, qubits: [qubit] }); return this; } /** * Add CNOT gate */ cnot(control, target) { this.validateQubit(control); this.validateQubit(target); this.gates.push({ type: types_clean_1.GateType.CNOT, qubits: [control, target] }); return this; } /** * Add rotation around X axis */ rx(qubit, angle) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.RX, qubits: [qubit], parameters: [angle] }); return this; } /** * Add rotation around Y axis */ ry(qubit, angle) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.RY, qubits: [qubit], parameters: [angle] }); return this; } /** * Add rotation around Z axis */ rz(qubit, angle) { this.validateQubit(qubit); this.gates.push({ type: types_clean_1.GateType.RZ, qubits: [qubit], parameters: [angle] }); return this; } /** * Build the quantum circuit */ build() { return { name: this.name, qubits: this.qubits, classicalBits: this.qubits, gates: this.gates.map((gate, index) => ({ id: `gate-${index}`, type: gate.type, qubits: gate.qubits, parameters: gate.parameters })), measurements: Array.from({ length: this.qubits }, (_, i) => ({ id: `measurement-${i}`, qubit: i, classicalBit: i })) }; } validateQubit(qubit) { if (qubit < 0 || qubit >= this.qubits) { throw new Error(`Invalid qubit index: ${qubit}. Circuit has ${this.qubits} qubits.`); } } } exports.QuantumCircuitBuilder = QuantumCircuitBuilder; /** * Pre-built Quantum Circuit Templates */ class QuantumCircuitTemplates { /** * Create Bell state preparation circuit */ static bellState() { return new QuantumCircuitBuilder('Bell State', 2) .h(0) .cnot(0, 1) .build(); } /** * Create GHZ state preparation circuit */ static ghzState(qubits) { const builder = new QuantumCircuitBuilder('GHZ State', qubits).h(0); for (let i = 1; i < qubits; i++) { builder.cnot(0, i); } return builder.build(); } /** * Create quantum teleportation circuit */ static quantumTeleportation() { return new QuantumCircuitBuilder('Quantum Teleportation', 3) .h(1) .cnot(1, 2) .cnot(0, 1) .h(0) .build(); } /** * Create quantum random number generator circuit */ static randomNumberGenerator(bits) { const builder = new QuantumCircuitBuilder('Random Number Generator', bits); for (let i = 0; i < bits; i++) { builder.h(i); } return builder.build(); } } exports.QuantumCircuitTemplates = QuantumCircuitTemplates; // Default export exports.default = QuantumSDK; //# sourceMappingURL=index-clean.js.map