File size: 14,093 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
{-# LANGUAGE DeriveGeneric #-}

module QuantumModule where

import ManifoldGeometry hiding (decoherenceRate)
import Data.List (nubBy)
import Data.Function (on)
import GHC.Generics (Generic)
import System.Random (RandomGen, StdGen, randomR, mkStdGen)

-- ─────────────────────────────────────────────────────────────────────────────
-- Quantum Superposition & Decoherence
-- ─────────────────────────────────────────────────────────────────────────────

-- | Complex number for quantum amplitudes
data Complex = Complex
  { realPart :: Double
  , imagPart :: Double
  } deriving (Show, Generic)

-- | Complex conjugate
complexConj :: Complex -> Complex
complexConj (Complex r i) = Complex r (-i)

-- | Complex magnitude
complexMag :: Complex -> Double
complexMag (Complex r i) = sqrt (r*r + i*i)

-- | Complex multiplication
complexMult :: Complex -> Complex -> Complex
complexMult (Complex r1 i1) (Complex r2 i2) =
  Complex (r1*r2 - i1*i2) (r1*i2 + i1*r2)

-- | Probability density |ψ|²
probabilityDensity :: Complex -> Double
probabilityDensity psi = (complexMag psi) ^ 2

-- ─────────────────────────────────────────────────────────────────────────────
-- Quantum Branches (Many-Worlds)
-- ─────────────────────────────────────────────────────────────────────────────

data QuantumBranch = QuantumBranch
  { branchId :: Int
  , branchLabel :: String
  , amplitude :: Complex
  , stateVector :: Vector
  , probability :: Double               -- Probability of this branch
  , decoherenceTime :: Double           -- When this branch becomes classical
  } deriving (Show, Generic)

-- ─────────────────────────────────────────────────────────────────────────────
-- Superposition State
-- ─────────────────────────────────────────────────────────────────────────────

data QuantumSuperposition = QuantumSuperposition
  { superpositionId :: String
  , branches :: [QuantumBranch]
  , collapseThreshold :: Double         -- Below this probability, branch dies
  , decoherenceRate :: Double           -- Rate of decoherence: dρ/dt = -Γρ
  , currentStep :: Int
  } deriving (Show, Generic)

-- | Normalize amplitudes (enforce Σ|ψ_i|² = 1)
normalizeAmplitudes :: [QuantumBranch] -> [QuantumBranch]
normalizeAmplitudes branches =
  let totalProb = sum [probabilityDensity (amplitude b) | b <- branches]
      normFactor = if totalProb > 0 then 1.0 / sqrt totalProb else 1.0
      renormalizedBranches = map (\b -> b
        { amplitude = let Complex r i = amplitude b
                      in Complex (r * normFactor) (i * normFactor)
        , probability = probabilityDensity (amplitude b)
        }) branches
  in renormalizedBranches

-- ─────────────────────────────────────────────────────────────────────────────
-- Wave Function Collapse (Measurement)
-- ─────────────────────────────────────────────────────────────────────────────

-- | Sample from superposition (non-deterministic collapse)
-- Probability of branch i = |ψ_i|²
sampleSuperposition :: StdGen -> QuantumSuperposition -> (QuantumBranch, StdGen)
sampleSuperposition gen qs =
  let normalized = normalizeAmplitudes (branches qs)
      probs = map probability normalized
      (r, gen') = randomR (0.0, 1.0) gen
      chosenBranch = selectByProbability r probs normalized
  in (chosenBranch, gen')

-- | Select branch based on cumulative probability
selectByProbability :: Double -> [Double] -> [QuantumBranch] -> QuantumBranch
selectByProbability r probs branches =
  go r 0 probs branches
  where
    go _ _ [] [] = error "No branches"
    go _ _ (_ : _) [] = error "Probability mismatch"
    go p cumProb (prob : probRest) (branch : branchRest)
      | p < cumProb + prob = branch
      | otherwise = go p (cumProb + prob) probRest branchRest
    go _ _ _ _ = error "Probability selection failed"

-- ─────────────────────────────────────────────────────────────────────────────
-- Branch Exploration (Without Collapse)
-- ─────────────────────────────────────────────────────────────────────────────

-- | Significant branches (keep if probability > threshold)
significantBranches :: QuantumSuperposition -> [QuantumBranch]
significantBranches qs =
  filter (\b -> probability b > collapseThreshold qs) (branches qs)

-- | Count live branches (non-negligible probability)
countLiveBranches :: QuantumSuperposition -> Int
countLiveBranches qs = length (significantBranches qs)

-- | Entanglement entropy (Shannon entropy of branch probabilities)
entanglementEntropy :: QuantumSuperposition -> Double
entanglementEntropy qs =
  let probs = map probability (branches qs)
      nonZeroProbs = filter (> 0.0001) probs
  in -(sum [p * log p | p <- nonZeroProbs])

-- ─────────────────────────────────────────────────────────────────────────────
-- Decoherence: Branch Pruning Over Time
-- ─────────────────────────────────────────────────────────────────────────────

-- | Apply decoherence for one time step
-- Branches with low probability decay exponentially
decoherence :: QuantumSuperposition -> Double -> QuantumSuperposition
decoherence qs dt =
  let decayedBranches = map (\b ->
        let oldProb = probability b
            decayFactor = exp (-(decoherenceRate qs) * dt)
            newProb = oldProb * decayFactor
            newAmplitude = let Complex r i = amplitude b
                               scaleFactor = sqrt decayFactor
                           in Complex (r * scaleFactor) (i * scaleFactor)
        in b { probability = newProb, amplitude = newAmplitude }
        ) (branches qs)
      -- Renormalize
      normalized = normalizeAmplitudes decayedBranches
      -- Remove dead branches
      alive = filter (\b -> probability b > collapseThreshold qs) normalized
  in qs { branches = alive, currentStep = currentStep qs + 1 }

-- ─────────────────────────────────────────────────────────────────────────────
-- Quantum Tunneling (Probabilistic State Transfer)
-- ─────────────────────────────────────────────────────────────────────────────

-- | Tunneling probability through barrier
-- P ∝ exp(-2ΞΊL) where ΞΊ = √(2m(V-E))/ℏ
tunnelingProbability :: Double -> Double -> Double -> Double
tunnelingProbability barrierHeight particleEnergy barrierWidth =
  let eff_mass = 9.109e-31  -- electron mass
      hbar = 1.054571817e-34
      v_diff = barrierHeight - particleEnergy
      kappa = if v_diff > 0
              then sqrt (2.0 * eff_mass * v_diff) / hbar
              else 0
      prob = exp (-2.0 * kappa * barrierWidth)
  in min 1.0 prob

-- | Stochastic tunnel attempt
attemptTunneling :: StdGen -> Vector -> Double -> IO (Vector, Bool)
attemptTunneling gen pos tunnelingProb = do
  let (r, _) = randomR (0.0, 1.0) gen
      didTunnel = r < tunnelingProb
      -- Tunnel to random nearby position
      newPos = if didTunnel
               then vectorAdd pos (Vector [sin r, cos r, 0])
               else pos
  return (newPos, didTunnel)

-- ─────────────────────────────────────────────────────────────────────────────
-- Quantum Superposition Evolution (SchrΓΆdinger-like)
-- ─────────────────────────────────────────────────────────────────────────────

-- | Apply unitary transformation to branches
-- U |ψ⟩ = e^(-iHt/ℏ) |ψ⟩
unitaryEvolution :: QuantumSuperposition -> Double -> QuantumSuperposition
unitaryEvolution qs dt =
  let evolved = map (\b ->
        let oldPhase = atan2 (imagPart (amplitude b)) (realPart (amplitude b))
            energyContribution = (fromIntegral (branchId b) :: Double) * dt  -- Dummy Hamiltonian
            newPhase = oldPhase + energyContribution
            mag = complexMag (amplitude b)
            newAmp = Complex (mag * cos newPhase) (mag * sin newPhase)
        in b { amplitude = newAmp }
        ) (branches qs)
  in qs { branches = normalizeAmplitudes evolved }

-- ─────────────────────────────────────────────────────────────────────────────
-- Quantum Region Configuration
-- ─────────────────────────────────────────────────────────────────────────────

-- | Helper to create quantum branch
makeBranch :: Int -> Int -> Vector -> QuantumBranch
makeBranch i numBranches center = QuantumBranch
  { branchId = i
  , branchLabel = "branch-" ++ show i
  , amplitude = Complex (1.0 / sqrt (fromIntegral numBranches)) 0.0
  , stateVector = vectorAdd center (Vector [sin (fromIntegral i), cos (fromIntegral i), 0])
  , probability = 1.0 / fromIntegral numBranches
  , decoherenceTime = 1.0 / fromIntegral (i + 1)
  }

-- | Initialize superposition in region
initializeSuperposition :: String -> Int -> Vector -> QuantumSuperposition
initializeSuperposition regionId numBranches center =
  let initialBranches = [makeBranch i numBranches center | i <- [0..numBranches-1]]
  in QuantumSuperposition
    { superpositionId = regionId
    , branches = initialBranches
    , collapseThreshold = 0.001
    , decoherenceRate = 0.1
    , currentStep = 0
    }

-- ─────────────────────────────────────────────────────────────────────────────
-- WORM-sealed quantum observations
-- ─────────────────────────────────────────────────────────────────────────────

data QuantumObservation = QuantumObservation
  { qStep :: Int
  , qBranchesAlive :: Int
  , qEntropy :: Double
  , qCollapsedBranch :: Int
  , qWormSeal :: String
  } deriving (Show, Generic)

-- | WORM seal observation
sealQuantumObservation :: Int -> QuantumSuperposition -> Int -> QuantumObservation
sealQuantumObservation step qs collapsedId =
  let numAlive = countLiveBranches qs
      entropy = entanglementEntropy qs
      seal = "WORM[quantum:step=" ++ show step
             ++ ":branches_alive=" ++ show numAlive
             ++ ":entropy=" ++ show (round (entropy * 100) :: Integer)
             ++ ":collapsed_branch=" ++ show collapsedId ++ "]"
  in QuantumObservation step numAlive entropy collapsedId seal

-- ─────────────────────────────────────────────────────────────────────────────
-- Bell Test Simulator (EPR pairs)
-- ─────────────────────────────────────────────────────────────────────────────

-- | Entangled pair state |Φ+⟩ = (|00⟩ + |11⟩)/√2
eprPairSuperposition :: QuantumSuperposition
eprPairSuperposition = QuantumSuperposition
  { superpositionId = "epr-pair"
  , branches =
    [ QuantumBranch
      { branchId = 0
      , branchLabel = "|00⟩"
      , amplitude = Complex (1.0 / sqrt 2.0) 0.0
      , stateVector = Vector [0, 0, 0]
      , probability = 0.5
      , decoherenceTime = 0.0
      }
    , QuantumBranch
      { branchId = 1
      , branchLabel = "|11⟩"
      , amplitude = Complex (1.0 / sqrt 2.0) 0.0
      , stateVector = Vector [1, 1, 0]
      , probability = 0.5
      , decoherenceTime = 0.0
      }
    ]
  , collapseThreshold = 0.001
  , decoherenceRate = 0.0  -- Perfect coherence for Bell test
  , currentStep = 0
  }