-
Notifications
You must be signed in to change notification settings - Fork 6
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
Conversation
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
tket2/src/circuit.rs
Outdated
.unwrap() | ||
} | ||
|
||
/// 2-qubit circuit with a Hadamard, a CNOT, and a Rz gate, |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
tket2/tket2/src/circuit/command.rs
Lines 528 to 538 in 9b17a6e
/// 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.
There was a problem hiding this comment.
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
## 🤖 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/).
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 countsCustomOp
s, traversing containers as needed.I also improved the circuit unit tests, to include circuits in modules and circuits in
FuncDefn
s (instead ofDFG
s).Some notes:
Circuit::name
returnsNone
. I'll address that in another PR.