@@ -19,7 +19,7 @@ def Quartz_UnitaryOpInterface : OpInterface<"UnitaryOpInterface"> {
1919 let description = [{
2020 This interface provides a unified API for all operations that apply or
2121 produce a unitary transformation. This includes base gates, user-defined
22- gates, modified operations (control, inverse, power), and sequences.
22+ gates, modifier operations (control, inverse, power), and sequences.
2323
2424 The interface enables uniform introspection and composition capabilities
2525 across all unitary operations in the Quartz dialect.
@@ -29,10 +29,18 @@ def Quartz_UnitaryOpInterface : OpInterface<"UnitaryOpInterface"> {
2929
3030 let methods = [
3131 // Qubit accessors
32+ InterfaceMethod<
33+ "Returns the number of qubits acted on by the unitary operation.",
34+ "size_t", "getNumQubits", (ins)
35+ >,
3236 InterfaceMethod<
3337 "Returns the number of target qubits (excluding control qubits).",
3438 "size_t", "getNumTargets", (ins)
3539 >,
40+ InterfaceMethod<
41+ "Returns the number of control qubits (both positive and negative).",
42+ "size_t", "getNumControls", (ins)
43+ >,
3644 InterfaceMethod<
3745 "Returns the number of positive control qubits.",
3846 "size_t", "getNumPosControls", (ins)
@@ -41,6 +49,10 @@ def Quartz_UnitaryOpInterface : OpInterface<"UnitaryOpInterface"> {
4149 "Returns the number of negative control qubits.",
4250 "size_t", "getNumNegControls", (ins)
4351 >,
52+ InterfaceMethod<
53+ "Returns the i-th qubit (targets + controls combined).",
54+ "::mlir::Value", "getQubit", (ins "size_t":$i)
55+ >,
4456 InterfaceMethod<
4557 "Returns the i-th target qubit.",
4658 "::mlir::Value", "getTarget", (ins "size_t":$i)
@@ -54,41 +66,25 @@ def Quartz_UnitaryOpInterface : OpInterface<"UnitaryOpInterface"> {
5466 "::mlir::Value", "getNegControl", (ins "size_t":$i)
5567 >,
5668
57- // Value semantics threading (identity in reference semantics)
58- InterfaceMethod<
59- "Returns the i-th input qubit (targets + controls combined).",
60- "::mlir::Value", "getInput", (ins "size_t":$i)
61- >,
62- InterfaceMethod<
63- "Returns the i-th output qubit (targets + controls combined). "
64- "In reference semantics, returns the same as getInput.",
65- "::mlir::Value", "getOutput", (ins "size_t":$i),
66- /*methodBody=*/"", /*defaultImplementation=*/[{
67- return $_op.getInput(i);
68- }]
69- >,
69+ // Parameter handling
7070 InterfaceMethod<
71- "Returns the output qubit corresponding to the given input qubit. "
72- "In reference semantics, returns the input itself.",
73- "::mlir::Value", "getOutputForInput", (ins "::mlir::Value":$input),
74- /*methodBody=*/"", /*defaultImplementation=*/[{
75- return input;
76- }]
71+ "Returns the number of parameters.",
72+ "size_t", "getNumParams", (ins)
7773 >,
74+ /// TODO: This is missing many convenience methods for handling parameters. Some inspiration is in the RFC
75+
76+ // Convenience methods
7877 InterfaceMethod<
79- "Returns the input qubit corresponding to the given output qubit. "
80- "In reference semantics, returns the output itself.",
81- "::mlir::Value", "getInputForOutput", (ins "::mlir::Value":$output),
82- /*methodBody=*/"", /*defaultImplementation=*/[{
83- return output;
84- }]
78+ "Returns true if the operation has any control qubits, otherwise false.",
79+ "bool", "isControlled", (ins),
80+ [{ return getNumControls() > 0; }]
8581 >,
86-
87- // Parameter handling
8882 InterfaceMethod<
89- "Returns the number of parameters.",
90- "size_t", "getNumParams", (ins)
83+ "Returns true if the operation only acts on a single qubit.",
84+ "bool", "isSingleQubit", (ins),
85+ [{ return getNumQubits() == 1; }]
9186 >,
87+ /// TODO: I am fairly sure that there are quite some further convenience methods that would be helpful here, e.g., whether it is a two-qubit gate
9288
9389 // Matrix extraction
9490 InterfaceMethod<
@@ -100,28 +96,15 @@ def Quartz_UnitaryOpInterface : OpInterface<"UnitaryOpInterface"> {
10096 "Returns std::nullopt if the operation is symbolic or dynamic.",
10197 "std::optional<::mlir::DenseElementsAttr>", "tryGetStaticMatrix", (ins)
10298 >,
103-
104- // Modifier state
105- InterfaceMethod<
106- "Returns true if the operation is inverted.",
107- "bool", "isInverted", (ins),
108- /*methodBody=*/"", /*defaultImplementation=*/[{
109- return false;
110- }]
111- >,
112- InterfaceMethod<
113- "Returns the power exponent if applicable, otherwise std::nullopt.",
114- "std::optional<double>", "getPower", (ins),
115- /*methodBody=*/"", /*defaultImplementation=*/[{
116- return std::nullopt;
117- }]
118- >,
99+ /// TODO: probably also worth to check with the RFC and compare
119100
120101 // Identification
121102 InterfaceMethod<
122103 "Returns the base symbol/mnemonic of the operation.",
123104 "::llvm::StringRef", "getBaseSymbol", (ins)
124105 >,
106+
107+ /// TODO: I am fairly sure the RFC had some more functions here.
125108 ];
126109}
127110
0 commit comments