@@ -138,6 +138,20 @@ class OpSignatureAnalyzer {
138138
139139 operator bool () const { return isValid; }
140140
141+ Value getUpdatedQreg (PatternRewriter &rewriter, Location loc)
142+ {
143+ Value updatedQreg = signature.inWireIndices [0 ].getReg ();
144+ if (signature.needAllocQreg ) {
145+ // allocate a new qreg with the number of qubits
146+ auto nqubits = signature.inWireIndices .size () + signature.inCtrlWireIndices .size ();
147+ IntegerAttr nqubitsAttr = IntegerAttr::get (rewriter.getI64Type (), nqubits);
148+ auto allocOp = rewriter.create <quantum::AllocOp>(
149+ loc, quantum::QuregType::get (rewriter.getContext ()), nullptr , nqubitsAttr);
150+ updatedQreg = allocOp.getQreg ();
151+ }
152+ return updatedQreg;
153+ }
154+
141155 // Prepare the operands for calling the decomposition function
142156 // There are two cases:
143157 // 1. The first input is a qreg, which means the decomposition function is a qreg mode function
@@ -159,15 +173,7 @@ class OpSignatureAnalyzer {
159173 int operandIdx = 0 ;
160174 if (isa<quantum::QuregType>(funcInputs[0 ])) {
161175 // Allocate a new qreg if needed
162- Value updatedQreg = signature.inWireIndices [0 ].getReg ();
163- if (signature.needAllocQreg ) {
164- // allocate a new qreg with the number of qubits
165- auto nqubits = signature.inWireIndices .size () + signature.inCtrlWireIndices .size ();
166- IntegerAttr nqubitsAttr = IntegerAttr::get (rewriter.getI64Type (), nqubits);
167- auto allocOp = rewriter.create <quantum::AllocOp>(
168- loc, quantum::QuregType::get (rewriter.getContext ()), nullptr , nqubitsAttr);
169- updatedQreg = allocOp.getQreg ();
170- }
176+ Value updatedQreg = getUpdatedQreg (rewriter, loc);
171177
172178 for (auto [i, qubit] : llvm::enumerate (signature.inQubits )) {
173179 const QubitIndex &index = signature.inWireIndices [i];
@@ -198,23 +204,23 @@ class OpSignatureAnalyzer {
198204 // preprocessing indices
199205 // If needAllocQreg, the indices should be updated to from 0 to nqubits - 1
200206 // instead of the original indices, since we will use the new qreg for the indices
201- auto wireIndices = signature.inWireIndices ;
207+ auto inWireIndices = signature.inWireIndices ;
202208 auto ctrlWireIndices = signature.inCtrlWireIndices ;
203209 if (signature.needAllocQreg ) {
204- for (auto [i, index] : llvm::enumerate (wireIndices )) {
210+ for (auto [i, index] : llvm::enumerate (inWireIndices )) {
205211 auto attr = IntegerAttr::get (rewriter.getI64Type (), i);
206- wireIndices [i] = QubitIndex (attr, index.getReg ());
212+ inWireIndices [i] = QubitIndex (attr, index.getReg ());
207213 }
208- auto inWireIndicesSize = wireIndices .size ();
214+ auto inWireIndicesSize = inWireIndices .size ();
209215 for (auto [i, index] : llvm::enumerate (ctrlWireIndices)) {
210216 auto attr = IntegerAttr::get (rewriter.getI64Type (), i + inWireIndicesSize);
211217 ctrlWireIndices[i] = QubitIndex (attr, index.getReg ());
212218 }
213219 }
214220
215- if (!wireIndices .empty ()) {
221+ if (!inWireIndices .empty ()) {
216222 operands[operandIdx] =
217- fromTensorOrAsIs (wireIndices , funcInputs[operandIdx], rewriter, loc);
223+ fromTensorOrAsIs (inWireIndices , funcInputs[operandIdx], rewriter, loc);
218224 operandIdx++;
219225 }
220226
0 commit comments