Skip to content

Commit

Permalink
Fix: Use is to avoid obtaining the gil.
Browse files Browse the repository at this point in the history
- Remove `Display` trait.
  • Loading branch information
raynelfss committed Jul 3, 2024
1 parent b6aeba3 commit 48290a2
Showing 1 changed file with 2 additions and 27 deletions.
29 changes: 2 additions & 27 deletions crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,38 +177,13 @@ 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),
(Param::ParameterExpression(one), Param::ParameterExpression(other)) => one.is(other),
(Param::Obj(one), Param::Obj(other)) => one.is(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 48290a2

Please sign in to comment.