|
| 1 | +//! Passes that call to tket1-passes using the tket-c-api. |
| 2 | +
|
| 3 | +use rayon::iter::ParallelIterator; |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use pyo3::prelude::*; |
| 7 | +use tket::serialize::pytket::{EncodeOptions, EncodedCircuit}; |
| 8 | +use tket::Circuit; |
| 9 | +use tket_qsystem::pytket::{qsystem_decoder_config, qsystem_encoder_config}; |
| 10 | + |
| 11 | +use crate::circuit::try_with_circ; |
| 12 | +use crate::utils::{create_py_exception, ConvertPyErr}; |
| 13 | + |
| 14 | +/// An optimisation pass that applies a number of rewrite rules for simplifying |
| 15 | +/// Clifford gate sequences, similar to Duncan & Fagan |
| 16 | +/// (https://arxiv.org/abs/1901.10114). Produces a circuit comprising TK1 gates |
| 17 | +/// and the two-qubit gate specified as the target. |
| 18 | +/// |
| 19 | +/// Parameters: |
| 20 | +/// - allow_swaps: whether the rewriting may introduce implicit wire swaps. |
| 21 | +/// - traverse_subcircuits: Whether to apply the optimisation to nested |
| 22 | +/// subregions in the hugr too, rather than just the top-level region. |
| 23 | +// |
| 24 | +// TODO: We should also expose `target_gate` here, but the most appropriate |
| 25 | +// parameter type [`crate::ops::PyTketOp`] doesn't include `TK2` -.- |
| 26 | +#[pyfunction] |
| 27 | +#[pyo3(signature = (circ, *, allow_swaps = true, traverse_subcircuits = true))] |
| 28 | +pub fn clifford_simp<'py>( |
| 29 | + circ: &Bound<'py, PyAny>, |
| 30 | + allow_swaps: bool, |
| 31 | + traverse_subcircuits: bool, |
| 32 | +) -> PyResult<Bound<'py, PyAny>> { |
| 33 | + let py = circ.py(); |
| 34 | + |
| 35 | + try_with_circ(circ, |circ, typ| { |
| 36 | + let circ = run_tket1_pass(circ, traverse_subcircuits, |tk1_circ| { |
| 37 | + tk1_circ.clifford_simp(tket_json_rs::OpType::CX, allow_swaps) |
| 38 | + })?; |
| 39 | + |
| 40 | + let circ = typ.convert(py, circ)?; |
| 41 | + PyResult::Ok(circ) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +/// Squash single qubit gates into PhasedX and Rz gates. Also remove identity |
| 46 | +/// gates. Commute Rz gates to the back if possible. |
| 47 | +/// |
| 48 | +/// Parameters: |
| 49 | +/// - traverse_subcircuits: Whether to apply the optimisation to nested |
| 50 | +/// subregions in the hugr too, rather than just the top-level region. |
| 51 | +#[pyfunction] |
| 52 | +#[pyo3(signature = (circ, *, traverse_subcircuits = true))] |
| 53 | +pub fn squash_phasedx_rz<'py>( |
| 54 | + circ: &Bound<'py, PyAny>, |
| 55 | + traverse_subcircuits: bool, |
| 56 | +) -> PyResult<Bound<'py, PyAny>> { |
| 57 | + let py = circ.py(); |
| 58 | + |
| 59 | + try_with_circ(circ, |circ, typ| { |
| 60 | + let circ = run_tket1_pass(circ, traverse_subcircuits, |tk1_circ| { |
| 61 | + tk1_circ.squash_phasedx_rz() |
| 62 | + })?; |
| 63 | + |
| 64 | + let circ = typ.convert(py, circ)?; |
| 65 | + PyResult::Ok(circ) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +fn run_tket1_pass<F>(mut circ: Circuit, traverse_subcircuits: bool, pass: F) -> PyResult<Circuit> |
| 70 | +where |
| 71 | + F: Fn(&mut tket1_passes::Tket1Circuit) -> Result<(), tket1_passes::PassError> + Send + Sync, |
| 72 | +{ |
| 73 | + let mut encoded_circ = EncodedCircuit::new( |
| 74 | + &circ, |
| 75 | + EncodeOptions::new() |
| 76 | + .with_config(qsystem_encoder_config()) |
| 77 | + .with_subcircuits(traverse_subcircuits), |
| 78 | + ) |
| 79 | + .convert_pyerrs()?; |
| 80 | + |
| 81 | + encoded_circ |
| 82 | + .par_iter_mut() |
| 83 | + .try_for_each(|(_, circ)| -> Result<(), tket1_passes::PassError> { |
| 84 | + let mut tk1_circ = tket1_passes::Tket1Circuit::from_serial_circuit(circ)?; |
| 85 | + pass(&mut tk1_circ)?; |
| 86 | + *circ = tk1_circ.to_serial_circuit()?; |
| 87 | + Ok(()) |
| 88 | + }) |
| 89 | + .convert_pyerrs()?; |
| 90 | + |
| 91 | + encoded_circ |
| 92 | + .reassemble_inplace(circ.hugr_mut(), Some(Arc::new(qsystem_decoder_config()))) |
| 93 | + .convert_pyerrs()?; |
| 94 | + |
| 95 | + Ok(circ) |
| 96 | +} |
| 97 | + |
| 98 | +create_py_exception!( |
| 99 | + tket1_passes::PassError, |
| 100 | + PytketPassError, |
| 101 | + "Error from a call to tket-c-api" |
| 102 | +); |
0 commit comments