qleisli 0.2.6

Experimental quantum language with linear ownership, exact contracts and independent finite IR verification
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! Explicit field maps for raw operations and circuit actions.
use super::{Codec, Decoder, Encoder, Error, Result, Value};
use crate::ir::{CircuitAction, ProtectedUse, RawOp, UnitaryStep};

impl Codec for RawOp {
    fn write(&self, c: &mut Encoder) -> Result<Value> {
        c.charge(1)?;
        match self {
            Self::CertifiedCompute {
                source,
                source_out,
                ancilla_wires,
                function,
                use_steps,
                logical_steps,
            } => Ok(Value::object([
                ("tag", Value::String("certified_compute".into())),
                ("source", source.write(c)?),
                ("source_out", source_out.write(c)?),
                ("ancilla_wires", ancilla_wires.write(c)?),
                ("function", function.write(c)?),
                ("use_steps", use_steps.write(c)?),
                ("logical_steps", logical_steps.write(c)?),
            ])),
            Self::ApplyUnitary {
                input,
                output,
                steps,
            } => Ok(Value::object([
                ("tag", Value::String("apply_unitary".into())),
                ("input", input.write(c)?),
                ("output", output.write(c)?),
                ("steps", steps.write(c)?),
            ])),
            Self::Init0 { output, wire } => Ok(Value::object([
                ("tag", Value::String("init0".into())),
                ("output", output.write(c)?),
                ("wire", wire.write(c)?),
            ])),
            Self::Gate {
                gate,
                input,
                output,
            } => Ok(Value::object([
                ("tag", Value::String("gate".into())),
                ("gate", gate.write(c)?),
                ("input", input.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::Cnot {
                control,
                target,
                control_out,
                target_out,
            } => Ok(Value::object([
                ("tag", Value::String("cnot".into())),
                ("control", control.write(c)?),
                ("target", target.write(c)?),
                ("control_out", control_out.write(c)?),
                ("target_out", target_out.write(c)?),
            ])),
            Self::Toffoli {
                control_a,
                control_b,
                target,
                control_a_out,
                control_b_out,
                target_out,
            } => Ok(Value::object([
                ("tag", Value::String("toffoli".into())),
                ("control_a", control_a.write(c)?),
                ("control_b", control_b.write(c)?),
                ("target", target.write(c)?),
                ("control_a_out", control_a_out.write(c)?),
                ("control_b_out", control_b_out.write(c)?),
                ("target_out", target_out.write(c)?),
            ])),
            Self::QuantumIf {
                control,
                target,
                control_out,
                target_out,
                zero_ops,
                one_ops,
            } => Ok(Value::object([
                ("tag", Value::String("quantum_if".into())),
                ("control", control.write(c)?),
                ("target", target.write(c)?),
                ("control_out", control_out.write(c)?),
                ("target_out", target_out.write(c)?),
                ("zero_ops", zero_ops.write(c)?),
                ("one_ops", one_ops.write(c)?),
            ])),
            Self::Split {
                input,
                left,
                right,
                left_bits,
            } => Ok(Value::object([
                ("tag", Value::String("split".into())),
                ("input", input.write(c)?),
                ("left", left.write(c)?),
                ("right", right.write(c)?),
                ("left_bits", left_bits.write(c)?),
            ])),
            Self::Join {
                left,
                right,
                output,
            } => Ok(Value::object([
                ("tag", Value::String("join".into())),
                ("left", left.write(c)?),
                ("right", right.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::LiftBasis {
                input,
                output,
                output_wires,
                table,
            } => Ok(Value::object([
                ("tag", Value::String("lift_basis".into())),
                ("input", input.write(c)?),
                ("output", output.write(c)?),
                ("output_wires", output_wires.write(c)?),
                ("table", table.write(c)?),
            ])),
            Self::MeasureZ { input, output } => Ok(Value::object([
                ("tag", Value::String("measure_z".into())),
                ("input", input.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::Reset {
                input,
                output,
                fresh_wire,
            } => Ok(Value::object([
                ("tag", Value::String("reset".into())),
                ("input", input.write(c)?),
                ("output", output.write(c)?),
                ("fresh_wire", fresh_wire.write(c)?),
            ])),
            Self::Discard { input } => Ok(Value::object([
                ("tag", Value::String("discard".into())),
                ("input", input.write(c)?),
            ])),
            Self::ClassicalConst { value, output } => Ok(Value::object([
                ("tag", Value::String("classical_const".into())),
                ("value", value.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::ClassicalNot { input, output } => Ok(Value::object([
                ("tag", Value::String("classical_not".into())),
                ("input", input.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::ClassicalXor {
                left,
                right,
                output,
            } => Ok(Value::object([
                ("tag", Value::String("classical_xor".into())),
                ("left", left.write(c)?),
                ("right", right.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::ClassicalAnd {
                left,
                right,
                output,
            } => Ok(Value::object([
                ("tag", Value::String("classical_and".into())),
                ("left", left.write(c)?),
                ("right", right.write(c)?),
                ("output", output.write(c)?),
            ])),
            Self::ClassicalBranch {
                condition,
                then_ops,
                else_ops,
                quantum_phis,
                classical_phis,
            } => Ok(Value::object([
                ("tag", Value::String("classical_branch".into())),
                ("condition", condition.write(c)?),
                ("then_ops", then_ops.write(c)?),
                ("else_ops", else_ops.write(c)?),
                ("quantum_phis", quantum_phis.write(c)?),
                ("classical_phis", classical_phis.write(c)?),
            ])),
            Self::ComputeUseUncompute {
                source,
                source_out,
                targets,
                ancilla_wires,
                function,
                use_ops,
            } => Ok(Value::object([
                ("tag", Value::String("compute_use_uncompute".into())),
                ("source", source.write(c)?),
                ("source_out", source_out.write(c)?),
                ("targets", targets.write(c)?),
                ("ancilla_wires", ancilla_wires.write(c)?),
                ("function", function.write(c)?),
                ("use_ops", use_ops.write(c)?),
            ])),
        }
    }
    fn read(v: &Value, c: &Decoder<'_>) -> Result<Self> {
        match v.field("tag")?.text()? {
            "certified_compute" => {
                v.fields(&[
                    "tag",
                    "source",
                    "source_out",
                    "ancilla_wires",
                    "function",
                    "use_steps",
                    "logical_steps",
                ])?;
                Ok(Self::CertifiedCompute {
                    source: Codec::read(v.field("source")?, c)?,
                    source_out: Codec::read(v.field("source_out")?, c)?,
                    ancilla_wires: Codec::read(v.field("ancilla_wires")?, c)?,
                    function: Codec::read(v.field("function")?, c)?,
                    use_steps: Codec::read(v.field("use_steps")?, c)?,
                    logical_steps: Codec::read(v.field("logical_steps")?, c)?,
                })
            }
            "apply_unitary" => {
                v.fields(&["tag", "input", "output", "steps"])?;
                Ok(Self::ApplyUnitary {
                    input: Codec::read(v.field("input")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                    steps: Codec::read(v.field("steps")?, c)?,
                })
            }
            "init0" => {
                v.fields(&["tag", "output", "wire"])?;
                Ok(Self::Init0 {
                    output: Codec::read(v.field("output")?, c)?,
                    wire: Codec::read(v.field("wire")?, c)?,
                })
            }
            "gate" => {
                v.fields(&["tag", "gate", "input", "output"])?;
                Ok(Self::Gate {
                    gate: Codec::read(v.field("gate")?, c)?,
                    input: Codec::read(v.field("input")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "cnot" => {
                v.fields(&["tag", "control", "target", "control_out", "target_out"])?;
                Ok(Self::Cnot {
                    control: Codec::read(v.field("control")?, c)?,
                    target: Codec::read(v.field("target")?, c)?,
                    control_out: Codec::read(v.field("control_out")?, c)?,
                    target_out: Codec::read(v.field("target_out")?, c)?,
                })
            }
            "toffoli" => {
                v.fields(&[
                    "tag",
                    "control_a",
                    "control_b",
                    "target",
                    "control_a_out",
                    "control_b_out",
                    "target_out",
                ])?;
                Ok(Self::Toffoli {
                    control_a: Codec::read(v.field("control_a")?, c)?,
                    control_b: Codec::read(v.field("control_b")?, c)?,
                    target: Codec::read(v.field("target")?, c)?,
                    control_a_out: Codec::read(v.field("control_a_out")?, c)?,
                    control_b_out: Codec::read(v.field("control_b_out")?, c)?,
                    target_out: Codec::read(v.field("target_out")?, c)?,
                })
            }
            "quantum_if" => {
                v.fields(&[
                    "tag",
                    "control",
                    "target",
                    "control_out",
                    "target_out",
                    "zero_ops",
                    "one_ops",
                ])?;
                Ok(Self::QuantumIf {
                    control: Codec::read(v.field("control")?, c)?,
                    target: Codec::read(v.field("target")?, c)?,
                    control_out: Codec::read(v.field("control_out")?, c)?,
                    target_out: Codec::read(v.field("target_out")?, c)?,
                    zero_ops: Codec::read(v.field("zero_ops")?, c)?,
                    one_ops: Codec::read(v.field("one_ops")?, c)?,
                })
            }
            "split" => {
                v.fields(&["tag", "input", "left", "right", "left_bits"])?;
                Ok(Self::Split {
                    input: Codec::read(v.field("input")?, c)?,
                    left: Codec::read(v.field("left")?, c)?,
                    right: Codec::read(v.field("right")?, c)?,
                    left_bits: Codec::read(v.field("left_bits")?, c)?,
                })
            }
            "join" => {
                v.fields(&["tag", "left", "right", "output"])?;
                Ok(Self::Join {
                    left: Codec::read(v.field("left")?, c)?,
                    right: Codec::read(v.field("right")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "lift_basis" => {
                v.fields(&["tag", "input", "output", "output_wires", "table"])?;
                Ok(Self::LiftBasis {
                    input: Codec::read(v.field("input")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                    output_wires: Codec::read(v.field("output_wires")?, c)?,
                    table: Codec::read(v.field("table")?, c)?,
                })
            }
            "measure_z" => {
                v.fields(&["tag", "input", "output"])?;
                Ok(Self::MeasureZ {
                    input: Codec::read(v.field("input")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "reset" => {
                v.fields(&["tag", "input", "output", "fresh_wire"])?;
                Ok(Self::Reset {
                    input: Codec::read(v.field("input")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                    fresh_wire: Codec::read(v.field("fresh_wire")?, c)?,
                })
            }
            "discard" => {
                v.fields(&["tag", "input"])?;
                Ok(Self::Discard {
                    input: Codec::read(v.field("input")?, c)?,
                })
            }
            "classical_const" => {
                v.fields(&["tag", "value", "output"])?;
                Ok(Self::ClassicalConst {
                    value: Codec::read(v.field("value")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "classical_not" => {
                v.fields(&["tag", "input", "output"])?;
                Ok(Self::ClassicalNot {
                    input: Codec::read(v.field("input")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "classical_xor" => {
                v.fields(&["tag", "left", "right", "output"])?;
                Ok(Self::ClassicalXor {
                    left: Codec::read(v.field("left")?, c)?,
                    right: Codec::read(v.field("right")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "classical_and" => {
                v.fields(&["tag", "left", "right", "output"])?;
                Ok(Self::ClassicalAnd {
                    left: Codec::read(v.field("left")?, c)?,
                    right: Codec::read(v.field("right")?, c)?,
                    output: Codec::read(v.field("output")?, c)?,
                })
            }
            "classical_branch" => {
                v.fields(&[
                    "tag",
                    "condition",
                    "then_ops",
                    "else_ops",
                    "quantum_phis",
                    "classical_phis",
                ])?;
                Ok(Self::ClassicalBranch {
                    condition: Codec::read(v.field("condition")?, c)?,
                    then_ops: Codec::read(v.field("then_ops")?, c)?,
                    else_ops: Codec::read(v.field("else_ops")?, c)?,
                    quantum_phis: Codec::read(v.field("quantum_phis")?, c)?,
                    classical_phis: Codec::read(v.field("classical_phis")?, c)?,
                })
            }
            "compute_use_uncompute" => {
                v.fields(&[
                    "tag",
                    "source",
                    "source_out",
                    "targets",
                    "ancilla_wires",
                    "function",
                    "use_ops",
                ])?;
                Ok(Self::ComputeUseUncompute {
                    source: Codec::read(v.field("source")?, c)?,
                    source_out: Codec::read(v.field("source_out")?, c)?,
                    targets: Codec::read(v.field("targets")?, c)?,
                    ancilla_wires: Codec::read(v.field("ancilla_wires")?, c)?,
                    function: Codec::read(v.field("function")?, c)?,
                    use_ops: Codec::read(v.field("use_ops")?, c)?,
                })
            }
            _ => Err(Error::format("unknown node tag")),
        }
    }
}
impl Codec for CircuitAction {
    fn write(&self, c: &mut Encoder) -> Result<Value> {
        c.charge(1)?;
        match self {
            Self::Contract {
                indices,
                evidence,
                adjoint,
            } => Ok(Value::object([
                ("tag", Value::String("contract".into())),
                ("indices", indices.write(c)?),
                ("evidence", evidence.write(c)?),
                ("adjoint", adjoint.write(c)?),
            ])),
            Self::Hadamard { target } => Ok(Value::object([
                ("tag", Value::String("hadamard".into())),
                ("target", target.write(c)?),
            ])),
            Self::Monomial {
                indices,
                permutation,
                phases,
            } => Ok(Value::object([
                ("tag", Value::String("monomial".into())),
                ("indices", indices.write(c)?),
                ("permutation", permutation.write(c)?),
                ("phases", phases.write(c)?),
            ])),
        }
    }
    fn read(v: &Value, c: &Decoder<'_>) -> Result<Self> {
        match v.field("tag")?.text()? {
            "contract" => {
                v.fields(&["tag", "indices", "evidence", "adjoint"])?;
                Ok(Self::Contract {
                    indices: Codec::read(v.field("indices")?, c)?,
                    evidence: Codec::read(v.field("evidence")?, c)?,
                    adjoint: Codec::read(v.field("adjoint")?, c)?,
                })
            }
            "hadamard" => {
                v.fields(&["tag", "target"])?;
                Ok(Self::Hadamard {
                    target: Codec::read(v.field("target")?, c)?,
                })
            }
            "monomial" => {
                v.fields(&["tag", "indices", "permutation", "phases"])?;
                Ok(Self::Monomial {
                    indices: Codec::read(v.field("indices")?, c)?,
                    permutation: Codec::read(v.field("permutation")?, c)?,
                    phases: Codec::read(v.field("phases")?, c)?,
                })
            }
            _ => Err(Error::format("unknown node tag")),
        }
    }
}
impl Codec for ProtectedUse {
    fn write(&self, c: &mut Encoder) -> Result<Value> {
        c.charge(1)?;
        match self {
            Self::ProtectedGate { bit, gate } => Ok(Value::object([
                ("tag", Value::String("protected_gate".into())),
                ("bit", bit.write(c)?),
                ("gate", gate.write(c)?),
            ])),
            Self::ControlledTargetGate {
                controls,
                target_index,
                gate,
            } => Ok(Value::object([
                ("tag", Value::String("controlled_target_gate".into())),
                ("controls", controls.write(c)?),
                ("target_index", target_index.write(c)?),
                ("gate", gate.write(c)?),
            ])),
            Self::ControlledPhase { controls, phase } => Ok(Value::object([
                ("tag", Value::String("controlled_phase".into())),
                ("controls", controls.write(c)?),
                ("phase", phase.write(c)?),
            ])),
        }
    }
    fn read(v: &Value, c: &Decoder<'_>) -> Result<Self> {
        match v.field("tag")?.text()? {
            "protected_gate" => {
                v.fields(&["tag", "bit", "gate"])?;
                Ok(Self::ProtectedGate {
                    bit: Codec::read(v.field("bit")?, c)?,
                    gate: Codec::read(v.field("gate")?, c)?,
                })
            }
            "controlled_target_gate" => {
                v.fields(&["tag", "controls", "target_index", "gate"])?;
                Ok(Self::ControlledTargetGate {
                    controls: Codec::read(v.field("controls")?, c)?,
                    target_index: Codec::read(v.field("target_index")?, c)?,
                    gate: Codec::read(v.field("gate")?, c)?,
                })
            }
            "controlled_phase" => {
                v.fields(&["tag", "controls", "phase"])?;
                Ok(Self::ControlledPhase {
                    controls: Codec::read(v.field("controls")?, c)?,
                    phase: Codec::read(v.field("phase")?, c)?,
                })
            }
            _ => Err(Error::format("unknown node tag")),
        }
    }
}
impl Codec for UnitaryStep {
    fn write(&self, c: &mut Encoder) -> Result<Value> {
        c.charge(1)?;
        match self {
            Self::Gate { gate, target_index } => Ok(Value::object([
                ("tag", Value::String("gate".into())),
                ("gate", gate.write(c)?),
                ("target_index", target_index.write(c)?),
            ])),
            Self::Cnot {
                control_index,
                target_index,
            } => Ok(Value::object([
                ("tag", Value::String("cnot".into())),
                ("control_index", control_index.write(c)?),
                ("target_index", target_index.write(c)?),
            ])),
            Self::Toffoli {
                control_a_index,
                control_b_index,
                target_index,
            } => Ok(Value::object([
                ("tag", Value::String("toffoli".into())),
                ("control_a_index", control_a_index.write(c)?),
                ("control_b_index", control_b_index.write(c)?),
                ("target_index", target_index.write(c)?),
            ])),
            Self::ScalarPhase(phase) => Ok(Value::object([
                ("tag", Value::String("scalar_phase".into())),
                ("phase", phase.write(c)?),
            ])),
        }
    }
    fn read(v: &Value, c: &Decoder<'_>) -> Result<Self> {
        match v.field("tag")?.text()? {
            "gate" => {
                v.fields(&["tag", "gate", "target_index"])?;
                Ok(Self::Gate {
                    gate: Codec::read(v.field("gate")?, c)?,
                    target_index: Codec::read(v.field("target_index")?, c)?,
                })
            }
            "cnot" => {
                v.fields(&["tag", "control_index", "target_index"])?;
                Ok(Self::Cnot {
                    control_index: Codec::read(v.field("control_index")?, c)?,
                    target_index: Codec::read(v.field("target_index")?, c)?,
                })
            }
            "toffoli" => {
                v.fields(&["tag", "control_a_index", "control_b_index", "target_index"])?;
                Ok(Self::Toffoli {
                    control_a_index: Codec::read(v.field("control_a_index")?, c)?,
                    control_b_index: Codec::read(v.field("control_b_index")?, c)?,
                    target_index: Codec::read(v.field("target_index")?, c)?,
                })
            }
            "scalar_phase" => {
                v.fields(&["tag", "phase"])?;
                Ok(Self::ScalarPhase(Codec::read(v.field("phase")?, c)?))
            }
            _ => Err(Error::format("unknown node tag")),
        }
    }
}