-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add method to compute estimated duration of scheduled circuit (backport #13783) #13881
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e2908b
Add method to compute estimated duration of scheduled circuit (#13783)
mtreinish 78110e7
Update rust code to work with 1.4.0 rust data model
mtreinish dac65bb
Fix bug in arithmetic for converting dt to sec
mtreinish 6443a75
Add fix test
ElePT bc61e6e
Fix lint
ElePT 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2025 | ||
// | ||
// This code is licensed under the Apache License, Version 2.0. You may | ||
// obtain a copy of this license in the LICENSE.txt file in the root directory | ||
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// Any modifications or derivative works of this code must retain this | ||
// copyright notice, and modified files need to carry a notice indicating | ||
// that they have been altered from the originals. | ||
|
||
use pyo3::intern; | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType, Wire}; | ||
use qiskit_circuit::operations::{Operation, OperationRef, Param}; | ||
|
||
use crate::nlayout::PhysicalQubit; | ||
use crate::target_transpiler::Target; | ||
use crate::QiskitError; | ||
use rustworkx_core::dag_algo::longest_path; | ||
use rustworkx_core::petgraph::stable_graph::StableDiGraph; | ||
use rustworkx_core::petgraph::visit::{EdgeRef, IntoEdgeReferences}; | ||
|
||
/// Estimate the duration of a scheduled circuit in seconds | ||
#[pyfunction] | ||
pub(crate) fn compute_estimated_duration(dag: &DAGCircuit, target: &Target) -> PyResult<f64> { | ||
let dt = target.dt; | ||
|
||
let get_duration = | ||
|edge: <&StableDiGraph<NodeType, Wire> as IntoEdgeReferences>::EdgeRef| -> PyResult<f64> { | ||
let node_weight = &dag.dag()[edge.target()]; | ||
match node_weight { | ||
NodeType::Operation(inst) => { | ||
let name = inst.op.name(); | ||
let qubits = dag.get_qargs(inst.qubits); | ||
let physical_qubits: Vec<PhysicalQubit> = | ||
qubits.iter().map(|x| PhysicalQubit::new(x.0)).collect(); | ||
|
||
if name == "delay" { | ||
let dur = &inst.params.as_ref().unwrap()[0]; | ||
let OperationRef::Instruction(op) = inst.op.view() else { | ||
unreachable!("Invalid type for delay instruction"); | ||
}; | ||
Python::with_gil(|py| { | ||
let unit: String = op | ||
.instruction | ||
.getattr(py, intern!(py, "unit"))? | ||
.extract(py)?; | ||
if unit == "dt" { | ||
if let Some(dt) = dt { | ||
match dur { | ||
Param::Float(val) => Ok(val * dt), | ||
Param::Obj(val) => { | ||
let dur_float: f64 = val.extract(py)?; | ||
Ok(dur_float * dt) | ||
}, | ||
Param::ParameterExpression(_) => Err(QiskitError::new_err( | ||
"Circuit contains parameterized delays, can't compute a duration estimate with this circuit" | ||
)), | ||
} | ||
} else { | ||
Err(QiskitError::new_err( | ||
"Circuit contains delays in dt but the target doesn't specify dt" | ||
)) | ||
} | ||
} else if unit == "s" { | ||
match dur { | ||
Param::Float(val) => Ok(*val), | ||
_ => Err(QiskitError::new_err( | ||
"Invalid type for parameter value for delay in circuit", | ||
)), | ||
} | ||
} else { | ||
Err(QiskitError::new_err( | ||
"Circuit contains delays in units other then seconds or dt, the circuit is not scheduled." | ||
)) | ||
} | ||
}) | ||
} else if name == "barrier" { | ||
Ok(0.) | ||
} else { | ||
match target.get_duration(name, &physical_qubits) { | ||
Some(dur) => Ok(dur), | ||
None => Err(QiskitError::new_err(format!( | ||
"Duration not found for {} on qubits: {:?}", | ||
name, qubits | ||
))), | ||
} | ||
} | ||
} | ||
NodeType::QubitOut(_) | NodeType::ClbitOut(_) => Ok(0.), | ||
NodeType::ClbitIn(_) | NodeType::QubitIn(_) => { | ||
Err(QiskitError::new_err("Invalid circuit provided")) | ||
} | ||
_ => Err(QiskitError::new_err( | ||
"Circuit contains Vars, duration can't be calculated with classical variables", | ||
)), | ||
} | ||
}; | ||
match longest_path(dag.dag(), get_duration)? { | ||
Some((_, weight)) => Ok(weight), | ||
None => Err(QiskitError::new_err("Invalid circuit provided")), | ||
} | ||
} | ||
|
||
pub fn compute_duration(m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(compute_estimated_duration))?; | ||
Ok(()) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
releasenotes/notes/add-estimate_duration-method-a35bf8eef4b2f210.yaml
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
features_circuits: | ||
- | | ||
Added a new method, :meth:`.QuantumCircuit.estimate_duration`, to compute | ||
the estimated duration of a scheduled circuit output from the :mod:`.transpiler`. | ||
This should be used if you need an estimate of the full circuit duration instead | ||
of the deprecated :attr:`.QuantumCircuit.duration` attribute. |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.