|
| 1 | +// This code is part of Qiskit. |
| 2 | +// |
| 3 | +// (C) Copyright IBM 2025 |
| 4 | +// |
| 5 | +// This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +// obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// Any modifications or derivative works of this code must retain this |
| 10 | +// copyright notice, and modified files need to carry a notice indicating |
| 11 | +// that they have been altered from the originals. |
| 12 | + |
| 13 | +use hashbrown::HashMap; |
| 14 | + |
| 15 | +use pyo3::exceptions::PyKeyError; |
| 16 | +use pyo3::prelude::*; |
| 17 | +use pyo3::types::PyDict; |
| 18 | +use pyo3::Python; |
| 19 | + |
| 20 | +use crate::TranspilerError; |
| 21 | +use qiskit_circuit::dag_circuit::DAGCircuit; |
| 22 | +use qiskit_circuit::PhysicalQubit; |
| 23 | + |
| 24 | +#[derive(Clone)] |
| 25 | +pub(crate) enum CallbackType { |
| 26 | + Python(PyObject), |
| 27 | + Native(fn(&[f64], &[PhysicalQubit]) -> DAGCircuit), |
| 28 | +} |
| 29 | + |
| 30 | +impl CallbackType { |
| 31 | + fn call(&self, angles: &[f64], qubits: &[PhysicalQubit]) -> PyResult<DAGCircuit> { |
| 32 | + match self { |
| 33 | + Self::Python(inner) => { |
| 34 | + let qubits: Vec<usize> = qubits.iter().map(|x| x.index()).collect(); |
| 35 | + Python::with_gil(|py| inner.bind(py).call1((angles, qubits))?.extract()) |
| 36 | + } |
| 37 | + Self::Native(inner) => Ok(inner(angles, qubits)), |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Registry of Angle Wrapping function |
| 43 | +/// |
| 44 | +/// This class internally contains a mapping of instruction names from a :class:`.Target` to |
| 45 | +/// callbacks for wrapping angles that are outside the specified bounds. |
| 46 | +#[pyclass(module = "qiskit._accelerate.angle_bound_registry")] |
| 47 | +#[pyo3(name = "WrapAngleRegistry")] |
| 48 | +pub struct PyWrapAngleRegistry(WrapAngleRegistry); |
| 49 | + |
| 50 | +#[pymethods] |
| 51 | +impl PyWrapAngleRegistry { |
| 52 | + #[new] |
| 53 | + pub fn new() -> Self { |
| 54 | + PyWrapAngleRegistry(WrapAngleRegistry::new()) |
| 55 | + } |
| 56 | + |
| 57 | + /// Get a replacement circuit for |
| 58 | + pub fn substitute_angle_bounds( |
| 59 | + &self, |
| 60 | + name: &str, |
| 61 | + angles: Vec<f64>, |
| 62 | + qubits: Vec<PhysicalQubit>, |
| 63 | + ) -> PyResult<Option<DAGCircuit>> { |
| 64 | + self.0.substitute_angle_bounds(name, &angles, &qubits) |
| 65 | + } |
| 66 | + |
| 67 | + pub fn add_wrapper(&mut self, name: String, callback: PyObject) { |
| 68 | + self.0.registry.insert(name, CallbackType::Python(callback)); |
| 69 | + } |
| 70 | + |
| 71 | + fn __getstate__(&self, py: Python) -> PyResult<Py<PyDict>> { |
| 72 | + let bounds_dict = PyDict::new(py); |
| 73 | + for (name, bound) in self.get_inner().registry.iter() { |
| 74 | + if let CallbackType::Python(obj) = bound { |
| 75 | + bounds_dict.set_item(name, obj.clone_ref(py))?; |
| 76 | + } else { |
| 77 | + return Err(TranspilerError::new_err( |
| 78 | + "Target contains native code bounds callbacks which can't be serialized", |
| 79 | + )); |
| 80 | + } |
| 81 | + } |
| 82 | + Ok(bounds_dict.unbind()) |
| 83 | + } |
| 84 | + |
| 85 | + fn __setstate__(&mut self, data: Bound<PyDict>) -> PyResult<()> { |
| 86 | + for (key, val) in data.iter() { |
| 87 | + let name: String = key.extract()?; |
| 88 | + self.add_wrapper(name, val.unbind()); |
| 89 | + } |
| 90 | + Ok(()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl PyWrapAngleRegistry { |
| 95 | + pub fn get_inner(&self) -> &WrapAngleRegistry { |
| 96 | + &self.0 |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Store the mapping between gate names and callbacks for wrapping that instructions' angles |
| 101 | +/// which are outside the specified bounds. |
| 102 | +pub struct WrapAngleRegistry { |
| 103 | + registry: HashMap<String, CallbackType>, |
| 104 | +} |
| 105 | + |
| 106 | +impl WrapAngleRegistry { |
| 107 | + pub fn new() -> Self { |
| 108 | + WrapAngleRegistry { |
| 109 | + registry: HashMap::new(), |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + pub fn add_native( |
| 114 | + &mut self, |
| 115 | + name: String, |
| 116 | + callback: fn(&[f64], &[PhysicalQubit]) -> DAGCircuit, |
| 117 | + ) { |
| 118 | + self.registry.insert(name, CallbackType::Native(callback)); |
| 119 | + } |
| 120 | + |
| 121 | + /// Get a replacement circuit for an instruction outside the specified bounds. |
| 122 | + pub fn substitute_angle_bounds( |
| 123 | + &self, |
| 124 | + name: &str, |
| 125 | + angles: &[f64], |
| 126 | + qubits: &[PhysicalQubit], |
| 127 | + ) -> PyResult<Option<DAGCircuit>> { |
| 128 | + if let Some(callback) = self.registry.get(name) { |
| 129 | + Some(callback.call(angles, qubits)).transpose() |
| 130 | + } else { |
| 131 | + Err(PyKeyError::new_err("Name: {} not in registry")) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Default for WrapAngleRegistry { |
| 137 | + fn default() -> Self { |
| 138 | + Self::new() |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl Default for PyWrapAngleRegistry { |
| 143 | + fn default() -> Self { |
| 144 | + Self::new() |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +pub fn angle_bound_mod(m: &Bound<PyModule>) -> PyResult<()> { |
| 149 | + m.add_class::<PyWrapAngleRegistry>()?; |
| 150 | + Ok(()) |
| 151 | +} |
0 commit comments