Skip to content

Commit 0bb43cc

Browse files
authored
Remove Python dependency for creating a blank dag (#14069)
* Remove Python dependency for creating a blank dag This commit removes the py token argument for the dag constructor which wasn't strictly needed. The only pieces of the dagcircuit struct which need python is the metadata atribute (which is an `Option<PyDict>`) and the var tracking sets. This commit rearranges the constructor so it doesn't actively need python. This is achieved by moving the var tracking sets inside a once lock which is only initialized in a context where vars are being used. The metadata is changed to None by default. The other use case was the control flow module which wasn't strictly required and is removed in this PR and using the import once cell mechanism that we use for this everywhere else. * Remove conditional import of OnceLock since it's always used * Update rust tests * Remove _bound suffix from method names * Add docstring to VarsByType
1 parent 15b1e5d commit 0bb43cc

4 files changed

Lines changed: 166 additions & 161 deletions

File tree

crates/accelerate/src/basis/basis_translator/compose_transforms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(super) fn compose_transforms<'a>(
5151
.call1((&gate_name, num_params))?
5252
.extract()?;
5353

54-
let mut dag = DAGCircuit::new(py)?;
54+
let mut dag = DAGCircuit::new()?;
5555
// Create the mock gate and add to the circuit, use Python for this.
5656
let qubits = QuantumRegister::new_owning("q".to_string(), gate_num_qubits);
5757
dag.add_qreg(qubits)?;

crates/accelerate/src/gate_direction.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn apply_operation_back(
435435
}
436436

437437
fn cx_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
438-
let new_dag = &mut DAGCircuit::new(py)?;
438+
let new_dag = &mut DAGCircuit::new()?;
439439
let qargs = add_qreg(new_dag, 2)?;
440440
let qargs = qargs.as_slice();
441441

@@ -449,7 +449,7 @@ fn cx_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
449449
}
450450

451451
fn ecr_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
452-
let new_dag = &mut DAGCircuit::new(py)?;
452+
let new_dag = &mut DAGCircuit::new()?;
453453
new_dag.add_global_phase(&Param::Float(-PI / 2.0))?;
454454
let qargs = add_qreg(new_dag, 2)?;
455455
let qargs = qargs.as_slice();
@@ -468,7 +468,7 @@ fn ecr_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
468468
}
469469

470470
fn cz_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
471-
let new_dag = &mut DAGCircuit::new(py)?;
471+
let new_dag = &mut DAGCircuit::new()?;
472472
let qargs = add_qreg(new_dag, 2)?;
473473
let qargs = qargs.as_slice();
474474

@@ -478,7 +478,7 @@ fn cz_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
478478
}
479479

480480
fn swap_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
481-
let new_dag = &mut DAGCircuit::new(py)?;
481+
let new_dag = &mut DAGCircuit::new()?;
482482
let qargs = add_qreg(new_dag, 2)?;
483483
let qargs = qargs.as_slice();
484484

@@ -488,7 +488,7 @@ fn swap_replacement_dag(py: Python) -> PyResult<DAGCircuit> {
488488
}
489489

490490
fn rxx_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
491-
let new_dag = &mut DAGCircuit::new(py)?;
491+
let new_dag = &mut DAGCircuit::new()?;
492492
let qargs = add_qreg(new_dag, 2)?;
493493
let qargs = qargs.as_slice();
494494

@@ -504,7 +504,7 @@ fn rxx_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
504504
}
505505

506506
fn ryy_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
507-
let new_dag = &mut DAGCircuit::new(py)?;
507+
let new_dag = &mut DAGCircuit::new()?;
508508
let qargs = add_qreg(new_dag, 2)?;
509509
let qargs = qargs.as_slice();
510510

@@ -520,7 +520,7 @@ fn ryy_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
520520
}
521521

522522
fn rzz_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
523-
let new_dag = &mut DAGCircuit::new(py)?;
523+
let new_dag = &mut DAGCircuit::new()?;
524524
let qargs = add_qreg(new_dag, 2)?;
525525
let qargs = qargs.as_slice();
526526

@@ -536,7 +536,7 @@ fn rzz_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
536536
}
537537

538538
fn rzx_replacement_dag(py: Python, param: &[Param]) -> PyResult<DAGCircuit> {
539-
let new_dag = &mut DAGCircuit::new(py)?;
539+
let new_dag = &mut DAGCircuit::new()?;
540540
let qargs = add_qreg(new_dag, 2)?;
541541
let qargs = qargs.as_slice();
542542

0 commit comments

Comments
 (0)