quil_rs/
lib.rs

1// Copyright 2021 Rigetti Computing
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Welcome to the Rust implementation of the
16//! [Quil quantum programming language](https://github.com/quil-lang/quil).
17//!
18//! Within this crate you'll find:
19//!
20//! * Builder utilities for Quil [programs], [instructions], and [expressions]
21//! * A [parser] and [serializer] for converting Quil to and from text strings
22//! * A [constructor for timing graphs], for understanding and debugging Quil-T
23//!   pulse control programs
24//!
25//! This crate is still early in its development and does not fully support all
26//! Quil features, nor claim a stable API. Prior to `v1.0`, minor-version changes
27//! are considered breaking changes. Please pin your versions when needed, and
28//! closely follow the
29//! [changelog](https://github.com/rigetti/quil-rust/releases) when upgrading.
30//!
31//! [constructor for timing graphs]: crate::program::graph::ScheduledProgram#method.get_dot_format
32//! [expressions]: crate::expression::Expression
33//! [instructions]: crate::instruction::Instruction
34//! [parser]: crate::program::Program#method.from_str
35//! [programs]: crate::program::Program
36//! [serializer]: crate::program::Program#method.to_string
37
38pub mod expression;
39mod floating_point_eq;
40pub mod instruction;
41mod macros;
42pub(crate) mod parser;
43pub mod program;
44pub mod quil;
45pub mod reserved;
46pub mod units;
47pub mod validation;
48pub mod waveform;
49
50pub use program::Program;
51
52#[cfg(feature = "python")]
53pub mod quilpy;
54
55/// Implement `__new__` and `__getnewargs__` for the common cases.
56///
57/// # Purpose
58///
59/// The `__getnewargs__` method is used by Python's `copy` and `pickle` modules,
60/// hence why the name: this enables a type to be (un)pickled and (deep)copied.
61///
62/// # Usage
63///
64/// The implementation generated simply `clone`s all the fields names given to the macro,
65/// though you can customize the body of the `__new__` implementation, if desired.
66/// You can choose the name of the `__new__` method and its visibility,
67/// so the easiest way to use this macro with a `#[pyclass]` that you also use in Rust is like:
68///
69/// ```ignore
70/// pub struct Foo {
71///     bar: i32,
72///     baz: String,
73/// }
74///
75/// pickleable_new! {
76///     impl Foo {
77///         pub fn new(bar: i32, baz: String);
78///     }
79/// }
80/// ```
81///
82/// That will expand to the default body, namely:
83///
84/// ```ignore
85/// #[pymethods]
86/// impl Foo {
87///     pub fn new(bar: i32, baz: String) -> Self {
88///         Self {
89///             bar,
90///             baz,
91///         }
92///     }
93///
94///     fn __getnewargs__(&self) -> (i32, String) {
95///         (
96///             self.bar.clone(),
97///             self.baz.clone(),
98///         )
99///     }
100/// }
101/// ```
102///
103/// You can supply a body for the constructor if you need other logic, such as validation;
104/// however, if you want to accept different parameter names or type than the struct's fields,
105/// you'll have to tell the macro the struct's field names and return types.
106///
107/// ```ignore
108/// pickleable_new! {
109///     impl Foo {
110///         pub fn new(bar: i32, baz: &str) -> Self {
111///             Self {
112///                 bar,
113///                 baz: baz.to_string(),
114///             }
115///         }
116///     }
117/// }
118/// ```
119///
120/// That will generate the same `__getnewargs__` implementation, but use your given `new` body.
121///
122/// # Note
123///
124/// This macro expands to two `impl` blocks: one for if the `python` feature is enabled,
125/// which includes the `#[pymethods]` block with its `#[new]` and `__getnewargs__` methods,
126/// and a second that for when the `python` feature is not enabled,
127/// which simply implements the constructor.
128macro_rules! pickleable_new {
129    // Default implementation: just list the fields and types, and this will do the rest.
130    (
131        $(#[$impl_meta:meta])*
132        impl $name:ident {
133            $(#[$meta:meta])*
134            $pub:vis fn $new:ident( $($field:ident: $field_type:ty$(,)?)*);
135        }
136    ) => {
137        pickleable_new! {
138            $(#[$impl_meta])*
139            impl $name {
140                $(#[$meta])*
141                $pub fn $new($($field: $field_type,)*) -> $name {
142                    Self {
143                        $($field,)*
144                    }
145                }
146            }
147        }
148    };
149
150    // If __new__ needs actual logic, you can supply a body.
151    (
152        $(#[$impl_meta:meta])*
153        impl $name:ident {
154            $(#[$meta:meta])*
155            $pub:vis fn $new:ident( $($field:ident: $field_type:ty$(,)?)*) -> $ret:ty {
156                $($body:tt)+
157            }
158        }
159    ) => {
160        $(#[$impl_meta])*
161        #[cfg(feature = "python")]
162        #[cfg_attr(feature = "stubs", pyo3_stub_gen::derive::gen_stub_pymethods)]
163        #[pyo3::pymethods]
164        impl $name {
165            $(#[$meta])*
166            #[new]
167            $pub fn $new($($field: $field_type,)*) -> $ret {
168                $($body)+
169            }
170
171            fn __getnewargs__(&self) -> ($($field_type,)*) {
172                (
173                    $((self.$field).clone(),)*
174                )
175            }
176        }
177
178        $(#[$impl_meta])*
179        #[cfg(not(feature = "python"))]
180        impl $name {
181            $(#[$meta])*
182            $pub fn $new($($field: $field_type,)*) -> $ret {
183                $($body)+
184            }
185        }
186    };
187}
188
189pub(crate) use pickleable_new;