@aleph-ai/tinyaleph 1.5.7 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/alexander-module.js +1469 -0
- package/core/arithmetic-link-kernel.js +1338 -0
- package/core/index.js +95 -2
- package/examples/01-hello-world.js +69 -0
- package/examples/02-basic-hash.js +90 -0
- package/examples/02-observer-stack.js +385 -0
- package/examples/03-quantum-coin.js +136 -0
- package/examples/05-symbolic-resonance.js +146 -0
- package/examples/06-symbol-database.js +150 -0
- package/examples/07-semantic-inference.js +223 -0
- package/examples/08-compound-symbols.js +219 -0
- package/examples/README.md +170 -0
- package/examples/ai/01-embeddings.js +155 -0
- package/examples/ai/02-semantic-memory.js +243 -0
- package/examples/ai/03-reasoning.js +243 -0
- package/examples/ai/04-knowledge-graph.js +279 -0
- package/examples/ai/05-llm-integration.js +333 -0
- package/examples/ai/06-agent.js +294 -0
- package/examples/ai/07-hybrid-ai.js +223 -0
- package/examples/ai/08-entropy-reasoning.js +259 -0
- package/examples/ai/09-concept-learning.js +271 -0
- package/examples/ai/10-prompt-primes.js +312 -0
- package/examples/ai/11-rag.js +332 -0
- package/examples/ai/12-neuro-symbolic.js +321 -0
- package/examples/ai/README.md +80 -0
- package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
- package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
- package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
- package/examples/arithmetic-topology/04-alexander-module.js +117 -0
- package/examples/arithmetic-topology/05-signature-memory.js +118 -0
- package/examples/arithmetic-topology/README.md +291 -0
- package/examples/bioinformatics/01-dna-encoding.js +108 -0
- package/examples/bioinformatics/02-central-dogma.js +162 -0
- package/examples/bioinformatics/03-protein-folding.js +206 -0
- package/examples/bioinformatics/04-dna-computing.js +192 -0
- package/examples/bioinformatics/05-molecular-binding.js +209 -0
- package/examples/chat.js +105 -0
- package/examples/crt-homology/01-residue-encoding.js +87 -0
- package/examples/crt-homology/02-birkhoff-attention.js +100 -0
- package/examples/crt-homology/03-homology-loss.js +132 -0
- package/examples/crt-homology/04-crt-resoformer.js +132 -0
- package/examples/crt-homology/README.md +67 -0
- package/examples/crypto/01-password-hash.js +210 -0
- package/examples/crypto/02-key-derivation.js +210 -0
- package/examples/crypto/03-hmac.js +229 -0
- package/examples/crypto/04-file-integrity.js +263 -0
- package/examples/crypto/05-content-hash.js +263 -0
- package/examples/crypto/README.md +99 -0
- package/examples/demo-modular.js +223 -0
- package/examples/demo-two-layer.js +196 -0
- package/examples/discrete/01-integer-sine-table.js +120 -0
- package/examples/discrete/02-codebook-tunneling.js +118 -0
- package/examples/discrete/03-canonical-fusion.js +135 -0
- package/examples/discrete/04-tick-gate.js +139 -0
- package/examples/discrete/README.md +142 -0
- package/examples/formal-semantics/01-typed-terms.js +156 -0
- package/examples/formal-semantics/02-reduction.js +202 -0
- package/examples/formal-semantics/03-lambda-translation.js +206 -0
- package/examples/formal-semantics/04-enochian-language.js +257 -0
- package/examples/formal-semantics/README.md +98 -0
- package/examples/math/01-quaternions.js +237 -0
- package/examples/math/02-octonions.js +192 -0
- package/examples/math/03-prime-factorization.js +215 -0
- package/examples/math/04-vector-spaces.js +210 -0
- package/examples/math/05-gaussian-primes.js +234 -0
- package/examples/math/README.md +93 -0
- package/examples/physics/01-oscillator.js +177 -0
- package/examples/physics/02-lyapunov.js +201 -0
- package/examples/physics/03-collapse.js +183 -0
- package/examples/physics/04-kuramoto.js +212 -0
- package/examples/physics/05-entropy.js +226 -0
- package/examples/physics/05-sync-models.js +298 -0
- package/examples/physics/06-primeon-ladder.js +233 -0
- package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
- package/examples/physics/README.md +126 -0
- package/examples/resonance/01-prime-hilbert-space.js +140 -0
- package/examples/resonance/02-prime-resonance-network.js +221 -0
- package/examples/resonance/03-resoformer.js +349 -0
- package/examples/resonance/04-resoformer-training.js +329 -0
- package/examples/resonance/05-language-model.js +484 -0
- package/examples/resonance/README.md +238 -0
- package/examples/run-examples.js +417 -0
- package/examples/scientific/01-single-qubit.js +185 -0
- package/examples/scientific/02-two-qubit.js +209 -0
- package/examples/scientific/03-quantum-circuits.js +270 -0
- package/examples/scientific/04-measurement.js +229 -0
- package/examples/scientific/05-algorithms.js +245 -0
- package/examples/scientific/06-random.js +225 -0
- package/examples/scientific/07-wavefunction.js +192 -0
- package/examples/scientific/README.md +118 -0
- package/examples/semantic/01-vocabulary.js +186 -0
- package/examples/semantic/02-similarity.js +263 -0
- package/examples/semantic/03-word-algebra.js +295 -0
- package/examples/semantic/04-clustering.js +348 -0
- package/examples/semantic/05-classification.js +386 -0
- package/examples/semantic/06-dna-encoding.js +228 -0
- package/examples/semantic/07-search.js +304 -0
- package/examples/semantic/08-qa-system.js +278 -0
- package/examples/semantic/README.md +116 -0
- package/examples/topology/01-108-invariant.js +81 -0
- package/examples/topology/02-trefoil-constants.js +112 -0
- package/examples/topology/03-gauge-symmetry.js +112 -0
- package/examples/topology/04-free-energy-dynamics.js +124 -0
- package/examples/topology/README.md +129 -0
- package/index.js +32 -0
- package/modular.js +63 -2
- package/package.json +8 -3
- package/physics/alk-kuramoto.js +817 -0
- package/physics/index.js +23 -2
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example Quantum Measurement
|
|
3
|
+
* @description Explore quantum measurement and state collapse
|
|
4
|
+
*
|
|
5
|
+
* Measurement in quantum mechanics:
|
|
6
|
+
* - Collapses superposition to definite state
|
|
7
|
+
* - Results are probabilistic
|
|
8
|
+
* - Cannot be undone
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const { ScientificBackend, Hypercomplex } = require('../../modular');
|
|
12
|
+
|
|
13
|
+
// ===========================================
|
|
14
|
+
// SETUP
|
|
15
|
+
// ===========================================
|
|
16
|
+
|
|
17
|
+
const backend = new ScientificBackend({ dimension: 4 });
|
|
18
|
+
|
|
19
|
+
console.log('TinyAleph Quantum Measurement Example');
|
|
20
|
+
console.log('=====================================\n');
|
|
21
|
+
|
|
22
|
+
// ===========================================
|
|
23
|
+
// MEASUREMENT BASICS
|
|
24
|
+
// ===========================================
|
|
25
|
+
|
|
26
|
+
console.log('Measurement Basics:');
|
|
27
|
+
console.log('-'.repeat(50) + '\n');
|
|
28
|
+
|
|
29
|
+
console.log('Before measurement: |ψ⟩ = α|0⟩ + β|1⟩');
|
|
30
|
+
console.log('After measurement: |0⟩ or |1⟩ (definite state)');
|
|
31
|
+
console.log('Probability: P(0) = |α|², P(1) = |β|²\n');
|
|
32
|
+
|
|
33
|
+
// Create superposition
|
|
34
|
+
var ket0 = backend.encode('|0⟩');
|
|
35
|
+
var superposition = backend.applyGate(ket0, 'H');
|
|
36
|
+
var state = backend.primesToState(superposition);
|
|
37
|
+
|
|
38
|
+
console.log('Superposition state |+⟩:');
|
|
39
|
+
console.log(' Components: [' + state.c.slice(0, 2).map(function(v) { return v.toFixed(4); }).join(', ') + ']');
|
|
40
|
+
console.log(' Norm: ' + state.norm().toFixed(4) + '\n');
|
|
41
|
+
|
|
42
|
+
// ===========================================
|
|
43
|
+
// REPEATED MEASUREMENTS
|
|
44
|
+
// ===========================================
|
|
45
|
+
|
|
46
|
+
console.log('='.repeat(50));
|
|
47
|
+
console.log('Repeated Measurements:');
|
|
48
|
+
console.log('='.repeat(50) + '\n');
|
|
49
|
+
|
|
50
|
+
function runMeasurements(trials, prepareState) {
|
|
51
|
+
var results = {};
|
|
52
|
+
|
|
53
|
+
for (var i = 0; i < trials; i++) {
|
|
54
|
+
var state = prepareState();
|
|
55
|
+
var result = backend.measure(state);
|
|
56
|
+
results[result] = (results[result] || 0) + 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return results;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Equal superposition
|
|
63
|
+
console.log('Measuring |+⟩ = (|0⟩ + |1⟩)/√2:\n');
|
|
64
|
+
|
|
65
|
+
var equalResults = runMeasurements(1000, function() {
|
|
66
|
+
var primes = backend.applyGate(ket0, 'H');
|
|
67
|
+
return backend.primesToState(primes);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
for (var result in equalResults) {
|
|
71
|
+
if (equalResults.hasOwnProperty(result)) {
|
|
72
|
+
var count = equalResults[result];
|
|
73
|
+
var pct = (count / 1000 * 100).toFixed(1);
|
|
74
|
+
console.log(' |' + result + '⟩: ' + count + ' (' + pct + '%)');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
console.log(' Expected: ~50% each\n');
|
|
78
|
+
|
|
79
|
+
// ===========================================
|
|
80
|
+
// BIASED SUPERPOSITION
|
|
81
|
+
// ===========================================
|
|
82
|
+
|
|
83
|
+
console.log('='.repeat(50));
|
|
84
|
+
console.log('Biased Superpositions:');
|
|
85
|
+
console.log('='.repeat(50) + '\n');
|
|
86
|
+
|
|
87
|
+
console.log('States with unequal amplitudes have biased probabilities.\n');
|
|
88
|
+
|
|
89
|
+
// Simulate biased state (|ψ⟩ = √0.8|0⟩ + √0.2|1⟩)
|
|
90
|
+
function simulateBiasedMeasurement(prob0) {
|
|
91
|
+
return Math.random() < prob0 ? '0' : '1';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
var biasedResults = { '0': 0, '1': 0 };
|
|
95
|
+
var trials = 1000;
|
|
96
|
+
var prob0 = 0.8;
|
|
97
|
+
|
|
98
|
+
for (var i = 0; i < trials; i++) {
|
|
99
|
+
var result = simulateBiasedMeasurement(prob0);
|
|
100
|
+
biasedResults[result]++;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
console.log('Measuring |ψ⟩ = √0.8|0⟩ + √0.2|1⟩:\n');
|
|
104
|
+
console.log(' |0⟩: ' + biasedResults['0'] + ' (' + (biasedResults['0']/trials*100).toFixed(1) + '%)');
|
|
105
|
+
console.log(' |1⟩: ' + biasedResults['1'] + ' (' + (biasedResults['1']/trials*100).toFixed(1) + '%)');
|
|
106
|
+
console.log(' Expected: ~80% |0⟩, ~20% |1⟩');
|
|
107
|
+
|
|
108
|
+
// ===========================================
|
|
109
|
+
// MEASUREMENT COLLAPSE
|
|
110
|
+
// ===========================================
|
|
111
|
+
|
|
112
|
+
console.log('\n' + '='.repeat(50));
|
|
113
|
+
console.log('Measurement Collapse:');
|
|
114
|
+
console.log('='.repeat(50) + '\n');
|
|
115
|
+
|
|
116
|
+
console.log('After measurement, the state is collapsed.');
|
|
117
|
+
console.log('Repeated measurements of the SAME state give SAME result.\n');
|
|
118
|
+
|
|
119
|
+
// Demonstrate collapse
|
|
120
|
+
var collapsedState = null;
|
|
121
|
+
var firstMeasurement = null;
|
|
122
|
+
var subsequentResults = [];
|
|
123
|
+
|
|
124
|
+
// First measurement collapses the state
|
|
125
|
+
for (var i = 0; i < 10; i++) {
|
|
126
|
+
if (i === 0) {
|
|
127
|
+
var primes = backend.applyGate(ket0, 'H');
|
|
128
|
+
collapsedState = backend.primesToState(primes);
|
|
129
|
+
firstMeasurement = backend.measure(collapsedState);
|
|
130
|
+
subsequentResults.push(firstMeasurement);
|
|
131
|
+
} else {
|
|
132
|
+
// After collapse, measurement is deterministic
|
|
133
|
+
subsequentResults.push(firstMeasurement);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
console.log('Initial measurement: |' + firstMeasurement + '⟩');
|
|
138
|
+
console.log('Subsequent measurements (same collapsed state):');
|
|
139
|
+
console.log(' ' + subsequentResults.join(', '));
|
|
140
|
+
console.log(' All same (state is collapsed)');
|
|
141
|
+
|
|
142
|
+
// ===========================================
|
|
143
|
+
// MULTI-QUBIT MEASUREMENT
|
|
144
|
+
// ===========================================
|
|
145
|
+
|
|
146
|
+
console.log('\n' + '='.repeat(50));
|
|
147
|
+
console.log('Multi-Qubit Measurement:');
|
|
148
|
+
console.log('='.repeat(50) + '\n');
|
|
149
|
+
|
|
150
|
+
console.log('Measuring multiple qubits:');
|
|
151
|
+
console.log(' Can measure all at once or sequentially');
|
|
152
|
+
console.log(' Entangled qubits show correlations\n');
|
|
153
|
+
|
|
154
|
+
// Simulate Bell state measurement
|
|
155
|
+
var bellResults = { '00': 0, '01': 0, '10': 0, '11': 0 };
|
|
156
|
+
trials = 1000;
|
|
157
|
+
|
|
158
|
+
for (var i = 0; i < trials; i++) {
|
|
159
|
+
// Bell state has 50% |00⟩ and 50% |11⟩
|
|
160
|
+
var r = Math.random();
|
|
161
|
+
if (r < 0.5) {
|
|
162
|
+
bellResults['00']++;
|
|
163
|
+
} else {
|
|
164
|
+
bellResults['11']++;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
console.log('Measuring Bell state |Φ+⟩ = (|00⟩ + |11⟩)/√2:\n');
|
|
169
|
+
for (var outcome in bellResults) {
|
|
170
|
+
if (bellResults.hasOwnProperty(outcome)) {
|
|
171
|
+
var count = bellResults[outcome];
|
|
172
|
+
console.log(' |' + outcome + '⟩: ' + count + ' (' + (count/trials*100).toFixed(1) + '%)');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
console.log(' Note: |01⟩ and |10⟩ never occur (entanglement)');
|
|
176
|
+
|
|
177
|
+
// ===========================================
|
|
178
|
+
// MEASUREMENT STATISTICS
|
|
179
|
+
// ===========================================
|
|
180
|
+
|
|
181
|
+
console.log('\n' + '='.repeat(50));
|
|
182
|
+
console.log('Measurement Statistics:');
|
|
183
|
+
console.log('='.repeat(50) + '\n');
|
|
184
|
+
|
|
185
|
+
function computeStatistics(results, total) {
|
|
186
|
+
var stats = {};
|
|
187
|
+
|
|
188
|
+
for (var outcome in results) {
|
|
189
|
+
if (results.hasOwnProperty(outcome)) {
|
|
190
|
+
var count = results[outcome];
|
|
191
|
+
var frequency = count / total;
|
|
192
|
+
var stdError = Math.sqrt(frequency * (1 - frequency) / total);
|
|
193
|
+
|
|
194
|
+
stats[outcome] = {
|
|
195
|
+
count: count,
|
|
196
|
+
frequency: frequency,
|
|
197
|
+
stdError: stdError
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return stats;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
var stats = computeStatistics(equalResults, 1000);
|
|
206
|
+
|
|
207
|
+
console.log('Statistical analysis of equal superposition:\n');
|
|
208
|
+
for (var outcome in stats) {
|
|
209
|
+
if (stats.hasOwnProperty(outcome)) {
|
|
210
|
+
var s = stats[outcome];
|
|
211
|
+
console.log(' |' + outcome + '⟩:');
|
|
212
|
+
console.log(' Frequency: ' + (s.frequency * 100).toFixed(2) + '%');
|
|
213
|
+
console.log(' Standard Error: ' + (s.stdError * 100).toFixed(2) + '%');
|
|
214
|
+
console.log(' 95% CI: [' + ((s.frequency - 1.96*s.stdError)*100).toFixed(1) + '%, ' + ((s.frequency + 1.96*s.stdError)*100).toFixed(1) + '%]');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ===========================================
|
|
219
|
+
// KEY TAKEAWAYS
|
|
220
|
+
// ===========================================
|
|
221
|
+
|
|
222
|
+
console.log('\n' + '='.repeat(50));
|
|
223
|
+
console.log('KEY TAKEAWAYS:');
|
|
224
|
+
console.log('1. Measurement collapses superposition');
|
|
225
|
+
console.log('2. Results are probabilistic (|amplitude|²)');
|
|
226
|
+
console.log('3. Collapsed states give deterministic results');
|
|
227
|
+
console.log('4. Entangled qubits show correlated outcomes');
|
|
228
|
+
console.log('5. Statistical analysis quantifies uncertainty');
|
|
229
|
+
console.log('6. Cannot predict individual measurement outcomes');
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example Quantum Algorithms
|
|
3
|
+
* @description Implement basic quantum algorithms
|
|
4
|
+
*
|
|
5
|
+
* Famous quantum algorithms that demonstrate quantum advantage:
|
|
6
|
+
* - Deutsch-Jozsa: Determines function properties
|
|
7
|
+
* - Grover: Searches unstructured data
|
|
8
|
+
* - Quantum Teleportation: Transfers quantum states
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const { ScientificBackend, Hypercomplex } = require('../../modular');
|
|
12
|
+
|
|
13
|
+
// ===========================================
|
|
14
|
+
// SETUP
|
|
15
|
+
// ===========================================
|
|
16
|
+
|
|
17
|
+
const backend = new ScientificBackend({ dimension: 8 });
|
|
18
|
+
|
|
19
|
+
console.log('TinyAleph Quantum Algorithms Example');
|
|
20
|
+
console.log('====================================\n');
|
|
21
|
+
|
|
22
|
+
// ===========================================
|
|
23
|
+
// DEUTSCH-JOZSA ALGORITHM
|
|
24
|
+
// ===========================================
|
|
25
|
+
|
|
26
|
+
console.log('Deutsch-Jozsa Algorithm:');
|
|
27
|
+
console.log('-'.repeat(50) + '\n');
|
|
28
|
+
|
|
29
|
+
console.log('Problem: Given f(x), determine if it is:');
|
|
30
|
+
console.log(' - Constant: f(x) = 0 for all x, or f(x) = 1 for all x');
|
|
31
|
+
console.log(' - Balanced: f(x) = 0 for half, f(x) = 1 for half\n');
|
|
32
|
+
|
|
33
|
+
console.log('Classical: Need up to 2^(n-1) + 1 queries');
|
|
34
|
+
console.log('Quantum: Need exactly 1 query!\n');
|
|
35
|
+
|
|
36
|
+
function deutschJozsa(oracle) {
|
|
37
|
+
// Simplified simulation of Deutsch-Jozsa
|
|
38
|
+
// In full implementation, we'd apply H gates, oracle, H gates, measure
|
|
39
|
+
|
|
40
|
+
var steps = [
|
|
41
|
+
'Initialize |0...0⟩|1⟩',
|
|
42
|
+
'Apply H to all qubits',
|
|
43
|
+
'Apply oracle U_f',
|
|
44
|
+
'Apply H to input qubits',
|
|
45
|
+
'Measure input qubits'
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
console.log('Algorithm steps:');
|
|
49
|
+
for (var i = 0; i < steps.length; i++) {
|
|
50
|
+
console.log(' ' + (i + 1) + '. ' + steps[i]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Simulate result based on oracle type
|
|
54
|
+
var result = oracle.type === 'constant' ? '000' : '001'; // Non-zero means balanced
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
measurement: result,
|
|
58
|
+
conclusion: result === '000' ? 'Constant' : 'Balanced',
|
|
59
|
+
queries: 1
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Test with constant and balanced oracles
|
|
64
|
+
var constantOracle = { type: 'constant', f: function(x) { return 0; } };
|
|
65
|
+
var balancedOracle = { type: 'balanced', f: function(x) { return x % 2; } };
|
|
66
|
+
|
|
67
|
+
console.log('\nConstant function (f(x) = 0):');
|
|
68
|
+
var result1 = deutschJozsa(constantOracle);
|
|
69
|
+
console.log(' Measurement: |' + result1.measurement + '⟩');
|
|
70
|
+
console.log(' Conclusion: ' + result1.conclusion);
|
|
71
|
+
console.log(' Queries used: ' + result1.queries);
|
|
72
|
+
|
|
73
|
+
console.log('\nBalanced function (f(x) = x mod 2):');
|
|
74
|
+
var result2 = deutschJozsa(balancedOracle);
|
|
75
|
+
console.log(' Measurement: |' + result2.measurement + '⟩');
|
|
76
|
+
console.log(' Conclusion: ' + result2.conclusion);
|
|
77
|
+
console.log(' Queries used: ' + result2.queries);
|
|
78
|
+
|
|
79
|
+
// ===========================================
|
|
80
|
+
// GROVER'S SEARCH ALGORITHM
|
|
81
|
+
// ===========================================
|
|
82
|
+
|
|
83
|
+
console.log('\n' + '='.repeat(50));
|
|
84
|
+
console.log("Grover's Search Algorithm:");
|
|
85
|
+
console.log('='.repeat(50) + '\n');
|
|
86
|
+
|
|
87
|
+
console.log('Problem: Find a marked item in an unstructured database');
|
|
88
|
+
console.log('Classical: O(N) queries needed');
|
|
89
|
+
console.log('Quantum: O(√N) queries needed!\n');
|
|
90
|
+
|
|
91
|
+
function groverSearch(databaseSize, markedItem) {
|
|
92
|
+
var n = Math.ceil(Math.log2(databaseSize));
|
|
93
|
+
var iterations = Math.floor(Math.PI / 4 * Math.sqrt(databaseSize));
|
|
94
|
+
|
|
95
|
+
console.log('Database size: ' + databaseSize);
|
|
96
|
+
console.log('Qubits needed: ' + n);
|
|
97
|
+
console.log('Grover iterations: ' + iterations);
|
|
98
|
+
|
|
99
|
+
// Simulate Grover steps
|
|
100
|
+
var steps = [
|
|
101
|
+
'Initialize: |s⟩ = H⊗n|0⟩ (equal superposition)',
|
|
102
|
+
'Repeat ' + iterations + ' times:',
|
|
103
|
+
' a. Apply oracle O_f (phase flip marked item)',
|
|
104
|
+
' b. Apply diffusion operator D',
|
|
105
|
+
'Measure'
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
console.log('\nAlgorithm:');
|
|
109
|
+
for (var i = 0; i < steps.length; i++) {
|
|
110
|
+
console.log(' ' + steps[i]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Probability of finding marked item
|
|
114
|
+
var successProb = Math.sin((2 * iterations + 1) * Math.asin(1 / Math.sqrt(databaseSize))) ** 2;
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
markedItem: markedItem,
|
|
118
|
+
iterations: iterations,
|
|
119
|
+
successProbability: successProb,
|
|
120
|
+
classicalQueries: databaseSize / 2
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var groverResult = groverSearch(16, 7);
|
|
125
|
+
console.log('\nResult:');
|
|
126
|
+
console.log(' Looking for item: ' + groverResult.markedItem);
|
|
127
|
+
console.log(' Success probability: ' + (groverResult.successProbability * 100).toFixed(1) + '%');
|
|
128
|
+
console.log(' Quantum queries: ' + groverResult.iterations);
|
|
129
|
+
console.log(' Classical average: ' + groverResult.classicalQueries);
|
|
130
|
+
console.log(' Speedup: ' + (groverResult.classicalQueries / groverResult.iterations).toFixed(1) + 'x');
|
|
131
|
+
|
|
132
|
+
// ===========================================
|
|
133
|
+
// QUANTUM TELEPORTATION
|
|
134
|
+
// ===========================================
|
|
135
|
+
|
|
136
|
+
console.log('\n' + '='.repeat(50));
|
|
137
|
+
console.log('Quantum Teleportation:');
|
|
138
|
+
console.log('='.repeat(50) + '\n');
|
|
139
|
+
|
|
140
|
+
console.log('Transfer quantum state |ψ⟩ using entanglement and classical bits.\n');
|
|
141
|
+
|
|
142
|
+
function teleportation(inputState) {
|
|
143
|
+
console.log('Protocol:');
|
|
144
|
+
console.log(' 1. Alice and Bob share Bell pair |Φ+⟩');
|
|
145
|
+
console.log(' 2. Alice has state to teleport: ' + inputState);
|
|
146
|
+
console.log(' 3. Alice performs Bell measurement on her qubits');
|
|
147
|
+
console.log(' 4. Alice sends 2 classical bits to Bob');
|
|
148
|
+
console.log(' 5. Bob applies correction based on classical bits');
|
|
149
|
+
console.log(' 6. Bob recovers ' + inputState + '\n');
|
|
150
|
+
|
|
151
|
+
// Simulate teleportation
|
|
152
|
+
var measurementOutcomes = ['00', '01', '10', '11'];
|
|
153
|
+
var outcome = measurementOutcomes[Math.floor(Math.random() * 4)];
|
|
154
|
+
|
|
155
|
+
var corrections = {
|
|
156
|
+
'00': 'None (I)',
|
|
157
|
+
'01': 'X gate',
|
|
158
|
+
'10': 'Z gate',
|
|
159
|
+
'11': 'Z then X gates'
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
inputState: inputState,
|
|
164
|
+
bellMeasurement: outcome,
|
|
165
|
+
correction: corrections[outcome],
|
|
166
|
+
outputState: inputState,
|
|
167
|
+
success: true
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
var teleResult = teleportation('α|0⟩ + β|1⟩');
|
|
172
|
+
console.log('Simulation:');
|
|
173
|
+
console.log(' Input state: ' + teleResult.inputState);
|
|
174
|
+
console.log(' Bell measurement: |' + teleResult.bellMeasurement + '⟩');
|
|
175
|
+
console.log(' Bob applies: ' + teleResult.correction);
|
|
176
|
+
console.log(' Output state: ' + teleResult.outputState);
|
|
177
|
+
console.log(' Teleportation successful: ' + teleResult.success);
|
|
178
|
+
|
|
179
|
+
// ===========================================
|
|
180
|
+
// QUANTUM PHASE ESTIMATION
|
|
181
|
+
// ===========================================
|
|
182
|
+
|
|
183
|
+
console.log('\n' + '='.repeat(50));
|
|
184
|
+
console.log('Quantum Phase Estimation:');
|
|
185
|
+
console.log('='.repeat(50) + '\n');
|
|
186
|
+
|
|
187
|
+
console.log('Estimates the phase θ of a unitary U where U|ψ⟩ = e^(2πiθ)|ψ⟩');
|
|
188
|
+
console.log('Key subroutine for many quantum algorithms.\n');
|
|
189
|
+
|
|
190
|
+
function phaseEstimation(truePhase, precision) {
|
|
191
|
+
var n = precision;
|
|
192
|
+
|
|
193
|
+
console.log('True phase: ' + truePhase);
|
|
194
|
+
console.log('Precision bits: ' + n);
|
|
195
|
+
console.log('Phase resolution: 1/' + Math.pow(2, n) + ' = ' + (1/Math.pow(2, n)).toFixed(4));
|
|
196
|
+
|
|
197
|
+
// Simulate phase estimation
|
|
198
|
+
// In real algorithm: controlled-U gates, inverse QFT, measure
|
|
199
|
+
|
|
200
|
+
var estimatedBinary = Math.round(truePhase * Math.pow(2, n));
|
|
201
|
+
var estimatedPhase = estimatedBinary / Math.pow(2, n);
|
|
202
|
+
var error = Math.abs(truePhase - estimatedPhase);
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
truePhase: truePhase,
|
|
206
|
+
estimatedPhase: estimatedPhase,
|
|
207
|
+
error: error,
|
|
208
|
+
precision: n
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
var qpe = phaseEstimation(0.375, 4);
|
|
213
|
+
console.log('\nResult:');
|
|
214
|
+
console.log(' True phase: ' + qpe.truePhase);
|
|
215
|
+
console.log(' Estimated: ' + qpe.estimatedPhase);
|
|
216
|
+
console.log(' Error: ' + qpe.error);
|
|
217
|
+
|
|
218
|
+
// ===========================================
|
|
219
|
+
// ALGORITHM COMPARISON
|
|
220
|
+
// ===========================================
|
|
221
|
+
|
|
222
|
+
console.log('\n' + '='.repeat(50));
|
|
223
|
+
console.log('Algorithm Comparison:');
|
|
224
|
+
console.log('='.repeat(50) + '\n');
|
|
225
|
+
|
|
226
|
+
console.log('Algorithm Problem Classical Quantum');
|
|
227
|
+
console.log('-'.repeat(70));
|
|
228
|
+
console.log('Deutsch-Jozsa Function type O(2^n) O(1)');
|
|
229
|
+
console.log('Grover Search O(N) O(√N)');
|
|
230
|
+
console.log('Shor Factoring O(exp) O((log N)³)');
|
|
231
|
+
console.log('Phase Estimation Eigenvalue O(poly) O(log(1/ε))');
|
|
232
|
+
console.log('Teleportation State transfer N/A O(1) + 2 bits');
|
|
233
|
+
|
|
234
|
+
// ===========================================
|
|
235
|
+
// KEY TAKEAWAYS
|
|
236
|
+
// ===========================================
|
|
237
|
+
|
|
238
|
+
console.log('\n' + '='.repeat(50));
|
|
239
|
+
console.log('KEY TAKEAWAYS:');
|
|
240
|
+
console.log('1. Deutsch-Jozsa shows exponential speedup');
|
|
241
|
+
console.log('2. Grover provides quadratic speedup for search');
|
|
242
|
+
console.log('3. Teleportation transfers states without moving qubits');
|
|
243
|
+
console.log('4. Phase estimation underlies many algorithms');
|
|
244
|
+
console.log('5. Quantum advantage varies by problem type');
|
|
245
|
+
console.log('6. Not all problems have quantum speedup');
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example Quantum Random Number Generation
|
|
3
|
+
* @description Generate true random numbers using quantum measurement
|
|
4
|
+
*
|
|
5
|
+
* Quantum mechanics provides a source of true randomness:
|
|
6
|
+
* - Measurement outcomes are fundamentally unpredictable
|
|
7
|
+
* - Not pseudo-random, truly random
|
|
8
|
+
* - Used in cryptography and simulations
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const { ScientificBackend, Hypercomplex } = require('../../modular');
|
|
12
|
+
|
|
13
|
+
// ===========================================
|
|
14
|
+
// SETUP
|
|
15
|
+
// ===========================================
|
|
16
|
+
|
|
17
|
+
const backend = new ScientificBackend({ dimension: 16 });
|
|
18
|
+
|
|
19
|
+
console.log('TinyAleph Quantum Random Number Generator');
|
|
20
|
+
console.log('=========================================\n');
|
|
21
|
+
|
|
22
|
+
// ===========================================
|
|
23
|
+
// QUANTUM RANDOM BIT
|
|
24
|
+
// ===========================================
|
|
25
|
+
|
|
26
|
+
console.log('Quantum Random Bit:');
|
|
27
|
+
console.log('-'.repeat(50) + '\n');
|
|
28
|
+
|
|
29
|
+
console.log('Creating superposition and measuring produces a random bit.\n');
|
|
30
|
+
|
|
31
|
+
function quantumRandomBit() {
|
|
32
|
+
// Create |+⟩ state (equal superposition of |0⟩ and |1⟩)
|
|
33
|
+
var superposition = backend.encode('|+⟩'); // [2, 3] = equal weights
|
|
34
|
+
var state = backend.primesToState(superposition);
|
|
35
|
+
|
|
36
|
+
// Use the built-in measure method
|
|
37
|
+
var result = backend.measure(state);
|
|
38
|
+
return result.outcome;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
console.log('Generating 20 quantum random bits:');
|
|
42
|
+
var bits = [];
|
|
43
|
+
for (var i = 0; i < 20; i++) {
|
|
44
|
+
bits.push(quantumRandomBit());
|
|
45
|
+
}
|
|
46
|
+
console.log(' ' + bits.join(''));
|
|
47
|
+
|
|
48
|
+
// ===========================================
|
|
49
|
+
// RANDOM INTEGERS
|
|
50
|
+
// ===========================================
|
|
51
|
+
|
|
52
|
+
console.log('\n' + '='.repeat(50));
|
|
53
|
+
console.log('Random Integers:');
|
|
54
|
+
console.log('='.repeat(50) + '\n');
|
|
55
|
+
|
|
56
|
+
function quantumRandomInt(max) {
|
|
57
|
+
var bitsNeeded = Math.ceil(Math.log2(max + 1));
|
|
58
|
+
var attempts = 0;
|
|
59
|
+
var maxAttempts = 100; // Prevent infinite loop
|
|
60
|
+
|
|
61
|
+
// Rejection sampling for uniform distribution
|
|
62
|
+
while (attempts < maxAttempts) {
|
|
63
|
+
attempts++;
|
|
64
|
+
var value = 0;
|
|
65
|
+
for (var i = 0; i < bitsNeeded; i++) {
|
|
66
|
+
value = (value << 1) | quantumRandomBit();
|
|
67
|
+
}
|
|
68
|
+
if (value <= max) {
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Fallback to modulo if rejection sampling fails
|
|
73
|
+
return value % (max + 1);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
console.log('Random integers 0-9:');
|
|
77
|
+
var ints = [];
|
|
78
|
+
for (var i = 0; i < 10; i++) {
|
|
79
|
+
ints.push(quantumRandomInt(9));
|
|
80
|
+
}
|
|
81
|
+
console.log(' ' + ints.join(', '));
|
|
82
|
+
|
|
83
|
+
console.log('\nRandom integers 0-99:');
|
|
84
|
+
var largerInts = [];
|
|
85
|
+
for (var i = 0; i < 5; i++) {
|
|
86
|
+
largerInts.push(quantumRandomInt(99));
|
|
87
|
+
}
|
|
88
|
+
console.log(' ' + largerInts.join(', '));
|
|
89
|
+
|
|
90
|
+
// ===========================================
|
|
91
|
+
// RANDOM BYTES
|
|
92
|
+
// ===========================================
|
|
93
|
+
|
|
94
|
+
console.log('\n' + '='.repeat(50));
|
|
95
|
+
console.log('Random Bytes:');
|
|
96
|
+
console.log('='.repeat(50) + '\n');
|
|
97
|
+
|
|
98
|
+
function quantumRandomBytes(n) {
|
|
99
|
+
var bytes = [];
|
|
100
|
+
for (var i = 0; i < n; i++) {
|
|
101
|
+
var byte = 0;
|
|
102
|
+
for (var j = 0; j < 8; j++) {
|
|
103
|
+
byte = (byte << 1) | quantumRandomBit();
|
|
104
|
+
}
|
|
105
|
+
bytes.push(byte);
|
|
106
|
+
}
|
|
107
|
+
return bytes;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var randomBytes = quantumRandomBytes(8);
|
|
111
|
+
console.log('8 random bytes (decimal):');
|
|
112
|
+
console.log(' ' + randomBytes.join(', '));
|
|
113
|
+
|
|
114
|
+
console.log('\nHexadecimal:');
|
|
115
|
+
var hex = randomBytes.map(function(b) {
|
|
116
|
+
return b.toString(16).padStart(2, '0');
|
|
117
|
+
}).join('');
|
|
118
|
+
console.log(' 0x' + hex);
|
|
119
|
+
|
|
120
|
+
// ===========================================
|
|
121
|
+
// RANDOM UUID
|
|
122
|
+
// ===========================================
|
|
123
|
+
|
|
124
|
+
console.log('\n' + '='.repeat(50));
|
|
125
|
+
console.log('Random UUID:');
|
|
126
|
+
console.log('='.repeat(50) + '\n');
|
|
127
|
+
|
|
128
|
+
function quantumUUID() {
|
|
129
|
+
var bytes = quantumRandomBytes(16);
|
|
130
|
+
|
|
131
|
+
// Set version 4 (random) and variant bits
|
|
132
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
133
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
134
|
+
|
|
135
|
+
var hex = bytes.map(function(b) {
|
|
136
|
+
return b.toString(16).padStart(2, '0');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return hex.slice(0, 4).join('') + '-' +
|
|
140
|
+
hex.slice(4, 6).join('') + '-' +
|
|
141
|
+
hex.slice(6, 8).join('') + '-' +
|
|
142
|
+
hex.slice(8, 10).join('') + '-' +
|
|
143
|
+
hex.slice(10, 16).join('');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
console.log('Quantum random UUIDs:');
|
|
147
|
+
for (var i = 0; i < 3; i++) {
|
|
148
|
+
console.log(' ' + quantumUUID());
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ===========================================
|
|
152
|
+
// STATISTICAL TESTING
|
|
153
|
+
// ===========================================
|
|
154
|
+
|
|
155
|
+
console.log('\n' + '='.repeat(50));
|
|
156
|
+
console.log('Statistical Testing:');
|
|
157
|
+
console.log('='.repeat(50) + '\n');
|
|
158
|
+
|
|
159
|
+
function chiSquareTest(observed, expected) {
|
|
160
|
+
var chiSquare = 0;
|
|
161
|
+
for (var i = 0; i < observed.length; i++) {
|
|
162
|
+
var diff = observed[i] - expected[i];
|
|
163
|
+
chiSquare += (diff * diff) / expected[i];
|
|
164
|
+
}
|
|
165
|
+
return chiSquare;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Generate many random bits and test distribution
|
|
169
|
+
var trials = 500; // Reduced for faster execution
|
|
170
|
+
var counts = [0, 0];
|
|
171
|
+
|
|
172
|
+
for (var i = 0; i < trials; i++) {
|
|
173
|
+
counts[quantumRandomBit()]++;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
var expected = [trials / 2, trials / 2];
|
|
177
|
+
var chiSq = chiSquareTest(counts, expected);
|
|
178
|
+
|
|
179
|
+
console.log('Bit distribution test (' + trials + ' trials):');
|
|
180
|
+
console.log(' Zeros: ' + counts[0] + ' (expected: ' + expected[0] + ')');
|
|
181
|
+
console.log(' Ones: ' + counts[1] + ' (expected: ' + expected[1] + ')');
|
|
182
|
+
console.log(' Chi-square: ' + chiSq.toFixed(4));
|
|
183
|
+
console.log(' Passes if χ² < 3.84 (95% confidence): ' + (chiSq < 3.84 ? 'PASS' : 'FAIL'));
|
|
184
|
+
|
|
185
|
+
// ===========================================
|
|
186
|
+
// CRYPTOGRAPHIC APPLICATIONS
|
|
187
|
+
// ===========================================
|
|
188
|
+
|
|
189
|
+
console.log('\n' + '='.repeat(50));
|
|
190
|
+
console.log('Cryptographic Applications:');
|
|
191
|
+
console.log('='.repeat(50) + '\n');
|
|
192
|
+
|
|
193
|
+
console.log('Quantum randomness is ideal for cryptography:\n');
|
|
194
|
+
|
|
195
|
+
// Generate a random key
|
|
196
|
+
var keyBits = [];
|
|
197
|
+
for (var i = 0; i < 128; i++) {
|
|
198
|
+
keyBits.push(quantumRandomBit());
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
console.log('128-bit random key:');
|
|
202
|
+
var keyHex = '';
|
|
203
|
+
for (var i = 0; i < 128; i += 4) {
|
|
204
|
+
var nibble = (keyBits[i] << 3) | (keyBits[i+1] << 2) | (keyBits[i+2] << 1) | keyBits[i+3];
|
|
205
|
+
keyHex += nibble.toString(16);
|
|
206
|
+
}
|
|
207
|
+
console.log(' ' + keyHex);
|
|
208
|
+
|
|
209
|
+
// Generate random nonce
|
|
210
|
+
console.log('\nRandom nonce (64-bit):');
|
|
211
|
+
var nonce = quantumRandomBytes(8);
|
|
212
|
+
console.log(' ' + nonce.map(function(b) { return b.toString(16).padStart(2, '0'); }).join(''));
|
|
213
|
+
|
|
214
|
+
// ===========================================
|
|
215
|
+
// KEY TAKEAWAYS
|
|
216
|
+
// ===========================================
|
|
217
|
+
|
|
218
|
+
console.log('\n' + '='.repeat(50));
|
|
219
|
+
console.log('KEY TAKEAWAYS:');
|
|
220
|
+
console.log('1. Quantum measurement provides true randomness');
|
|
221
|
+
console.log('2. Not pseudo-random - fundamentally unpredictable');
|
|
222
|
+
console.log('3. Build larger random values from random bits');
|
|
223
|
+
console.log('4. Use rejection sampling for uniform distribution');
|
|
224
|
+
console.log('5. Statistical tests verify randomness quality');
|
|
225
|
+
console.log('6. Essential for cryptographic key generation');
|