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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# This file is automatically generated by pyo3_stub_gen
# ruff: noqa: E501, F401
r"""
A list of the instructions in the block, in order of definition.
This does not include the label or terminator instructions.
"""
r"""
The label of the block, if any.
This is used to target this block in control flow.
"""
r"""
The control flow terminator instruction of the block, if any.
If this is ``None``, the implicit behavior is to "continue" to the subsequent block.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
...
r"""
Return the length of the longest path
from an initial instruction (one with no prerequisite instructions)
to a final instruction (one with no dependent instructions),
where the length of a path is the number of gate instructions in the path.
:param gate_minimum_qubit_count:
The minimum number of qubits in a gate
for it to be counted in the depth.
"""
...
r"""
Details about the expansion of a calibration.
"""
r"""
The calibration used to expand the instruction.
"""
r"""
The source map describing the nested expansions made.
"""
r"""
The range of instructions in the expanded list
which were generated by this expansion.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
A collection of Quil calibrations (`DEFCAL` instructions) with utility methods.
This exposes the semantics similar to [`CalibrationSet`] to Python users,
so see the documentation there for more information.
"""
r"""
Return a list of all [`CalibrationDefinition`]s in the set.
"""
r"""
Return a list of all [`MeasureCalibrationDefinition`]s in the set.
"""
...
r"""
Return the count of contained calibrations.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
Given an instruction, return the instructions to which it is expanded if there is a match.
Recursively calibrate instructions, returning an error if a calibration directly or indirectly
expands into itself.
Return only the expanded instructions; for more information about the expansion process,
see [`Self::expand_with_detail`].
"""
r"""
Append another [`CalibrationSet`] onto this one.
Calibrations with conflicting [`CalibrationSignature`]s are overwritten by the ones in the
given set.
"""
r"""
Return the final calibration which matches the gate per the `QuilT` specification:
A calibration matches a gate if:
1. It has the same name
2. It has the same modifiers
3. It has the same qubit count (any mix of fixed & variable)
4. It has the same parameter count (both specified and unspecified)
5. All fixed qubits in the calibration definition match those in the gate
6. All specified parameters in the calibration definition match those in the gate
"""
r"""
Returns the last-specified ``MeasureCalibrationDefinition`` that matches the target
qubit (if any), or otherwise the last-specified one that specified no qubit.
If multiple calibrations match the measurement, the precedence is as follows:
1. Match fixed qubit.
2. Match variable qubit.
3. Match no qubit.
In the case of multiple calibrations with equal precedence, the last one wins.
"""
r"""
Insert a [`CalibrationDefinition`] into the set.
If a calibration with the same [signature][crate::instruction::CalibrationSignature] already
exists in the set, it will be replaced and the old calibration will be returned.
"""
r"""
Insert a [`MeasureCalibrationDefinition`] into the set.
If a calibration with the same [signature][crate::instruction::CalibrationSignature] already
exists in the set, it will be replaced and the old calibration will be returned.
"""
r"""
Return true if this contains no data.
"""
r"""
Return the Quil instructions which describe the contained calibrations.
"""
r"""
The source of a calibration, either a [`CalibrationIdentifier`] or a
[`MeasureCalibrationIdentifier`].
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
Describes a `DEFCAL` instruction
"""
=
...
...
...
...
r"""
Describes a `DEFCAL MEASURE` instruction
"""
=
...
...
...
...
r"""
Error raised if the computed schedule is invalid.
"""
...
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
Return a list of all the basic blocks in the control flow graph, in order of definition.
"""
r"""
Return ``True`` if the program has dynamic control flow, i.e. contains a conditional branch instruction.
``False`` does not imply that there is only one basic block in the program.
Multiple basic blocks may have non-conditional control flow among them,
in which the execution order is deterministic and does not depend on program state.
This may be a sequence of basic blocks with fixed `JUMP`s or without explicit terminators.
"""
r"""
A collection of Quil frames (`DEFFRAME` instructions) with utility methods.
"""
...
r"""
Return the number of frames described within.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
Retrieve the `FrameAttributes` of a `Frame` by its `FrameIdentifier`.
"""
...
r"""
Return a list of all `FrameIdentifier`s described by this `FrameSet`.
"""
r"""
Insert a new frame by ID, overwriting any existing one.
"""
r"""
Return a new `FrameSet` which describes only the given `FrameIdentifier`s.
"""
r"""
Return true if this describes no frames.
"""
r"""
Merge another [FrameSet] with this one, overwriting any existing keys
"""
r"""
Return the Quil instructions which describe the contained frames.
"""
r"""
A source map describing how instructions in a source program were
expanded into a target program. Each entry describes an instruction
index in the source program which were expanded accordingto either
a calibration or a sequence gate definition.
"""
...
r"""
Given a particular calibration (`DEFCAL` or `DEFCAL MEASURE`), =
return the locations in the source which were expanded using that calibration.
This is `O(n)` where `n` is the number of first-level calibration expansions performed,
which is at worst `O(i)` where `i` is the number of source instructions.
"""
r"""
Given a gate signature, return the locations in the source program which were
expanded using that gate signature.
This is `O(n)` where `n` is the number of first-level sequence gate expansions performed,
which is at worst `O(i)` where `i` is the number of source instructions.
"""
r"""
Return all source ranges in the source map
which were used to generate the target index.
This is `O(n)` where `n` is the number of first-level expansions performed,
which is at worst `O(i)` where `i` is the number of source instructions.
"""
r"""
Return all target ranges which were used to generate the source range.
This is `O(n)` where `n` is the number of first-level expansions performed,
which is at worst `O(i)` where `i` is the number of source instructions.
"""
r"""
A source map entry, mapping a range of source instructions by index to an
`InstructionTarget`.
Note that both `source_location` and `target_location` are relative to the scope of expansion.
In the case of a nested expansion, both describe the location relative only to that
level of expansion and *not* the original program.
"""
r"""
The instruction index within the source program's body instructions.
"""
r"""
The location of the expanded instruction within the target program's body instructions.
"""
r"""
The result of having expanded a certain instruction within a program.
- `calibration`: The instruction has a matching Quil-T calibration and was expanded by it into
other instructions, as described by a `CalibrationExpansion`.
- `defgate_sequence`: The instruction has a matching `DEFGATE ... AS SEQUENCE` and was expanded
by it into other instructions, as described by a `DefGateSequenceExpansion`.
- `unmodified`: The instruction was not expanded and is described by an integer, the index of the instruction
within the resulting program's body instructions.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
=
...
...
...
...
=
...
...
...
...
=
...
...
...
...
...
...
...
...
...
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
[`DefGateSequenceExpansion`] references data from [`quil_rs::instruction::GateDefinition`]s used to expand instructions.
As such, it is incompatible with Python's memory management,
so we define an owned type here.
"""
r"""
The range of instructions in the expanded list which were generated by this expansion.
"""
r"""
The signature of the sequence gate definition which was used to expand the instruction.
"""
...
r"""
The source map describing the nested expansions made.
"""
r"""
A Quil Program instance describes a quantum program with metadata used in execution.
This contains not only instructions which are executed in turn on the quantum processor, but
also the "headers" used to describe and manipulate those instructions, such as calibrations
and frame definitions.
# For Python Users
The `quil.program` module contains classes for constructing and representing a Quil program.
## Examples
### Source Mapping for Calibration Expansion
```python
import inspect
from quil.program import Program
program_text = inspect.cleandoc(
'''
DEFCAL X 0:
Y 0
DEFCAL Y 0:
Z 0
X 0 # This instruction is index 0
Y 0 # This instruction is index 1
'''
)
# First, we parse the program and expand its calibrations
program = Program.parse(program_text)
expansion = program.expand_calibrations_with_source_map()
source_map = expansion.source_map()
# This is what we expect the expanded program to be. X and Y have each been replaced by Z.
expected_program_text = inspect.cleandoc(
'''
DEFCAL X 0:
Y 0
DEFCAL Y 0:
Z 0
Z 0 # This instruction is index 0
Z 0 # This instruction is index 1
'''
)
assert expansion.program().to_quil() == Program.parse(expected_program_text).to_quil()
# In order to discover _which_ calibration led to the first Z in the resulting program, we
# can interrogate the expansion source mapping.
#
# For instance, the X at index 0 should have been replaced with a Z at index 0.
# Here's how we can confirm that:
# First, we list the calibration expansion targets for that first instruction...
targets = source_map.list_targets_for_source_index(0)
# ...then we extract the expanded instruction.
# If the instruction had _not_ been expanded (i.e. there was no matching calibration), then `as_expanded()` would return `None`.
expanded = targets[0].as_expanded()
# This line shows how that `X 0` was expanded into instruction index 0 (only) within the expanded program.
# The end of the range is exclusive.
assert expanded.range() == range(0, 1)
# We can also map instructions in reverse: given an instruction index in the expanded program, we can find the source index.
# This is useful for understanding the provenance of instructions in the expanded program.
sources = source_map.list_sources_for_target_index(1)
# In this case, the instruction was expanded from the source program at index 1.
assert sources == [1]
```
"""
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
r"""
This will raise an error if the program contains any unresolved
placeholders. This is because they can't be converted to valid quil,
nor can they be serialized and deserialized in a consistent way.
"""
...
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
...
r"""
Add an instruction to the end of the program.
Note, parsing extern signatures is deferred here to maintain infallibility
of [`Program::add_instruction`]. This means that invalid `PRAGMA EXTERN`
instructions are still added to the [`Program::extern_pragma_map`];
duplicate `PRAGMA EXTERN` names are overwritten.
"""
r"""
Add a list of instructions to the end of the program.
"""
r"""
Like `Clone`, but does not clone the body instructions.
"""
r"""
Return the [control flow graph][] of the program.
[control flow graph]: https://en.wikipedia.org/wiki/Control-flow_graph
"""
r"""
Return a deep copy of the `Program`.
"""
r"""
Creates a new conjugate transpose of the [`Program`] by reversing the order of gate
instructions and applying the DAGGER modifier to each.
# Errors
Errors if any of the instructions in the program are not [`Instruction::Gate`]
"""
r"""
Expand any instructions in the program which have a matching calibration,
leaving the others unchanged.
Return the expanded copy of the program.
Returns an error if any instruction expands into itself.
See [`Program::expand_calibrations_with_source_map`] for a version that returns a source mapping.
"""
r"""
Expand any instructions in the program which have a matching calibration,
leaving the others unchanged.
Return the expanded copy of the program
and a source mapping describing the expansions made.
"""
r"""
Expand any instructions in the program which have a matching sequence gate definition, leaving
the others unchanged.
Recurses though each instruction while ensuring there is no cycle in the expansion graph (i.e. no sequence
gate definitions expand directly or indirectly into itself).
:param predicate: If provided, only sequence gate definitions which match the predicate will be expanded.
Defaults to expanding all sequence gate definitions.
# Example
Below, we show the results of gate sequence expansion on a program that has two gate
sequence definitions. The first, `seq1`, has a matching calibration and we do not
want to expand it. The second, `seq2`, does not have a matching calibration and
we do want to expand it.
>>> quil = '''
... DEFCAL seq1 0 1:
... FENCE 0 1
... NONBLOCKING PULSE 0 "rf" drag_gaussian(duration: 6.000000000000001e-08, fwhm: 1.5000000000000002e-08, t0: 3.0000000000000004e-08, anh: -190000000.0, alpha: -1.6453719598238201, scale: 0.168265925924524, phase: 0.0, detuning: 0)
... NONBLOCKING PULSE 1 "rf" drag_gaussian(duration: 6.000000000000001e-08, fwhm: 1.5000000000000002e-08, t0: 3.0000000000000004e-08, anh: -190000000.0, alpha: -1.6453719598238201, scale: 0.168265925924524, phase: 0.0, detuning: 0)
... FENCE 0 1
...
... DEFGATE seq1() a b AS SEQUENCE:
... RX(pi/2) a
... RX(pi/2) b
...
... DEFGATE seq2(%theta, %psi, %phi) a AS SEQUENCE:
... RZ(%theta) a
... RX(pi/2) a
... RZ(%phi) a
...
... seq1 0 1
... seq2(1.5707963267948966, 3.141592653589793, 0) 0
... seq2(3.141592653589793, 0, 1.5707963267948966) 1
... '''
>>> program = Program.parse(quil);
>>> calibrated_gate_names = {calibration.identifier.name for calibration in program.calibrations.calibrations}
>>> expanded_program = program.expand_defgate_sequences(lambda name: name not in calibrated_gate_names)
>>>
>>> expected_quil = '''
... DEFCAL seq1 0 1:
... FENCE 0 1
... NONBLOCKING PULSE 0 "rf" drag_gaussian(duration: 6.000000000000001e-08, fwhm: 1.5000000000000002e-08, t0: 3.0000000000000004e-08, anh: -190000000.0, alpha: -1.6453719598238201, scale: 0.168265925924524, phase: 0.0, detuning: 0)
... NONBLOCKING PULSE 1 "rf" drag_gaussian(duration: 6.000000000000001e-08, fwhm: 1.5000000000000002e-08, t0: 3.0000000000000004e-08, anh: -190000000.0, alpha: -1.6453719598238201, scale: 0.168265925924524, phase: 0.0, detuning: 0)
... FENCE 0 1
...
... DEFGATE seq1 a b AS SEQUENCE:
... RX(pi/2) a
... RX(pi/2) b
...
... seq1 0 1
...
... RZ(1.5707963267948966) 0
... RX(pi/2) 0
... RZ(3.141592653589793) 0
... RX(pi/2) 0
... RZ(0) 0
...
... RZ(3.141592653589793) 1
... RX(pi/2) 1
... RZ(0) 1
... RX(pi/2) 1
... RZ(1.5707963267948966) 1
... '''
>>>
>>> expected_program = Program.parse(expected_quil)
>>>
>>> assert expanded_program == expected_program
"""
r"""
Expand any instructions in the program which have a matching sequence gate definition, leaving
the others unchanged. Note, the new program will drop any gate definitions which are no longer
referenced in the program.
Recurses though each instruction while ensuring there is no cycle in the expansion graph (i.e. no sequence
gate definitions expand directly or indirectly into itself).
:param predicate: If provided, only sequence gate definitions which match the predicate will be expanded.
Defaults to expanding all sequence gate definitions.
Return the expanded copy of the program and a source mapping describing the expansions made.
# Example
See `expand_defgate_sequences`.
"""
r"""
Return a new ``Program`` containing only the instructions
for which `predicate` returns true.
"""
r"""
Simplify this program into a new [`Program`] which contains only instructions
and definitions which are executed; effectively, perform dead code removal.
Removes:
- All calibrations, following calibration expansion
- Frame definitions which are not used by any instruction such as `PULSE` or `CAPTURE`
- Waveform definitions which are not used by any instruction
- `PRAGMA EXTERN` instructions which are not used by any `CALL` instruction (see
[`Program::extern_pragma_map`]).
When a valid program is simplified, it remains valid.
"""
r"""
Parse a `Program` from a string.
# Errors
Raises a `ProgramError` if the string isn't a valid ``Quil`` expression.
"""
r"""
Resolve [`LabelPlaceholder`]s and [`QubitPlaceholder`]s within the program using default resolvers.
See [`resolve_placeholders_with_custom_resolvers`](Self::resolve_placeholders_with_custom_resolvers),
[`default_target_resolver`](Self::default_target_resolver),
and [`default_qubit_resolver`](Self::default_qubit_resolver) for more information.
"""
r"""
Resolve ``TargetPlaceholder``s and ``QubitPlaceholder``s within the program.
The resolved values will remain unique to that placeholder
within the scope of the program.
If you provide ``target_resolver`` and/or ``qubit_resolver``,
those will be used to resolve those values respectively.
If your resolver returns `None` for a particular placeholder,
it will not be replaced but will be left as a placeholder.
If you do not provide a resolver for a placeholder,
a default resolver will be used which will generate
a unique value for that placeholder within the scope of the program
using an auto-incrementing value (for qubit) or suffix (for target)
while ensuring that unique value is not already in use within the program.
"""
r"""
Return a copy of all of the instructions which constitute this [`Program`].
"""
...
...
r"""
Return the unitary of a program.
# Errors
Returns an error if the program contains instructions other than `Gate`s.
"""
r"""
Return a copy of the [`Program`] wrapped in a loop that repeats `iterations` times.
The loop is constructed by wrapping the body of the program in classical Quil instructions.
The given `loop_count_reference` must refer to an INTEGER memory region. The value at the
reference given will be set to `iterations` and decremented in the loop. The loop will
terminate when the reference reaches 0. For this reason your program should not itself
modify the value at the reference unless you intend to modify the remaining number of
iterations (i.e. to break the loop).
The given `start_target` and `end_target` will be used as the entry and exit points for the
loop, respectively. You should provide unique [`Target`]s that won't be used elsewhere in
the program.
If `iterations` is 0, then a copy of the program is returned without any changes.
"""
r"""
Errors encountered related to a Program.
"""
...
...
r"""
A Schedule is a ``DependencyGraph`` flattened into a linear sequence of instructions,
each of which is assigned a start time and duration.
"""
r"""
The schedule duration, in seconds.
This is the maximum end time among all scheduled items.
"""
r"""
Scheduled items, in an unspecified order.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
A single item within a schedule, representing a single instruction within a basic block.
"""
r"""
The time span during which the instruction is scheduled.
"""
r"""
The index of the instruction within the basic block.
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""
r"""
A time span, in seconds.
"""
r"""
The duration of the time span, in seconds.
"""
r"""
The end time of the time span, in seconds.
This is the sum of the start time and duration.
"""
r"""
The inclusive start time of the time span,
in seconds relative to the start of the scheduling context (such as the basic block).
"""
...
r"""
Implements `__repr__` for Python in terms of the Rust
[`Debug`](std::fmt::Debug) implementation.
"""