ket/ir/
qubit.rs

1// SPDX-FileCopyrightText: 2024 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use std::hash::Hash;
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, PartialOrd, Ord, Hash)]
10pub enum LogicalQubit {
11    Main { index: usize },
12    Aux { index: usize },
13}
14
15impl Default for LogicalQubit {
16    fn default() -> Self {
17        LogicalQubit::Main { index: 0 }
18    }
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
22pub struct PhysicalQubit {
23    index: usize,
24}
25
26pub trait Qubit {
27    fn index(&self) -> usize;
28    fn is_aux(&self) -> bool {
29        false
30    }
31}
32
33impl LogicalQubit {
34    pub fn main(index: usize) -> Self {
35        if index >= 1 << 32 {
36            LogicalQubit::Aux {
37                index: (index >> 32) - 1,
38            }
39        } else {
40            LogicalQubit::Main { index }
41        }
42    }
43
44    pub fn aux(index: usize) -> Self {
45        LogicalQubit::Aux { index }
46    }
47
48    pub fn is_main(&self) -> bool {
49        !self.is_aux()
50    }
51}
52
53impl Qubit for LogicalQubit {
54    fn index(&self) -> usize {
55        match self {
56            LogicalQubit::Main { index } | LogicalQubit::Aux { index } => *index,
57        }
58    }
59
60    fn is_aux(&self) -> bool {
61        matches!(self, LogicalQubit::Aux { .. })
62    }
63}
64
65impl Qubit for PhysicalQubit {
66    fn index(&self) -> usize {
67        self.index
68    }
69}
70
71impl PhysicalQubit {
72    pub fn new(index: usize) -> Self {
73        PhysicalQubit { index }
74    }
75}
76
77impl From<usize> for PhysicalQubit {
78    fn from(index: usize) -> Self {
79        PhysicalQubit::new(index)
80    }
81}
82
83impl From<usize> for LogicalQubit {
84    fn from(index: usize) -> Self {
85        LogicalQubit::main(index)
86    }
87}