Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
170 commits
Select commit Hold shift + click to select a range
af4ba52
WIP
kevinhartman Feb 7, 2025
e423bf9
Add try_const to lift.
kevinhartman Feb 7, 2025
0a5917b
Try multiple singletons, new one for const.
kevinhartman Feb 7, 2025
a97434d
Revert "Try multiple singletons, new one for const."
kevinhartman Feb 7, 2025
1afc965
Remove Bool singleton test.
kevinhartman Feb 7, 2025
86655f1
Add const handling for stores, fix test bugs.
kevinhartman Feb 7, 2025
aaeae9b
Fix formatting.
kevinhartman Feb 7, 2025
a2a444b
Remove Duration and Stretch for now.
kevinhartman Feb 7, 2025
8ac2dc3
Cleanup, fix const bug in index.
kevinhartman Feb 7, 2025
9f8313c
Fix ordering issue for types with differing const-ness.
kevinhartman Feb 9, 2025
db9d9cb
Fix QPY serialization.
kevinhartman Feb 10, 2025
71b7e7a
Make expr.Lift default to non-const.
kevinhartman Feb 11, 2025
2091557
Revert to old test_expr_constructors.py.
kevinhartman Feb 11, 2025
7307be9
Make binary_logical lift independent again.
kevinhartman Feb 11, 2025
9b30284
Update tests, handle a few edge cases.
kevinhartman Feb 12, 2025
ce1faf1
Fix docstring.
kevinhartman Feb 12, 2025
4fee48f
Remove now redundant arg from tests.
kevinhartman Feb 12, 2025
88ab046
Add const testing for ordering.
kevinhartman Feb 12, 2025
c5a230f
Add const tests for shifts.
kevinhartman Feb 12, 2025
7c88d88
Add release note.
kevinhartman Feb 12, 2025
c58a7b8
Add const store tests.
kevinhartman Feb 12, 2025
d9e9a8c
Address lint, minor cleanup.
kevinhartman Feb 13, 2025
4a56150
Add Float type to classical expressions.
kevinhartman Feb 12, 2025
23b5961
Allow DANGEROUS conversion from Float to Bool.
kevinhartman Feb 12, 2025
8bf2e4f
Test Float ordering.
kevinhartman Feb 12, 2025
111eb32
Improve error messages for using Float with logical operators.
kevinhartman Feb 12, 2025
a839d51
Float tests for constructors.
kevinhartman Feb 12, 2025
a19b39a
Add release note.
kevinhartman Feb 12, 2025
7f02e56
Add Duration and Stretch classical types.
kevinhartman Feb 13, 2025
55af327
Add Duration type to qiskit.circuit.
kevinhartman Feb 13, 2025
86b7af9
Block Stretch from use in binary relations.
kevinhartman Feb 13, 2025
9b3c821
Add type ordering tests for Duration and Stretch.
kevinhartman Feb 13, 2025
69eab91
Test expr constructors for Duration and Stretch.
kevinhartman Feb 13, 2025
971cc26
Fix lint.
kevinhartman Feb 13, 2025
657b666
Implement operators +, -, *, /.
kevinhartman Feb 14, 2025
fc53d47
Add expression tests for arithmetic ops.
kevinhartman Feb 15, 2025
24f5125
Implement stretch support for circuit.
kevinhartman Feb 16, 2025
9e775e1
Initial support for writing QASM3.
kevinhartman Feb 16, 2025
25508bf
Reject const vars in add_var and add_input.
kevinhartman Feb 17, 2025
2c8ce43
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 17, 2025
ccf9441
Implement QPY support for const-typed expressions.
kevinhartman Feb 18, 2025
c6eab02
Remove invalid test.
kevinhartman Feb 18, 2025
edd7806
Update QPY version 14 desc.
kevinhartman Feb 18, 2025
8afa92e
Fix lint.
kevinhartman Feb 18, 2025
4e0f2df
Add serialization testing.
kevinhartman Feb 18, 2025
15ba943
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 18, 2025
81c5833
Merge branch 'const-expr' into expr-float
kevinhartman Feb 18, 2025
50c31d3
Test pre-v14 QPY rejects const-typed exprs.
kevinhartman Feb 18, 2025
7e43322
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 18, 2025
2449ee6
Merge branch 'const-expr' into expr-float
kevinhartman Feb 18, 2025
e55e189
QASM export for floats.
kevinhartman Feb 18, 2025
1d51022
QPY support for floats.
kevinhartman Feb 18, 2025
eb8f150
Fix lint.
kevinhartman Feb 18, 2025
415f62e
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 18, 2025
d38f3e9
Settle on Duration circuit core type.
kevinhartman Feb 19, 2025
fd66c1d
QPY serialization for durations and stretches.
kevinhartman Feb 19, 2025
e9b6d97
Add QPY testing.
kevinhartman Feb 19, 2025
52da3cc
QASM support for stretch and duration.
kevinhartman Feb 19, 2025
a3be688
Fix lint.
kevinhartman Feb 19, 2025
2ff7e7a
Merge branch 'expr-timing' into math-expr
kevinhartman Feb 19, 2025
6e8080c
Add arithmetic operators to QASM.
kevinhartman Feb 19, 2025
9266166
QPY testing for arithmetic operations.
kevinhartman Feb 19, 2025
e92dcc5
QASM testing for arithmetic operations.
kevinhartman Feb 19, 2025
2d8f1b7
Merge branch 'math-expr' into circuit-stretch
kevinhartman Feb 19, 2025
f5a5457
Update tests for blocked const vars.
kevinhartman Feb 19, 2025
6d7b2af
Only test declare stretch QASM; fix lint.
kevinhartman Feb 19, 2025
6ad69d4
Special case for stretch in add_uninitialized_var.
kevinhartman Feb 19, 2025
af71e3a
Remove outdated docstring comment.
kevinhartman Feb 19, 2025
bddbc66
Remove outdated comment.
kevinhartman Feb 19, 2025
07771f1
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 20, 2025
9332ed3
Merge branch 'const-expr' into expr-float
kevinhartman Feb 20, 2025
17241c6
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 20, 2025
ca91bfd
Don't use match since we still support Python 3.9.
kevinhartman Feb 20, 2025
368b0ac
Merge branch 'expr-timing' into math-expr
kevinhartman Feb 20, 2025
7fcb098
Merge branch 'math-expr' into circuit-stretch
kevinhartman Feb 20, 2025
723e4f0
Block const stores.
kevinhartman Feb 20, 2025
ad87e78
Fix enum match.
kevinhartman Feb 20, 2025
9afd68c
Merge branch 'expr-timing' into math-expr
kevinhartman Feb 20, 2025
86ac649
Merge branch 'math-expr' into circuit-stretch
kevinhartman Feb 20, 2025
d3bca5f
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 24, 2025
50deb93
Revert visitors.py.
kevinhartman Feb 24, 2025
f1dc1a1
Address review comments.
kevinhartman Feb 24, 2025
1f439a0
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 25, 2025
166d7b2
Improve type docs.
kevinhartman Feb 26, 2025
10b2c8d
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 26, 2025
9d6cf39
Revert QPY, since the old format can support constexprs.
kevinhartman Feb 26, 2025
8021e00
Move const-ness from Type to Expr.
kevinhartman Feb 26, 2025
a15141b
Revert QPY testing, no longer needed.
kevinhartman Feb 26, 2025
ca2785d
Add explicit validation of const expr.
kevinhartman Feb 26, 2025
e902009
Revert stuff I didn't need to touch.
kevinhartman Feb 26, 2025
16475c3
Update release note.
kevinhartman Feb 26, 2025
6b5930d
A few finishing touches.
kevinhartman Feb 27, 2025
be73180
Merge branch 'const-expr-expr' into expr-float-expr
kevinhartman Feb 27, 2025
25f1693
Fix-up after merge.
kevinhartman Feb 27, 2025
ecc343b
Merge branch 'expr-float-expr' into expr-timing-expr
kevinhartman Feb 27, 2025
b32e6f0
Fix-ups after merge.
kevinhartman Feb 27, 2025
864506d
Fix lint.
kevinhartman Feb 27, 2025
6bc728a
Fix comment and release note.
kevinhartman Feb 27, 2025
2d38178
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 27, 2025
84f5a66
Merge branch 'expr-timing' into math-expr
kevinhartman Feb 27, 2025
91b4c75
Fixes after merge.
kevinhartman Feb 27, 2025
e1dbd1d
Fix test.
kevinhartman Feb 28, 2025
1ad4388
Fix lint.
kevinhartman Feb 28, 2025
42258b8
Merge branch 'main' of github.com:Qiskit/qiskit into expr-float
kevinhartman Feb 28, 2025
481700a
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 28, 2025
e09762f
Special-case Var const-ness for Stretch type.
kevinhartman Feb 28, 2025
cbd55db
Address review comments.
kevinhartman Feb 28, 2025
f12afb4
Update release note.
kevinhartman Feb 28, 2025
8d059cd
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 28, 2025
e05d9ff
Update docs.
kevinhartman Feb 28, 2025
bd6bf35
Merge branch 'expr-timing' into math-expr
kevinhartman Feb 28, 2025
ef04db2
Add release notes and doc link.
kevinhartman Feb 28, 2025
0f14cca
Address review comments.
kevinhartman Mar 1, 2025
7db8222
Merge branch 'expr-float' into expr-timing
kevinhartman Mar 1, 2025
65f2198
Merge branch 'expr-timing' into math-expr
kevinhartman Mar 1, 2025
34e615b
Merge branch 'main' of github.com:Qiskit/qiskit into expr-timing
kevinhartman Mar 1, 2025
bbe67a6
Remove Stretch type.
kevinhartman Mar 2, 2025
0acc450
Remove a few more mentions of the binned stretch type.
kevinhartman Mar 2, 2025
7fea216
Add docstring for Duration.
kevinhartman Mar 2, 2025
4a317e8
Merge branch 'expr-timing' into math-expr
kevinhartman Mar 2, 2025
f5709ae
Remove Stretch type stuff.
kevinhartman Mar 2, 2025
3ae8e5e
Merge branch 'math-expr' into circuit-stretch
kevinhartman Mar 2, 2025
0ba0126
WIP
kevinhartman Mar 2, 2025
de32a57
Track stretch variables throughout circuits.
kevinhartman Mar 2, 2025
ed024ab
Update QASM exporter.
kevinhartman Mar 2, 2025
eb3cbcd
Fix existing tests and found bugs.
kevinhartman Mar 2, 2025
5634d63
Fix format.
kevinhartman Mar 2, 2025
37dec7a
Simplify structural eq visitor.
kevinhartman Mar 2, 2025
05cbcb1
Add num_identifiers.
kevinhartman Mar 2, 2025
96f4e06
Support QPY.
kevinhartman Mar 2, 2025
3f74a7f
Implement QASM visit for stretch expr.
kevinhartman Mar 3, 2025
7f0b785
Track stretches through DAGCircuit.
kevinhartman Mar 3, 2025
ccb3b88
Add visit_stretch to classical resource map.
kevinhartman Mar 3, 2025
576d194
Fix lint.
kevinhartman Mar 3, 2025
78b2951
Address review comments.
kevinhartman Mar 3, 2025
97bbeba
Remove unused import.
kevinhartman Mar 3, 2025
3a34220
Merge branch 'expr-timing' into math-expr
kevinhartman Mar 3, 2025
888e9bb
Merge branch 'math-expr' into circuit-stretch
kevinhartman Mar 3, 2025
fab8b36
Represent stretch with StretchDeclaration in QASM AST.
kevinhartman Mar 3, 2025
49f2af8
Remove visitor short circuit.
kevinhartman Mar 3, 2025
69b985b
Merge branch 'expr-timing' into math-expr
kevinhartman Mar 3, 2025
3b42c14
Merge branch 'math-expr' into circuit-stretch
kevinhartman Mar 3, 2025
f780d9a
Address review comments.
kevinhartman Mar 3, 2025
e7ca9c4
Address review comments.
kevinhartman Mar 3, 2025
d4cef21
Merge branch 'main' of github.com:Qiskit/qiskit into math-expr
kevinhartman Mar 3, 2025
60bcfdb
Support division by Uint.
kevinhartman Mar 3, 2025
2d3706e
Merge branch 'math-expr' into circuit-stretch
kevinhartman Mar 3, 2025
286aff7
Add release note.
kevinhartman Mar 3, 2025
d40cb32
Fix lint.
kevinhartman Mar 3, 2025
dfc02f3
Merge branch 'math-expr' into circuit-stretch
kevinhartman Mar 3, 2025
a13dc49
Add missing DAG stretch plumbing.
kevinhartman Mar 4, 2025
6d64f7f
Fix QPY serialization bug.
kevinhartman Mar 4, 2025
099f257
Fix lint.
kevinhartman Mar 4, 2025
f1e2557
Add qpy test.
kevinhartman Mar 4, 2025
dc972d4
Support remapping for stretch variables in compose.
kevinhartman Mar 4, 2025
ad54644
Merge branch 'main' of github.com:Qiskit/qiskit into math-expr
kevinhartman Mar 4, 2025
723445f
Merge branch 'math-expr' into circuit-stretch
kevinhartman Mar 4, 2025
ae08fae
Fix qpy compat test.
kevinhartman Mar 4, 2025
320b72d
Add negative test for QPY stretch expr.
kevinhartman Mar 4, 2025
c66842b
Merge branch 'main' of github.com:Qiskit/qiskit into circuit-stretch
kevinhartman Mar 5, 2025
827e619
Add more circuit testing.
kevinhartman Mar 5, 2025
4d7129e
Add circuit equality testing for stretch.
kevinhartman Mar 5, 2025
9d82809
Refer to 'stretches' instead of 'stretch variables'.
kevinhartman Mar 5, 2025
e84b48e
Remove | None for Stretch.name
kevinhartman Mar 5, 2025
62cf3ee
Address review comments.
kevinhartman Mar 6, 2025
e2044e6
Merge branch 'main' of github.com:Qiskit/qiskit into circuit-stretch
kevinhartman Mar 6, 2025
7634b3e
Update QPY desc.
kevinhartman Mar 6, 2025
2223d12
Fix num_identifiers.
kevinhartman Mar 6, 2025
823dd27
Merge branch 'main' of github.com:Qiskit/qiskit into circuit-stretch
kevinhartman Mar 6, 2025
a5ccc7b
Fix merge for control flow tests.
kevinhartman Mar 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/circuit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct QuantumCircuitData<'py> {
pub input_vars: Vec<Bound<'py, PyAny>>,
pub captured_vars: Vec<Bound<'py, PyAny>>,
pub declared_vars: Vec<Bound<'py, PyAny>>,
pub captured_stretches: Vec<Bound<'py, PyAny>>,
pub declared_stretches: Vec<Bound<'py, PyAny>>,
}

