forked from Thorium/FSharp.Azure.Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuantumDistributions.fsx
More file actions
419 lines (326 loc) · 15.7 KB
/
Copy pathQuantumDistributions.fsx
File metadata and controls
419 lines (326 loc) · 15.7 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// ============================================================================
// Quantum-Enhanced Statistical Distributions Example
// ============================================================================
//
// This example demonstrates how to use quantum random number generation (QRNG)
// to sample from standard statistical distributions with TRUE quantum randomness.
//
// Key Features:
// - Normal, LogNormal, Exponential, Uniform distributions
// - Pure quantum entropy (not pseudo-random)
// - Works with any quantum backend (LocalBackend, Rigetti, IonQ, etc.)
// - Inverse Transform Sampling method
//
// Use Cases:
// - Monte Carlo simulations requiring true randomness
// - Financial modeling (stock prices, option pricing)
// - Scientific simulations (particle physics, chemistry)
// - Machine learning (quantum-enhanced training data)
//
// ============================================================================
// Reference the compiled DLL directly (since package not published yet)
#r "../../src/FSharp.Azure.Quantum/bin/Debug/net10.0/FSharp.Azure.Quantum.dll"
open System
open FSharp.Azure.Quantum.Algorithms
open FSharp.Azure.Quantum.Algorithms.QuantumDistributions
open FSharp.Azure.Quantum.Backends
// ============================================================================
// Example 1: Basic Sampling (No Backend Required)
// ============================================================================
printfn "============================================"
printfn "Example 1: Basic Quantum Distribution Sampling"
printfn "============================================\n"
// Sample from Standard Normal N(0, 1)
let sampleStandardNormal () =
printfn "📊 Sampling from Standard Normal N(0, 1)..."
match sample StandardNormal with
| Ok result ->
printfn " ✓ Generated: %.4f" result.Value
printfn " ✓ Distribution: %s" (distributionName result.Distribution)
printfn " ✓ Quantum bits used: %d" result.QuantumBitsUsed
| Error msg ->
printfn " ✗ Error: %s" msg
sampleStandardNormal()
// Sample from Normal with custom parameters
let sampleCustomNormal () =
printfn "\n📊 Sampling from Normal N(100, 15)..."
let dist = Normal (mean = 100.0, stddev = 15.0)
match sample dist with
| Ok result ->
printfn " ✓ Generated: %.2f" result.Value
printfn " ✓ Expected mean: %.2f" (expectedMean dist |> Option.defaultValue 0.0)
printfn " ✓ Expected stddev: %.2f" (expectedStdDev dist |> Option.defaultValue 0.0)
| Error msg ->
printfn " ✗ Error: %s" msg
sampleCustomNormal()
// ============================================================================
// Example 2: Multiple Samples with Statistics
// ============================================================================
printfn "\n============================================"
printfn "Example 2: Multiple Samples & Statistics"
printfn "============================================\n"
let generateAndAnalyze () =
printfn "📈 Generating 1000 samples from N(50, 10)...\n"
let dist = Normal (mean = 50.0, stddev = 10.0)
match sampleMany dist 100 with
| Ok samples ->
let stats = computeStatistics samples
printfn "Statistical Results:"
printfn " Sample Count: %d" stats.Count
printfn " Sample Mean: %.2f (expected: 50.00)" stats.Mean
printfn " Sample StdDev: %.2f (expected: 10.00)" stats.StdDev
printfn " Min Value: %.2f" stats.Min
printfn " Max Value: %.2f" stats.Max
// Show histogram of values
printfn "\n Distribution histogram (10 bins):"
let binSize = (stats.Max - stats.Min) / 10.0
let values = samples |> Array.map (fun s -> s.Value)
for i in 0..9 do
let binStart = stats.Min + float i * binSize
let binEnd = binStart + binSize
let count = values |> Array.filter (fun v -> v >= binStart && v < binEnd) |> Array.length
let bar = String.replicate (count / 20) "█"
printfn " [%.1f - %.1f]: %s (%d)" binStart binEnd bar count
| Error msg ->
printfn " ✗ Error: %s" msg
generateAndAnalyze()
// ============================================================================
// Example 3: LogNormal Distribution (Stock Prices)
// ============================================================================
printfn "\n============================================"
printfn "Example 3: LogNormal for Stock Price Simulation"
printfn "============================================\n"
let simulateStockPrices () =
printfn "💰 Simulating stock price paths...\n"
// Stock parameters
let S0 = 100.0 // Initial price
let mu = 0.05 // Drift (5% annual return)
let sigma = 0.2 // Volatility (20%)
let T = 1.0 // Time horizon (1 year)
// LogNormal parameters for price at time T
let logMu = log(S0) + (mu - sigma**2.0/2.0) * T
let logSigma = sigma * sqrt(T)
let dist = LogNormal (mu = logMu, sigma = logSigma)
printfn "Stock Parameters:"
printfn " Initial Price (S₀): $%.2f" S0
printfn " Annual Return (μ): %.1f%%" (mu * 100.0)
printfn " Volatility (σ): %.1f%%" (sigma * 100.0)
printfn " Time Horizon: %.1f year" T
match sampleMany dist 10 with
| Ok samples ->
printfn "\n10 Simulated Price Paths (quantum randomness):"
samples
|> Array.iteri (fun i s ->
let return_ = (s.Value - S0) / S0 * 100.0
printfn " Path %2d: $%.2f (%.1f%% return)" (i+1) s.Value return_)
let stats = computeStatistics samples
printfn "\nSimulation Statistics:"
printfn " Average Final Price: $%.2f" stats.Mean
printfn " Price Range: $%.2f - $%.2f" stats.Min stats.Max
| Error msg ->
printfn " ✗ Error: %s" msg
simulateStockPrices()
// ============================================================================
// Example 4: Exponential Distribution (Time Between Events)
// ============================================================================
printfn "\n============================================"
printfn "Example 4: Exponential for Event Timing"
printfn "============================================\n"
let simulateServerRequests () =
printfn "🖥️ Simulating server request arrivals...\n"
let avgRequestsPerSecond = 5.0 // Lambda = 5
let dist = Exponential (lambda = avgRequestsPerSecond)
printfn "Server Parameters:"
printfn " Average requests/sec: %.1f" avgRequestsPerSecond
printfn " Expected time between: %.3f seconds\n" (1.0 / avgRequestsPerSecond)
match sampleMany dist 15 with
| Ok samples ->
printfn "Next 15 request arrival times (quantum randomness):"
let mutable cumulativeTime = 0.0
samples
|> Array.iteri (fun i s ->
cumulativeTime <- cumulativeTime + s.Value
printfn " Request %2d: %.3fs (cumulative: %.2fs)" (i+1) s.Value cumulativeTime)
let stats = computeStatistics samples
printfn "\nStatistics:"
printfn " Average inter-arrival: %.3fs (expected: %.3fs)"
stats.Mean (1.0 / avgRequestsPerSecond)
| Error msg ->
printfn " ✗ Error: %s" msg
simulateServerRequests()
// ============================================================================
// Example 5: Uniform Distribution (Random Selection)
// ============================================================================
printfn "\n============================================"
printfn "Example 5: Uniform for Random Selection"
printfn "============================================\n"
let randomDiceRolls () =
printfn "🎲 Simulating quantum dice rolls (1-6)...\n"
let dist = Uniform (min = 1.0, max = 7.0) // [1, 7) effectively gives [1, 6]
match sampleMany dist 20 with
| Ok samples ->
printfn "20 Quantum Dice Rolls:"
samples
|> Array.map (fun s -> int (floor s.Value))
|> Array.chunkBySize 10
|> Array.iteri (fun i chunk ->
let rolls = chunk |> Array.map string |> String.concat ", "
printfn " Rolls %2d-%2d: %s" (i*10+1) (i*10+10) rolls)
// Count frequencies
let frequencies =
samples
|> Array.map (fun s -> int (floor s.Value))
|> Array.countBy id
|> Array.sortBy fst
printfn "\nFrequency Distribution:"
frequencies |> Array.iter (fun (value, count) ->
let bar = String.replicate count "█"
printfn " %d: %s (%d)" value bar count)
| Error msg ->
printfn " ✗ Error: %s" msg
randomDiceRolls()
// ============================================================================
// Example 6: Custom Distribution
// ============================================================================
printfn "\n============================================"
printfn "Example 6: Custom Distribution"
printfn "============================================\n"
let customTransformExample () =
printfn "🎨 Using custom transform function...\n"
// Transform: Square the uniform random (skews toward 0)
let squareTransform (u: float) = u * u
let dist = Custom (name = "Square", transform = squareTransform)
printfn "Custom Distribution: Square(U)"
printfn " Transforms uniform U~(0,1) to U²\n"
match sampleMany dist 100 with
| Ok samples ->
let stats = computeStatistics samples
printfn "Statistical Results (1000 samples):"
printfn " Mean: %.4f (expected: 0.333)" stats.Mean
printfn " StdDev: %.4f" stats.StdDev
printfn " Range: [%.4f, %.4f]" stats.Min stats.Max
// Show that values cluster near 0
let below25pct = samples |> Array.filter (fun s -> s.Value < 0.25) |> Array.length
let below50pct = samples |> Array.filter (fun s -> s.Value < 0.5) |> Array.length
printfn "\nDistribution (skewed toward 0):"
printfn " Below 0.25: %d%%" (below25pct * 100 / 1000)
printfn " Below 0.50: %d%%" (below50pct * 100 / 1000)
| Error msg ->
printfn " ✗ Error: %s" msg
customTransformExample()
// ============================================================================
// Example 7: Backend Integration (OPTIONAL - Requires Real Quantum Hardware)
// ============================================================================
printfn "\n============================================"
printfn "Example 7: Real Quantum Backend (OPTIONAL)"
printfn "============================================\n"
printfn "🔧 This example shows how to use real quantum hardware.\n"
printfn "To run with actual quantum computers:"
printfn " 1. Uncomment the code below"
printfn " 2. Configure Azure Quantum workspace credentials"
printfn " 3. Choose backend: Rigetti, IonQ, Quantinuum, or Atom Computing\n"
(*
// Uncomment to use real quantum hardware:
async {
printfn "Connecting to Rigetti quantum computer via Azure Quantum...\n"
// Configure your Azure Quantum workspace
let! rigettiBackend =
RigettiBackend.create
"your-workspace-id"
"your-resource-group"
"eastus"
printfn "✓ Connected to Rigetti Aspen-M-3\n"
// Sample using real quantum hardware
let dist = Normal (mean = 0.0, stddev = 1.0)
printfn "Generating quantum sample on real hardware..."
let! result = sampleWithBackend dist rigettiBackend
match result with
| Ok sample ->
printfn "✓ REAL QUANTUM SAMPLE: %.4f" sample.Value
printfn " Generated using %d qubits on Rigetti hardware" sample.QuantumBitsUsed
printfn " True quantum randomness (not simulated!)"
| Error err ->
printfn "✗ Error: %s" err.Message
} |> Async.RunSynchronously
*)
// Alternative: Use LocalBackend for testing (simulates quantum behavior)
async {
printfn "Using LocalBackend (quantum simulation)...\n"
let backend = LocalBackend.LocalBackend() :> FSharp.Azure.Quantum.Core.BackendAbstraction.IQuantumBackend
let dist = StandardNormal
printfn "Generating 5 samples via LocalBackend..."
let! result = sampleManyWithBackend dist 5 backend None
match result with
| Ok samples ->
printfn "✓ Generated %d samples:\n" samples.Length
samples |> Array.iteri (fun i s ->
printfn " Sample %d: %.4f (%d qubits)" (i+1) s.Value s.QuantumBitsUsed)
| Error err ->
printfn "✗ Error: %s" err.Message
} |> Async.RunSynchronously
// ============================================================================
// Example 8: Monte Carlo Integration
// ============================================================================
printfn "\n============================================"
printfn "Example 8: Monte Carlo Integration with Quantum Randomness"
printfn "============================================\n"
let monteCarloIntegration () =
printfn "🎯 Estimating π using quantum Monte Carlo...\n"
// Estimate π by sampling points in unit square
// and counting how many fall inside quarter circle
let dist = Uniform (min = 0.0, max = 1.0)
match sampleMany dist 100 with // 1000 (x,y) pairs
| Ok samples ->
let points = samples |> Array.chunkBySize 2
let insideCircle =
points
|> Array.filter (fun pair ->
if pair.Length = 2 then
let x = pair.[0].Value
let y = pair.[1].Value
x*x + y*y <= 1.0
else false)
|> Array.length
let totalPoints = points.Length
let piEstimate = 4.0 * float insideCircle / float totalPoints
let error = abs(piEstimate - Math.PI)
printfn "Monte Carlo Results:"
printfn " Total points: %d" totalPoints
printfn " Inside circle: %d" insideCircle
printfn " π estimate: %.6f" piEstimate
printfn " True π: %.6f" Math.PI
printfn " Absolute error: %.6f" error
printfn " Relative error: %.3f%%" (error / Math.PI * 100.0)
printfn "\n ✓ Using TRUE quantum randomness (not pseudo-random!)"
| Error msg ->
printfn " ✗ Error: %s" msg
monteCarloIntegration()
// ============================================================================
// Summary
// ============================================================================
printfn "\n============================================"
printfn "Summary"
printfn "============================================\n"
printfn "✓ Demonstrated 8 quantum distribution examples:"
printfn " 1. Standard Normal sampling"
printfn " 2. Multiple samples with statistics"
printfn " 3. LogNormal for stock price simulation"
printfn " 4. Exponential for event timing"
printfn " 5. Uniform for random selection"
printfn " 6. Custom distribution transforms"
printfn " 7. Real quantum backend integration"
printfn " 8. Monte Carlo integration\n"
printfn "Key Features:"
printfn " • TRUE quantum randomness (not pseudo-random)"
printfn " • Works with any quantum backend (Rigetti, IonQ, etc.)"
printfn " • Industry-standard distributions"
printfn " • Statistical validation included"
printfn " • Production-ready error handling\n"
printfn "Next Steps:"
printfn " • Try different distribution parameters"
printfn " • Connect to real quantum hardware"
printfn " • Use for Monte Carlo simulations"
printfn " • Apply to financial modeling\n"
printfn "============================================"
printfn "Example Complete! ✨"
printfn "============================================"