Skip to main content

tket_qsystem/
target.rs

1//! Target platform selection for translating between HUGRs and pytket circuits.
2
3use hugr::HugrView;
4use strum::{EnumIter, EnumString, IntoStaticStr};
5use tket::serialize::pytket::{
6    PytketDecoderConfig, PytketEncoderConfig, default_decoder_config, default_encoder_config,
7};
8
9use crate::extension::qsystem::QSystemPlatform;
10use crate::pytket::{add_qsystem_decoders, qsystem_decoder_config, qsystem_encoder_config};
11
12/// Selects which set of encoder/decoder extensions is used when translating
13/// between HUGRs and pytket circuits.
14///
15/// This is a superset of [`QSystemPlatform`]: in addition to the native
16/// Quantinuum platforms it includes a platform-agnostic [`PlatformTarget::Tket`]
17/// variant that only uses the base `tket` extensions. The
18/// [`qsystem_platform`][PlatformTarget::qsystem_platform] conversion maps each
19/// target onto its [`QSystemPlatform`], returning `None` for the non-native
20/// `Tket` target.
21///
22/// The string representation of each variant (used by the python bindings) is
23/// the lowercase variant name, e.g. `"tket"`, `"sol"`, or `"helios"`.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, EnumIter, EnumString, IntoStaticStr)]
25#[strum(serialize_all = "lowercase")]
26#[non_exhaustive]
27pub enum PlatformTarget {
28    /// Platform-agnostic target using only the base `tket` extensions.
29    #[default]
30    Tket,
31    /// Quantinuum Sol platform.
32    Sol,
33    /// Quantinuum Helios platform.
34    Helios,
35}
36
37impl PlatformTarget {
38    /// The string identifier for this target, as used by the python bindings.
39    pub fn as_str(self) -> &'static str {
40        self.into()
41    }
42
43    /// The native [`QSystemPlatform`] for this target, if any.
44    ///
45    /// A qsystem platform is only relevant when a native target is selected;
46    /// the platform-agnostic [`PlatformTarget::Tket`] returns `None`.
47    pub fn qsystem_platform(self) -> Option<QSystemPlatform> {
48        match self {
49            PlatformTarget::Tket => None,
50            PlatformTarget::Sol => Some(QSystemPlatform::Sol),
51            PlatformTarget::Helios => Some(QSystemPlatform::Helios),
52        }
53    }
54
55    /// Pytket encoder configuration for this target.
56    ///
57    /// Only the extensions supported by the target are registered, so that a
58    /// HUGR is not implicitly rebased into a different gate set when encoded.
59    pub fn encoder_config<H: HugrView>(self) -> PytketEncoderConfig<H> {
60        match self.qsystem_platform() {
61            None => default_encoder_config(),
62            Some(platform) => qsystem_encoder_config(platform),
63        }
64    }
65
66    /// Pytket decoder configuration for this target.
67    ///
68    /// Native targets decode into their platform-specific operations. The
69    /// platform-agnostic [`PlatformTarget::Tket`] decodes into base
70    /// `tket.quantum` operations, with the Helios decoders registered as a
71    /// fallback for pytket commands without a base counterpart (e.g. `ZZPhase`
72    /// or `PhasedX`).
73    pub fn decoder_config(self) -> PytketDecoderConfig {
74        match self.qsystem_platform() {
75            None => {
76                let mut config = default_decoder_config();
77                add_qsystem_decoders(&mut config, QSystemPlatform::Helios);
78                config
79            }
80            Some(platform) => qsystem_decoder_config(platform),
81        }
82    }
83}