tket_json_rs/
register.rs

1//! Basic types for quantum and classical registers.
2
3use derive_more::{Display, From};
4#[cfg(feature = "schemars")]
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// An identifier for a bit or qubit in a register.
9///
10/// See [`Qubit`] and [`Bit`] for more specific types.
11///
12/// The first element is the name of the register, and the second element is a
13/// multi-dimensional index into the register.
14#[cfg_attr(feature = "schemars", derive(JsonSchema))]
15#[derive(Display, Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash)]
16#[display("{_0}[{}]", _1.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", "))]
17pub struct ElementId(pub String, pub Vec<i64>);
18
19/// An identifier for a qubit in a register.
20///
21/// See [`ElementId`] for the concrete generic index.
22#[cfg_attr(feature = "schemars", derive(JsonSchema))]
23#[derive(Display, Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash, From)]
24#[display("{id}")]
25#[serde(transparent)]
26pub struct Qubit {
27    /// Identifier for the qubit.
28    pub id: ElementId,
29}
30
31/// An identifier for a bit in a [`BitRegister`].
32///
33/// See [`ElementId`] for the concrete generic index.
34#[cfg_attr(feature = "schemars", derive(JsonSchema))]
35#[derive(Display, Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash, From)]
36#[display("{id}")]
37#[serde(transparent)]
38pub struct Bit {
39    /// Identifier for the bit.
40    pub id: ElementId,
41}
42
43/// A classical bit register.
44#[cfg_attr(feature = "schemars", derive(JsonSchema))]
45#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash)]
46pub struct BitRegister {
47    /// Name of the bit register.
48    pub name: String,
49    /// Number of bits in the register.
50    pub size: u32,
51}
52
53/// A vector of booleans.
54#[cfg_attr(feature = "schemars", derive(JsonSchema))]
55#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash)]
56#[serde(transparent)]
57pub struct Bitstring {
58    /// Vector of booleans.
59    pub vec: Vec<bool>,
60}
61
62impl From<Qubit> for ElementId {
63    fn from(q: Qubit) -> Self {
64        q.id
65    }
66}
67
68impl From<Bit> for ElementId {
69    fn from(b: Bit) -> Self {
70        b.id
71    }
72}