-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathQuantumOps.td
1123 lines (913 loc) · 35.7 KB
/
QuantumOps.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
// Copyright 2022-2023 Xanadu Quantum Technologies Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef QUANTUM_OPS
#define QUANTUM_OPS
include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "Quantum/IR/QuantumDialect.td"
include "Quantum/IR/QuantumInterfaces.td"
//===----------------------------------------------------------------------===//
// Quantum dialect enums.
//===----------------------------------------------------------------------===//
def NamedObservable : I32EnumAttr<"NamedObservable",
"Known named observables",
[
I32EnumAttrCase<"Identity", 0>,
I32EnumAttrCase<"PauliX", 1>,
I32EnumAttrCase<"PauliY", 2>,
I32EnumAttrCase<"PauliZ", 3>,
I32EnumAttrCase<"Hadamard", 4>,
]> {
let cppNamespace = "catalyst::quantum";
let genSpecializedAttr = 0;
}
//===----------------------------------------------------------------------===//
// Quantum dialect traits.
//===----------------------------------------------------------------------===//
def Unitary : NativeOpTrait<"UnitaryTrait">;
def Hermitian : NativeOpTrait<"HermitianTrait">;
def QuantumMemory : Resource<"QuantumMemory">;
//===----------------------------------------------------------------------===//
// Quantum dialect attributes.
//===----------------------------------------------------------------------===//
def NamedObservableAttr : EnumAttr<QuantumDialect, NamedObservable, "named_observable">;
//===----------------------------------------------------------------------===//
// Quantum dialect operations.
//===----------------------------------------------------------------------===//
class Quantum_Op<string mnemonic, list<Trait> traits = []> : Op<QuantumDialect, mnemonic, traits>;
def InitializeOp : Quantum_Op<"init"> {
let summary = "Initialize the quantum runtime.";
let assemblyFormat = [{
attr-dict
}];
}
def FinalizeOp : Quantum_Op<"finalize"> {
let summary = "Teardown the quantum runtime.";
let assemblyFormat = [{
attr-dict
}];
}
def DeviceInitOp : Quantum_Op<"device"> {
let summary = "Initialize a quantum device.";
let arguments = (ins
Optional<I64>:$shots,
StrAttr:$lib,
StrAttr:$name,
StrAttr:$kwargs
);
let assemblyFormat = [{
(`shots` `(` $shots^ `)`)? `[` $lib `,` $name `,` $kwargs `]` attr-dict
}];
}
def DeviceReleaseOp : Quantum_Op<"device_release"> {
let summary = "Release the active quantum device.";
let assemblyFormat = [{
attr-dict
}];
}
// -----
class Memory_Op<string mnemonic, list<Trait> traits = []> : Quantum_Op<mnemonic, traits>;
// The operation has no memory effect and is pure, therefore two similar alloc would be removed
// by a CSE pass. This needs to be changed in the future.
def AllocOp : Memory_Op<"alloc", [NoMemoryEffect]> {
let summary = "Allocate n qubits into a quantum register.";
let description = [{
}];
let arguments = (ins
Optional<I64>:$nqubits,
OptionalAttr<ConfinedAttr<I64Attr, [IntNonNegative]>>:$nqubits_attr
);
let results = (outs
QuregType:$qreg
);
let assemblyFormat = [{
`(` ($nqubits^):($nqubits_attr)? `)` attr-dict `:` type(results)
}];
}
def DeallocOp : Memory_Op<"dealloc"> {
let summary = "Deallocate a quantum register.";
let description = [{
}];
let arguments = (ins
QuregType:$qreg
);
let assemblyFormat = [{
$qreg attr-dict `:` type(operands)
}];
let hasCanonicalizeMethod = 1;
}
def ExtractOp : Memory_Op<"extract", [NoMemoryEffect]> {
let summary = "Extract a qubit value from a register.";
let description = [{
}];
let arguments = (ins
QuregType:$qreg,
Optional<I64>:$idx,
OptionalAttr<ConfinedAttr<I64Attr, [IntNonNegative]>>:$idx_attr
);
let results = (outs
QubitType:$qubit
);
let assemblyFormat = [{
$qreg `[` ($idx^):($idx_attr)? `]` attr-dict `:` type($qreg) `->` type(results)
}];
let hasVerifier = 1;
let hasFolder = 1;
}
def InsertOp : Memory_Op<"insert", [NoMemoryEffect]> {
let summary = "Update the qubit value of a register.";
let description = [{
}];
let arguments = (ins
QuregType:$in_qreg,
Optional<I64>:$idx,
OptionalAttr<ConfinedAttr<I64Attr, [IntNonNegative]>>:$idx_attr,
QubitType:$qubit
);
let results = (outs
QuregType:$out_qreg
);
let assemblyFormat = [{
$in_qreg `[` ($idx^):($idx_attr)? `]` `,` $qubit attr-dict `:` type($in_qreg) `,` type($qubit)
}];
let hasCanonicalizeMethod = 1;
let hasVerifier = 1;
let hasFolder = 1;
}
// -----
class Gate_Op<string mnemonic, list<Trait> traits = []> :
Quantum_Op<mnemonic, traits # [QuantumOperation]> {
code extraBaseClassDeclaration = [{
std::vector<mlir::Value> getQubitOperands() {
std::vector<mlir::Value> values;
values.insert(values.end(), getInQubits().begin(), getInQubits().end());
return values;
}
void setQubitOperands(mlir::ValueRange replacements) {
mlir::MutableOperandRange qubits = getInQubitsMutable();
assert(qubits.size() == replacements.size() && "must provide values for all qubits");
qubits.assign(replacements);
}
std::vector<mlir::OpResult> getQubitResults() {
std::vector<mlir::OpResult> values;
values.insert(values.end(), getOutQubits().begin(), getOutQubits().end());
return values;
}
}];
let extraClassDeclaration = extraBaseClassDeclaration;
}
class UnitaryGate_Op<string mnemonic, list<Trait> traits = []> :
Gate_Op<mnemonic, traits # [QuantumGate, Unitary]> {
code extraBaseClassDeclaration = [{
std::vector<mlir::Value> getQubitOperands() {
std::vector<mlir::Value> values;
values.insert(values.end(), getInQubits().begin(), getInQubits().end());
values.insert(values.end(), getInCtrlQubits().begin(), getInCtrlQubits().end());
return values;
}
void setQubitOperands(mlir::ValueRange replacements) {
mlir::MutableOperandRange qubits = getInQubitsMutable();
mlir::MutableOperandRange ctrls = getInCtrlQubitsMutable();
assert(qubits.size() + ctrls.size() == replacements.size() &&
"must provide values for all qubits (including controls)");
qubits.assign(replacements.take_front(qubits.size()));
ctrls.assign(replacements.take_back(ctrls.size()));
}
std::vector<mlir::OpResult> getQubitResults() {
std::vector<mlir::OpResult> values;
values.insert(values.end(), getOutQubits().begin(), getOutQubits().end());
values.insert(values.end(), getOutCtrlQubits().begin(), getOutCtrlQubits().end());
return values;
}
bool getAdjointFlag() {
return getAdjoint().has_value() ? getAdjoint().value() : false;
}
void setAdjointFlag(bool adjoint) {
setAdjoint(adjoint);
};
mlir::ValueRange getCtrlValueOperands() {
return getInCtrlValues();
}
void setCtrlValueOperands(mlir::ValueRange replacements) {
mlir::MutableOperandRange ctrlValues = getInCtrlValuesMutable();
assert(ctrlValues.size() == replacements.size() &&
"must provide values for all control values");
ctrlValues.assign(replacements);
}
mlir::ValueRange getNonCtrlQubitOperands() {
return getInQubits();
}
void setNonCtrlQubitOperands(mlir::ValueRange replacements) {
mlir::MutableOperandRange qubits = getInQubitsMutable();
assert(qubits.size() == replacements.size() &&
"must provide values for all qubit values");
qubits.assign(replacements);
}
mlir::ValueRange getCtrlQubitOperands() {
return getInCtrlQubits();
}
void setCtrlQubitOperands(mlir::ValueRange replacements) {
mlir::MutableOperandRange ctrls = getInCtrlQubitsMutable();
assert(ctrls.size() == replacements.size() &&
"must provide values for all control qubit values");
ctrls.assign(replacements);
}
mlir::ResultRange getNonCtrlQubitResults() {
return getOutQubits();
}
mlir::ResultRange getCtrlQubitResults() {
return getOutCtrlQubits();
}
}];
let extraClassDeclaration = extraBaseClassDeclaration;
}
def SetStateOp : Gate_Op<"set_state", []> {
let summary = "Set state to a complex vector.";
let description = [{
This operation is useful for simulators implementing state preparation.
Instead of decomposing state preparation into multiple operations, this
operation shortcuts all of that into a single operation.
}];
let arguments = (ins
AnyTypeOf<[
1DTensorOf<[Complex<F64>]>, MemRefRankOf<[Complex<F64>], [1]>
]>:$in_state,
Variadic<QubitType>:$in_qubits
);
let results = (outs
Variadic<QubitType>:$out_qubits
);
let assemblyFormat = [{
`(` $in_state `)` $in_qubits attr-dict `:` functional-type(operands, results)
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
bool isBufferized() {
return mlir::isa<mlir::MemRefType>(getInState().getType());
}
}];
}
def SetBasisStateOp : Gate_Op<"set_basis_state", []> {
let summary = "Set basis state.";
let description = [{
This operation is useful for simulators implementing set basis state.
Instead of decomposing basis state into multiple operations, this
operation shortcuts all of that into a single operation.
This signature matches the one in pennylane-lightning which expects
only a single integer as opposed to a binary digit.
}];
let arguments = (ins
AnyTypeOf<[
1DTensorOf<[I1]>, MemRefRankOf<[I1], [1]>
]>:$basis_state,
Variadic<QubitType>:$in_qubits
);
let results = (outs
Variadic<QubitType>:$out_qubits
);
let assemblyFormat = [{
`(` $basis_state`)` $in_qubits attr-dict `:` functional-type(operands, results)
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
bool isBufferized() {
return mlir::isa<mlir::MemRefType>(getBasisState().getType());
}
}];
}
def CustomOp : UnitaryGate_Op<"custom", [DifferentiableGate, NoMemoryEffect,
AttrSizedOperandSegments, AttrSizedResultSegments]> {
let summary = "A generic quantum gate on n qubits with m floating point parameters.";
let description = [{
}];
let arguments = (ins
Variadic<F64>:$params,
Variadic<QubitType>:$in_qubits,
StrAttr:$gate_name,
OptionalAttr<UnitAttr>:$adjoint,
Variadic<QubitType>:$in_ctrl_qubits,
Variadic<I1>:$in_ctrl_values
);
let results = (outs
Variadic<QubitType>:$out_qubits,
Variadic<QubitType>:$out_ctrl_qubits
);
let builders = [
OpBuilder<
// Convenience builder for a gate with parameters and controls
// Note that number of out_qubits = number of in_qubits,
// and number of out_ctrl_qubits = number of in_ctrl_qubits
(ins
"llvm::StringRef":$gate,
"mlir::ValueRange":$in_qubits,
"mlir::ValueRange":$in_ctrl_qubits,
"mlir::ValueRange":$in_ctrl_values,
"mlir::ValueRange":$params,
CArg<"bool", "false">:$adjoint
),[{
CustomOp::build($_builder, $_state,
/*out_qubits=*/ mlir::TypeRange(in_qubits),
/*out_ctrl_qubits=*/ mlir::TypeRange(in_ctrl_qubits),
/*params=*/ params,
/*in_qubits=*/ in_qubits,
/*gate_name=*/ $_builder.getStringAttr(gate),
/*(optional) adjoint=*/ nullptr,
/*in_ctrl_qubits=*/ in_ctrl_qubits,
/*in_ctrl_values=*/ in_ctrl_values
);
if (adjoint){
$_state.addAttribute("adjoint", $_builder.getUnitAttr());
}
}]>,
OpBuilder<
// Convenience builder for a gate with parameters and no controls
(ins
"llvm::StringRef":$gate,
"mlir::ValueRange":$in_qubits,
"mlir::ValueRange":$params,
CArg<"bool", "false">:$adjoint
),[{
CustomOp::build($_builder, $_state,
gate, in_qubits, mlir::ValueRange(), mlir::ValueRange(),
params, adjoint);
}]>,
OpBuilder<
// Convenience builder for a gate with controls and no parameters
(ins
"llvm::StringRef":$gate,
"mlir::ValueRange":$in_qubits,
"mlir::ValueRange":$in_ctrl_qubits,
"mlir::ValueRange":$in_ctrl_values,
CArg<"bool", "false">:$adjoint
),[{
CustomOp::build($_builder, $_state,
gate, in_qubits, in_ctrl_qubits, in_ctrl_values,
mlir::ValueRange(), adjoint);
}]>,
OpBuilder<
// Convenience builder for a gate with no parameters and no controls
(ins
"llvm::StringRef":$gate,
"mlir::ValueRange":$in_qubits,
CArg<"bool", "false">:$adjoint
),[{
CustomOp::build($_builder, $_state,
gate, in_qubits, mlir::ValueRange(), adjoint);
}]>,
];
let assemblyFormat = [{
$gate_name `(` $params `)` $in_qubits attr-dict ( `ctrls` `(` $in_ctrl_qubits^ `)` )? ( `ctrlvals` `(` $in_ctrl_values^ `)` )? `:` type($out_qubits) (`ctrls` type($out_ctrl_qubits)^ )?
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
mlir::ValueRange getAllParams() {
return getParams();
}
}];
let hasCanonicalizeMethod = 1;
}
def StaticCustomOp : UnitaryGate_Op<"static_custom", [NoMemoryEffect,
AttrSizedOperandSegments,
AttrSizedResultSegments]> {
let summary = "A generic quantum gate with static parameters in form of a DenseF64ArrayAttr.";
let description = [{
This operation represents a quantum gate with parameters defined statically as a
DenseF64ArrayAttr, rather than passed dynamically as operands. This is useful for gates
with parameters known at compile-time.
}];
let arguments = (ins
DenseF64ArrayAttr:$static_params,
Variadic<QubitType>:$in_qubits,
StrAttr:$gate_name,
OptionalAttr<UnitAttr>:$adjoint,
Variadic<QubitType>:$in_ctrl_qubits,
Variadic<I1>:$in_ctrl_values
);
let results = (outs
Variadic<QubitType>:$out_qubits,
Variadic<QubitType>:$out_ctrl_qubits
);
let assemblyFormat = [{
$gate_name $static_params $in_qubits attr-dict ( `ctrls` `(` $in_ctrl_qubits^ `)` )?
( `ctrlvals` `(` $in_ctrl_values^ `)` )? `:` type($out_qubits)
(`ctrls` type($out_ctrl_qubits)^ )?
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
llvm::ArrayRef<double> getAllParams() {
return getStaticParams();
}
}];
let hasCanonicalizeMethod = 1;
}
def GlobalPhaseOp : UnitaryGate_Op<"gphase", [DifferentiableGate, AttrSizedOperandSegments]> {
let summary = "Global Phase.";
let description = [{
}];
let arguments = (ins
F64:$params,
OptionalAttr<UnitAttr>:$adjoint,
Variadic<QubitType>:$in_ctrl_qubits,
Variadic<I1>:$in_ctrl_values
);
let results = (outs
Variadic<QubitType>:$out_ctrl_qubits
);
let assemblyFormat = [{
`(` $params `)` attr-dict ( `ctrls` `(` $in_ctrl_qubits^ `)` )? ( `ctrlvals` `(` $in_ctrl_values^ `)` )? `:` (`ctrls` type($out_ctrl_qubits)^ )?
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
mlir::ValueRange getAllParams() {
return getODSOperands(getParamOperandIdx());
}
// Simulate missing operands and results for the default impl of the quantum gate interface.
mlir::OperandRange getInQubits() {
return {getOperands().begin(), getOperands().begin()};
}
mlir::MutableOperandRange getInQubitsMutable() {
return mlir::MutableOperandRange(getOperation(), 0, 0);
}
mlir::ResultRange getOutQubits() {
return {getResults().begin(), getResults().begin()};
}
}];
}
def MultiRZOp : UnitaryGate_Op<"multirz", [DifferentiableGate, NoMemoryEffect,
AttrSizedOperandSegments, AttrSizedResultSegments]> {
let summary = "Apply an arbitrary multi Z rotation";
let description = [{
The `quantum.multirz` operation applies an arbitrary multi Z rotation to the state-vector.
The arguments are the rotation angle `theta` and a set of qubits the operation acts on.
}];
let arguments = (ins
F64:$theta,
Variadic<QubitType>:$in_qubits,
OptionalAttr<UnitAttr>:$adjoint,
Variadic<QubitType>:$in_ctrl_qubits,
Variadic<I1>:$in_ctrl_values
);
let results = (outs
Variadic<QubitType>:$out_qubits,
Variadic<QubitType>:$out_ctrl_qubits
);
let assemblyFormat = [{
`(` $theta `)` $in_qubits attr-dict ( `ctrls` `(` $in_ctrl_qubits^ `)` )? ( `ctrlvals` `(` $in_ctrl_values^ `)` )? `:` type($out_qubits) (`ctrls` type($out_ctrl_qubits)^ )?
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
mlir::ValueRange getAllParams() {
return getODSOperands(getParamOperandIdx());
}
}];
let hasCanonicalizeMethod = 1;
}
def QubitUnitaryOp : UnitaryGate_Op<"unitary", [ParametrizedGate, NoMemoryEffect,
AttrSizedOperandSegments, AttrSizedResultSegments]> {
let summary = "Apply an arbitrary fixed unitary matrix";
let description = [{
The `quantum.unitary` operation applies an arbitrary fixed unitary matrix to the
state-vector. The arguments are a set of qubits and a 2-dim matrix of complex numbers
that represents a Unitary matrix of size 2^(number of qubits) * 2^(number of qubits).
}];
let arguments = (ins
AnyTypeOf<[
2DTensorOf<[Complex<F64>]>, MemRefRankOf<[Complex<F64>], [2]>
]>:$matrix,
Variadic<QubitType>:$in_qubits,
OptionalAttr<UnitAttr>:$adjoint,
Variadic<QubitType>:$in_ctrl_qubits,
Variadic<I1>:$in_ctrl_values
);
let results = (outs
Variadic<QubitType>:$out_qubits,
Variadic<QubitType>:$out_ctrl_qubits
);
let assemblyFormat = [{
`(` $matrix `:` type($matrix) `)` $in_qubits attr-dict ( `ctrls` `(` $in_ctrl_qubits^ `)` )? ( `ctrlvals` `(` $in_ctrl_values^ `)` )? `:` type($out_qubits) (`ctrls` type($out_ctrl_qubits)^ )?
}];
let extraClassDeclaration = extraBaseClassDeclaration # [{
mlir::ValueRange getAllParams() {
return getODSOperands(getParamOperandIdx());
}
}];
let hasVerifier = 1;
}
// -----
class Region_Op<string mnemonic, list<Trait> traits = []> :
Quantum_Op<mnemonic, traits # [NoMemoryEffect]>;
def AdjointOp : Region_Op<"adjoint", [QuantumRegion, SingleBlockImplicitTerminator<"YieldOp">]> {
let summary = "Calculate the adjoint of the enclosed operations";
let regions = (region SizedRegion<1>:$region);
let arguments = (ins
QuregType:$qreg
);
let results = (outs
QuregType:$out_qreg
);
let assemblyFormat = [{
`(` $qreg `)` attr-dict `:` type(operands) $region
}];
let hasVerifier = 1;
}
def YieldOp : Quantum_Op<"yield", [Pure, ReturnLike, Terminator, ParentOneOf<["AdjointOp"]>]> {
let summary = "Return results from quantum program regions";
let arguments = (ins
Variadic<AnyTypeOf<[QuregType]>>:$results
);
let assemblyFormat = [{
attr-dict ($results^ `:` type($results))?
}];
let builders = [
OpBuilder<(ins), [{ /* nothing to do */ }]>
];
}
// -----
class Observable_Op<string mnemonic, list<Trait> traits = []> :
Quantum_Op<mnemonic, traits # [Pure]>;
def ComputationalBasisOp : Observable_Op<"compbasis"> {
let summary = "Define a pseudo-obeservable of the computational basis for use in measurements";
let description = [{
The `quantum.compbasis` operation defines a quantum observable to be used by other
operations such as measurement processes. The specific observable defined here is a
"pseudo" observable to represent measurements in the computational basis.
The only arguments are the list of qubits to measure.
Example:
```mlir
func.func @foo(%q0: !quantum.bit, %q1: !quantum.bit)
{
%res = quantum.compbasis %q0, %q1 : !quantum.obs
func.return
}
```
}];
let arguments = (ins
Variadic<QubitType>:$qubits
);
let results = (outs
ObservableType:$obs
);
let assemblyFormat = [{
$qubits attr-dict `:` type(results)
}];
}
def NamedObsOp : Observable_Op<"namedobs"> {
let summary = "Define a Named observable for use in measurements";
let description = [{
The `quantum.namedobs` operation defines a quantum observable to be used by measurement
processes. The specific observable defined here represents one of 5 named observables
{Identity, PauliX, PauliY, PauliZ, Hadamard} on a qubit. The arguments are a qubit to
measure as well as an encoding operator for the qubit as an integer between 0-4.
Example:
```mlir
func.func @foo(%q: !quantum.bit)
{
%res = quantum.namedobs %q[4] : !quantum.obs
func.return
}
```
}];
let arguments = (ins
QubitType:$qubit,
NamedObservableAttr:$type
);
let results = (outs
ObservableType:$obs
);
let assemblyFormat = [{
$qubit `[` $type `]` attr-dict `:` type(results)
}];
}
def HermitianOp : Observable_Op<"hermitian"> {
let summary = "Define a Hermitian observable for use in measurements";
let description = [{
The `quantum.hermitian` operation defines a quantum observable to be used by measurement
processes. The specific observable defined here represents the Hermitian observable on a
set of qubits. The arguments are a set of qubits to measure as well as a row-major flatten
matrix of complex numbers that represents a Hermitian matrix that must be of size
2^(number of qubits) * 2^(number of qubits).
}];
let arguments = (ins
AnyTypeOf<[
2DTensorOf<[Complex<F64>]>, MemRefRankOf<[Complex<F64>], [2]>
]>:$matrix,
Variadic<QubitType>:$qubits
);
let results = (outs ObservableType:$obs);
let assemblyFormat = [{
`(` $matrix `:` type($matrix) `)` $qubits attr-dict `:` type(results)
}];
let hasVerifier = 1;
}
def TensorOp : Observable_Op<"tensor"> {
let summary = "Define a tensor product of observables for use in measurements";
let description = [{
The `quantum.tensor` operation defines a quantum observable to be used by other
operations such as measurement processes. The specific observable defined here represents
the tensor product of observables on a set of qubits. The arguments are a set of
`quantum.Observable` generated by `quantum.namedobs` and `quantum.hermitian`.
Example:
```mlir
func.func @foo(%q0: !quantum.bit, %q1: !quantum.bit, %m: tensor<2x2xcomplex<f64>>)
{
%obs1 = quantum.namedobs %q0[4] : !quantum.obs
%obs2 = quantum.hermitian(%m: tensor<2x2xcomplex<f64>>) %q1 : !quantum.obs
%res = quantum.tensorprod %obs1, %obs2 : !quantum.obs
func.return
}
```
}];
let arguments = (ins
Variadic<ObservableType>:$terms
);
let results = (outs
ObservableType:$obs
);
let assemblyFormat = [{
$terms attr-dict `:` type(results)
}];
}
def HamiltonianOp : Observable_Op<"hamiltonian"> {
let summary = "Define a Hamiltonian observable for use in measurements";
let description = [{
The `quantum.hamiltonian` operation defines a quantum observable to be used by other
operations such as measurement processes. The specific observable defined here represents
the hamiltonian of observables. The arguments are a set of coefficients and a set of
`quantum.Observable` generated by `quantum.namedobs`, `quantum.hermitian`,
or `quantum.tensorobs`.
Example:
```mlir
func.func @foo(%q0: !quantum.bit, %q1: !quantum.bit, %c: tensor<2xf64>) {
%obs1 = quantum.namedobs %q0[4] : !quantum.obs
%obs2 = quantum.namedobs %q1[2] : !quantum.obs
%res = quantum.hamiltonian(%c: tensor<2xf64>) %obs1, %obs2 : !quantum.obs
func.return
}
```
}];
let arguments = (ins
AnyTypeOf<[
1DTensorOf<[F64]>, MemRefRankOf<[F64], [1]>
]>:$coeffs,
Variadic<ObservableType>:$terms
);
let results = (outs
ObservableType:$obs
);
let assemblyFormat = [{
`(` $coeffs `:` type($coeffs) `)` $terms attr-dict `:` type(results)
}];
}
// -----
class Measurement_Op<string mnemonic, list<Trait> traits = []> :
Quantum_Op<mnemonic, traits # [MeasurementProcess]>;
def MeasureOp : Quantum_Op<"measure"> {
let summary = "A single-qubit projective measurement in the computational basis.";
let description = [{
}];
let arguments = (ins
QubitType:$in_qubit,
OptionalAttr<ConfinedAttr<I32Attr, [IntMinValue<0>, IntMaxValue<1>]>>:$postselect
);
let results = (outs
I1:$mres,
QubitType:$out_qubit
);
let assemblyFormat = [{
$in_qubit attr-dict `:` type(results)
}];
}
def SampleOp : Measurement_Op<"sample"> {
let summary = "Sample eigenvalues from the given observable for the current state";
let description = [{
The `quantum.sample` operation represents the measurement process of sampling eigenvalues
from an observable on the current quantum state.
The only SSA argument is an observable that must be defined by an operation in the local scope.
from an observable on the current quantum state.
The number of samples to draw is determined by the device shots argument in the device initialization operation in the local scope.
Note that the return value type depends on the type of observable provided. Computational
basis samples are returned as a 2D array of shape (shot number, number of qubits), with all
other obversables the output is a 1D array of lenth equal to the shot number.
Example:
```mlir
func.func @foo(%q0: !quantum.bit, %q1: !quantum.bit, %shots: i64)
{
quantum.device shots(%shots) ["rtd_lightning.so", "lightning.qubit", "{my_attr: my_attr_value}"]
%obs1 = quantum.compbasis %q0, %q1 : !quantum.obs
%samples = quantum.samples %obs1 : tensor<?xf64>
%obs2 = quantum.pauli %q0[3], %q1[1] : !quantum.obs
%samples2 = quantum.samples %obs2 : tensor<?x2xf64>
func.return
}
```
}];
let arguments = (ins
ObservableType:$obs,
Optional<
AnyTypeOf<[
MemRefRankOf<[F64], [1]>,
MemRefRankOf<[F64], [2]>
]>
>:$in_data
);
let results = (outs
Optional<
AnyTypeOf<[
1DTensorOf<[F64]>,
2DTensorOf<[F64]>
]>
>:$samples
);
let assemblyFormat = [{
$obs ( `in` `(` $in_data^ `:` type($in_data) `)` )? attr-dict ( `:` type($samples)^ )?
}];
let extraClassDeclaration = [{
bool isBufferized() {
return getResultTypes().empty();
}
}];
let hasVerifier = 1;
}
def CountsOp : Measurement_Op<"counts", [SameVariadicOperandSize, SameVariadicResultSize]> {
let summary = "Compute sample counts for the given observable for the current state";
let description = [{
The `quantum.counts` operation represents the measurement process of sampling eigenvalues
from an observable on the current quantum state and counting the frequency of each
eigenvalue.
The only SSA argument is an observable that must be defined by an operation in the local scope.
from an observable on the current quantum state.
The number of samples to draw is determined by the device shots argument in the device initialization operation in the local scope.
Note that the "counts dictionary" is returned as two separate arrays of the same length, one
array for the eigenvalues, and one for count of each eigenvalue. When operating in the
computational basis, the "eigenvalues" are the possible bitstrings one could measure on the
given qubits, encoded as (floating-point) integers.
Example:
```mlir
func.func @foo(%q0: !quantum.bit, %q1: !quantum.bit, %shots: i64)
{
quantum.device shots(%shots) ["rtd_lightning.so", "lightning.qubit", "{my_attr: my_attr_value}"]
%obs = quantum.compbasis %q0, %q1 : !quantum.obs
%counts = quantum.counts %obs : tensor<4xf64>, tensor<4xi64>
%obs2 = quantum.pauli %q0[3], %q1[1] : !quantum.obs
%counts2 = quantum.counts %obs2 : tensor<2xf64>, tensor<2xi64>
func.return
}
```
}];
let arguments = (ins
ObservableType:$obs,
Optional<MemRefRankOf<[F64], [1]>>:$in_eigvals,
Optional<MemRefRankOf<[I64], [1]>>:$in_counts
);
let results = (outs
Optional<1DTensorOf<[F64]>>:$eigvals,
Optional<1DTensorOf<[I64]>>:$counts
);
let assemblyFormat = [{
$obs
( `in` `(` $in_eigvals^ `:` type($in_eigvals) `,` $in_counts `:` type($in_counts) `)` )?
attr-dict ( `:` type($eigvals)^ `,` type($counts) )?
}];
let extraClassDeclaration = [{
bool isBufferized() {
return getResultTypes().empty();
}
}];
let hasVerifier = 1;
}
def ExpvalOp : Measurement_Op<"expval"> {
let summary = "Compute the expectation value of the given observable for the current state";
let description = [{
The `quantum.expval` operation represents the measurement process of computing the
expectation value of an observable on the current quantum state. While this quantity can
be computed analytically on simulators, for hardware execution or shot noise
simulation, the shots attached to the device
in the local scope is used.
The only SSA argument is an observable that must be defined by an operation in the local
scope.
Example:
```mlir
func.func @foo(%q: !quantum.bit)
{
%obs = quantum.namedobs %q[4] : !quantum.obs
%expval = quantum.expval %obs : f64
func.return
}
```
}];
let arguments = (ins
ObservableType:$obs
);
let results = (outs
F64:$expval
);
let assemblyFormat = [{
$obs attr-dict `:` type(results)
}];
}
def VarianceOp : Measurement_Op<"var"> {
let summary = "Compute the variance of the given observable for the current state";