/
QUIROps.td
1294 lines (1077 loc) · 47.5 KB
/
QUIROps.td
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- QUIROps.td - QUIR dialect ops -----------------------*- tablegen -*-===//
//
// (C) Copyright IBM 2021, 2022.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
//
//===----------------------------------------------------------------------===//
#ifndef QUIR_OPS
#define QUIR_OPS
include "Dialect/QUIR/IR/QUIRInterfaces.td"
include "Dialect/QUIR/IR/QUIRTraits.td"
include "Dialect/QUIR/IR/QUIRAttributes.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Interfaces/CallInterfaces.td"
include "mlir/Interfaces/CastInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/IR/FunctionInterfaces.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/SymbolInterfaces.td"
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
// Define a side effect that identifies an operation as not dead while not
// interfering with memory operations (e.g., allows store-forwarding across
// this operation).
// Note that operations without memory effects defined will be treated
// conservatively (i.e., not making any assumptions).
// see lib/Interfaces/SideEffectInterfaces.cpp:isTriviallyDeadImpl()
// see lib/Dialect/Affine/Utils/Utils.cpp:hasNoInterveningEffect()
def NonInterferingNonDeadSideEffect : MemoryEffects<[MemFree<DefaultResource>]>;
// TODO: This op should be extracted to a system-level dialect
def QUIR_SystemInitOp : QUIR_Op<"system_init", [IsolatedFromAbove]> {
let summary = "Initializes the system";
let description = [{
The `quir.system_init` operation causes the system to be initialized,
preparing for execution and synchronizing the system.
}];
let assemblyFormat = [{
attr-dict
}];
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_SystemFinalizeOp : QUIR_Op<"system_finalize", [IsolatedFromAbove]> {
let summary = "Finalizes the system";
let description = [{
The `quir.system_finalize` operation causes the system to be finalized,
cleaning up results and shutting down execution.
}];
let assemblyFormat = [{
attr-dict
}];
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_ShotInitOp : QUIR_Op<"shot_init", [IsolatedFromAbove]> {
let summary = "Initialization for shot loops";
let description = [{
The `quir.shot_init` operation causes the system to initialize the shot
loop to maintain consistent initial conditions for each shot
}];
let assemblyFormat = [{
attr-dict
}];
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_SynchronizeOp : QUIR_Op<"synchronize", [
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Synchronize the system";
let description = [{
The `quir.synchronize` operation causes the system to perform a
synchronization so that each of the independent controllers is locked
into the same time step again. The synchronize op accepts qubit
arguments, this is to simplify the tracking of synchronization timing
relative to other qubit-based operations, like measurement and reset.
}];
let arguments = (ins Variadic<AnyQubit>:$qubits);
let results = (outs);
let assemblyFormat = [{
$qubits attr-dict `:` functional-type($qubits, results)
}];
}
def QUIR_ConstantOp : QUIR_Op<"constant",
[ConstantLike, NoSideEffect,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
TypesMatchWith<
"result and attribute have the same type",
"value", "result", "$_self">]> {
let summary = "QUIR-specific (angle or duration) constants";
let description = [{
The `quir.constant` operation produces an SSA value equal to some constant
specified by an attribute. Created to support QUIR-specific constants, but
can support any buildable attribute with a matching result type.
Example:
```
// Angle constant
%1 = quir.constant #quir.angle<1.5 : !quir.angle>
```
}];
let arguments = (ins AnyAttr:$value);
let results = (outs AnyType:$result);
let builders = [
OpBuilder<(ins "Attribute":$value),
[{ build($_builder, $_state, value.getType(), value); }]>,
OpBuilder<(ins "Attribute":$value, "Type":$type),
[{ build($_builder, $_state, type, value); }]>,
];
let extraClassDeclaration = [{
/// Whether the constant op can be constructed with a particular value and
/// type.
static bool isBuildableWith(Attribute value, Type type);
// Return the angle value from the value attribute
APFloat getAngleValueFromConstant();
}];
let hasFolder = 1;
let assemblyFormat = "attr-dict $value";
}
def QUIR_DeclareVariableOp : QUIR_Op<"declare_variable", [Symbol]> {
let summary = "Declares a classical variable";
let description = [{
The `quir.declare_variable` operation declares a classical variable
with the given name (sym_name), type, and an optional constant
initializer.
If present, the attributes `input` and `output` indicate that this
variable is an input or output variable, respectively.
Example:
```mlir
quir.declare_variable "myVar" : i1 = true
```
}];
let arguments = (ins
SymbolNameAttr:$sym_name,
TypeAttr:$type,
UnitAttr:$input,
UnitAttr:$output,
OptionalAttr<AnyAttr>:$initial_value
);
let results = (outs);
let assemblyFormat = [{
attr-dict $sym_name `:` $type (`=` $initial_value^)?
}];
let builders = [
OpBuilder<(ins "::llvm::StringRef":$sym_name, "::mlir::TypeAttr":$type), [{
$_state.addAttribute("sym_name", $_builder.getStringAttr(sym_name));
$_state.addAttribute("type", type);
}]>
];
let verifier = [{
auto t = (*this).type();
if( t.isa<AngleType>() || t.isa<CBitType>() || t.isa<DurationType>() || t.isa<StretchType>() || t.isIntOrIndexOrFloat() || t.isa<ComplexType>())
return success();
std::string str;
llvm::raw_string_ostream os(str);
t.print(os);
return emitOpError("MLIR type " + str + " not supported for declarations.");
}];
let extraClassDeclaration = [{
bool isInputVariable() { return input(); }
bool isOutputVariable() { return output(); }
}];
}
def QUIR_DeclareArrayOp : QUIR_Op<"declare_array", [Symbol]> {
let summary = "Declares a classical array";
let description = [{
The `quir.declare_array` operation declares a classical array
with the given name (sym_name), element type, and number of elements.
Example:
```mlir
quir.declare_array "myVar" : i1[2]
```
}];
let arguments = (ins
SymbolNameAttr:$sym_name,
TypeAttr:$type,
IndexAttr:$num_elements
);
let results = (outs);
let assemblyFormat = [{
attr-dict $sym_name `:` $type `[` $num_elements `]`
}];
let verifier = [{
auto t = (*this).type();
if( t.isa<AngleType>() || t.isa<CBitType>() || t.isa<DurationType>() || t.isa<StretchType>() || t.isIntOrIndexOrFloat())
return success();
return failure();
}];
}
def QUIR_UseVariableOp : QUIR_Op<"use_variable",
[DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
let summary = "Use the current value of a variable";
let description = [{
The operation `quir.use_variable` returns the current value (wrt to
`quir.assign_variable` operations) of the classical variable with the
given name.
Example:
```mlir
%2 = quir.use_variable "a" : !quir.cbit<1>
```
}];
let arguments = (ins
FlatSymbolRefAttr:$variable_name
);
let results = (outs AnyClassical:$res);
let assemblyFormat = [{
$variable_name `:` type($res) attr-dict
}];
// op is verified by its traits
let verifier = ?;
}
def QUIR_UseArrayElementOp : QUIR_Op<"use_array_element",
[DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
let summary = "Use the current value of an array element";
let description = [{
The operation `quir.use_array_element` returns the current value (wrt
to `quir.assign_array_element` operations) of an element in a classical
array, where the array is specified by name and the element by index.
Example:
```mlir
%2 = quir.use_array_element "result"[6] : i1
```
}];
let arguments = (ins
FlatSymbolRefAttr:$variable_name,
IndexAttr:$index
);
let results = (outs AnyClassical:$res);
let assemblyFormat = [{
$variable_name `[` $index `]` `:` type($res) attr-dict
}];
// op is verified by its traits
let verifier = ?;
}
def QUIR_VariableAssignOp : QUIR_Op<"assign_variable",
[DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
let summary = "Assign a new value to a classical variable";
let description = [{
The operation `quir.assign_variable` assigns a new value to a classical
variable given by name.
}];
let arguments = (ins
FlatSymbolRefAttr:$variable_name,
AnyClassical:$assigned_value
);
let results = (outs);
let assemblyFormat = [{
attr-dict $variable_name `:` type($assigned_value) `=` $assigned_value
}];
// op is verified by its traits
let verifier = ?;
}
def QUIR_AssignArrayElementOp : QUIR_Op<"assign_array_element",
[DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
let summary = "Assigns a new value to an element of a classical array";
let description = [{
The operation `quir.assign_array_element` assigns a new value to an
element of a classical array, where the array is specified by name and
the element by index.
}];
let arguments = (ins
FlatSymbolRefAttr:$variable_name,
IndexAttr:$index,
AnyClassical:$assigned_value
);
let results = (outs);
let assemblyFormat = [{
attr-dict $variable_name `[` $index `]` `:` type($assigned_value) `=` $assigned_value
}];
// op is verified by its traits
let verifier = ?;
}
def QUIR_AssignCbitBitOp : QUIR_Op<"assign_cbit_bit",
[DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
let summary = "Assign a single bit in a classical bit register";
let description = [{
The operation `quir.assign_cbit_bit` assigns a new value to an
individual bit of a classical bit register in a variable.
Example:
```mlir
quir.assign_cbit_bit "b"<2> [0] : i1 = %9
```
}];
let arguments = (ins
FlatSymbolRefAttr:$variable_name,
IndexAttr:$index,
IndexAttr:$cbit_width,
I1:$assigned_bit
);
let results = (outs);
let assemblyFormat = [{
attr-dict $variable_name `<` $cbit_width `>` `[` $index `]` `:` type($assigned_bit) `=` $assigned_bit
}];
// op is verified by its traits
let verifier = ?;
}
def QUIR_DeclareQubitOp : QUIR_Op<"declare_qubit", [NonInterferingNonDeadSideEffect]> {
let summary = "Declare a new physical qubit.";
let description = [{
The `quir.declare_qubit` operation creates a new physical qubit.
Example:
```mlir
%1 = quir.declare_qubit {id = 0 : i32}: !quir.qubit<1>
```
}];
let arguments = (ins OptionalAttr<I32Attr>:$id);
let results = (outs AnyQubit:$res);
let assemblyFormat = [{
attr-dict `:` type($res)
}];
}
def QUIR_DeclareDurationOp : QUIR_Op<"declare_duration", [NoSideEffect]> {
let summary = "Declare a new duration.";
let description = [{
The `quir.declare_duration` operation creates a new OpenQASM3.0 duration, representing a duration of time.
Example:
```mlir
%dur = quir.declare_duration {value = "10ns"} : !quir.duration
```
}];
let arguments = (ins StrAttr:$value);
let results = (outs QUIR_DurationType:$out);
let assemblyFormat = [{
attr-dict `:` type($out)
}];
}
def QUIR_DeclareStretchOp : QUIR_Op<"declare_stretch", [NoSideEffect]> {
let summary = "Declare a new stretch.";
let description = [{
The `quir.declare_stretch` operation creates a new OpenQASM3.0 stretch, representing an unknown duration of time.
Example:
```mlir
%dur = "quir.declare_stretch"() : () -> !quir.stretch
```
}];
let results = (outs QUIR_StretchType:$out);
let assemblyFormat = [{
attr-dict `:` type($out)
}];
}
def QUIR_ResetQubitOp : QUIR_Op<"reset", [
IsolatedFromAbove, CPTPOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Perform a reset operation on the given qubits";
let description = [{
The `quir.reset` operation performs a reset operation on all the qubits
given as an argument. If multiple qubits are given, then the reset
operations may happen in parallel.
Example
```mlir
quir.reset %qc0 : !quir.qubit<1>
quir.reset %qc1, %qc2 : !quir.qubit<1>, !quir.qubit<1>
```
}];
let arguments = (ins Variadic<Qubit<1>>:$qubits);
let assemblyFormat = [{
attr-dict $qubits `:` type($qubits)
}];
}
def QUIR_BuiltinCXOp : QUIR_Op<"builtin_CX", [
UnitaryOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Controlled NOT gate";
let description = [{
The `quir.builtin_CX` operation performs a controlled not i.e. flips %target iff %control is a 1.
Example:
```mlir
quir.builtin_CX %control, %target : !quir.qubit<1>, !quir.qubit<1>
```
}];
let arguments = (ins AnyQubit:$control, AnyQubit:$target);
let assemblyFormat = [{
attr-dict $control `,` $target `:` type($control) `,` type($target)
}];
}
def QUIR_Builtin_UOp : QUIR_Op<"builtin_U", [
UnitaryOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Generic unitary gate";
let description = [{
The `quir.builtin_U` operation performs a single qubit unitary rotation.
Example:
```mlir
quir.builtin_U %target, %theta_0, %phi_0, %lambda_0 : !quir.qubit<1>, !quir.angle<1>, !quir.angle<1>, !quir.angle<1>
```
}];
let arguments = (ins AnyQubit:$target, AnyAngle:$theta, AnyAngle:$phi, AnyAngle:$lambda);
let assemblyFormat = [{
attr-dict $target `,` $theta `,` $phi `,` $lambda `:` type($target) `,` type($theta) `,` type($phi) `,` type($lambda)
}];
}
def QUIR_CallGateOp : QUIR_Op<"call_gate", [DeclareOpInterfaceMethods<CallOpInterface>,
UnitaryOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "A call to a user-defined quantum gate that needs to be specialized for input operands";
let description = [{
The `quir.call_gate` operation represents calls to user-defined quantum gates
that needs to be specialized for its arguments. The callee gate is attached to a symbol reference via
an attribute. The arguments list must match the arguments provided by the callee. For example:
```mlir
quir.call_gate @userGateZX(%target, %theta, %phi) : (!quir.qubit<1>, !quir.angle<1>, !quir.angle<1>) -> ()
```
This is valid only if the named user gate `userGateZX` exists and takes a qubit and 2 angles as arguments.
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyAngleOrQubit>:$operands);
let results = (outs );
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
}];
let builders = [
OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
}]>,
OpBuilder<(ins "SymbolRefAttr":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
}]>,
OpBuilder<(ins "StringRef":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, SymbolRefAttr::get($_builder.getContext(), callee), operands);
}]>];
let extraClassDeclaration = [{
StringRef getCallee() { return callee(); }
FunctionType getCalleeType();
operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
}];
}
def QUIR_CallDefCalGateOp : QUIR_Op<"call_defcal_gate", [
DeclareOpInterfaceMethods<CallOpInterface>,
UnitaryOp,
NonInterferingNonDeadSideEffect]> {
let summary = "A call to a user-defined defcal quantum gate that needs to be specialized for input operands";
let description = [{
The `quir.call_defcal_gate` operation represents calls to user-defined quantum gates controlled with pulse-level descriptions
that needs to be specialized for its arguments. The callee gate is attached to a symbol reference via
an attribute. The arguments list must match the arguments provided by the callee. For example:
```mlir
quir.call_defcal_gate @defcalGateZX(%target, %theta, %phi) : (!quir.qubit<1>, !quir.angle<1>, !quir.angle<1>) ->
```
This is valid only if the named user gate `defcalGateZX` exists and takes a qubit and 2 angles as arguments.
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyAngleOrQubit>:$operands);
let results = (outs );
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
}];
let builders = [
OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
}]>,
OpBuilder<(ins "SymbolRefAttr":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
}]>,
OpBuilder<(ins "StringRef":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, SymbolRefAttr::get($_builder.getContext(), callee), operands);
}]>];
let extraClassDeclaration = [{
StringRef getCallee() { return callee(); }
FunctionType getCalleeType();
operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
}];
}
def QUIR_CallDefcalMeasureOp : QUIR_Op<"call_defcal_measure", [
DeclareOpInterfaceMethods<CallOpInterface>,
CPTPOp,
NonInterferingNonDeadSideEffect]> {
let summary = "A call to a user-defined defcal qubit measurement that needs to be specialized for input operands";
let description = [{
The `quir.call_defcal_measure` operation represents calls to user-defined qubit measurements controlled with pulse-level descriptions
that needs to be specialized for its arguments. The callee gate is attached to a symbol reference via
an attribute. The arguments list must match the arguments provided by the callee. For example:
```mlir
%c1 = quir.call_defcal_measure @defcalMeasure(%target) : (!quir.qubit<1>) -> i1
```
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyAngleOrQubit>:$operands);
let results = (outs I1:$res);
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type(operands, results)
}];
let builders = [
OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
$_state.addTypes(callee.getType().getResults());
}]>,
OpBuilder<(ins "SymbolRefAttr":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
$_state.addTypes(res);
}]>,
OpBuilder<(ins "StringRef":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, SymbolRefAttr::get($_builder.getContext(), callee), res,
operands);
}]>];
let extraClassDeclaration = [{
StringRef getCallee() { return callee(); }
FunctionType getCalleeType();
operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
}];
let verifier = [{
bool qubitFound = false;
for (auto arg : (*this)->getOperands()) {
if(qubitFound) {
if(arg.getType().isa<QubitType>()) {
return emitOpError("requires exactly one qubit argument");
}
} else {
if(arg.getType().isa<QubitType>()) {
qubitFound = true;
}
}
}
if (qubitFound) {
return success();
} else {
return emitOpError("requires exactly one qubit");
}
}];
}
def QUIR_CallSubroutineOp : QUIR_Op<"call_subroutine", [DeclareOpInterfaceMethods<CallOpInterface>]> {
let summary = "A call to a user-defined subroutine function that needs to be specialized for input operands";
let description = [{
The `quir.call_subroutine` operation represents calls to user-defined subroutine functions
that needs to be specialized for its arguments. The callee function is attached to a symbol reference via
an attribute. The arguments list must match the argument types (but not widths) provided by the callee.
Subroutines may receive and return classical types and may receive qubits as arguments.
Example:
```mlir
%majority = quir.call_subroutine @phase_4times(%q1, %phi) : (!quir.qubit<1>, !quir.angle<20>) -> ()
```
Function localization and specialization can be performed to enable this call to be transformed into
a std.call op.
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyTypeOf<[AnyClassical, AnyQubit]>>:$operands);
let results = (outs Optional<AnyClassical>:$res);
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
}];
let builders = [
OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
$_state.addTypes(callee.getType().getResults());
}]>,
OpBuilder<(ins "SymbolRefAttr":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
$_state.addTypes(res);
}]>,
OpBuilder<(ins "StringRef":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, SymbolRefAttr::get($_builder.getContext(), callee), res,
operands);
}]>];
let extraClassDeclaration = [{
StringRef getCallee() { return callee(); }
FunctionType getCalleeType();
operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
}];
}
def QUIR_CallKernelOp : QUIR_Op<"call_kernel", [DeclareOpInterfaceMethods<CallOpInterface>]> {
let summary = "A call to a user-defined kernel function that needs to be specialized for input operands";
let description = [{
The `quir.call_kernel` operation represents calls to user-defined kernel function
that needs to be specialized for its arguments. The callee function is attached to a symbol reference via
an attribute. The arguments list must match the arguments provided by the callee. Kernels may only receive
and return classical types.
Example:
```mlir
%majority = quir.call_kernel @majority_vote(%cbitarray) : (memref<3xi1>) -> i1
```
This is valid only if the named kernel @majority_vote exists and takes a cbit array as arguments and
returns an i1 as a result.
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyClassical>:$operands);
let results = (outs Optional<AnyClassical>:$res);
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
}];
let builders = [
OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
$_state.addTypes(callee.getType().getResults());
}]>,
OpBuilder<(ins "SymbolRefAttr":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
$_state.addTypes(res);
}]>,
OpBuilder<(ins "StringRef":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, SymbolRefAttr::get($_builder.getContext(), callee), res,
operands);
}]>];
}
def QUIR_CallCircuitOp : QUIR_Op<"call_circuit", [CallOpInterface, MemRefsNormalizable, DeclareOpInterfaceMethods<SymbolUserOpInterface>, DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>]> {
let summary = "Call a circuit operation";
let description = [{
The `quir.call_circuit` operation represents calls to launch a quantum circuit in the target system.
The callee function is attached to a symbol reference via an attribute. The arguments list must match
the argument types provided by the callee. The calling of a circuit represents the classical<->quantum
interaction within the specified program. All classical input values to the circuit should be transferred
before the invocation of the circuit routine to enable deterministic execution of the quantum circuit.
TODO: Currently all qubits must be declared within the circuit for lowering to hardware due to how
qubit allocations are currently tracked with `declare_qubit`. In the future we should consider
supporting quantum arguments (qubits) to make truly reuseable circuit routines that align
with the Qiskit and OpenQASM3 circuit definitions.
Example:
```mlir
%classical_result = quir.call_circuit @vqe(%theta) : (quir.angle<32>) -> (i1, i1)
```
}];
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyTypeOf<[AnyClassical, AnyQubit]>>:$operands);
let results = (outs Variadic<AnyClassical>:$res);
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
}];
let builders = [
OpBuilder<(ins "CircuitOp":$callee, CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
$_state.addTypes(callee.getType().getResults());
}]>,
OpBuilder<(ins "SymbolRefAttr":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
$_state.addTypes(res);
}]>,
OpBuilder<(ins "StringRef":$callee, "TypeRange":$res,
CArg<"ValueRange", "{}">:$operands), [{
build($_builder, $_state, SymbolRefAttr::get($_builder.getContext(), callee), res,
operands);
}]>];
let extraClassDeclaration = [{
StringRef getCallee() { return callee(); }
FunctionType getCalleeType();
operand_range getArgOperands() {
return {arg_operand_begin(), arg_operand_end()};
}
operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
CallInterfaceCallable getCallableForCallee() {
return (*this)->getAttrOfType<SymbolRefAttr>("callee");
}
}];
}
def QUIR_MeasureOp : QUIR_Op<"measure", [
CPTPOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Measure qubits";
let description = [{
The `quir.measure` operation represents a quantum measurement
performed in parallel on multiple qubits. It returns a number of
classical bits equal to the number of input qubits.
Example:
```mlir
%c1, %c2 = quir.measure(%q1, %q2) : (!quir.qubit<1>, !quir.qubit<1>) -> (i1, i1)
```
}];
let arguments = (ins Variadic<Qubit<1>>:$qubits);
let results = (outs Variadic<I1>:$outs);
let assemblyFormat = [{
`(` $qubits `)` attr-dict `:` functional-type($qubits, $outs)
}];
}
def QUIR_CastOp : QUIR_Op<"cast", [NoSideEffect]> {
let summary = "Cast between classical types";
let description = [{
The `quir.cast` operation represents a cast between different classical types, including non-QUIR types.
Example:
```mlir
%ang1 = "quir.cast"(%creg) : (memref<10xi1>) -> !quir.angle<20>
```
}];
let arguments = (ins AnyClassical:$arg);
let results = (outs AnyClassical:$out);
let hasCanonicalizer = 1;
}
def QUIR_DelayOp : QUIR_Op<"delay", [
UnitaryOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Add a delay operation of a given duration or stretch to a qubit";
let description = [{
The `quir.delay` operation represents a delay operation of the given
duration or stretch to a qubit, group of qubits, or all qubits (when no
target qubit is given).
Example:
```mlir
%dur1 = quir.declare_duration {value = "10ns"} : !quir.duration
"quir.delay"(%dur1, %q1_1) : (!quir.duration, !quir.qubit)
```
}];
let arguments = (ins DurationOrStretch:$time, Variadic<AnyQubit>:$qubits);
let results = (outs );
let assemblyFormat = [{
attr-dict $time `,` `(` $qubits `)` `:` type($time) `,` functional-type($qubits, results)
}];
}
def QUIR_DelayCyclesOp : QUIR_Op<"delay_cycles", [
UnitaryOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Add a delay operation of a given number of cycles to a qubit";
let description = [{
The `quir.delay_cycles` operation represents a delay operation of the given
number of cycles to a qubit, group of qubits, or all qubits (when no
target qubit is given).
Example:
```mlir
"quir.delay_cycles"(%q1_1) { time = 1000 : i64}: (!quir.qubit) -> ()
```
}];
let arguments = (ins I64Attr:$time, Variadic<AnyQubit>:$qubits);
let results = (outs );
let assemblyFormat = [{
`(` $qubits `)` attr-dict `:` functional-type($qubits, results)
}];
}
def QUIR_BarrierOp : QUIR_Op<"barrier", [
UnitaryOp,
DeclareOpInterfaceMethods<QubitOpInterface, ["getOperatedQubits"]>,
NonInterferingNonDeadSideEffect]> {
let summary = "Add a barrier operation";
let description = [{
The `quir.barrier` operation represents a barrier operation that synchronizes subsequent
operations across all qubits. It ensures that all subsequent operations are blocked until
the last operation of the input set is complete.
Example:
```mlir
quir.barrier %qc0 : (!quir.qubit<1>) -> ()
quir.barrier %qc0, %qc1 : (!quir.qubit<1>, !quir.qubit<1>) -> ()
```
}];
let arguments = (ins Variadic<AnyQubit>:$qubits);
let results = (outs);
let assemblyFormat = [{
$qubits attr-dict `:` functional-type($qubits, results)
}];
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_BroadcastOp : QUIR_Op<"broadcast", [NonInterferingNonDeadSideEffect]> {
let summary = "Broadcast a value from this controller to all others";
let description = [{
The `quir.broadcast` operation represents a broadcast command that sends a value
from this controller to all others. All other controllers should have a
corresponding `quir.recv` operation. If only one controller should receive the
message then the `quir.send` operation should be used instead.
Example:
```mlir
%angle1 = quir.constant #quir.angle<0.2 : !quir.angle<20>>
quir.broadcast %angle1 : !quir.angle<20>
```
}];
let arguments = (ins AnyClassical:$val);
let assemblyFormat = [{
attr-dict $val `:` type($val)
}];
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_SendOp : QUIR_Op<"send", [NonInterferingNonDeadSideEffect]> {
let summary = "Send a classical value from this controller to another";
let description = [{
The `quir.send` operation represents a send command from one controller to another.
A corresponding `quir.recv` operation should receive the information sent.
Example:
```mlir
%cbit = "quir.measure"(%target) : (!quir.qubit<1>) -> i1
quir.send %cbit : i1
```
}];
let arguments = (ins AnyClassical:$val, IndexAttr:$id);
let assemblyFormat = [{
attr-dict $val `to` $id `:` type($val)
}];
}
// This should be in OpBase.td but for some reason isn't, maybe in LLVM 15?
def IndexArrayAttr : TypedArrayAttrBase<IndexAttr,
"index array attribute"> {
let constBuilderCall = "$_builder.getIndexArrayAttr($0)";
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_RecvOp : QUIR_Op<"recv", [NonInterferingNonDeadSideEffect]> {
let summary = "Receive classical values from other controllers";
let description = [{
The `quir.recv` operation represents a receive command for potentially
multiple values. The fromIds array attribute indicates the Id of the
sending control node for each independent value.
Example:
```mlir
%meas:2 = quir.recv {fromId = [0 : index, 1 : index]} : i1, i1
```
}];
let arguments = (ins OptionalAttr<IndexArrayAttr>:$fromIds);
let results = (outs Variadic<AnyClassical>:$vals);
let assemblyFormat = [{
attr-dict `:` type($vals)
}];
}
// TODO: This op should be extracted to a system-level dialect
def QUIR_ParallelControlFlowOp : QUIR_Op<"parallel_control_flow", [
SingleBlockImplicitTerminator<"ParallelEndOp">,
RecursiveSideEffects]> {
let summary = "Contain a group of control flow ops that can run in parallel";
let description = [{
The `quir.parallel_control_flow` operation has a single region and block
that holds a collection of scf control flow ops that can be executed in
parallel.
Example:
```mlir