UNPKG

@iota-big3/sdk-quantum

Version:

Quantum-ready architecture with post-quantum cryptography

389 lines 13.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuantumCircuitTemplates = exports.QuantumCircuitBuilder = exports.QuantumAlgorithmTemplates = exports.QuantumUtils = exports.QuantumSDK = exports.QuantumManager = void 0; const tslib_1 = require("tslib"); const events_1 = require("events"); const quantum_manager_1 = require("./core/quantum-manager"); crypto; from; 'crypto'; AlgorithmType, EncryptionResult, GateType, KeyPair, PostQuantumAlgorithm, QuantumAdvantage, QuantumAlgorithm, QuantumCircuit, QuantumDataEntry, QuantumError, QuantumEvent, QuantumSDKConfig, SecurityLevel, SignatureResult, SimulationResult; from; './types'; var quantum_manager_2 = require("./core/quantum-manager"); Object.defineProperty(exports, "QuantumManager", { enumerable: true, get: function () { return quantum_manager_2.QuantumManager; } }); tslib_1.__exportStar(require("./types"), exports); class QuantumSDK extends events_1.EventEmitter { constructor(config = {}) { super(); this.config = config; this.manager = new quantum_manager_1.QuantumManager(config); this.manager.on('quantum-event', (event) => { this.emit('event', event); this.emit(event.type, event); }); } async generateKeyPair(algorithm) { return this.manager.generateKeyPair(algorithm); } async encrypt(data, keyId) { try { return await this.manager.encrypt(data, keyId); } catch (error) { this.emit('error', error); throw new QuantumError('Encryption failed', 'ENCRYPT_ERROR'); } } async decrypt(encryptionResult) { try { return await this.manager.decrypt(encryptionResult); } catch (error) { this.emit('error', error); throw new QuantumError('Decryption failed', 'DECRYPT_ERROR'); } } async sign(data, keyId) { return this.manager.sign(data, keyId); } async createCircuit(name, qubits) { return this.manager.createCircuit(name, qubits); } async executeCircuit(circuitId, shots) { return this.manager.executeCircuit(circuitId, shots); } async storeData(key, data) { return this.manager.storeData(key, data); } async healthCheck() { return this.manager.healthCheck(); } isEnabled() { return this.manager.isEnabled(); } enable() { this.manager.enable(); } disable() { this.manager.disable(); } getKeyPairs() { return this.manager.getKeyPairs(); } getCircuits() { return this.manager.getCircuits(); } getDataEntries() { return this.manager.getDataEntries(); } } exports.QuantumSDK = QuantumSDK; class QuantumUtils { static generateSecureRandom(length) { const bytes = new Uint8Array(length); if (typeof crypto !== 'undefined' && crypto.getRandomValues) { crypto.getRandomValues(bytes); } else { for (let i = 0; i < length; i++) { bytes[i] = Math.floor(crypto.randomInt(0, Number.MAX_SAFE_INTEGER) / Number.MAX_SAFE_INTEGER * 256); } } return bytes; } static calculateQuantumHash(data) { 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'); } static validateSecurityLevel(algorithm, level) { const algorithmLevels = { [PostQuantumAlgorithm.KYBER]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_3, SecurityLevel.LEVEL_5], [PostQuantumAlgorithm.DILITHIUM]: [SecurityLevel.LEVEL_2, SecurityLevel.LEVEL_3, SecurityLevel.LEVEL_5], [PostQuantumAlgorithm.FALCON]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_5], [PostQuantumAlgorithm.SPHINCS_PLUS]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_3, SecurityLevel.LEVEL_5], [PostQuantumAlgorithm.NTRU]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_3], [PostQuantumAlgorithm.SABER]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_3, SecurityLevel.LEVEL_5], [PostQuantumAlgorithm.FRODO_KEM]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_3, SecurityLevel.LEVEL_5], [PostQuantumAlgorithm.CLASSIC_MCELIECE]: [SecurityLevel.LEVEL_1, SecurityLevel.LEVEL_3, SecurityLevel.LEVEL_5] }; return algorithmLevels[algorithm]?.includes(level) || false; } static estimateQuantumAdvantage(classicalComplexity, quantumComplexity) { if (classicalComplexity.includes('2^n') && quantumComplexity.includes('n^')) { return QuantumAdvantage.EXPONENTIAL; } if (classicalComplexity.includes('n^2') && quantumComplexity.includes('n')) { return QuantumAdvantage.QUADRATIC; } if (classicalComplexity.includes('n') && quantumComplexity.includes('log')) { return QuantumAdvantage.POLYNOMIAL; } return QuantumAdvantage.UNKNOWN; } 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; } 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; class QuantumAlgorithmTemplates { 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: AlgorithmType.SEARCH, complexity: { timeComplexity: 'O(√N)', spaceComplexity: 'O(log N)', quantumAdvantage: QuantumAdvantage.QUADRATIC, requiredQubits: qubits, requiredDepth: iterations * 4 }, 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 } ] }; } 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: AlgorithmType.FACTORING, complexity: { timeComplexity: 'O((log N)³)', spaceComplexity: 'O(log N)', quantumAdvantage: QuantumAdvantage.EXPONENTIAL, requiredQubits: qubits, requiredDepth: qubits * 100 }, parameters: [ { name: 'number', type: 'integer', description: 'Number to factor', required: true, defaultValue: numberToFactor, constraints: { min: 4, max: Math.pow(2, 30) } } ] }; } static createQFTAlgorithm(qubits) { return { id: 'quantum-fourier-transform', name: 'Quantum Fourier Transform', description: 'Quantum version of the discrete Fourier transform', type: AlgorithmType.SIMULATION, complexity: { timeComplexity: 'O(n²)', spaceComplexity: 'O(n)', quantumAdvantage: 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 } } ] }; } static createVQEAlgorithm(moleculeSize) { const qubits = moleculeSize * 2; return { id: 'variational-quantum-eigensolver', name: 'Variational Quantum Eigensolver', description: 'Hybrid quantum-classical algorithm for finding ground state energies', type: AlgorithmType.CHEMISTRY, complexity: { timeComplexity: 'O(poly(n))', spaceComplexity: 'O(n)', quantumAdvantage: QuantumAdvantage.POLYNOMIAL, requiredQubits: qubits, requiredDepth: 100 }, 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; class QuantumCircuitBuilder { constructor(name, qubits) { this.gates = []; this.name = name; this.qubits = qubits; } h(qubit) { this.validateQubit(qubit); this.gates.push({ type: GateType.H, qubits: [qubit] }); return this; } x(qubit) { this.validateQubit(qubit); this.gates.push({ type: GateType.X, qubits: [qubit] }); return this; } y(qubit) { this.validateQubit(qubit); this.gates.push({ type: GateType.Y, qubits: [qubit] }); return this; } z(qubit) { this.validateQubit(qubit); this.gates.push({ type: GateType.Z, qubits: [qubit] }); return this; } cnot(control, target) { this.validateQubit(control); this.validateQubit(target); this.gates.push({ type: GateType.CNOT, qubits: [control, target] }); return this; } rx(qubit, angle) { this.validateQubit(qubit); this.gates.push({ type: GateType.RX, qubits: [qubit], parameters: [angle] }); return this; } ry(qubit, angle) { this.validateQubit(qubit); this.gates.push({ type: GateType.RY, qubits: [qubit], parameters: [angle] }); return this; } rz(qubit, angle) { this.validateQubit(qubit); this.gates.push({ type: GateType.RZ, qubits: [qubit], parameters: [angle] }); return this; } 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; class QuantumCircuitTemplates { static bellState() { return new QuantumCircuitBuilder('Bell State', 2) .h(0) .cnot(0, 1) .build(); } 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(); } static quantumTeleportation() { return new QuantumCircuitBuilder('Quantum Teleportation', 3) .h(1) .cnot(1, 2) .cnot(0, 1) .h(0) .build(); } 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; exports.default = QuantumSDK; //# sourceMappingURL=index.js.map