Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CXMappingPass a StandardPass with round-trip serialization and deserialization #1575

Merged
merged 19 commits into from
Sep 19, 2024

Conversation

cqc-alec
Copy link
Collaborator

@cqc-alec cqc-alec commented Sep 12, 2024

Description

Use AutoRebase instead of RebaseCustom in the pass; and wrap up the SequencePass in a StandardPass so that serialization and deserialization are inverse.

Related issues

Fixes #1573 .

Checklist

  • I have performed a self-review of my code.
  • I have commented hard-to-understand parts of my code.
  • I have made corresponding changes to the public API documentation.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the changelog with any user-facing changes.

@cqc-alec cqc-alec changed the title Ae/cxmapser Make CXMappingPass a StandardPass with round-trip serialization and deserialization Sep 12, 2024
PassPtr return_pass =
rebase_pass >> gen_full_mapping_pass(arc, placement_ptr, config);
if (delay_measures) return_pass = return_pass >> DelayMeasures();
return_pass = return_pass >> rebase_pass >>
gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx);
return return_pass;
Transform t{[=](Circuit& circ) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not totally confident in this: should we be passing a std::shared_ptr<unit_bimaps_t> as well as a Circuit& to the Transform constructor? If so what should we do with it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would the unit_bimaps_t object be for? I might be forgetting some TKET infrastructure here. Certainly we don't need to pass any kind of qubit placement if that's what it's referring to (as the circuit UnitID are updated).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure myself. Seems to be used in placement passes where units are renamed (e.g. gen_placement_pass()). So I assume not necessary here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - I'm now not sure. Just double checking, the unit_bimaps_t object tracks how qubits are re-labelled/permuted by optimisation? I'm now wondering if we should have always had them and not having them was an oversight? gen_routing_pass does seem to have them, so I think in-fact we do need them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my last commit. I don't know if this is correct, but I don't know what else to do with the maps either!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


      // Relabel all two-qubit gates
      CompilationUnit cu(circ);
      bool changed = rebase_pass->apply(cu);
      circ = cu.get_circ_ref();
      // Now try placement, falling back on "LinePlacement" if "GraphPlacement" fails
      try {
        changed = changed | placement_ptr->place(circ, maps);
      } catch (const std::runtime_error& e) {
        std::stringstream ss;
        ss << "PlacementPass failed with message: " << e.what()
          << " Fall back to LinePlacement.";
        tket_log()->warn(ss.str());
        Placement::Ptr line_placement_ptr = std::make_shared<LinePlacement>(
            placement_ptr->get_architecture_ref());
        changed = changed | line_placement_ptr->place(circ, maps);

      }
      // Now route
      MappingManager mm(std::make_shared<Architecture>(arc));
      changed = changed | mm.route_circuit_with_maps(circ, config, maps);
      
      // Now decompose routing gates to cx gates (this won't change maps)
      cu(circ);
      if(delay_measures){
        DelayMeasures()->apply(cu);
      }
      gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx)->apply(cu);
      circ = cu.get_circ_ref();
      return changed;
  }```

@cqc-alec cqc-alec marked this pull request as ready for review September 12, 2024 10:17
Copy link
Contributor

@sjdilkes sjdilkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, but I've asked for some clarification on the question.

PassPtr return_pass =
rebase_pass >> gen_full_mapping_pass(arc, placement_ptr, config);
if (delay_measures) return_pass = return_pass >> DelayMeasures();
return_pass = return_pass >> rebase_pass >>
gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx);
return return_pass;
Transform t{[=](Circuit& circ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would the unit_bimaps_t object be for? I might be forgetting some TKET infrastructure here. Certainly we don't need to pass any kind of qubit placement if that's what it's referring to (as the circuit UnitID are updated).

PassPtr return_pass =
rebase_pass >> gen_full_mapping_pass(arc, placement_ptr, config);
if (delay_measures) return_pass = return_pass >> DelayMeasures();
return_pass = return_pass >> rebase_pass >>
gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx);
return return_pass;
Transform t{[=](Circuit& circ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - I'm now not sure. Just double checking, the unit_bimaps_t object tracks how qubits are re-labelled/permuted by optimisation? I'm now wondering if we should have always had them and not having them was an oversight? gen_routing_pass does seem to have them, so I think in-fact we do need them?

PassPtr return_pass =
rebase_pass >> gen_full_mapping_pass(arc, placement_ptr, config);
if (delay_measures) return_pass = return_pass >> DelayMeasures();
return_pass = return_pass >> rebase_pass >>
gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx);
return return_pass;
Transform t{[=](Circuit& circ, std::shared_ptr<unit_bimaps_t> maps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?
`Transform t{[=](Circuit& circ, std::shared_ptr<unit_bimaps_t> maps) {

  // Relabel all two-qubit gates
  CompilationUnit cu(circ);
  bool changed = rebase_pass->apply(cu);
  circ = cu.get_circ_ref();
  // Now try placement, falling back on "LinePlacement" if "GraphPlacement" fails
  try {
    changed = changed | placement_ptr->place(circ, maps);
  } catch (const std::runtime_error& e) {
    std::stringstream ss;
    ss << "PlacementPass failed with message: " << e.what()
      << " Fall back to LinePlacement.";
    tket_log()->warn(ss.str());
    Placement::Ptr line_placement_ptr = std::make_shared<LinePlacement>(
        placement_ptr->get_architecture_ref());
    changed = changed | line_placement_ptr->place(circ, maps);

  }
  // Now route
  MappingManager mm(std::make_shared<Architecture>(arc));
  changed = changed | mm.route_circuit_with_maps(circ, config, maps);
  
  // Now decompose routing gates to cx gates (this won't change maps)
  cu(circ);
  if(delay_measures){
    DelayMeasures()->apply(cu);
  }
  gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx)->apply(cu);
  circ = cu.get_circ_ref();
  return changed;

}`

PassPtr return_pass =
rebase_pass >> gen_full_mapping_pass(arc, placement_ptr, config);
if (delay_measures) return_pass = return_pass >> DelayMeasures();
return_pass = return_pass >> rebase_pass >>
gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx);
return return_pass;
Transform t{[=](Circuit& circ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


      // Relabel all two-qubit gates
      CompilationUnit cu(circ);
      bool changed = rebase_pass->apply(cu);
      circ = cu.get_circ_ref();
      // Now try placement, falling back on "LinePlacement" if "GraphPlacement" fails
      try {
        changed = changed | placement_ptr->place(circ, maps);
      } catch (const std::runtime_error& e) {
        std::stringstream ss;
        ss << "PlacementPass failed with message: " << e.what()
          << " Fall back to LinePlacement.";
        tket_log()->warn(ss.str());
        Placement::Ptr line_placement_ptr = std::make_shared<LinePlacement>(
            placement_ptr->get_architecture_ref());
        changed = changed | line_placement_ptr->place(circ, maps);

      }
      // Now route
      MappingManager mm(std::make_shared<Architecture>(arc));
      changed = changed | mm.route_circuit_with_maps(circ, config, maps);
      
      // Now decompose routing gates to cx gates (this won't change maps)
      cu(circ);
      if(delay_measures){
        DelayMeasures()->apply(cu);
      }
      gen_decompose_routing_gates_to_cxs_pass(arc, directed_cx)->apply(cu);
      circ = cu.get_circ_ref();
      return changed;
  }```

@@ -480,14 +481,53 @@ PassPtr gen_cx_mapping_pass(
bool delay_measures) {
OpTypeSet gate_set = all_single_qubit_types();
gate_set.insert(OpType::CX);
PassPtr rebase_pass =
gen_rebase_pass(gate_set, CircPool::CX(), CircPool::tk1_to_tk1);
PassPtr rebase_pass = gen_auto_rebase_pass(gate_set);
PassPtr return_pass =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the references to return pass here I think

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used below in the line

  PassConditions conditions = return_pass->get_conditions();

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...which I think is still correct, although there may be an easier way to construct these conditions.

@cqc-alec cqc-alec merged commit 9c62c9b into main Sep 19, 2024
32 checks passed
@cqc-alec cqc-alec deleted the ae/cxmapser branch September 19, 2024 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants