-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,14 +147,31 @@ impl<T: HugrView> Circuit<T> { | |
.expect("Circuit has no I/O nodes") | ||
} | ||
|
||
/// The number of quantum gates in the circuit. | ||
/// The number of operations in the circuit. | ||
/// | ||
/// This includes [`Tk2Op`]s, pytket ops, and any other custom operations. | ||
/// | ||
/// Nested circuits are traversed to count their operations. | ||
/// | ||
/// [`Tk2Op`]: crate::Tk2Op | ||
#[inline] | ||
pub fn num_gates(&self) -> usize | ||
pub fn num_operations(&self) -> usize | ||
where | ||
Self: Sized, | ||
{ | ||
// TODO: Discern quantum gates in the commands iterator. | ||
self.hugr().children(self.parent).count() - 2 | ||
let mut count = 0; | ||
let mut roots = vec![self.parent]; | ||
while let Some(node) = roots.pop() { | ||
for child in self.hugr().children(node) { | ||
let optype = self.hugr().get_optype(child); | ||
if optype.is_custom_op() { | ||
count += 1; | ||
} else if OpTag::DataflowParent.is_superset(optype.tag()) { | ||
roots.push(child); | ||
} | ||
} | ||
} | ||
count | ||
} | ||
|
||
/// Count the number of qubits in the circuit. | ||
|
@@ -471,6 +488,7 @@ fn update_signature( | |
#[cfg(test)] | ||
mod tests { | ||
use cool_asserts::assert_matches; | ||
use rstest::{fixture, rstest}; | ||
|
||
use hugr::types::FunctionType; | ||
use hugr::{ | ||
|
@@ -479,38 +497,83 @@ mod tests { | |
}; | ||
|
||
use super::*; | ||
use crate::utils::build_module_with_circuit; | ||
use crate::{json::load_tk1_json_str, utils::build_simple_circuit, Tk2Op}; | ||
|
||
fn test_circuit() -> Circuit { | ||
#[fixture] | ||
fn tk1_circuit() -> Circuit { | ||
load_tk1_json_str( | ||
r#"{ "phase": "0", | ||
"bits": [["c", [0]]], | ||
"qubits": [["q", [0]], ["q", [1]]], | ||
"commands": [ | ||
{"args": [["q", [0]]], "op": {"type": "H"}}, | ||
{"args": [["q", [0]], ["q", [1]]], "op": {"type": "CX"}}, | ||
{"args": [["q", [1]]], "op": {"type": "X"}} | ||
{"args": [["q", [1]]], "op": {"params": ["0.25"], "type": "Rz"}} | ||
], | ||
"implicit_permutation": [[["q", [0]], ["q", [0]]], [["q", [1]], ["q", [1]]]] | ||
}"#, | ||
) | ||
.unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_circuit_properties() { | ||
let circ = test_circuit(); | ||
/// 2-qubit circuit with a Hadamard, a CNOT, and a Rz gate. | ||
#[fixture] | ||
fn simple_circuit() -> Circuit { | ||
build_simple_circuit(2, |circ| { | ||
circ.append(Tk2Op::H, [0])?; | ||
circ.append(Tk2Op::CX, [0, 1])?; | ||
circ.append(Tk2Op::X, [1])?; | ||
|
||
assert_eq!(circ.name(), None); | ||
assert_eq!(circ.circuit_signature().body().input_count(), 3); | ||
assert_eq!(circ.circuit_signature().body().output_count(), 3); | ||
assert_eq!(circ.qubit_count(), 2); | ||
assert_eq!(circ.num_gates(), 3); | ||
// TODO: Replace the `X` with the following once Hugr adds `CircuitBuilder::add_constant`. | ||
// See https://github.com/CQCL/hugr/pull/1168 | ||
|
||
//let angle = circ.add_constant(ConstF64::new(0.5)); | ||
//circ.append_and_consume( | ||
// Tk2Op::RzF64, | ||
// [CircuitUnit::Linear(1), CircuitUnit::Wire(angle)], | ||
//)?; | ||
Ok(()) | ||
}) | ||
.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 commentThe reason will be displayed to describe this comment to others. Learn more. docstring is innacurate (X not Rz) |
||
/// defined inside a module. | ||
#[fixture] | ||
fn simple_module() -> Circuit { | ||
build_module_with_circuit(2, |circ| { | ||
circ.append(Tk2Op::H, [0])?; | ||
circ.append(Tk2Op::CX, [0, 1])?; | ||
circ.append(Tk2Op::X, [1])?; | ||
Ok(()) | ||
}) | ||
.unwrap() | ||
} | ||
|
||
#[rstest] | ||
#[case::simple(simple_circuit(), 2, 0, None)] | ||
#[case::module(simple_module(), 2, 0, None)] | ||
#[case::tk1(tk1_circuit(), 2, 1, None)] | ||
fn test_circuit_properties( | ||
#[case] circ: Circuit, | ||
#[case] qubits: usize, | ||
#[case] bits: usize, | ||
#[case] name: Option<&str>, | ||
) { | ||
assert_eq!(circ.name(), name); | ||
assert_eq!(circ.circuit_signature().body().input_count(), qubits + bits); | ||
assert_eq!( | ||
circ.circuit_signature().body().output_count(), | ||
qubits + bits | ||
); | ||
assert_eq!(circ.qubit_count(), qubits); | ||
assert_eq!(circ.num_operations(), 3); | ||
|
||
assert_eq!(circ.units().count(), 3); | ||
assert_eq!(circ.units().count(), qubits + bits); | ||
assert_eq!(circ.nonlinear_units().count(), 0); | ||
assert_eq!(circ.linear_units().count(), 3); | ||
assert_eq!(circ.qubits().count(), 2); | ||
assert_eq!(circ.linear_units().count(), qubits + bits); | ||
assert_eq!(circ.qubits().count(), qubits); | ||
} | ||
|
||
#[test] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
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