forked from Thorium/FSharp.Azure.Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendComparison.fsx
More file actions
261 lines (210 loc) · 11.5 KB
/
Copy pathBackendComparison.fsx
File metadata and controls
261 lines (210 loc) · 11.5 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
// ============================================================================
// Backend Comparison - Ising vs Fibonacci Anyons
// ============================================================================
//
// This example compares different topological backend configurations:
// 1. Ising anyons (Microsoft Majorana - experimentally realizable)
// 2. Fibonacci anyons (theoretical gold standard - universal braiding)
//
// Key Differences:
// - Ising: Clifford-only (needs magic states for universality)
// - Fibonacci: Universal (braiding alone is universal for QC)
// - Ising: Simpler fusion rules (easier to implement in hardware)
// - Fibonacci: More complex but more powerful
//
// ============================================================================
#r "../../src/FSharp.Azure.Quantum.Topological/bin/Debug/net10.0/FSharp.Azure.Quantum.Topological.dll"
open FSharp.Azure.Quantum.Topological
open System.Diagnostics
// ============================================================================
// Backend Configuration Comparison
// ============================================================================
printfn "=== Topological Backend Comparison ==="
printfn ""
// Create both backend types
let isingBackend = TopologicalBackend.createSimulator AnyonSpecies.AnyonType.Ising 10
let fibonacciBackend = TopologicalBackend.createSimulator AnyonSpecies.AnyonType.Fibonacci 10
printfn "┌─────────────────────────────────────────────────────────────┐"
printfn "│ Backend: Ising Anyons (Microsoft Majorana) │"
printfn "├─────────────────────────────────────────────────────────────┤"
printfn "│ Capabilities: │"
let isingCaps = isingBackend.Capabilities
printfn "│ Supported Anyons: %A" isingCaps.SupportedAnyonTypes
printfn "│ Max Anyons: %A" isingCaps.MaxAnyons
printfn "│ Braiding: %b" isingCaps.SupportsBraiding
printfn "│ Measurement: %b" isingCaps.SupportsMeasurement
printfn "│ F-Moves: %b" isingCaps.SupportsFMoves
printfn "│ Error Correction: %b" isingCaps.SupportsErrorCorrection
printfn "└─────────────────────────────────────────────────────────────┘"
printfn ""
printfn "┌─────────────────────────────────────────────────────────────┐"
printfn "│ Backend: Fibonacci Anyons (Theoretical Universal) │"
printfn "├─────────────────────────────────────────────────────────────┤"
printfn "│ Capabilities: │"
let fibCaps = fibonacciBackend.Capabilities
printfn "│ Supported Anyons: %A" fibCaps.SupportedAnyonTypes
printfn "│ Max Anyons: %A" fibCaps.MaxAnyons
printfn "│ Braiding: %b" fibCaps.SupportsBraiding
printfn "│ Measurement: %b" fibCaps.SupportsMeasurement
printfn "│ F-Moves: %b" fibCaps.SupportsFMoves
printfn "│ Error Correction: %b" fibCaps.SupportsErrorCorrection
printfn "└─────────────────────────────────────────────────────────────┘"
printfn ""
// ============================================================================
// Fusion Rule Comparison
// ============================================================================
printfn "=== Fusion Rules Comparison ==="
printfn ""
printfn "Ising Anyons: {1, σ, ψ}"
printfn " σ × σ = 1 + ψ (creates superposition)"
printfn " σ × ψ = σ (fermion acts like Z gate)"
printfn " ψ × ψ = 1 (fermions annihilate)"
printfn ""
printfn "Fibonacci Anyons: {1, τ}"
printfn " τ × τ = 1 + τ (creates superposition)"
printfn " (Simpler particle set but MORE powerful!)"
printfn ""
// ============================================================================
// Performance Comparison: Same Operation on Both Backends
// ============================================================================
printfn "=== Performance Comparison: Initialize + Braid ==="
printfn ""
let measurePerformance backend anyonType label = task {
let sw = Stopwatch.StartNew()
let! result = topological backend {
do! TopologicalBuilder.initialize anyonType 6
do! TopologicalBuilder.braid 0
do! TopologicalBuilder.braid 2
do! TopologicalBuilder.braid 4
}
sw.Stop()
match result with
| Ok () ->
printfn "✅ %s: %.3f ms" label sw.Elapsed.TotalMilliseconds
return Ok sw.Elapsed.TotalMilliseconds
| Error err ->
printfn "❌ %s: Failed - %s" label err.Message
return Error err
}
let isingTime = measurePerformance isingBackend AnyonSpecies.AnyonType.Ising "Ising Backend "
|> Async.AwaitTask |> Async.RunSynchronously
let fibTime = measurePerformance fibonacciBackend AnyonSpecies.AnyonType.Fibonacci "Fibonacci Backend"
|> Async.AwaitTask |> Async.RunSynchronously
printfn ""
match (isingTime, fibTime) with
| (Ok t1, Ok t2) ->
if t1 < t2 then
printfn "Ising backend is %.2fx faster" (t2 / t1)
else
printfn "Fibonacci backend is %.2fx faster" (t1 / t2)
| _ ->
printfn "Performance comparison incomplete"
printfn ""
// ============================================================================
// Computational Power Comparison
// ============================================================================
printfn "=== Computational Power ==="
printfn ""
printfn "┌──────────────────┬─────────────────┬──────────────────────┐"
printfn "│ Capability │ Ising Anyons │ Fibonacci Anyons │"
printfn "├──────────────────┼─────────────────┼──────────────────────┤"
printfn "│ Clifford Gates │ ✅ Yes │ ✅ Yes │"
printfn "│ T Gate │ ❌ Magic States │ ✅ Braiding Only │"
printfn "│ Universal QC │ ⚠️ Hybrid │ ✅ Pure Braiding │"
printfn "│ Hardware Status │ ✅ Experimental │ ❌ Theoretical │"
printfn "│ Fusion Outcomes │ 3 particles │ 2 particles │"
printfn "│ Complexity │ Lower │ Higher │"
printfn "└──────────────────┴─────────────────┴──────────────────────┘"
printfn ""
// ============================================================================
// Fusion Measurement Statistics
// ============================================================================
printfn "=== Fusion Measurement Statistics ==="
printfn ""
let runFusionStatistics (backend: TopologicalBackend.ITopologicalBackend) anyonType label numTrials = task {
let outcomes = System.Collections.Generic.Dictionary<AnyonSpecies.Particle, int>()
for i in 1..numTrials do
let! initResult = backend.Initialize anyonType 2
match initResult with
| Ok state ->
let! measureResult = backend.MeasureFusion 0 state
match measureResult with
| Ok (outcome, _, _) ->
if outcomes.ContainsKey(outcome) then
outcomes.[outcome] <- outcomes.[outcome] + 1
else
outcomes.[outcome] <- 1
| Error _ -> ()
| Error _ -> ()
printfn "%s (2 anyons, %d trials):" label numTrials
for kvp in outcomes do
let percentage = (float kvp.Value / float numTrials) * 100.0
printfn " %A: %d times (%.1f%%)" kvp.Key kvp.Value percentage
printfn ""
}
runFusionStatistics isingBackend AnyonSpecies.AnyonType.Ising "Ising σ × σ" 1000
|> Async.AwaitTask |> Async.RunSynchronously
runFusionStatistics fibonacciBackend AnyonSpecies.AnyonType.Fibonacci "Fibonacci τ × τ" 1000
|> Async.AwaitTask |> Async.RunSynchronously
// ============================================================================
// Which Backend to Use?
// ============================================================================
printfn "=== Which Backend Should You Use? ==="
printfn ""
printfn "Choose ISING ANYONS if:"
printfn " ✅ You want to match Microsoft's hardware roadmap"
printfn " ✅ You're simulating Majorana-based topological computers"
printfn " ✅ You need realistic hardware emulation"
printfn " ✅ You're implementing Clifford circuits (H, S, CNOT, CZ)"
printfn " ⚠️ Note: Requires magic state distillation for T gates"
printfn ""
printfn "Choose FIBONACCI ANYONS if:"
printfn " ✅ You want theoretical exploration of universal braiding"
printfn " ✅ You're researching topological quantum computation theory"
printfn " ✅ You need pure braiding universality (no magic states)"
printfn " ⚠️ Note: Not yet realized in physical systems"
printfn ""
// ============================================================================
// Example: Validation Test
// ============================================================================
printfn "=== Validation: Capability Check ==="
printfn ""
let validateBackend (backend: TopologicalBackend.ITopologicalBackend) requirements =
let result = TopologicalBackend.validateCapabilities backend requirements
match result with
| Ok () ->
printfn "✅ Backend meets all requirements"
| Error err ->
printfn "❌ Backend validation failed: %s" err.Message
// Require Ising anyons with braiding support
let isingRequirements = {
TopologicalBackend.SupportedAnyonTypes = [AnyonSpecies.AnyonType.Ising]
TopologicalBackend.MaxAnyons = Some 4
TopologicalBackend.SupportsBraiding = true
TopologicalBackend.SupportsMeasurement = true
TopologicalBackend.SupportsFMoves = false
TopologicalBackend.SupportsErrorCorrection = false
}
printfn "Testing Ising backend against requirements:"
validateBackend isingBackend isingRequirements
printfn ""
// Try to use Ising backend for Fibonacci (should fail)
let wrongRequirements = {
TopologicalBackend.SupportedAnyonTypes = [AnyonSpecies.AnyonType.Fibonacci]
TopologicalBackend.MaxAnyons = None
TopologicalBackend.SupportsBraiding = true
TopologicalBackend.SupportsMeasurement = true
TopologicalBackend.SupportsFMoves = false
TopologicalBackend.SupportsErrorCorrection = false
}
printfn "Testing Ising backend with Fibonacci requirements (should fail):"
validateBackend isingBackend wrongRequirements
printfn ""
printfn "=== Backend Comparison Complete ==="
printfn ""
printfn "Key Takeaways:"
printfn "1. Ising anyons match Microsoft's Majorana hardware"
printfn "2. Fibonacci anyons are theoretically more powerful"
printfn "3. Both backends support core topological operations"
printfn "4. Performance characteristics are similar for basic operations"
printfn "5. Choose based on use case (hardware emulation vs theory)"