Helper function to create a test module with a single gate operation
| 79 | |
| 80 | // Helper function to create a test module with a single gate operation |
| 81 | ModuleOp createTestModule(MLIRContext *context, StringRef gateSpecStr) { |
| 82 | auto [gateName, numControls, isAdj] = parseGateSpec(gateSpecStr); |
| 83 | |
| 84 | // Limit the number of controls to 2 |
| 85 | numControls = std::min<size_t>(numControls, 2); |
| 86 | |
| 87 | size_t numQubits; |
| 88 | if (gateName == "swap" || gateName == "exp_pauli") { |
| 89 | assert(numControls == 0); |
| 90 | // exp_pauli can have any number of qubits, we hardcode to 2 for the test. |
| 91 | numQubits = 2; |
| 92 | } else { |
| 93 | numQubits = numControls + 1; |
| 94 | } |
| 95 | |
| 96 | OpBuilder builder(context); |
| 97 | auto module = ModuleOp::create(builder, builder.getUnknownLoc()); |
| 98 | builder.setInsertionPointToEnd(module.getBody()); |
| 99 | |
| 100 | // Create function type: (qubits...) -> () |
| 101 | SmallVector<Type> inputTypes; |
| 102 | auto refType = cudaq::quake::RefType::get(context); |
| 103 | for (size_t i = 0; i < numQubits; ++i) { |
| 104 | inputTypes.push_back(refType); |
| 105 | } |
| 106 | auto funcType = builder.getFunctionType(inputTypes, {}); |
| 107 | |
| 108 | // Create function |
| 109 | auto func = func::FuncOp::create(builder, builder.getUnknownLoc(), |
| 110 | "test_func", funcType); |
| 111 | auto *entry = func.addEntryBlock(); |
| 112 | builder.setInsertionPointToStart(entry); |
| 113 | |
| 114 | // Get operands (controls and target) |
| 115 | SmallVector<Value> controls; |
| 116 | for (size_t i = 0; i < numControls; ++i) { |
| 117 | controls.push_back(entry->getArgument(i)); |
| 118 | } |
| 119 | Value target = entry->getArgument(numControls); |
| 120 | |
| 121 | // Create the gate operation based on gate name |
| 122 | Location loc = builder.getUnknownLoc(); |
| 123 | |
| 124 | Value pi_2 = cudaq::opt::factory::createFloatConstant(loc, builder, M_PI_2, |
| 125 | builder.getF64Type()); |
| 126 | |
| 127 | if (gateName == "h") { |
| 128 | cudaq::quake::HOp::create(builder, loc, isAdj, controls, target); |
| 129 | } else if (gateName == "s") { |
| 130 | cudaq::quake::SOp::create(builder, loc, isAdj, controls, target); |
| 131 | } else if (gateName == "t") { |
| 132 | cudaq::quake::TOp::create(builder, loc, isAdj, controls, target); |
| 133 | } else if (gateName == "x") { |
| 134 | cudaq::quake::XOp::create(builder, loc, isAdj, controls, target); |
| 135 | } else if (gateName == "y") { |
| 136 | cudaq::quake::YOp::create(builder, loc, isAdj, controls, target); |
| 137 | } else if (gateName == "z") { |
| 138 | cudaq::quake::ZOp::create(builder, loc, isAdj, controls, target); |
no test coverage detected