Skip to content

Commit

Permalink
Initial: Extend functionality of Param
Browse files Browse the repository at this point in the history
- Implement `PartialEq` using `Python::with_gil()` to compare parameters through Python.
- Add display method for debugging.
  • Loading branch information
raynelfss committed Jul 1, 2024
1 parent 26680dc commit 8598628
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,42 @@ impl ToPyObject for Param {
}
}

impl PartialEq for Param {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Param::Float(s), Param::Float(other)) => s == other,
(Param::ParameterExpression(one), Param::ParameterExpression(other)) => {
compare(one, other)
}
(Param::Obj(one), Param::Obj(other)) => compare(one, other),
_ => false,
}
}
}

impl std::fmt::Display for Param {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display_name: String = Python::with_gil(|py| -> PyResult<String> {
match self {
Param::ParameterExpression(obj) => obj.call_method0(py, "__repr__")?.extract(py),
Param::Float(float_param) => Ok(format!("Parameter({})", float_param)),
Param::Obj(obj) => obj.call_method0(py, "__repr__")?.extract(py),
}
})
.unwrap_or("None".to_owned());
write!(f, "{}", display_name)
}
}

/// Perform comparison between two Python objects
pub fn compare(one: &PyObject, other: &PyObject) -> bool {
Python::with_gil(|py| -> PyResult<bool> {
let other_bound = other.bind(py);
Ok(other_bound.eq(one)? || other_bound.is(one))
})
.unwrap_or_default()
}

#[derive(Clone, Debug, Copy, Eq, PartialEq, Hash)]
#[pyclass(module = "qiskit._accelerate.circuit")]
pub enum StandardGate {
Expand Down

0 comments on commit 8598628

Please sign in to comment.