Commit 38b09dc3 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files


This is half of the fix: front-end to disconnect the use-def chain before and after the modified gate application.

Todo: extract merging pass also need to respect these gate modifiers
Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 27525e59
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -102,6 +102,35 @@ mlir::Value get_or_extract_qubit(const std::string &qreg_name,
  }
}

mlir::Value get_or_extract_qubit(const std::string &qubit_name,
                                 mlir::Location location,
                                 ScopedSymbolTable &symbol_table,
                                 mlir::OpBuilder &builder) {
  const std::string qreg_name =
      "__qcor__mlir__single_qubit_register_" + qubit_name;
  if (symbol_table.has_symbol(qubit_name)) {
    mlir::Value qubit_value = symbol_table.get_symbol(qubit_name);
    if (!symbol_table.verify_qubit_ssa_dominance_property(
            qubit_value, builder.getInsertionBlock())) {
      // The cache SSA value is not valid:
      symbol_table.erase_symbol(qubit_name);
      symbol_table.invalidate_qubit_extracts(qreg_name);
      mlir::Value reextracted_qubit =
          get_or_extract_qubit(qreg_name, 0, location, symbol_table, builder);
      symbol_table.add_symbol(qubit_name, reextracted_qubit);
      return reextracted_qubit;
    } else {
      return qubit_value;
    }
  } else {
    symbol_table.invalidate_qubit_extracts(qreg_name);
    mlir::Value reextracted_qubit =
        get_or_extract_qubit(qreg_name, 0, location, symbol_table, builder);
    symbol_table.add_symbol(qubit_name, reextracted_qubit);
    return reextracted_qubit;
  }
}

mlir::Value get_or_create_constant_integer_value(
    const std::size_t idx, mlir::Location location, mlir::Type type,
    ScopedSymbolTable &symbol_table, mlir::OpBuilder &builder) {
+6 −0
Original line number Diff line number Diff line
@@ -50,6 +50,12 @@ mlir::Value get_or_extract_qubit(const std::string &qreg_name,
                                 const std::size_t idx, mlir::Location location,
                                 ScopedSymbolTable &symbol_table,
                                 mlir::OpBuilder &builder);
// Get the qubit (possibly re-extract from the internally tracked size-1
// register)
mlir::Value get_or_extract_qubit(const std::string &qubit_name,
                                 mlir::Location location,
                                 ScopedSymbolTable &symbol_table,
                                 mlir::OpBuilder &builder);

mlir::Value get_or_create_constant_integer_value(
    const std::size_t idx, mlir::Location location, mlir::Type int_like_type,
+21 −1
Original line number Diff line number Diff line
@@ -300,6 +300,11 @@ antlrcpp::Any qasm3_visitor::visitQuantumGateCall(
      auto idx_str = idx_identifier->expressionList()->expression(0)->getText();
      const auto qubit_symbol_name =
          symbol_table.array_qubit_symbol_name(qbit_var_name, idx_str);
      if (!modifiers.empty()) {
        // This is a *modified* instruction, i.e., wrapped gate op
        // must invalidate the qubit SSA use-def chain -> required re-extract:
        symbol_table.erase_symbol(qubit_symbol_name);
      }
      mlir::Value value;
      try {
        // try catch is on this std::stoi(), if idx_str is not an integer,
@@ -361,7 +366,11 @@ antlrcpp::Any qasm3_visitor::visitQuantumGateCall(

    } else {
      // this is a qubit
      auto qbit = symbol_table.get_symbol(qbit_var_name);
      if (!modifiers.empty()) {
        symbol_table.erase_symbol(qbit_var_name);
      }
      auto qbit =
          get_or_extract_qubit(qbit_var_name, location, symbol_table, builder);
      qbit_values.push_back(qbit);
      qubit_symbol_table_keys.push_back(qbit_var_name);
    }
@@ -402,6 +411,10 @@ antlrcpp::Any qasm3_visitor::visitQuantumGateCall(
    }
  }

  // Cache these symbol keys before erasing.
  // All qubits involved in modified gate ops (target and controls) need to be
  // re-extracted afterwards.
  const auto cached_qubit_symbol_table_keys = qubit_symbol_table_keys;
  // Potentially get the ctrl qubit
  mlir::Value ctrl_bit;
  if (has_ctrl) {
@@ -512,6 +525,13 @@ antlrcpp::Any qasm3_visitor::visitQuantumGateCall(
    action_and_extrainfo.pop();
  }

  // Simularly, disconnect the SSA use-def afterward if this is a modified
  // block.
  if (!modifiers.empty()) {
    for (const auto &key : cached_qubit_symbol_table_keys) {
      symbol_table.erase_symbol(key);
    }
  }
  return 0;
}