impl<'py> FromPyObject<'py> for QuantumCircuitData<'py> {
Expand Down Expand Up @@ -63,6 +65,14 @@ impl<'py> FromPyObject<'py> for QuantumCircuitData<'py> {
.call_method0(intern!(py, "iter_declared_vars"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
captured_stretches: ob
.call_method0(intern!(py, "iter_captured_stretches"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
declared_stretches: ob
.call_method0(intern!(py, "iter_declared_stretches"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
})
}
}
Expand Down
231 changes: 228 additions & 3 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ pub struct DAGCircuit {
control_flow_module: PyControlFlowModule,
vars_info: HashMap<String, DAGVarInfo>,
vars_by_type: [Py<PySet>; 3],

captured_stretches: IndexMap<String, Py<PyAny>, RandomState>,
declared_stretches: IndexMap<String, Py<PyAny>, RandomState>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -391,6 +394,8 @@ impl DAGCircuit {
PySet::empty(py)?.unbind(),
PySet::empty(py)?.unbind(),
],
captured_stretches: IndexMap::default(),
declared_stretches: IndexMap::default(),
})
}

Expand Down Expand Up @@ -1497,6 +1502,12 @@ impl DAGCircuit {
{
target_dag.add_var(py, &var, DAGVarType::Declare)?;
}
for stretch in self.captured_stretches.values() {
target_dag.add_captured_stretch(py, stretch.bind(py))?;
}
for stretch in self.declared_stretches.values() {
target_dag.add_declared_stretch(stretch.bind(py))?;
}
} else if vars_mode == "captures" {
for var in self.vars_by_type[DAGVarType::Input as usize]
.bind(py)
Expand All @@ -1516,6 +1527,12 @@ impl DAGCircuit {
{
target_dag.add_var(py, &var, DAGVarType::Capture)?;
}
for stretch in self.captured_stretches.values() {
target_dag.add_captured_stretch(py, stretch.bind(py))?;
}
for stretch in self.declared_stretches.values() {
target_dag.add_captured_stretch(py, stretch.bind(py))?;
}
} else if vars_mode != "drop" {
return Err(PyValueError::new_err(format!(
"unknown vars_mode: '{}'",
Expand Down Expand Up @@ -1813,20 +1830,26 @@ impl DAGCircuit {
dag.add_input_var(py, &var?)?;
}
if inline_captures {
for var in other.iter_captured_vars(py)?.bind(py) {
for var in other.iter_captures(py)?.bind(py) {
let var = var?;
if !dag.has_var(&var)? {
if !dag.has_identifier(&var)? {
return Err(DAGCircuitError::new_err(format!("Variable '{}' to be inlined is not in the base DAG. If you wanted it to be automatically added, use `inline_captures=False`.", var)));
}
}
} else {
for var in other.iter_captured_vars(py)?.bind(py) {
dag.add_captured_var(py, &var?)?;
}
for stretch in other.iter_captured_stretches(py)?.bind(py) {
dag.add_captured_stretch(py, &stretch?)?;
}
}
for var in other.iter_declared_vars(py)?.bind(py) {
dag.add_declared_var(py, &var?)?;
}
for var in other.iter_declared_stretches(py)?.bind(py) {
dag.add_declared_stretch(&var?)?;
}

let variable_mapper = PyVariableMapper::new(
py,
Expand Down Expand Up @@ -2244,6 +2267,32 @@ impl DAGCircuit {
}
}

if self.captured_stretches.len() != other.captured_stretches.len()
|| self.declared_stretches.len() != other.declared_stretches.len()
{
return Ok(false);
}

for (our_stretch, their_stretch) in self
.captured_stretches
.values()
.zip(other.captured_stretches.values())
{
if !our_stretch.bind(py).eq(their_stretch)? {
return Ok(false);
}
}

for (our_stretch, their_stretch) in self
.declared_stretches
.values()
.zip(other.declared_stretches.values())
{
if !our_stretch.bind(py).eq(their_stretch)? {
return Ok(false);
}
}

Comment thread
kevinhartman marked this conversation as resolved.
let self_bit_indices = {
let indices = self
.qubits
Expand Down Expand Up @@ -4270,6 +4319,47 @@ impl DAGCircuit {
Ok(())
}

/// Add a captured stretch to the circuit.
///
/// Args:
/// var: the stretch to add.
fn add_captured_stretch(&mut self, py: Python, var: &Bound<PyAny>) -> PyResult<()> {
if !self.vars_by_type[DAGVarType::Input as usize]
.bind(py)
.is_empty()
{
return Err(DAGCircuitError::new_err(
"cannot add captures to a circuit with inputs",
));
}
let var_name: String = var.getattr("name")?.extract::<String>()?;
if self.vars_info.contains_key(&var_name) {
return Err(DAGCircuitError::new_err(
"cannot add stretch as its name shadows an existing identifier",
));
}
if let Some(previous) = self.declared_stretches.get(&var_name) {
if var.eq(previous)? {
return Err(DAGCircuitError::new_err("already present in the circuit"));
}
return Err(DAGCircuitError::new_err(
"cannot add stretch as its name shadows an existing identifier",
));
}
if let Some(previous) = self.captured_stretches.get(&var_name) {
if var.eq(previous)? {
return Err(DAGCircuitError::new_err("already present in the circuit"));
}
return Err(DAGCircuitError::new_err(
"cannot add stretch as its name shadows an existing identifier",
));
}

self.captured_stretches
.insert(var_name, var.clone().unbind());
Ok(())
}

/// Add a declared local variable to the circuit.
///
/// Args:
Expand All @@ -4279,6 +4369,39 @@ impl DAGCircuit {
Ok(())
}

/// Add a declared stretch to the circuit.
///
/// Args:
/// var: the stretch to add.
fn add_declared_stretch(&mut self, var: &Bound<PyAny>) -> PyResult<()> {
let var_name: String = var.getattr("name")?.extract::<String>()?;
if self.vars_info.contains_key(&var_name) {
return Err(DAGCircuitError::new_err(
"cannot add stretch as its name shadows an existing identifier",
));
}
if let Some(previous) = self.declared_stretches.get(&var_name) {
if var.eq(previous)? {
return Err(DAGCircuitError::new_err("already present in the circuit"));
}
return Err(DAGCircuitError::new_err(
"cannot add stretch as its name shadows an existing identifier",
));
}
if let Some(previous) = self.captured_stretches.get(&var_name) {
if var.eq(previous)? {
return Err(DAGCircuitError::new_err("already present in the circuit"));
}
return Err(DAGCircuitError::new_err(
"cannot add stretch as its name shadows an existing identifier",
));
}

self.declared_stretches
.insert(var_name, var.clone().unbind());
Ok(())
}

/// Total number of classical variables tracked by the circuit.
#[getter]
fn num_vars(&self) -> usize {
Expand Down Expand Up @@ -4325,6 +4448,36 @@ impl DAGCircuit {
}
}

/// Is this stretch in the DAG?
///
/// Args:
/// var: the stretch or name to check.
fn has_stretch(&self, var: &Bound<PyAny>) -> PyResult<bool> {
match var.extract::<String>() {
Ok(name) => Ok(self.declared_stretches.contains_key(&name)
|| self.captured_stretches.contains_key(&name)),
Err(_) => {
let raw_name = var.getattr("name")?;
let var_name: String = raw_name.extract()?;
if let Some(stretch) = self.declared_stretches.get(&var_name) {
return Ok(stretch.is(var));
}
if let Some(stretch) = self.captured_stretches.get(&var_name) {
return Ok(stretch.is(var));
}
Ok(false)
}
}
}

/// Is this identifier in the DAG?
///
/// Args:
/// var: the identifier or name to check.
fn has_identifier(&self, var: &Bound<PyAny>) -> PyResult<bool> {
Ok(self.has_var(var)? || self.has_stretch(var)?)
}

/// Iterable over the input classical variables tracked by the circuit.
fn iter_input_vars(&self, py: Python) -> PyResult<Py<PyIterator>> {
Ok(self.vars_by_type[DAGVarType::Input as usize]
Expand All @@ -4345,6 +4498,29 @@ impl DAGCircuit {
.unbind())
}

/// Iterable over the captured stretches tracked by the circuit.
fn iter_captured_stretches(&self, py: Python) -> PyResult<Py<PyIterator>> {
Ok(PyList::new(py, self.captured_stretches.values())?
.into_any()
.try_iter()?
.unbind())
}

/// Iterable over all captured identifiers tracked by the circuit.
fn iter_captures(&self, py: Python) -> PyResult<Py<PyIterator>> {
let out_set = PySet::empty(py)?;
for var in self.vars_by_type[DAGVarType::Capture as usize]
.bind(py)
.iter()
{
out_set.add(var)?;
}
for stretch in self.captured_stretches.values() {
out_set.add(stretch)?;
}
Ok(out_set.into_any().try_iter()?.unbind())
}

/// Iterable over the declared classical variables tracked by the circuit.
fn iter_declared_vars(&self, py: Python) -> PyResult<Py<PyIterator>> {
Ok(self.vars_by_type[DAGVarType::Declare as usize]
Expand All @@ -4355,6 +4531,14 @@ impl DAGCircuit {
.unbind())
}

/// Iterable over the declared stretches tracked by the circuit.
fn iter_declared_stretches(&self, py: Python) -> PyResult<Py<PyIterator>> {
Ok(PyList::new(py, self.declared_stretches.values())?
.into_any()
.try_iter()?
.unbind())
}

/// Iterable over all the classical variables tracked by the circuit.
fn iter_vars(&self, py: Python) -> PyResult<Py<PyIterator>> {
let out_set = PySet::empty(py)?;
Expand All @@ -4366,6 +4550,18 @@ impl DAGCircuit {
Ok(out_set.into_any().try_iter()?.unbind())
}

/// Iterable over all the stretches tracked by the circuit.
fn iter_stretches(&self, py: Python) -> PyResult<Py<PyIterator>> {
let out_set = PySet::empty(py)?;
for s in self.captured_stretches.values() {
out_set.add(s)?;
}
for s in self.declared_stretches.values() {
out_set.add(s)?;
}
Ok(out_set.into_any().try_iter()?.unbind())
}

fn _has_edge(&self, source: usize, target: usize) -> bool {
self.dag
.contains_edge(NodeIndex::new(source), NodeIndex::new(target))
Expand Down Expand Up @@ -5794,7 +5990,14 @@ impl DAGCircuit {
return Err(DAGCircuitError::new_err("already present in the circuit"));
}
return Err(DAGCircuitError::new_err(
"cannot add var as its name shadows an existing var",
"cannot add var as its name shadows an existing identifier",
));
}
if self.declared_stretches.contains_key(&var_name)
|| self.captured_stretches.contains_key(&var_name)
{
return Err(DAGCircuitError::new_err(
"cannot add var as its name shadows an existing identifier",
));
}

Expand Down Expand Up @@ -5915,6 +6118,8 @@ impl DAGCircuit {
PySet::empty(py)?.unbind(),
PySet::empty(py)?.unbind(),
],
captured_stretches: IndexMap::default(),
declared_stretches: IndexMap::default(),
})
}

Expand Down Expand Up @@ -6411,6 +6616,24 @@ impl DAGCircuit {
new_dag.add_var(py, var, DAGVarType::Capture)?;
}

new_dag
.captured_stretches
.reserve(qc.captured_stretches.len());

for var in qc.captured_stretches {
let name: String = var.getattr("name")?.extract::<String>()?;
new_dag.captured_stretches.insert(name, var.unbind());
}

new_dag
.declared_stretches
.reserve(qc.declared_stretches.len());

for var in qc.declared_stretches {
let name: String = var.getattr("name")?.extract::<String>()?;
new_dag.declared_stretches.insert(name, var.unbind());
}

// Add all the registers
if let Some(qregs) = qc.qregs {
for qreg in qregs.iter() {
Expand Down Expand Up @@ -6460,6 +6683,8 @@ impl DAGCircuit {
input_vars: Vec::new(),
captured_vars: Vec::new(),
declared_vars: Vec::new(),
captured_stretches: Vec::new(),
declared_stretches: Vec::new(),
};
Self::from_circuit(py, circ, copy_op, None, None)
}
Expand Down
5 changes: 4 additions & 1 deletion qiskit/circuit/_classical_resource_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
self,
target_cregs: typing.Iterable[ClassicalRegister],
bit_map: typing.Mapping[Bit, Bit],
var_map: typing.Mapping[expr.Var, expr.Var] | None = None,
var_map: typing.Mapping[expr.Var | expr.Stretch, expr.Var | expr.Stretch] | None = None,
*,
add_register: typing.Callable[[ClassicalRegister], None] | None = None,
):
Expand Down Expand Up @@ -132,6 +132,9 @@ def visit_var(self, node, /):
return expr.Var(self._map_register(node.var), node.type)
return self.var_map.get(node, node)

def visit_stretch(self, node, /):
return self.var_map.get(node, node)

def visit_value(self, node, /):
return expr.Value(node.value, node.type)

Expand Down
Loading