-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIntegerVariablesExample.fsx
More file actions
340 lines (279 loc) · 13.4 KB
/
IntegerVariablesExample.fsx
File metadata and controls
340 lines (279 loc) · 13.4 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
/// Integer Variables Example - Native Integer Support for QAOA
///
/// USE CASE: Work with integer decision variables directly in quantum optimization
///
/// PROBLEM: Many real-world optimization problems involve integer variables:
/// production quantities, resource allocation, scheduling, configuration tuning.
/// This example demonstrates multiple encoding strategies (Binary, OneHot,
/// DomainWall, BoundedInteger) with automatic qubit allocation and constraint
/// enforcement.
(*
===============================================================================
Background Theory
===============================================================================
Integer variables in QUBO require encoding into binary (qubit) representations.
The choice of encoding affects qubit count, constraint structure, and solution
quality:
- Binary / BoundedInteger: log2(range) qubits. Value = sum of 2^k * x_k.
Most qubit-efficient for large ranges but loses ordering locality.
- OneHot: one qubit per value. Exactly one qubit is 1. Best for unordered
categories (mutually exclusive choices). Constraint: sum(x_k) = 1.
- DomainWall: (n-1) qubits for n values. A wall of 1s followed by 0s.
Natural for ordered levels (priorities, quality tiers). Saves 1 qubit
vs OneHot while preserving adjacency structure.
Key Equations:
- BoundedInteger: value = low + sum_{k=0}^{ceil(log2(range))-1} 2^k * x_k
- OneHot constraint: (sum_k x_k - 1)^2 = 0
- DomainWall constraint: x_{k+1} <= x_k (monotone decreasing)
References:
[1] Glover et al., "Quantum Bridge Analytics I", 4OR 17, 335-371 (2019).
[2] Lucas, "Ising formulations of many NP problems", Front. Phys. 2 (2014).
Usage:
dotnet fsi IntegerVariablesExample.fsx (defaults)
dotnet fsi IntegerVariablesExample.fsx -- --help
dotnet fsi IntegerVariablesExample.fsx -- --example production
dotnet fsi IntegerVariablesExample.fsx -- --quiet --csv results.csv
*)
//#r "nuget: FSharp.Azure.Quantum"
#r "nuget: Microsoft.Extensions.Logging.Abstractions, 10.0.0"
#r "../../src/FSharp.Azure.Quantum/bin/Debug/net10.0/FSharp.Azure.Quantum.dll"
#load "../_common/Cli.fs"
#load "../_common/Data.fs"
#load "../_common/Reporting.fs"
open FSharp.Azure.Quantum
open System
open FSharp.Azure.Quantum.Examples.Common
// ==============================================================================
// CLI ARGUMENT PARSING
// ==============================================================================
let argv = fsi.CommandLineArgs |> Array.skip 1
let args = Cli.parse argv
Cli.exitIfHelp
"IntegerVariablesExample.fsx"
"Integer variable encodings for quantum QAOA optimization."
[ { Cli.OptionSpec.Name = "example"; Description = "Example: encodings|production|scheduling|routes|mixed|all"; Default = Some "encodings" }
{ Cli.OptionSpec.Name = "output"; Description = "Write results to JSON file"; Default = None }
{ Cli.OptionSpec.Name = "csv"; Description = "Write results to CSV file"; Default = None }
{ Cli.OptionSpec.Name = "quiet"; Description = "Suppress informational output"; Default = None } ]
args
let quiet = Cli.hasFlag "quiet" args
let outputPath = Cli.tryGet "output" args
let csvPath = Cli.tryGet "csv" args
let exampleName = Cli.getOr "example" "encodings" args
// ==============================================================================
// DISPLAY HELPERS
// ==============================================================================
let printHeader title =
if not quiet then
printfn ""
printfn "%s" title
printfn "%s" (String.replicate (String.length title) "-")
// ==============================================================================
// RESULT ROW BUILDER
// ==============================================================================
let encodingRow
(example: string)
(encoding: string)
(qubits: int)
(detail: string) : Map<string, string> =
Map.ofList
[ "example", example
"encoding", encoding
"qubits", sprintf "%d" qubits
"detail", detail ]
// ==============================================================================
// DOMAIN TYPES
// ==============================================================================
type Product =
{ Name: string
Profit: float
Resource1: int
Resource2: int }
type Task =
{ Id: string
Duration: int
Deadline: int }
type Route =
{ Name: string
Distance: float
Traffic: string }
// ==============================================================================
// EXAMPLES
// ==============================================================================
let allResults = ResizeArray<Map<string, string>>()
/// Example 1: Compare encoding strategies for range [0,15]
let runEncodings () =
printHeader "Example 1: Encoding Strategy Comparison"
let encodings =
[ ("OneHot", VariableEncoding.OneHot 16)
("DomainWall", VariableEncoding.DomainWall 16)
("BoundedInteger", VariableEncoding.BoundedInteger (0, 15)) ]
if not quiet then
printfn " Integer range: [0, 15] (16 values)"
printfn ""
printfn " %-20s | Qubits | Efficiency" "Encoding"
printfn " %s-+--------+-----------" (String.replicate 20 "-")
for (name, enc) in encodings do
let q = VariableEncoding.qubitCount enc
let eff = 16.0 / float q
if not quiet then
printfn " %-20s | %6d | %.2fx" name q eff
allResults.Add (encodingRow "encodings" name q (sprintf "%.2fx efficiency" eff))
if not quiet then
printfn ""
printfn " Best for unordered categories: OneHot"
printfn " Best for ordered levels: DomainWall"
printfn " Best for large ranges: BoundedInteger (log scaling)"
/// Example 2: Production planning with BoundedInteger
let runProduction () =
printHeader "Example 2: Production Planning (BoundedInteger)"
let products =
[ { Name = "Product A"; Profit = 50.0; Resource1 = 2; Resource2 = 1 }
{ Name = "Product B"; Profit = 40.0; Resource1 = 1; Resource2 = 2 }
{ Name = "Product C"; Profit = 60.0; Resource1 = 3; Resource2 = 1 } ]
let maxQty = 5
let enc = VariableEncoding.BoundedInteger (0, maxQty)
let qPerVar = VariableEncoding.qubitCount enc
let totalQ = qPerVar * products.Length
if not quiet then
printfn " R1 available: 10, R2 available: 8"
printfn " Products:"
for p in products do
printfn " %s: profit=$%.0f, R1=%d, R2=%d" p.Name p.Profit p.Resource1 p.Resource2
printfn ""
printfn " Encoding: BoundedInteger [0, %d], %d qubits/var, %d total" maxQty qPerVar totalQ
// Verify encode/decode roundtrip
if not quiet then
printfn " Roundtrip verification:"
for qty in [ 0; 1; 3; 5 ] do
let bits = VariableEncoding.encode enc qty
let decoded = VariableEncoding.decode enc bits
let bitsStr = bits |> List.map string |> String.concat ""
printfn " qty %d -> %s -> %d" qty bitsStr decoded
allResults.Add (encodingRow "production" "BoundedInteger" totalQ (sprintf "%d vars x %d qubits" products.Length qPerVar))
/// Example 3: Scheduling with DomainWall encoding
let runScheduling () =
printHeader "Example 3: Priority-Based Scheduling (DomainWall)"
let tasks =
[ { Id = "Task A"; Duration = 3; Deadline = 5 }
{ Id = "Task B"; Duration = 2; Deadline = 3 }
{ Id = "Task C"; Duration = 4; Deadline = 7 }
{ Id = "Task D"; Duration = 1; Deadline = 2 } ]
let levels = 5
let enc = VariableEncoding.DomainWall levels
let q = VariableEncoding.qubitCount enc
if not quiet then
printfn " %d tasks, priority levels 1-%d" tasks.Length levels
printfn " DomainWall encoding: %d qubits (vs %d for OneHot)" q levels
printfn " Bit patterns:"
for p in 1 .. levels do
let bits = VariableEncoding.encode enc p
let bitsStr = bits |> List.map string |> String.concat ""
let decoded = VariableEncoding.decode enc bits
printfn " Priority %d: %s -> %d" p bitsStr decoded
allResults.Add (encodingRow "scheduling" "DomainWall" (q * tasks.Length) (sprintf "%d tasks x %d levels" tasks.Length levels))
/// Example 4: Route selection with OneHot
let runRoutes () =
printHeader "Example 4: Route Selection (OneHot)"
let routes =
[ { Name = "Highway"; Distance = 25.0; Traffic = "Heavy" }
{ Name = "City"; Distance = 18.0; Traffic = "Moderate" }
{ Name = "Scenic"; Distance = 35.0; Traffic = "Light" }
{ Name = "Express"; Distance = 22.0; Traffic = "Variable" } ]
let enc = VariableEncoding.OneHot routes.Length
let q = VariableEncoding.qubitCount enc
if not quiet then
printfn " %d routes, OneHot: %d qubits (one per route)" routes.Length q
printfn " Constraint: exactly one bit = 1"
printfn " Bit patterns:"
for i in 0 .. routes.Length - 1 do
let bits = VariableEncoding.encode enc i
let bitsStr = bits |> List.map string |> String.concat " "
printfn " %s: [%s]" routes.[i].Name bitsStr
let constraintWeight = 10.0
let penalty = VariableEncoding.constraintPenalty enc constraintWeight
if not quiet then
printfn " Constraint penalty (weight=%.0f): diag=%.0f, off-diag=%.0f"
constraintWeight penalty.[0, 0] penalty.[0, 1]
allResults.Add (encodingRow "routes" "OneHot" q (sprintf "%d routes" routes.Length))
/// Example 5: Mixed integer variables
let runMixed () =
printHeader "Example 5: Mixed Integer Variables"
let variables =
[ { Name = "room_booked"; VarType = BinaryVar }
{ Name = "attendees"; VarType = IntegerVar (0, 20) }
{ Name = "time_slot"; VarType = CategoricalVar ([ "Morning"; "Afternoon"; "Evening" ]) } ]
if not quiet then
printfn " Conference room booking:"
for v in variables do
match v.VarType with
| BinaryVar ->
let enc = VariableEncoding.Binary
printfn " %s: Binary (%d qubit)" v.Name (VariableEncoding.qubitCount enc)
| IntegerVar (lo, hi) ->
let enc = VariableEncoding.BoundedInteger (lo, hi)
printfn " %s: Integer [%d,%d] (%d qubits)" v.Name lo hi (VariableEncoding.qubitCount enc)
| CategoricalVar cats ->
let enc = VariableEncoding.OneHot cats.Length
printfn " %s: Categorical %A (%d qubits)" v.Name cats (VariableEncoding.qubitCount enc)
let quboMatrix = QuboEncoding.encodeVariables variables
if not quiet then
printfn " QUBO matrix: %d total qubits" quboMatrix.Size
printfn " Variable names: %A" quboMatrix.VariableNames
allResults.Add (encodingRow "mixed" "Mixed" quboMatrix.Size (sprintf "%d vars" variables.Length))
// ==============================================================================
// MAIN EXECUTION
// ==============================================================================
if not quiet then
printfn "======================================"
printfn "Integer Variables in Quantum QAOA"
printfn "======================================"
match exampleName.ToLowerInvariant() with
| "all" ->
runEncodings ()
runProduction ()
runScheduling ()
runRoutes ()
runMixed ()
| "encodings" -> runEncodings ()
| "production" -> runProduction ()
| "scheduling" -> runScheduling ()
| "routes" -> runRoutes ()
| "mixed" -> runMixed ()
| other ->
eprintfn "Unknown example: '%s'. Use: encodings|production|scheduling|routes|mixed|all" other
exit 1
if not quiet then
printfn ""
printfn "======================================"
printfn "Integer Variables Complete!"
printfn "======================================"
// ==============================================================================
// STRUCTURED OUTPUT
// ==============================================================================
let resultRows = allResults |> Seq.toList
match outputPath with
| Some path ->
Reporting.writeJson path resultRows
if not quiet then printfn "Results written to %s" path
| None -> ()
match csvPath with
| Some path ->
let header = [ "example"; "encoding"; "qubits"; "detail" ]
let rows =
resultRows
|> List.map (fun m ->
header |> List.map (fun h -> m |> Map.tryFind h |> Option.defaultValue ""))
Reporting.writeCsv path header rows
if not quiet then printfn "Results written to %s" path
| None -> ()
// ==============================================================================
// USAGE HINTS
// ==============================================================================
if argv.Length = 0 && not quiet then
printfn ""
printfn "Tip: Run with --help to see all options:"
printfn " dotnet fsi IntegerVariablesExample.fsx -- --help"
printfn " dotnet fsi IntegerVariablesExample.fsx -- --example all"
printfn " dotnet fsi IntegerVariablesExample.fsx -- --quiet --output results.json"
printfn ""