-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathQuakeOps.cpp
More file actions
1299 lines (1142 loc) · 48.6 KB
/
QuakeOps.cpp
File metadata and controls
1299 lines (1142 loc) · 48.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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 (c) 2022 - 2026 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "cudaq/Optimizer/Builder/Factory.h"
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
#include "cudaq/Optimizer/Dialect/CC/CCTypes.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeDialect.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include <unordered_set>
using namespace mlir;
#include "CanonicalPatterns.inc"
static LogicalResult verifyWireResultsAreLinear(Operation *op) {
for (Value v : op->getOpResults())
if (isa<quake::WireType>(v.getType())) {
// Terminators can forward wire values, but they are not quantum
// operations.
if (v.hasOneUse() || v.use_empty())
continue;
// Allow a single cf.cond_br to use the value twice, once for each arm.
std::unordered_set<Operation *> uniqs;
for (auto *op : v.getUsers())
uniqs.insert(op);
if (uniqs.size() == 1 &&
(*uniqs.begin())->hasTrait<OpTrait::IsTerminator>())
continue;
return op->emitOpError(
"wires are a linear type and must have exactly one use");
}
return success();
}
/// When a quake operation is in value form, the number of wire arguments (wire
/// arity) must be the same as the number of wires returned as results (wire
/// coarity). This function verifies that this property is true.
LogicalResult quake::verifyWireArityAndCoarity(Operation *op) {
std::size_t arity = 0;
std::size_t coarity = 0;
auto getCounts = [&](auto op) {
for (auto arg : op.getTargets())
if (isa<quake::WireType>(arg.getType()))
++arity;
coarity = op.getWires().size();
};
if (auto gate = dyn_cast<OperatorInterface>(op)) {
for (auto arg : gate.getControls())
if (isa<quake::WireType>(arg.getType()))
++arity;
getCounts(gate);
} else if (auto meas = dyn_cast<MeasurementInterface>(op)) {
getCounts(meas);
}
if (arity == coarity)
return success();
return op->emitOpError("arity does not equal coarity of wires");
}
bool quake::isSupportedMappingOperation(Operation *op) {
return isa<OperatorInterface, MeasurementInterface, SinkOp, ReturnWireOp>(op);
}
ValueRange quake::getQuantumTypesFromRange(ValueRange range) {
// Skip over classical types at the beginning
int numClassical = 0;
for (auto operand : range) {
if (!isa<RefType, VeqType, WireType>(operand.getType()))
numClassical++;
else
break;
}
ValueRange retVals = range.drop_front(numClassical);
// Make sure all remaining operands are quantum
for (auto operand : retVals)
if (!isa<RefType, VeqType, WireType>(operand.getType()))
return retVals.drop_front(retVals.size());
return retVals;
}
ValueRange quake::getQuantumResults(Operation *op) {
return getQuantumTypesFromRange(op->getResults());
}
ValueRange quake::getQuantumOperands(Operation *op) {
return getQuantumTypesFromRange(op->getOperands());
}
LogicalResult quake::setQuantumOperands(Operation *op, ValueRange quantumVals) {
ValueRange quantumOperands = getQuantumTypesFromRange(op->getOperands());
if (quantumOperands.size() != quantumVals.size())
return failure();
// Count how many classical operands at beginning
auto numClassical = op->getOperands().size() - quantumOperands.size();
for (auto &&[i, quantumVal] : llvm::enumerate(quantumVals))
op->setOperand(numClassical + i, quantumVal);
return success();
}
//===----------------------------------------------------------------------===//
// AllocaOp
//===----------------------------------------------------------------------===//
Value quake::createConstantAlloca(PatternRewriter &builder, Location loc,
OpResult result, ValueRange args) {
auto newAlloca = [&]() {
if (isa<quake::VeqType>(result.getType()) &&
cast<quake::VeqType>(result.getType()).hasSpecifiedSize()) {
return builder.create<quake::AllocaOp>(
loc, cast<quake::VeqType>(result.getType()).getSize());
}
auto constOp = cast<arith::ConstantOp>(args[0].getDefiningOp());
return builder.create<quake::AllocaOp>(
loc, static_cast<std::size_t>(
cast<IntegerAttr>(constOp.getValue()).getInt()));
}();
return builder.create<quake::RelaxSizeOp>(
loc, quake::VeqType::getUnsized(builder.getContext()), newAlloca);
}
LogicalResult quake::AllocaOp::verify() {
// Result must be RefType or VeqType by construction.
if (auto resTy = dyn_cast<VeqType>(getResult().getType())) {
if (resTy.hasSpecifiedSize()) {
if (getSize())
return emitOpError("unexpected size operand");
} else {
if (auto size = getSize()) {
if (auto cnt =
dyn_cast_or_null<arith::ConstantOp>(size.getDefiningOp())) {
std::int64_t argSize = cast<IntegerAttr>(cnt.getValue()).getInt();
// TODO: This is a questionable check. We could have a very large
// unsigned value that appears to be negative because of two's
// complement. On the other hand, allocating 2^64 - 1 qubits isn't
// going to go well.
if (argSize < 0)
return emitOpError("expected a non-negative integer size.");
}
} else {
return emitOpError("size operand required");
}
}
} else {
// Size has no semantics for any type other than quake.veq.
if (getSize())
return emitOpError("cannot specify size with this quantum type");
if (!quake::isConstantQuantumRefType(getResult().getType()))
return emitOpError("struq type must have specified size");
}
// Check the uses. If any use is a InitializeStateOp, then it must be the only
// use.
Operation *self = getOperation();
if (!self->getUsers().empty() && !self->hasOneUse())
for (auto *op : self->getUsers())
if (isa<quake::InitializeStateOp>(op))
return emitOpError("init_state must be the only use");
return success();
}
void quake::AllocaOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
// Use a canonicalization pattern as folding the constant into the veq type
// changes the type. Uses may still expect a veq with unspecified size.
// Folding is strictly reductive and doesn't allow the creation of ops.
patterns.add<FuseConstantToAllocaPattern>(context);
}
quake::InitializeStateOp quake::AllocaOp::getInitializedState() {
auto *self = getOperation();
if (self->hasOneUse()) {
auto x = self->getUsers().begin();
return dyn_cast<quake::InitializeStateOp>(*x);
}
return {};
}
//===----------------------------------------------------------------------===//
// Apply
//===----------------------------------------------------------------------===//
LogicalResult quake::ApplyOp::verify() {
FunctionType asSig;
if (auto callee = getCallee()) {
auto fn =
SymbolTable::lookupNearestSymbolFrom<func::FuncOp>(*this, *callee);
if (!fn)
return emitOpError("callee must be declared");
asSig = fn.getFunctionType();
} else {
Value callable = getIndirectCallee().front();
asSig = cast<cudaq::cc::CallableType>(callable.getType()).getSignature();
}
// Arity of callee's signature must be equal to number of arguments provided.
bool callingCallable = false;
if (getActuals().size() == asSig.getInputs().size() + 1) {
callingCallable = true;
if (!isa<cudaq::cc::CallableType>(getActuals().front().getType()))
return emitOpError("hidden argument must be callable");
} else if (getActuals().size() != asSig.getInputs().size()) {
return emitOpError("number of arguments must be consistent");
}
// Quantum reference type values are allowed to implicitly coerce to a relaxed
// veq type when they appear as arguments to a `quake.apply` op. Specifically,
// lowering the apply op is required to add a `quake.concat` op to manifest
// the type conversion.
auto isRelaxedVeq = [](Type ty1, Type ty2) {
if (auto veq2 = dyn_cast<quake::VeqType>(ty2))
return quake::isQuantumReferenceType(ty1) && !veq2.hasSpecifiedSize();
return false;
};
SmallVector<Type> actualTypes{getActuals().getTypes().begin() +
(callingCallable ? 1 : 0),
getActuals().getTypes().end()};
// The args are the formal arguments and they must match.
for (auto [ty1, ty2] : llvm::zip(actualTypes, asSig.getInputs()))
if (ty1 != ty2 && !isRelaxedVeq(ty1, ty2))
return emitOpError("argument types must match");
// The results are the formal results and they must match.
for (auto [ty1, ty2] : llvm::zip(getResultTypes(), asSig.getResults()))
if (ty1 != ty2 && !isRelaxedVeq(ty1, ty2))
return emitOpError("result types must match");
return success();
}
void quake::ApplyOp::print(OpAsmPrinter &p) {
if (getIsAdj())
p << "<adj>";
p << ' ';
bool isDirect = getCallee().has_value();
if (isDirect)
p.printAttributeWithoutType(getCalleeAttr());
else
p << getIndirectCallee();
p << ' ';
if (!getControls().empty())
p << '[' << getControls() << "] ";
p << getActuals() << " : ";
SmallVector<Type> operandTys{(*this)->getOperandTypes().begin(),
(*this)->getOperandTypes().end()};
p.printFunctionalType(ArrayRef<Type>{operandTys}.drop_front(isDirect ? 0 : 1),
(*this)->getResultTypes());
p.printOptionalAttrDict(
(*this)->getAttrs(),
{"operand_segment_sizes", "is_adj", getCalleeAttrNameStr()});
}
ParseResult quake::ApplyOp::parse(OpAsmParser &parser, OperationState &result) {
if (succeeded(parser.parseOptionalLess())) {
if (parser.parseKeyword("adj") || parser.parseGreater())
return failure();
result.addAttribute("is_adj", parser.getBuilder().getUnitAttr());
}
OpAsmParser::UnresolvedOperand calleeOpnd;
SmallVector<OpAsmParser::UnresolvedOperand> calleeOperand;
bool isDirect;
if (parser.parseOptionalOperand(calleeOpnd).has_value()) {
isDirect = false;
calleeOperand.push_back(calleeOpnd);
} else {
isDirect = true;
NamedAttrList attrs;
SymbolRefAttr funcAttr;
if (parser.parseCustomAttributeWithFallback(
funcAttr, parser.getBuilder().getType<NoneType>(),
getCalleeAttrNameStr(), attrs))
return failure();
result.addAttribute(getCalleeAttrNameStr(), funcAttr);
}
SmallVector<OpAsmParser::UnresolvedOperand> controlOperands;
if (succeeded(parser.parseOptionalLSquare()))
if (parser.parseOperandList(controlOperands) || parser.parseRSquare())
return failure();
SmallVector<OpAsmParser::UnresolvedOperand> miscOperands;
if (parser.parseOperandList(miscOperands) || parser.parseColon())
return failure();
FunctionType applyTy;
if (parser.parseType(applyTy) ||
parser.parseOptionalAttrDict(result.attributes))
return failure();
result.addAttribute("operand_segment_sizes",
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(calleeOperand.size()),
static_cast<int32_t>(controlOperands.size()),
static_cast<int32_t>(miscOperands.size())}));
result.addTypes(applyTy.getResults());
if (isDirect) {
if (parser.resolveOperands(
llvm::concat<const OpAsmParser::UnresolvedOperand>(
calleeOperand, controlOperands, miscOperands),
applyTy.getInputs(), parser.getNameLoc(), result.operands))
return failure();
} else {
auto loc = parser.getNameLoc();
auto fnTy = parser.getBuilder().getFunctionType(
applyTy.getInputs().drop_front(controlOperands.size()),
applyTy.getResults());
auto callableTy = cudaq::cc::CallableType::get(parser.getContext(), fnTy);
if (parser.resolveOperands(calleeOperand, callableTy, loc,
result.operands) ||
parser.resolveOperands(
llvm::concat<const OpAsmParser::UnresolvedOperand>(controlOperands,
miscOperands),
applyTy.getInputs(), loc, result.operands))
return failure();
}
return success();
}
//===----------------------------------------------------------------------===//
// ApplyNoiseOp
//===----------------------------------------------------------------------===//
void quake::ApplyNoiseOp::print(OpAsmPrinter &p) {
// noise_func or key
p << ' ';
if (auto fn = getNoiseFuncAttr())
p << fn;
else
p << getKey();
p << '(' << getParameters() << ") " << getQubits() << " : ";
SmallVector<Type> operandTys{(*this)->getOperandTypes().begin(),
(*this)->getOperandTypes().end()};
p.printFunctionalType(operandTys, (*this)->getResultTypes());
p.printOptionalAttrDict((*this)->getAttrs(),
{"operand_segment_sizes", getNoiseFuncAttrName()});
}
ParseResult quake::ApplyNoiseOp::parse(OpAsmParser &parser,
OperationState &result) {
SmallVector<OpAsmParser::UnresolvedOperand> keyOperand;
if (parser.parseOperandList(keyOperand))
return failure();
bool isDirect = keyOperand.empty();
if (keyOperand.size() > 1)
return failure();
if (isDirect) {
NamedAttrList attrs;
SymbolRefAttr funcAttr;
if (parser.parseCustomAttributeWithFallback(
funcAttr, parser.getBuilder().getType<NoneType>(),
getNoiseFuncAttrNameStr(), attrs))
return failure();
result.addAttribute(getNoiseFuncAttrNameStr(), funcAttr);
}
SmallVector<OpAsmParser::UnresolvedOperand> parameterOperands;
if (succeeded(parser.parseOptionalLParen()))
if (parser.parseOperandList(parameterOperands) || parser.parseRParen())
return failure();
SmallVector<OpAsmParser::UnresolvedOperand> targetOperands;
if (parser.parseOperandList(targetOperands) || parser.parseColon())
return failure();
FunctionType applyTy;
if (parser.parseType(applyTy) ||
parser.parseOptionalAttrDict(result.attributes))
return failure();
result.addAttribute("operand_segment_sizes",
parser.getBuilder().getDenseI32ArrayAttr(
{static_cast<int32_t>(keyOperand.size()),
static_cast<int32_t>(parameterOperands.size()),
static_cast<int32_t>(targetOperands.size())}));
result.addTypes(applyTy.getResults());
if (parser.resolveOperands(llvm::concat<const OpAsmParser::UnresolvedOperand>(
keyOperand, parameterOperands, targetOperands),
applyTy.getInputs(), parser.getNameLoc(),
result.operands))
return failure();
return success();
}
LogicalResult quake::ApplyNoiseOp::verify() {
// Must have either a noise_func or a key and not both.
if (!getNoiseFuncAttr()) {
if (!getKey())
return emitOpError("must have a noise function or a key");
if (getKey().getType() != IntegerType::get(getContext(), 64))
return emitOpError("key must be i64");
} else {
if (getKey())
return emitOpError("cannot have a noise function and a key");
}
// Parameters must be exactly one stdvec or 0 or more ptr<floating-point>.
auto params = getParameters();
if (params.size() == 1) {
if (auto stdvecTy = dyn_cast<cudaq::cc::StdvecType>(params[0].getType())) {
if (stdvecTy.getElementType() != Float64Type::get(getContext()))
return emitOpError("must be std::vector<double>");
} else if (auto ptrTy =
dyn_cast<cudaq::cc::PointerType>(params[0].getType())) {
if (!isa<FloatType>(ptrTy.getElementType()))
return emitOpError("must be floating-point");
} else {
return emitOpError("must be std::vector<double> or floating-point");
}
} else {
for (auto p : params)
if (auto ptrTy = dyn_cast<cudaq::cc::PointerType>(p.getType()))
if (!isa<FloatType>(ptrTy.getElementType()))
return emitOpError("must be floating-point");
}
// Must have at least 1 qubit in qubits.
if (getQubits().empty())
return emitOpError("must have at least one qubit");
return success();
}
//===----------------------------------------------------------------------===//
// BorrowWire
//===----------------------------------------------------------------------===//
LogicalResult quake::BorrowWireOp::verify() {
std::int32_t id = getIdentity();
if (id < 0)
return emitOpError("id cannot be negative");
ModuleOp module = getOperation()->getParentOfType<ModuleOp>();
auto wires = module.lookupSymbol<quake::WireSetOp>(getSetName());
if (!wires)
return emitOpError("wire set could not be found");
std::int32_t setCardinality = wires.getCardinality();
if (id >= setCardinality)
return emitOpError("id is out of bounds for wire set");
return success();
}
//===----------------------------------------------------------------------===//
// Concat
//===----------------------------------------------------------------------===//
void quake::ConcatOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<ConcatSizePattern, ConcatNoOpPattern, UselessConcatOpPattern>(
context);
}
LogicalResult quake::ConcatOp::verify() {
bool isUnspecified = false;
std::size_t size = 0;
for (auto tq : getTargets()) {
Type ty = tq.getType();
if (auto veq = dyn_cast<quake::VeqType>(ty);
veq && !veq.hasSpecifiedSize()) {
isUnspecified = true;
break;
}
if (auto struq = dyn_cast<quake::StruqType>(ty);
struq && !struq.hasSpecifiedSize()) {
isUnspecified = true;
break;
}
size += getAllocationSize(ty);
}
auto resTy = cast<quake::VeqType>(getType());
if (isUnspecified && resTy.hasSpecifiedSize())
return emitOpError("veq size must be non-constant");
if (resTy.hasSpecifiedSize() && resTy.getSize() != size)
return emitOpError("veq size must equal size of aggregate operands");
return success();
}
//===----------------------------------------------------------------------===//
// ExpPauliRef
//===----------------------------------------------------------------------===//
static ParseResult
parseRawString(OpAsmParser &parser,
std::optional<OpAsmParser::UnresolvedOperand> &value,
StringAttr &rawString) {
std::string stringVal;
auto loc = UnknownLoc::get(parser.getContext());
if (succeeded(parser.parseOptionalString(&stringVal))) {
value = std::nullopt;
rawString = StringAttr::get(parser.getContext(), stringVal);
return success();
}
OpAsmParser::UnresolvedOperand operand;
if (parser.parseOperand(operand))
return emitError(loc, "must be an operand");
value = operand;
rawString = StringAttr{};
return success();
}
template <typename OP>
void printRawString(OpAsmPrinter &printer, OP refOp, Value stringVal,
StringAttr rawString) {
if (stringVal)
printer.printOperand(stringVal);
else if (rawString)
printer << rawString;
}
void quake::ExpPauliOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<BindExpPauliWord, AdjustAdjointExpPauliPattern>(context);
}
LogicalResult quake::ExpPauliOp::verify() {
if (getPauliLiteralAttr()) {
if (getPauli())
return emitOpError("cannot have both a literal and a value Pauli word");
} else {
if (!getPauli())
return emitOpError("must have either a literal or a value Pauli word");
}
if (!(getParameters().empty() || getParameters().size() == 1))
return emitOpError("can only have 0 or 1 parameter");
return verifyWireResultsAreLinear(getOperation());
}
//===----------------------------------------------------------------------===//
// ExtractRef
//===----------------------------------------------------------------------===//
static ParseResult
parseRawIndex(OpAsmParser &parser,
std::optional<OpAsmParser::UnresolvedOperand> &index,
IntegerAttr &rawIndex) {
std::size_t constantIndex = quake::ExtractRefOp::kDynamicIndex;
OptionalParseResult parsedInteger =
parser.parseOptionalInteger(constantIndex);
if (parsedInteger.has_value()) {
if (failed(parsedInteger.value()))
return failure();
index = std::nullopt;
} else {
OpAsmParser::UnresolvedOperand operand;
if (parser.parseOperand(operand))
return failure();
index = operand;
}
auto i64Ty = IntegerType::get(parser.getContext(), 64);
rawIndex = IntegerAttr::get(i64Ty, constantIndex);
return success();
}
template <typename OP>
void printRawIndex(OpAsmPrinter &printer, OP refOp, Value index,
IntegerAttr rawIndex) {
if (rawIndex.getValue() == OP::kDynamicIndex)
printer.printOperand(index);
else
printer << rawIndex.getValue();
}
void quake::ExtractRefOp::getCanonicalizationPatterns(
RewritePatternSet &patterns, MLIRContext *context) {
patterns.add<FuseConstantToExtractRefPattern, ForwardConcatExtractSingleton,
ForwardConcatExtractPattern, ExtractRefFromSubVeqPattern>(
context);
}
LogicalResult quake::ExtractRefOp::verify() {
if (getIndex()) {
if (getRawIndex() != kDynamicIndex)
return emitOpError(
"must not have both a constant index and an index argument.");
} else {
if (getRawIndex() == kDynamicIndex) {
return emitOpError("invalid constant index value");
} else {
auto veqSize = getVeq().getType().getSize();
if (getVeq().getType().hasSpecifiedSize() && getRawIndex() >= veqSize)
return emitOpError("invalid index [" + std::to_string(getRawIndex()) +
"] because >= size [" + std::to_string(veqSize) +
"]");
}
}
return success();
}
//===----------------------------------------------------------------------===//
// GetMemberOp
//===----------------------------------------------------------------------===//
LogicalResult quake::GetMemberOp::verify() {
std::uint32_t index = getIndex();
auto strTy = cast<quake::StruqType>(getStruq().getType());
std::uint32_t size = strTy.getNumMembers();
if (index >= size)
return emitOpError("invalid index [" + std::to_string(index) +
"] because >= size [" + std::to_string(size) + "]");
if (getType() != strTy.getMembers()[index])
return emitOpError("result type does not match member " +
std::to_string(index) + " type");
return success();
}
void quake::GetMemberOp::getCanonicalizationPatterns(
RewritePatternSet &patterns, MLIRContext *context) {
patterns.add<BypassMakeStruq>(context);
}
//===----------------------------------------------------------------------===//
// GetMeasureOp
//===----------------------------------------------------------------------===//
LogicalResult quake::GetMeasureOp::verify() {
if (getIndex()) {
if (getRawIndex() != kDynamicIndex)
return emitOpError(
"must not have both a constant index and an index argument.");
} else {
if (getRawIndex() == kDynamicIndex) {
return emitOpError("invalid constant index value");
} else {
auto msSize = getMeasurements().getType().getSize();
if (getMeasurements().getType().hasSpecifiedSize() &&
getRawIndex() >= msSize)
return emitOpError("invalid index [" + std::to_string(getRawIndex()) +
"] because >= size [" + std::to_string(msSize) +
"]");
}
}
return success();
}
//===----------------------------------------------------------------------===//
// InitializeStateOp
//===----------------------------------------------------------------------===//
LogicalResult quake::InitializeStateOp::verify() {
auto ptrTy = cast<cudaq::cc::PointerType>(getState().getType());
Type ty = ptrTy.getElementType();
if (auto arrTy = dyn_cast<cudaq::cc::ArrayType>(ty)) {
if (!arrTy.isUnknownSize()) {
std::size_t size = arrTy.getSize();
if (!std::has_single_bit(size))
return emitOpError(
"initialize state vector must be power of 2, but is " +
std::to_string(size) + " instead.");
}
if (!isa<FloatType, ComplexType>(arrTy.getElementType()))
return emitOpError("invalid data pointer type");
} else if (!isa<FloatType, ComplexType, quake::StateType>(ty)) {
return emitOpError("invalid data pointer type");
}
return success();
}
void quake::InitializeStateOp::getCanonicalizationPatterns(
RewritePatternSet &patterns, MLIRContext *context) {
patterns.add<ForwardAllocaTypePattern>(context);
}
//===----------------------------------------------------------------------===//
// MakeStruqOp
//===----------------------------------------------------------------------===//
LogicalResult quake::MakeStruqOp::verify() {
if (getType().getNumMembers() != getNumOperands())
return emitOpError("result type has different member count than operands");
for (auto [ty, opnd] : llvm::zip(getType().getMembers(), getOperands())) {
if (ty == opnd.getType())
continue;
auto veqTy = dyn_cast<quake::VeqType>(ty);
auto veqOpndTy = dyn_cast<quake::VeqType>(opnd.getType());
if (veqTy && !veqTy.hasSpecifiedSize() && veqOpndTy &&
veqOpndTy.hasSpecifiedSize())
continue;
return emitOpError("member type not compatible with operand type");
}
return success();
}
//===----------------------------------------------------------------------===//
// RelaxSizeOp
//===----------------------------------------------------------------------===//
LogicalResult quake::RelaxSizeOp::verify() {
if (cast<quake::VeqType>(getType()).hasSpecifiedSize())
emitOpError("return veq type must not specify a size");
return success();
}
void quake::RelaxSizeOp::getCanonicalizationPatterns(
RewritePatternSet &patterns, MLIRContext *context) {
patterns.add<ForwardRelaxedSizePattern>(context);
}
//===----------------------------------------------------------------------===//
// SubVeqOp
//===----------------------------------------------------------------------===//
LogicalResult quake::SubVeqOp::verify() {
if ((hasConstantLowerBound() && getRawLower() == kDynamicIndex) ||
(!hasConstantLowerBound() && getRawLower() != kDynamicIndex))
return emitOpError("invalid lower bound specified");
if ((hasConstantUpperBound() && getRawUpper() == kDynamicIndex) ||
(!hasConstantUpperBound() && getRawUpper() != kDynamicIndex))
return emitOpError("invalid upper bound specified");
if (hasConstantLowerBound() && hasConstantUpperBound()) {
if (getRawLower() > getRawUpper())
return emitOpError("invalid subrange specified");
if (auto veqTy = dyn_cast<quake::VeqType>(getVeq().getType()))
if (veqTy.hasSpecifiedSize())
if (getRawLower() >= veqTy.getSize() ||
getRawUpper() >= veqTy.getSize())
return emitOpError(
"subveq range does not fully intersect the input veq");
if (auto veqTy = dyn_cast<quake::VeqType>(getResult().getType()))
if (veqTy.hasSpecifiedSize())
if (veqTy.getSize() != getRawUpper() - getRawLower() + 1)
return emitOpError("incorrect size for result veq type");
}
return success();
}
void quake::SubVeqOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<FixUnspecifiedSubveqPattern, FuseConstantToSubveqPattern,
RemoveSubVeqNoOpPattern, CombineSubVeqsPattern>(context);
}
//===----------------------------------------------------------------------===//
// VeqSizeOp
//===----------------------------------------------------------------------===//
void quake::VeqSizeOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<FoldInitStateSizePattern, ForwardConstantVeqSizePattern>(
context);
}
//===----------------------------------------------------------------------===//
// WrapOp
//===----------------------------------------------------------------------===//
void quake::WrapOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<KillDeadWrapPattern>(context);
}
//===----------------------------------------------------------------------===//
// CallByRefOp
//===----------------------------------------------------------------------===//
// This is syntactic sugar for calling a kernel declared with quantum reference
// types and using "mismatched" arguments of quantum value types. This verify
// enforces all the restrictions on the call.
LogicalResult quake::CallByRefOp::verify() {
// Arguments must be classical or wire types, not ref types.
for (auto ty : getOperandTypes())
if (quake::isQuantumReferenceType(ty))
return emitOpError("quantum reference types are not allowed");
auto fn =
SymbolTable::lookupNearestSymbolFrom<func::FuncOp>(*this, getCallee());
if (!fn)
return emitOpError("callee must be declared");
FunctionType asSig = fn.getFunctionType();
// Arity of callee's signature must be equal to number of arguments provided.
if (getOperands().size() != asSig.getInputs().size())
return emitOpError("number of arguments must be consistent");
// Signature of callee must not contain quantum value types.
for (auto ty : asSig.getResults())
if (quake::isQuantumValueType(ty))
return emitOpError(
"quantum value types are not allowed in callee results");
for (auto ty : asSig.getInputs())
if (quake::isQuantumValueType(ty))
return emitOpError(
"quantum value types are not allowed in callee inputs");
// The first n results are the formal results and they must match.
const std::size_t formalResultsSize = asSig.getResults().size();
if (formalResultsSize)
for (auto [ty1, ty2] : llvm::zip(getResultTypes(), asSig.getResults()))
if (ty1 != ty2)
return emitOpError("result types must match");
// - Each wire type argument should match/promote to `as_signature`
// . The next output type in the results exactly
// . The arity of a ref type argument in the `as_signature` function type.
// - Each classical argument should match exactly.
SmallVector<Type> myResultTypes{getResultTypes().begin(),
getResultTypes().end()};
for (auto iter :
llvm::enumerate(llvm::zip(getOperandTypes(), asSig.getInputs()))) {
auto i = iter.index();
auto [operTy, sigTy] = iter.value();
if (quake::isQuantumValueType(operTy)) {
if (!quake::isQuantumReferenceType(sigTy))
return emitOpError("argument #" + std::to_string(i) +
" must be a quantum type");
if (quake::isConstantQuantumRefType(sigTy) &&
quake::getWireCount(operTy) != quake::getAllocationSize(sigTy))
return emitOpError("argument #" + std::to_string(i) +
" must match in size");
if (operTy != myResultTypes[formalResultsSize + i])
return emitOpError("result quantum value type #" +
std::to_string(formalResultsSize + i) +
" must match argument value type #" +
std::to_string(i));
} else {
if (operTy != sigTy)
return emitOpError("argument #" + std::to_string(i) +
" has incorrect type");
}
}
return success();
}
//===----------------------------------------------------------------------===//
// Measurements (MxOp, MyOp, MzOp)
//===----------------------------------------------------------------------===//
// Common verification for measurement operations.
template <typename MEAS>
LogicalResult verifyMeasurements(MEAS op, TypeRange targetsType,
const Type bitsType) {
if (failed(verifyWireResultsAreLinear(op)))
return failure();
bool mustBeCollection =
targetsType.size() > 1 ||
(targetsType.size() == 1 && isa<quake::VeqType>(targetsType[0]));
if (mustBeCollection) {
if (!isa<quake::MeasurementsType>(op.getMeasOut().getType()))
return op.emitOpError("must return `!quake.measurements`, when "
"measuring a qreg, a series of qubits, or both");
} else {
if (!isa<quake::MeasureType>(op.getMeasOut().getType()))
return op->emitOpError(
"must return `!quake.measure` when measuring exactly one qubit");
}
if (op.getRegisterName())
if (op.getRegisterName()->empty())
return op->emitError("quake measurement name cannot be empty.");
return success();
}
LogicalResult quake::MxOp::verify() {
return verifyMeasurements(*this, getTargets().getType(),
getMeasOut().getType());
}
LogicalResult quake::MyOp::verify() {
return verifyMeasurements(*this, getTargets().getType(),
getMeasOut().getType());
}
LogicalResult quake::MzOp::verify() {
return verifyMeasurements(*this, getTargets().getType(),
getMeasOut().getType());
}
void quake::MxOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<FuseSizeToMeasurementPattern<quake::MxOp>>(context);
}
void quake::MyOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<FuseSizeToMeasurementPattern<quake::MyOp>>(context);
}
void quake::MzOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add<FuseSizeToMeasurementPattern<quake::MzOp>>(context);
}
//===----------------------------------------------------------------------===//
// Discriminate
//===----------------------------------------------------------------------===//
LogicalResult quake::DiscriminateOp::verify() {
if (isa<quake::MeasurementsType>(getMeasurement().getType())) {
auto stdvecTy = dyn_cast<cudaq::cc::StdvecType>(getResult().getType());
if (!stdvecTy || !isa<IntegerType>(stdvecTy.getElementType()))
return emitOpError("must return a !cc.stdvec<integral> type, when "
"discriminating a measurements collection");
} else {
if (!isa<quake::MeasureType>(getMeasurement().getType()) ||
!isa<IntegerType>(getResult().getType()))
return emitOpError(
"must return integral type when discriminating exactly one qubit");
}
return success();
}
LogicalResult quake::BundleCableOp::verify() {
auto ty = cast<quake::CableType>(getResult().getType());
if (getWires().size() != ty.getSize())
return emitOpError("the bundle type size must equal the arity.");
return success();
}
LogicalResult quake::SplitCableOp::verify() {
if (getResults().size() != getCable().getType().getSize())
return emitOpError("the bundle type size must equal the coarity.");
return success();
}
LogicalResult quake::LeftTeeOp::verify() {
if (!getCable().getType().getSize())
return emitOpError("cannot remove a wire from an empty bundle.");
if (getIndex() >= getCable().getType().getSize())
return emitOpError("index into the bundle is out of bounds.");
if (getCableOut().getType().getSize() != getCable().getType().getSize() - 1)
return emitOpError("the bundle result type size must equal the size of the "
"bundle argument - 1.");
return success();
}
LogicalResult quake::RightTeeOp::verify() {
if (getIndex() > getCable().getType().getSize())
return emitOpError("index into the bundle is out of bounds.");
if (getCableOut().getType().getSize() != getCable().getType().getSize() + 1)
return emitOpError("the bundle result type size must equal the size of "
"the bundle argument + 1.");
return success();
}
//===----------------------------------------------------------------------===//
// WireSetOp
//===----------------------------------------------------------------------===//
ParseResult quake::WireSetOp::parse(OpAsmParser &parser,
OperationState &result) {
StringAttr name;
if (parser.parseSymbolName(name, getSymNameAttrName(result.name),
result.attributes))
return failure();
std::int32_t cardinality = 0;
if (parser.parseLSquare() || parser.parseInteger(cardinality) ||
parser.parseRSquare())
return failure();
result.addAttribute(getCardinalityAttrName(result.name),
parser.getBuilder().getI32IntegerAttr(cardinality));
Attribute sparseEle;
if (succeeded(parser.parseOptionalKeyword("adjacency")))
if (parser.parseAttribute(sparseEle, getAdjacencyAttrName(result.name),
result.attributes))
return failure();
if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
return failure();
return success();
}
void quake::WireSetOp::print(OpAsmPrinter &p) {
p << ' ';
p.printSymbolName(getSymName());
p << '[' << getCardinality() << ']';
if (auto adj = getAdjacency()) {
p << " adjacency ";
p.printAttribute(*adj);
}
p.printOptionalAttrDictWithKeyword(
(*this)->getAttrs(),
{getSymNameAttrName(), getCardinalityAttrName(), getAdjacencyAttrName()});
}
//===----------------------------------------------------------------------===//
// Operator interface
//===----------------------------------------------------------------------===//
// The following methods return to the operator's unitary matrix as a
// column-major array. For parameterizable operations, the matrix can only be
// built if the parameter can be computed at compilation time. These methods
// populate an empty array taken as a input. If the matrix was not successfully
// computed, the array will be left empty.
/// If the parameter is known at compilation-time, set the result value and