quil_rs/instruction/
waveform.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use indexmap::IndexMap;

use crate::{
    expression::Expression,
    quil::{write_join_quil, Quil, INDENT},
};

use super::write_parameter_string;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Waveform {
    pub matrix: Vec<Expression>,
    pub parameters: Vec<String>,
}

impl Waveform {
    pub fn new(matrix: Vec<Expression>, parameters: Vec<String>) -> Self {
        Self { matrix, parameters }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct WaveformDefinition {
    pub name: String,
    pub definition: Waveform,
}

impl WaveformDefinition {
    pub fn new(name: String, definition: Waveform) -> Self {
        Self { name, definition }
    }
}

impl Quil for WaveformDefinition {
    fn write(
        &self,
        f: &mut impl std::fmt::Write,
        fall_back_to_debug: bool,
    ) -> crate::quil::ToQuilResult<()> {
        write!(f, "DEFWAVEFORM {}", self.name)?;
        write_parameter_string(f, &self.definition.parameters)?;
        write!(f, ":\n{INDENT}")?;
        write_join_quil(f, fall_back_to_debug, &self.definition.matrix, ", ", "")
            .map_err(Into::into)
    }
}

#[cfg(test)]
mod test_waveform_definition {
    use super::{Waveform, WaveformDefinition};
    use crate::expression::Expression;
    use crate::quil::Quil;
    use crate::real;

    use insta::assert_snapshot;
    use rstest::rstest;

    #[rstest]
    #[case(
        "Simple WaveformDefinition",
        WaveformDefinition {
            name: "WF".to_string(),
            definition: Waveform {
                matrix: vec![
                    Expression::Number(real!(0.0)),
                    Expression::Number(real!(0.0)),
                    Expression::Number(real!(0.00027685415721916584))
                ],
                parameters: vec!["theta".to_string()]
            }
        }
    )]
    fn test_display(#[case] description: &str, #[case] waveform_def: WaveformDefinition) {
        insta::with_settings!({
            snapshot_suffix => description,
        }, {
            assert_snapshot!(waveform_def.to_quil_or_debug())
        })
    }
}

pub type WaveformParameters = IndexMap<String, Expression>;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WaveformInvocation {
    pub name: String,
    pub parameters: WaveformParameters,
}

impl WaveformInvocation {
    pub fn new(name: String, parameters: WaveformParameters) -> Self {
        Self { name, parameters }
    }
}

impl Quil for WaveformInvocation {
    fn write(
        &self,
        f: &mut impl std::fmt::Write,
        _fall_back_to_debug: bool,
    ) -> crate::quil::ToQuilResult<()> {
        let mut key_value_pairs = self
            .parameters
            .iter()
            .collect::<Vec<(&String, &Expression)>>();

        key_value_pairs.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));

        if key_value_pairs.is_empty() {
            write!(f, "{}", self.name,)?;
        } else {
            write!(
                f,
                "{}({})",
                self.name,
                key_value_pairs
                    .iter()
                    .map(|(k, v)| format!("{k}: {}", v.to_quil_or_debug()))
                    .collect::<Vec<String>>()
                    .join(", ")
            )?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod waveform_invocation_tests {
    use super::WaveformParameters;

    use crate::{instruction::WaveformInvocation, quil::Quil};

    #[test]
    fn format_no_parameters() {
        let invocation = WaveformInvocation {
            name: "CZ".into(),
            parameters: WaveformParameters::new(),
        };
        assert_eq!(invocation.to_quil_or_debug(), "CZ".to_string());
    }
}