@aleph-ai/tinyaleph 1.5.6 → 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.
Files changed (121) hide show
  1. package/core/alexander-module.js +1469 -0
  2. package/core/arithmetic-link-kernel.js +1338 -0
  3. package/core/index.js +95 -2
  4. package/examples/01-hello-world.js +69 -0
  5. package/examples/02-basic-hash.js +90 -0
  6. package/examples/02-observer-stack.js +385 -0
  7. package/examples/03-quantum-coin.js +136 -0
  8. package/examples/05-symbolic-resonance.js +146 -0
  9. package/examples/06-symbol-database.js +150 -0
  10. package/examples/07-semantic-inference.js +223 -0
  11. package/examples/08-compound-symbols.js +219 -0
  12. package/examples/README.md +170 -0
  13. package/examples/ai/01-embeddings.js +155 -0
  14. package/examples/ai/02-semantic-memory.js +243 -0
  15. package/examples/ai/03-reasoning.js +243 -0
  16. package/examples/ai/04-knowledge-graph.js +279 -0
  17. package/examples/ai/05-llm-integration.js +333 -0
  18. package/examples/ai/06-agent.js +294 -0
  19. package/examples/ai/07-hybrid-ai.js +223 -0
  20. package/examples/ai/08-entropy-reasoning.js +259 -0
  21. package/examples/ai/09-concept-learning.js +271 -0
  22. package/examples/ai/10-prompt-primes.js +312 -0
  23. package/examples/ai/11-rag.js +332 -0
  24. package/examples/ai/12-neuro-symbolic.js +321 -0
  25. package/examples/ai/README.md +80 -0
  26. package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
  27. package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
  28. package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
  29. package/examples/arithmetic-topology/04-alexander-module.js +117 -0
  30. package/examples/arithmetic-topology/05-signature-memory.js +118 -0
  31. package/examples/arithmetic-topology/README.md +291 -0
  32. package/examples/bioinformatics/01-dna-encoding.js +108 -0
  33. package/examples/bioinformatics/02-central-dogma.js +162 -0
  34. package/examples/bioinformatics/03-protein-folding.js +206 -0
  35. package/examples/bioinformatics/04-dna-computing.js +192 -0
  36. package/examples/bioinformatics/05-molecular-binding.js +209 -0
  37. package/examples/chat.js +105 -0
  38. package/examples/crt-homology/01-residue-encoding.js +87 -0
  39. package/examples/crt-homology/02-birkhoff-attention.js +100 -0
  40. package/examples/crt-homology/03-homology-loss.js +132 -0
  41. package/examples/crt-homology/04-crt-resoformer.js +132 -0
  42. package/examples/crt-homology/README.md +67 -0
  43. package/examples/crypto/01-password-hash.js +210 -0
  44. package/examples/crypto/02-key-derivation.js +210 -0
  45. package/examples/crypto/03-hmac.js +229 -0
  46. package/examples/crypto/04-file-integrity.js +263 -0
  47. package/examples/crypto/05-content-hash.js +263 -0
  48. package/examples/crypto/README.md +99 -0
  49. package/examples/demo-modular.js +223 -0
  50. package/examples/demo-two-layer.js +196 -0
  51. package/examples/discrete/01-integer-sine-table.js +120 -0
  52. package/examples/discrete/02-codebook-tunneling.js +118 -0
  53. package/examples/discrete/03-canonical-fusion.js +135 -0
  54. package/examples/discrete/04-tick-gate.js +139 -0
  55. package/examples/discrete/README.md +142 -0
  56. package/examples/formal-semantics/01-typed-terms.js +156 -0
  57. package/examples/formal-semantics/02-reduction.js +202 -0
  58. package/examples/formal-semantics/03-lambda-translation.js +206 -0
  59. package/examples/formal-semantics/04-enochian-language.js +257 -0
  60. package/examples/formal-semantics/README.md +98 -0
  61. package/examples/math/01-quaternions.js +237 -0
  62. package/examples/math/02-octonions.js +192 -0
  63. package/examples/math/03-prime-factorization.js +215 -0
  64. package/examples/math/04-vector-spaces.js +210 -0
  65. package/examples/math/05-gaussian-primes.js +234 -0
  66. package/examples/math/README.md +93 -0
  67. package/examples/physics/01-oscillator.js +177 -0
  68. package/examples/physics/02-lyapunov.js +201 -0
  69. package/examples/physics/03-collapse.js +183 -0
  70. package/examples/physics/04-kuramoto.js +212 -0
  71. package/examples/physics/05-entropy.js +226 -0
  72. package/examples/physics/05-sync-models.js +298 -0
  73. package/examples/physics/06-primeon-ladder.js +233 -0
  74. package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
  75. package/examples/physics/README.md +126 -0
  76. package/examples/resonance/01-prime-hilbert-space.js +140 -0
  77. package/examples/resonance/02-prime-resonance-network.js +221 -0
  78. package/examples/resonance/03-resoformer.js +349 -0
  79. package/examples/resonance/04-resoformer-training.js +329 -0
  80. package/examples/resonance/05-language-model.js +484 -0
  81. package/examples/resonance/README.md +238 -0
  82. package/examples/run-examples.js +417 -0
  83. package/examples/scientific/01-single-qubit.js +185 -0
  84. package/examples/scientific/02-two-qubit.js +209 -0
  85. package/examples/scientific/03-quantum-circuits.js +270 -0
  86. package/examples/scientific/04-measurement.js +229 -0
  87. package/examples/scientific/05-algorithms.js +245 -0
  88. package/examples/scientific/06-random.js +225 -0
  89. package/examples/scientific/07-wavefunction.js +192 -0
  90. package/examples/scientific/README.md +118 -0
  91. package/examples/semantic/01-vocabulary.js +186 -0
  92. package/examples/semantic/02-similarity.js +263 -0
  93. package/examples/semantic/03-word-algebra.js +295 -0
  94. package/examples/semantic/04-clustering.js +348 -0
  95. package/examples/semantic/05-classification.js +386 -0
  96. package/examples/semantic/06-dna-encoding.js +228 -0
  97. package/examples/semantic/07-search.js +304 -0
  98. package/examples/semantic/08-qa-system.js +278 -0
  99. package/examples/semantic/README.md +116 -0
  100. package/examples/topology/01-108-invariant.js +81 -0
  101. package/examples/topology/02-trefoil-constants.js +112 -0
  102. package/examples/topology/03-gauge-symmetry.js +112 -0
  103. package/examples/topology/04-free-energy-dynamics.js +124 -0
  104. package/examples/topology/README.md +129 -0
  105. package/index.js +32 -0
  106. package/modular.js +63 -2
  107. package/observer/agency.js +885 -0
  108. package/observer/assays.js +973 -0
  109. package/observer/boundary.js +1155 -0
  110. package/observer/entanglement.js +673 -0
  111. package/observer/hqe.js +1465 -0
  112. package/observer/index.js +158 -0
  113. package/observer/prsc.js +1289 -0
  114. package/observer/safety.js +815 -0
  115. package/observer/smf.js +1015 -0
  116. package/observer/symbolic-smf.js +726 -0
  117. package/observer/symbolic-temporal.js +790 -0
  118. package/observer/temporal.js +669 -0
  119. package/package.json +9 -3
  120. package/physics/alk-kuramoto.js +817 -0
  121. package/physics/index.js +23 -2
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @example Single Qubit Operations
3
+ * @description Simulate single qubit quantum gates
4
+ *
5
+ * TinyAleph can simulate quantum computing concepts:
6
+ * - Qubits represented as hypercomplex states
7
+ * - Quantum gates as transformations
8
+ * - Measurement with probabilistic collapse
9
+ */
10
+
11
+ const { ScientificBackend, Hypercomplex } = require('../../modular');
12
+
13
+ // ===========================================
14
+ // SETUP
15
+ // ===========================================
16
+
17
+ const backend = new ScientificBackend({ dimension: 2 });
18
+
19
+ console.log('TinyAleph Single Qubit Example');
20
+ console.log('==============================\n');
21
+
22
+ // ===========================================
23
+ // QUBIT BASICS
24
+ // ===========================================
25
+
26
+ console.log('Qubit Basics:');
27
+ console.log('-'.repeat(50) + '\n');
28
+
29
+ console.log('A qubit is a quantum bit that can be in superposition.');
30
+ console.log('Classical: 0 or 1');
31
+ console.log('Quantum: α|0⟩ + β|1⟩ where |α|² + |β|² = 1\n');
32
+
33
+ // Create basis states
34
+ var ket0 = backend.encode('|0⟩');
35
+ var ket1 = backend.encode('|1⟩');
36
+
37
+ console.log('Basis state |0⟩ primes: [' + ket0.join(', ') + ']');
38
+ console.log('Basis state |1⟩ primes: [' + ket1.join(', ') + ']');
39
+
40
+ // ===========================================
41
+ // QUANTUM GATES
42
+ // ===========================================
43
+
44
+ console.log('\n' + '='.repeat(50));
45
+ console.log('Quantum Gates:');
46
+ console.log('='.repeat(50) + '\n');
47
+
48
+ // Hadamard gate - creates superposition
49
+ console.log('Hadamard Gate (H):');
50
+ console.log(' Creates equal superposition from basis states');
51
+ console.log(' H|0⟩ = (|0⟩ + |1⟩)/√2');
52
+ console.log(' H|1⟩ = (|0⟩ - |1⟩)/√2\n');
53
+
54
+ var h_state = backend.applyGate(ket0, 'H');
55
+ console.log(' H|0⟩ result primes: [' + h_state.join(', ') + ']\n');
56
+
57
+ // Pauli-X gate - bit flip
58
+ console.log('Pauli-X Gate (NOT):');
59
+ console.log(' Flips the qubit state');
60
+ console.log(' X|0⟩ = |1⟩');
61
+ console.log(' X|1⟩ = |0⟩\n');
62
+
63
+ var x_state = backend.applyGate(ket0, 'X');
64
+ console.log(' X|0⟩ result primes: [' + x_state.join(', ') + ']\n');
65
+
66
+ // Pauli-Z gate - phase flip
67
+ console.log('Pauli-Z Gate:');
68
+ console.log(' Flips the phase of |1⟩');
69
+ console.log(' Z|0⟩ = |0⟩');
70
+ console.log(' Z|1⟩ = -|1⟩\n');
71
+
72
+ var z_state = backend.applyGate(ket1, 'Z');
73
+ console.log(' Z|1⟩ result primes: [' + z_state.join(', ') + ']\n');
74
+
75
+ // ===========================================
76
+ // SUPERPOSITION
77
+ // ===========================================
78
+
79
+ console.log('='.repeat(50));
80
+ console.log('Superposition:');
81
+ console.log('='.repeat(50) + '\n');
82
+
83
+ // Create superposition state
84
+ var superposition = backend.applyGate(ket0, 'H');
85
+ var state = backend.primesToState(superposition);
86
+
87
+ console.log('Superposition |+⟩ = H|0⟩:');
88
+ console.log(' State norm: ' + state.norm().toFixed(4));
89
+ console.log(' First components: [' + state.c.slice(0, 2).map(function(v) { return v.toFixed(4); }).join(', ') + ']\n');
90
+
91
+ // ===========================================
92
+ // MEASUREMENT
93
+ // ===========================================
94
+
95
+ console.log('='.repeat(50));
96
+ console.log('Measurement:');
97
+ console.log('='.repeat(50) + '\n');
98
+
99
+ console.log('Measuring a superposition collapses it probabilistically:\n');
100
+
101
+ // Measure superposition multiple times
102
+ var measurements = { '0': 0, '1': 0 };
103
+ var trials = 100;
104
+
105
+ for (var i = 0; i < trials; i++) {
106
+ var superState = backend.applyGate(ket0, 'H');
107
+ var qstate = backend.primesToState(superState);
108
+ var result = backend.measure(qstate);
109
+ measurements[result]++;
110
+ }
111
+
112
+ console.log('Measuring |+⟩ = (|0⟩ + |1⟩)/√2, ' + trials + ' trials:');
113
+ console.log(' |0⟩: ' + measurements['0'] + ' (' + (measurements['0']/trials*100).toFixed(1) + '%)');
114
+ console.log(' |1⟩: ' + measurements['1'] + ' (' + (measurements['1']/trials*100).toFixed(1) + '%)');
115
+ console.log(' Expected: ~50% each\n');
116
+
117
+ // ===========================================
118
+ // GATE SEQUENCES
119
+ // ===========================================
120
+
121
+ console.log('='.repeat(50));
122
+ console.log('Gate Sequences:');
123
+ console.log('='.repeat(50) + '\n');
124
+
125
+ // H then H should return to original
126
+ console.log('H·H = I (identity):');
127
+ var hh = backend.applyGate(backend.applyGate(ket0, 'H'), 'H');
128
+ console.log(' H(H|0⟩) primes: [' + hh.join(', ') + ']');
129
+ console.log(' Original |0⟩: [' + ket0.join(', ') + ']\n');
130
+
131
+ // X·X = I
132
+ console.log('X·X = I (identity):');
133
+ var xx = backend.applyGate(backend.applyGate(ket0, 'X'), 'X');
134
+ console.log(' X(X|0⟩) primes: [' + xx.join(', ') + ']');
135
+ console.log(' Original |0⟩: [' + ket0.join(', ') + ']\n');
136
+
137
+ // H·X·H = Z
138
+ console.log('H·X·H = Z (gate identity):');
139
+ var hxh = backend.applyGate(backend.applyGate(backend.applyGate(ket1, 'H'), 'X'), 'H');
140
+ var z_direct = backend.applyGate(ket1, 'Z');
141
+ console.log(' H(X(H|1⟩)) primes: [' + hxh.join(', ') + ']');
142
+ console.log(' Z|1⟩ primes: [' + z_direct.join(', ') + ']\n');
143
+
144
+ // ===========================================
145
+ // BLOCH SPHERE VISUALIZATION
146
+ // ===========================================
147
+
148
+ console.log('='.repeat(50));
149
+ console.log('Bloch Sphere Representation:');
150
+ console.log('='.repeat(50) + '\n');
151
+
152
+ console.log('Single qubit states can be visualized on a Bloch sphere:');
153
+ console.log(' |0⟩ = North pole');
154
+ console.log(' |1⟩ = South pole');
155
+ console.log(' |+⟩ = Positive X axis');
156
+ console.log(' |-⟩ = Negative X axis');
157
+ console.log(' |i⟩ = Positive Y axis');
158
+ console.log(' |-i⟩ = Negative Y axis\n');
159
+
160
+ var states = [
161
+ { name: '|0⟩', primes: ket0 },
162
+ { name: '|1⟩', primes: ket1 },
163
+ { name: '|+⟩', primes: backend.applyGate(ket0, 'H') },
164
+ { name: '|-⟩', primes: backend.applyGate(ket1, 'H') }
165
+ ];
166
+
167
+ console.log('State representations:');
168
+ for (var s = 0; s < states.length; s++) {
169
+ var st = states[s];
170
+ var qstate = backend.primesToState(st.primes);
171
+ console.log(' ' + st.name + ': norm=' + qstate.norm().toFixed(4) + ', primes=[' + st.primes.join(', ') + ']');
172
+ }
173
+
174
+ // ===========================================
175
+ // KEY TAKEAWAYS
176
+ // ===========================================
177
+
178
+ console.log('\n' + '='.repeat(50));
179
+ console.log('KEY TAKEAWAYS:');
180
+ console.log('1. Qubits can be in superposition of |0⟩ and |1⟩');
181
+ console.log('2. Hadamard gate creates equal superposition');
182
+ console.log('3. Pauli gates flip bits or phases');
183
+ console.log('4. Measurement collapses superposition probabilistically');
184
+ console.log('5. Gates can be composed into sequences');
185
+ console.log('6. Bloch sphere visualizes single qubit states');
@@ -0,0 +1,209 @@
1
+ /**
2
+ * @example Two-Qubit Systems
3
+ * @description Simulate two-qubit quantum systems and entanglement
4
+ *
5
+ * Two-qubit systems enable:
6
+ * - Tensor product states
7
+ * - Entanglement
8
+ * - Two-qubit gates like CNOT
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 Two-Qubit System Example');
20
+ console.log('===================================\n');
21
+
22
+ // ===========================================
23
+ // TWO-QUBIT BASICS
24
+ // ===========================================
25
+
26
+ console.log('Two-Qubit Basics:');
27
+ console.log('-'.repeat(50) + '\n');
28
+
29
+ console.log('A two-qubit system has 4 basis states:');
30
+ console.log(' |00⟩, |01⟩, |10⟩, |11⟩');
31
+ console.log(' General state: α|00⟩ + β|01⟩ + γ|10⟩ + δ|11⟩');
32
+ console.log(' where |α|² + |β|² + |γ|² + |δ|² = 1\n');
33
+
34
+ // Create basis states
35
+ var ket00 = backend.encode('|00⟩');
36
+ var ket01 = backend.encode('|01⟩');
37
+ var ket10 = backend.encode('|10⟩');
38
+ var ket11 = backend.encode('|11⟩');
39
+
40
+ console.log('Basis states:');
41
+ console.log(' |00⟩ primes: [' + ket00.join(', ') + ']');
42
+ console.log(' |01⟩ primes: [' + ket01.join(', ') + ']');
43
+ console.log(' |10⟩ primes: [' + ket10.join(', ') + ']');
44
+ console.log(' |11⟩ primes: [' + ket11.join(', ') + ']');
45
+
46
+ // ===========================================
47
+ // TENSOR PRODUCTS
48
+ // ===========================================
49
+
50
+ console.log('\n' + '='.repeat(50));
51
+ console.log('Tensor Products:');
52
+ console.log('='.repeat(50) + '\n');
53
+
54
+ console.log('Two independent qubits combine via tensor product:');
55
+ console.log(' |ψ₁⟩ ⊗ |ψ₂⟩\n');
56
+
57
+ // Simulate tensor product of two qubits
58
+ function tensorProduct(q1, q2) {
59
+ // In full simulation, this would create a 4-dimensional state
60
+ // Here we use encoding to represent the combined state
61
+ var label = '|' + q1 + q2 + '⟩';
62
+ return backend.encode(label);
63
+ }
64
+
65
+ var tp_00 = tensorProduct('0', '0');
66
+ var tp_01 = tensorProduct('0', '1');
67
+ var tp_10 = tensorProduct('1', '0');
68
+ var tp_11 = tensorProduct('1', '1');
69
+
70
+ console.log('Tensor products:');
71
+ console.log(' |0⟩ ⊗ |0⟩ = |00⟩: [' + tp_00.join(', ') + ']');
72
+ console.log(' |0⟩ ⊗ |1⟩ = |01⟩: [' + tp_01.join(', ') + ']');
73
+ console.log(' |1⟩ ⊗ |0⟩ = |10⟩: [' + tp_10.join(', ') + ']');
74
+ console.log(' |1⟩ ⊗ |1⟩ = |11⟩: [' + tp_11.join(', ') + ']');
75
+
76
+ // ===========================================
77
+ // CNOT GATE
78
+ // ===========================================
79
+
80
+ console.log('\n' + '='.repeat(50));
81
+ console.log('CNOT Gate (Controlled-NOT):');
82
+ console.log('='.repeat(50) + '\n');
83
+
84
+ console.log('CNOT flips the target qubit if control qubit is |1⟩:');
85
+ console.log(' CNOT|00⟩ = |00⟩');
86
+ console.log(' CNOT|01⟩ = |01⟩');
87
+ console.log(' CNOT|10⟩ = |11⟩ (target flipped)');
88
+ console.log(' CNOT|11⟩ = |10⟩ (target flipped)\n');
89
+
90
+ // Simulate CNOT
91
+ function applyCNOT(primes) {
92
+ var state = primes.join('');
93
+ // Map CNOT action based on input state
94
+ if (state === ket10.join('')) return ket11;
95
+ if (state === ket11.join('')) return ket10;
96
+ return primes; // |00⟩ and |01⟩ unchanged
97
+ }
98
+
99
+ console.log('CNOT results:');
100
+ console.log(' CNOT|00⟩ = [' + applyCNOT(ket00).join(', ') + ']');
101
+ console.log(' CNOT|01⟩ = [' + applyCNOT(ket01).join(', ') + ']');
102
+ console.log(' CNOT|10⟩ = [' + applyCNOT(ket10).join(', ') + ']');
103
+ console.log(' CNOT|11⟩ = [' + applyCNOT(ket11).join(', ') + ']');
104
+
105
+ // ===========================================
106
+ // ENTANGLEMENT
107
+ // ===========================================
108
+
109
+ console.log('\n' + '='.repeat(50));
110
+ console.log('Entanglement:');
111
+ console.log('='.repeat(50) + '\n');
112
+
113
+ console.log('Entangled states cannot be written as tensor products.');
114
+ console.log('Bell states are maximally entangled:\n');
115
+
116
+ // Bell states
117
+ console.log('Bell states:');
118
+ console.log(' |Φ+⟩ = (|00⟩ + |11⟩)/√2');
119
+ console.log(' |Φ-⟩ = (|00⟩ - |11⟩)/√2');
120
+ console.log(' |Ψ+⟩ = (|01⟩ + |10⟩)/√2');
121
+ console.log(' |Ψ-⟩ = (|01⟩ - |10⟩)/√2\n');
122
+
123
+ // Create Bell state |Φ+⟩: H on first qubit, then CNOT
124
+ console.log('Creating |Φ+⟩ = (|00⟩ + |11⟩)/√2:');
125
+ console.log(' 1. Start with |00⟩');
126
+ console.log(' 2. Apply H to first qubit -> (|0⟩+|1⟩)/√2 ⊗ |0⟩');
127
+ console.log(' 3. Apply CNOT -> (|00⟩ + |11⟩)/√2');
128
+
129
+ // Simulate Bell state creation
130
+ var bellState = backend.encode('|Phi+⟩');
131
+ console.log(' Bell state |Φ+⟩ primes: [' + bellState.join(', ') + ']');
132
+
133
+ // ===========================================
134
+ // ENTANGLEMENT PROPERTIES
135
+ // ===========================================
136
+
137
+ console.log('\n' + '='.repeat(50));
138
+ console.log('Entanglement Properties:');
139
+ console.log('='.repeat(50) + '\n');
140
+
141
+ console.log('Key properties of entangled states:');
142
+ console.log(' 1. Non-local correlations');
143
+ console.log(' 2. Cannot be factored into independent states');
144
+ console.log(' 3. Measuring one qubit affects the other instantly');
145
+ console.log(' 4. Basis of quantum communication and computing\n');
146
+
147
+ // Simulate measuring entangled pair
148
+ console.log('Measuring Bell state |Φ+⟩:');
149
+ console.log(' If first qubit = |0⟩ -> second qubit = |0⟩');
150
+ console.log(' If first qubit = |1⟩ -> second qubit = |1⟩');
151
+ console.log(' Always perfectly correlated!\n');
152
+
153
+ // Measurement simulation
154
+ var bellMeasurements = { '00': 0, '11': 0 };
155
+ var trials = 100;
156
+
157
+ for (var i = 0; i < trials; i++) {
158
+ // Simulate Bell state measurement
159
+ var r = Math.random();
160
+ if (r < 0.5) {
161
+ bellMeasurements['00']++;
162
+ } else {
163
+ bellMeasurements['11']++;
164
+ }
165
+ }
166
+
167
+ console.log('Simulated measurements (' + trials + ' trials):');
168
+ console.log(' |00⟩: ' + bellMeasurements['00'] + ' (' + (bellMeasurements['00']/trials*100).toFixed(1) + '%)');
169
+ console.log(' |11⟩: ' + bellMeasurements['11'] + ' (' + (bellMeasurements['11']/trials*100).toFixed(1) + '%)');
170
+ console.log(' Note: |01⟩ and |10⟩ never occur (perfect correlation)');
171
+
172
+ // ===========================================
173
+ // TWO-QUBIT GATES
174
+ // ===========================================
175
+
176
+ console.log('\n' + '='.repeat(50));
177
+ console.log('Two-Qubit Gates:');
178
+ console.log('='.repeat(50) + '\n');
179
+
180
+ console.log('Common two-qubit gates:\n');
181
+
182
+ console.log('CNOT (Controlled-NOT):');
183
+ console.log(' Control=|1⟩ flips target');
184
+
185
+ console.log('\nCZ (Controlled-Z):');
186
+ console.log(' Control=|1⟩ applies Z to target');
187
+ console.log(' CZ|11⟩ = -|11⟩\n');
188
+
189
+ console.log('SWAP:');
190
+ console.log(' Exchanges qubit values');
191
+ console.log(' SWAP|01⟩ = |10⟩');
192
+ console.log(' SWAP|10⟩ = |01⟩\n');
193
+
194
+ console.log('iSWAP:');
195
+ console.log(' SWAP with phase');
196
+ console.log(' iSWAP|01⟩ = i|10⟩');
197
+
198
+ // ===========================================
199
+ // KEY TAKEAWAYS
200
+ // ===========================================
201
+
202
+ console.log('\n' + '='.repeat(50));
203
+ console.log('KEY TAKEAWAYS:');
204
+ console.log('1. Two qubits have 4 basis states');
205
+ console.log('2. Independent qubits combine via tensor product');
206
+ console.log('3. CNOT creates entanglement');
207
+ console.log('4. Bell states are maximally entangled');
208
+ console.log('5. Entangled qubits show perfect correlations');
209
+ console.log('6. Two-qubit gates enable quantum algorithms');
@@ -0,0 +1,270 @@
1
+ /**
2
+ * @example Quantum Circuits
3
+ * @description Build and run quantum circuits
4
+ *
5
+ * Quantum circuits are sequences of quantum gates:
6
+ * - Compose gates into circuits
7
+ * - Execute circuits on quantum states
8
+ * - Measure outputs
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 Circuits Example');
20
+ console.log('===================================\n');
21
+
22
+ // ===========================================
23
+ // QUANTUM CIRCUIT CLASS
24
+ // ===========================================
25
+
26
+ function QuantumCircuit(numQubits) {
27
+ this.numQubits = numQubits;
28
+ this.gates = [];
29
+ }
30
+
31
+ QuantumCircuit.prototype.h = function(qubit) {
32
+ this.gates.push({ gate: 'H', qubit: qubit });
33
+ return this;
34
+ };
35
+
36
+ QuantumCircuit.prototype.x = function(qubit) {
37
+ this.gates.push({ gate: 'X', qubit: qubit });
38
+ return this;
39
+ };
40
+
41
+ QuantumCircuit.prototype.z = function(qubit) {
42
+ this.gates.push({ gate: 'Z', qubit: qubit });
43
+ return this;
44
+ };
45
+
46
+ QuantumCircuit.prototype.cnot = function(control, target) {
47
+ this.gates.push({ gate: 'CNOT', control: control, target: target });
48
+ return this;
49
+ };
50
+
51
+ QuantumCircuit.prototype.measure = function() {
52
+ this.gates.push({ gate: 'MEASURE' });
53
+ return this;
54
+ };
55
+
56
+ QuantumCircuit.prototype.toString = function() {
57
+ var lines = [];
58
+ for (var i = 0; i < this.numQubits; i++) {
59
+ lines.push('q' + i + ': ');
60
+ }
61
+
62
+ for (var g = 0; g < this.gates.length; g++) {
63
+ var gate = this.gates[g];
64
+ for (var i = 0; i < this.numQubits; i++) {
65
+ if (gate.gate === 'H' && gate.qubit === i) {
66
+ lines[i] += '--[H]--';
67
+ } else if (gate.gate === 'X' && gate.qubit === i) {
68
+ lines[i] += '--[X]--';
69
+ } else if (gate.gate === 'Z' && gate.qubit === i) {
70
+ lines[i] += '--[Z]--';
71
+ } else if (gate.gate === 'CNOT' && gate.control === i) {
72
+ lines[i] += '--[●]--';
73
+ } else if (gate.gate === 'CNOT' && gate.target === i) {
74
+ lines[i] += '--[⊕]--';
75
+ } else if (gate.gate === 'MEASURE') {
76
+ lines[i] += '--[M]--';
77
+ } else {
78
+ lines[i] += '-------';
79
+ }
80
+ }
81
+ }
82
+
83
+ return lines.join('\n');
84
+ };
85
+
86
+ // ===========================================
87
+ // BASIC CIRCUITS
88
+ // ===========================================
89
+
90
+ console.log('Basic Quantum Circuits:');
91
+ console.log('-'.repeat(50) + '\n');
92
+
93
+ // Create a simple circuit
94
+ var circuit1 = new QuantumCircuit(2);
95
+ circuit1.h(0).cnot(0, 1).measure();
96
+
97
+ console.log('Bell State Circuit (creates entanglement):');
98
+ console.log(circuit1.toString());
99
+ console.log('\nThis creates: (|00⟩ + |11⟩)/√2\n');
100
+
101
+ // Another circuit
102
+ var circuit2 = new QuantumCircuit(2);
103
+ circuit2.x(0).h(1).cnot(1, 0).measure();
104
+
105
+ console.log('Custom Circuit:');
106
+ console.log(circuit2.toString());
107
+ console.log('');
108
+
109
+ // ===========================================
110
+ // CIRCUIT EXECUTION
111
+ // ===========================================
112
+
113
+ console.log('='.repeat(50));
114
+ console.log('Circuit Execution:');
115
+ console.log('='.repeat(50) + '\n');
116
+
117
+ function executeCircuit(circuit, backend) {
118
+ // Initialize to |00...0⟩
119
+ var stateLabel = '|' + '0'.repeat(circuit.numQubits) + '⟩';
120
+ var state = backend.encode(stateLabel);
121
+
122
+ // Track state through gates
123
+ var history = [{ step: 'init', state: stateLabel }];
124
+
125
+ for (var g = 0; g < circuit.gates.length; g++) {
126
+ var gate = circuit.gates[g];
127
+
128
+ if (gate.gate === 'MEASURE') {
129
+ history.push({ step: 'measure', state: 'collapse' });
130
+ } else {
131
+ // Apply gate (simplified simulation)
132
+ var newState = backend.applyGate(state, gate.gate);
133
+ history.push({ step: gate.gate + ' q' + (gate.qubit !== undefined ? gate.qubit : gate.control), state: newState.join(',') });
134
+ state = newState;
135
+ }
136
+ }
137
+
138
+ return history;
139
+ }
140
+
141
+ console.log('Executing Bell State Circuit:\n');
142
+ var history = executeCircuit(circuit1, backend);
143
+ for (var i = 0; i < history.length; i++) {
144
+ var step = history[i];
145
+ console.log(' Step ' + i + ' (' + step.step + '): ' + (typeof step.state === 'string' ? step.state : '[' + step.state + ']'));
146
+ }
147
+
148
+ // ===========================================
149
+ // PARAMETERIZED CIRCUITS
150
+ // ===========================================
151
+
152
+ console.log('\n' + '='.repeat(50));
153
+ console.log('Parameterized Circuits:');
154
+ console.log('='.repeat(50) + '\n');
155
+
156
+ console.log('Parameterized circuits have adjustable rotation angles.');
157
+ console.log('Used in variational quantum algorithms like VQE and QAOA.\n');
158
+
159
+ function ParameterizedCircuit(numQubits) {
160
+ QuantumCircuit.call(this, numQubits);
161
+ this.parameters = {};
162
+ }
163
+
164
+ ParameterizedCircuit.prototype = Object.create(QuantumCircuit.prototype);
165
+
166
+ ParameterizedCircuit.prototype.rx = function(qubit, theta) {
167
+ this.gates.push({ gate: 'RX', qubit: qubit, theta: theta });
168
+ return this;
169
+ };
170
+
171
+ ParameterizedCircuit.prototype.ry = function(qubit, theta) {
172
+ this.gates.push({ gate: 'RY', qubit: qubit, theta: theta });
173
+ return this;
174
+ };
175
+
176
+ ParameterizedCircuit.prototype.rz = function(qubit, theta) {
177
+ this.gates.push({ gate: 'RZ', qubit: qubit, theta: theta });
178
+ return this;
179
+ };
180
+
181
+ var paramCircuit = new ParameterizedCircuit(2);
182
+ paramCircuit.ry(0, 'theta1').ry(1, 'theta2').cnot(0, 1);
183
+
184
+ console.log('Parameterized Circuit:');
185
+ console.log(' RY(θ₁) on q0');
186
+ console.log(' RY(θ₂) on q1');
187
+ console.log(' CNOT(q0, q1)');
188
+ console.log('\nParameters: θ₁ = 0.5, θ₂ = 1.2');
189
+
190
+ // ===========================================
191
+ // COMMON CIRCUIT PATTERNS
192
+ // ===========================================
193
+
194
+ console.log('\n' + '='.repeat(50));
195
+ console.log('Common Circuit Patterns:');
196
+ console.log('='.repeat(50) + '\n');
197
+
198
+ // GHZ state
199
+ console.log('GHZ State (3 qubits):');
200
+ console.log(' Creates: (|000⟩ + |111⟩)/√2');
201
+ var ghz = new QuantumCircuit(3);
202
+ ghz.h(0).cnot(0, 1).cnot(0, 2);
203
+ console.log(ghz.toString());
204
+
205
+ // W state
206
+ console.log('\nW State (conceptual):');
207
+ console.log(' Creates: (|001⟩ + |010⟩ + |100⟩)/√3');
208
+ console.log(' More complex circuit required\n');
209
+
210
+ // Teleportation
211
+ console.log('Quantum Teleportation (3 qubits):');
212
+ console.log(' Transfers quantum state using entanglement');
213
+ var teleport = new QuantumCircuit(3);
214
+ teleport.h(1).cnot(1, 2); // Create Bell pair
215
+ teleport.cnot(0, 1).h(0); // Bell measurement
216
+ teleport.measure();
217
+ console.log(teleport.toString());
218
+
219
+ // ===========================================
220
+ // CIRCUIT DEPTH AND WIDTH
221
+ // ===========================================
222
+
223
+ console.log('\n' + '='.repeat(50));
224
+ console.log('Circuit Metrics:');
225
+ console.log('='.repeat(50) + '\n');
226
+
227
+ function analyzeCircuit(circuit) {
228
+ var width = circuit.numQubits;
229
+ var depth = 0;
230
+ var gateCount = 0;
231
+ var gateCounts = {};
232
+
233
+ for (var g = 0; g < circuit.gates.length; g++) {
234
+ var gate = circuit.gates[g];
235
+ if (gate.gate !== 'MEASURE') {
236
+ gateCount++;
237
+ gateCounts[gate.gate] = (gateCounts[gate.gate] || 0) + 1;
238
+ }
239
+ }
240
+
241
+ // Simplified depth calculation
242
+ depth = circuit.gates.filter(function(g) { return g.gate !== 'MEASURE'; }).length;
243
+
244
+ return {
245
+ width: width,
246
+ depth: depth,
247
+ gateCount: gateCount,
248
+ gateCounts: gateCounts
249
+ };
250
+ }
251
+
252
+ var metrics = analyzeCircuit(circuit1);
253
+ console.log('Bell State Circuit Metrics:');
254
+ console.log(' Width (qubits): ' + metrics.width);
255
+ console.log(' Depth (layers): ' + metrics.depth);
256
+ console.log(' Total gates: ' + metrics.gateCount);
257
+ console.log(' Gate breakdown: ' + JSON.stringify(metrics.gateCounts));
258
+
259
+ // ===========================================
260
+ // KEY TAKEAWAYS
261
+ // ===========================================
262
+
263
+ console.log('\n' + '='.repeat(50));
264
+ console.log('KEY TAKEAWAYS:');
265
+ console.log('1. Circuits are sequences of quantum gates');
266
+ console.log('2. Gates act on specific qubits');
267
+ console.log('3. CNOT connects qubits (creates entanglement)');
268
+ console.log('4. Parameterized circuits enable optimization');
269
+ console.log('5. Circuit depth affects noise and runtime');
270
+ console.log('6. Common patterns: Bell, GHZ, teleportation');