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

feat: Replace Circuit::num_gates with num_operations #384

Merged
merged 2 commits into from
Jun 6, 2024
Merged

Conversation

aborgna-q
Copy link
Collaborator

@aborgna-q aborgna-q commented Jun 6, 2024

Closes #105. Closes #108.

num_gates used to count every node in the top-level region, giving unexpected results on results with constants, control flow, or anything other than simple gates.

num_operations now only counts CustomOps, traversing containers as needed.

I also improved the circuit unit tests, to include circuits in modules and circuits in FuncDefns (instead of DFGs).

Some notes:

@aborgna-q aborgna-q requested a review from ss2165 June 6, 2024 09:56
Copy link

codecov bot commented Jun 6, 2024

Codecov Report

Attention: Patch coverage is 73.68421% with 10 lines in your changes missing coverage. Please review.

Project coverage is 82.76%. Comparing base (50ee0fa) to head (9b17a6e).
Report is 1 commits behind head on main.

Files Patch % Lines
tket2/src/circuit.rs 67.85% 1 Missing and 8 partials ⚠️
tket2/src/rewrite/strategy.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #384      +/-   ##
==========================================
- Coverage   83.32%   82.76%   -0.56%     
==========================================
  Files          48       48              
  Lines        5042     5042              
  Branches     4591     4591              
==========================================
- Hits         4201     4173      -28     
- Misses        654      674      +20     
- Partials      187      195       +8     
Flag Coverage Δ
python 97.33% <ø> (ø)
rust 81.33% <73.68%> (-0.61%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

.unwrap()
}

/// 2-qubit circuit with a Hadamard, a CNOT, and a Rz gate,
Copy link
Member

Choose a reason for hiding this comment

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

docstring is innacurate (X not Rz)

self.hugr().children(self.parent).count() - 2
let mut count = 0;
let mut roots = vec![self.parent];
while let Some(node) = roots.pop() {
Copy link
Member

Choose a reason for hiding this comment

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

we don't have some existing graph/hierarchy traversal we could use? is this to avoid petgraph dependencies?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's hugr::DescendantsGraph but that's optimised for arbitrary accesses to the HugrView.
It keeps a cache of nodes it knows are in graph in a RefCell<HashMap<_,_>>, and updates it as it filters throught.
We can skip that here since we have a set traversal order.

I guess we could optimise that in Portgraph. I'll open an issue, but leave the explicit loop here for now.

Copy link
Member

Choose a reason for hiding this comment

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

why not just iterate over all nodes in the hugr?

Copy link
Collaborator Author

@aborgna-q aborgna-q Jun 6, 2024

Choose a reason for hiding this comment

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

Because we only care about the region describing the circuit, and its descendants.

If the hugr defines a module with multiple functions, we should only traverse the circuit's. E.g.

/// 2-qubit circuit with a Hadamard, a CNOT, and a T gate,
/// defined inside a module containing other circuits.
#[fixture]
fn module_with_circuits() -> Circuit {
let mut module = simple_module();
let other_circ = simple_circuit();
let hugr = module.hugr_mut();
hugr.insert_hugr(hugr.root(), other_circ.into_hugr());
return module;
}

This gets a bit interesting with any kind of control flow; should function calls count towards the operation count? Should conditionals count each branch?

Here I went with the best-defined option: traverse embedded DFG blocks, but ignore any control flow primitives.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Leaving this here to register the backlink:
CQCL/portgraph#135

@aborgna-q aborgna-q added this pull request to the merge queue Jun 6, 2024
Merged via the queue into main with commit 093e650 Jun 6, 2024
15 of 16 checks passed
@aborgna-q aborgna-q deleted the ab/num-ops branch June 6, 2024 14:30
github-merge-queue bot pushed a commit that referenced this pull request Jun 11, 2024
## 🤖 New release
* `tket2`: 0.1.0-alpha.1 -> 0.1.0-alpha.2

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.1.0-alpha.2](tket2-v0.1.0-alpha.1...tket2-v0.1.0-alpha.2)
- 2024-06-11

### Bug Fixes
- Commands iterator ignoring the hierarchy.
([#381](#381))

### New Features
- Replace `Circuit::num_gates` with `num_operations`
([#384](#384))
- Utilities for loading compiled guppy circuits
([#393](#393))

### Refactor
- [**breaking**] Replace Circuit trait with a struct
([#370](#370))
- [**breaking**] Rename `tket2::json` into `tket2::serialize::pytket`
([#392](#392))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix: Ignore consts/loadConsts in the command iterator? Circuit::num_gates not consistent with TKET n_gates
2 participants