Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 11 additions & 7 deletions crates/circuit/src/classical/expr/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

use std::error::Error;

use crate::classical::expr::{Binary, Cast, Index, Stretch, Unary, Value, Var};
use crate::classical::types::Type;
use pyo3::prelude::*;
Expand Down Expand Up @@ -140,16 +142,18 @@ impl Expr {
}

/// Visits all nodes by mutable reference, in a post-order traversal.
pub fn visit_mut<F>(&mut self, mut visitor: F) -> PyResult<()>
pub fn visit_mut<F, E>(&mut self, mut visitor: F) -> Result<(), E>
where
F: FnMut(ExprRefMut) -> PyResult<()>,
F: FnMut(ExprRefMut) -> Result<(), E>,
E: Error,
{
self.visit_mut_impl(&mut visitor)
}

fn visit_mut_impl<F>(&mut self, visitor: &mut F) -> PyResult<()>
fn visit_mut_impl<F, E>(&mut self, visitor: &mut F) -> Result<(), E>
where
F: FnMut(ExprRefMut) -> PyResult<()>,
F: FnMut(ExprRefMut) -> Result<(), E>,
E: Error,
{
match self {
Expr::Unary(u) => u.operand.visit_mut_impl(visitor)?,
Expand Down Expand Up @@ -481,7 +485,7 @@ mod tests {
use crate::classical::types::Type;
use crate::duration::Duration;
use num_bigint::BigUint;
use pyo3::PyResult;
use pyo3::{PyErr, PyResult};
use uuid::Uuid;

#[test]
Expand Down Expand Up @@ -704,7 +708,7 @@ mod tests {

expr.visit_mut(|x| {
assert!(order.next().unwrap()(&x));
Ok(())
Ok::<_, PyErr>(())
})?;

assert!(order.next().is_none());
Expand Down Expand Up @@ -740,7 +744,7 @@ mod tests {
expr.visit_mut(|x| match x {
ExprRefMut::Var(Var::Standalone { name, .. }) => {
*name = "updated".to_string();
Ok(())
Ok::<_, PyErr>(())
}
_ => Ok(()),
})?;
Expand Down
33 changes: 18 additions & 15 deletions crates/circuit/src/variable_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use crate::operations::{Condition, SwitchTarget};
use hashbrown::{HashMap, HashSet};
use num_bigint::BigUint;
use num_traits::Num;
use pyo3::PyResult;
use pyo3::prelude::*;
use std::cell::RefCell;
use std::error::Error;

pub(crate) struct VariableMapper {
target_cregs: Vec<ClassicalRegister>,
Expand Down Expand Up @@ -59,14 +58,15 @@ impl VariableMapper {
/// [DAGCircuit::compose]; nowhere else does this, and in general this would require *far*
/// more complex classical rewriting than Qiskit needs to worry about in the full expression
/// era.
pub fn map_condition<F>(
pub fn map_condition<F, E>(
&self,
condition: &Condition,
allow_reorder: bool,
mut add_register: F,
) -> PyResult<Condition>
) -> Result<Condition, E>
where
F: FnMut(&ClassicalRegister) -> PyResult<()>,
F: FnMut(&ClassicalRegister) -> Result<(), E>,
E: Error,
{
Ok(match condition {
Condition::Bit(target, value) => {
Expand Down Expand Up @@ -99,7 +99,7 @@ impl VariableMapper {
mapped_bits_set == register_set
})
.cloned()
.map(Ok::<_, PyErr>)
.map(Ok::<_, E>)
.unwrap_or_else(|| {
let mapped_theirs =
ClassicalRegister::new_alias(None, mapped_bits_order.clone());
Expand Down Expand Up @@ -138,13 +138,14 @@ impl VariableMapper {

/// Map the real-time variables in a `target` of a `SwitchCaseOp` to the new
/// circuit.
pub fn map_target<F>(
pub fn map_target<F, E>(
&self,
target: &SwitchTarget,
mut add_register: F,
) -> PyResult<SwitchTarget>
) -> Result<SwitchTarget, E>
where
F: FnMut(&ClassicalRegister) -> PyResult<()>,
F: FnMut(&ClassicalRegister) -> Result<(), E>,
E: Error,
{
Ok(match target {
SwitchTarget::Bit(bit) => SwitchTarget::Bit(self.bit_map.get(bit).cloned().unwrap()),
Expand All @@ -156,9 +157,10 @@ impl VariableMapper {
}

/// Map the variables in an [expr::Expr] node to the new circuit.
pub fn map_expr<F>(&self, expr: &expr::Expr, mut add_register: F) -> PyResult<expr::Expr>
pub fn map_expr<F, E>(&self, expr: &expr::Expr, mut add_register: F) -> Result<expr::Expr, E>
where
F: FnMut(&ClassicalRegister) -> PyResult<()>,
F: FnMut(&ClassicalRegister) -> Result<(), E>,
E: Error,
{
let mut mapped = expr.clone();
mapped.visit_mut(|e| match e {
Expand Down Expand Up @@ -194,13 +196,14 @@ impl VariableMapper {

/// Map the target's registers to suitable equivalents in the destination, adding an
/// extra one if there's no exact match."""
fn map_register<F>(
fn map_register<F, E>(
&self,
theirs: &ClassicalRegister,
mut add_register: F,
) -> PyResult<ClassicalRegister>
) -> Result<ClassicalRegister, E>
where
F: FnMut(&ClassicalRegister) -> PyResult<()>,
F: FnMut(&ClassicalRegister) -> Result<(), E>,
E: Error,
{
if let Some(mapped_theirs) = self.register_map.borrow().get(theirs.name()) {
return Ok(mapped_theirs.clone());
Expand All @@ -215,7 +218,7 @@ impl VariableMapper {
mapped_bits == register
})
.cloned()
.map(Ok::<_, PyErr>)
.map(Ok::<_, E>)
.unwrap_or_else(|| {
let mapped_theirs = ClassicalRegister::new_alias(None, mapped_bits.clone());
add_register(&mapped_theirs)?;
Expand Down
Loading