openqasm 0.1.2

Parser and translator for OpenQASM 2.0
Documentation
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use num::{
    integer::Roots,
    traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub},
    Signed,
};

use super::EvalError;

/// A value of type a + b * pi, with a, b rationals.
///
/// This is the main type used for the evaluation of parameters
/// in translation. This format is useful because it accurately
/// represents the most common values for parameters in quantum
/// programs accurately.
///
/// Since this set is only closed under addition, not multiplication
/// or other operations, approximations are taken when necessary.
/// In general, if every intermediate subexpression of a calculation
/// is representable in this form, then the implementation
/// makes an effort to find the exact value of the calculation.
/// However, this is not guaranteed, and you should only rely
/// on addition, subtraction and scalar multiplication to be exact.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Value {
    /// The rational part of the value.
    pub a: num::Rational64,
    /// The pi-rational part of the value.
    pub b: num::Rational64,
}

impl Value {
    pub const PI: Value = Value {
        a: Value::RAW_ZERO,
        b: num::Rational64::new_raw(1, 1),
    };

    // The value pi/2.
    pub const PI_2: Value = Value {
        a: Value::RAW_ZERO,
        b: num::Rational64::new_raw(1, 2),
    };

    pub const ZERO: Value = Value {
        a: Value::RAW_ZERO,
        b: Value::RAW_ZERO,
    };

    const RAW_ZERO: num::Rational64 = num::Rational64::new_raw(0, 1);
    const PI_SQUARED: num::Rational64 =
        num::Rational64::new_raw(6499908331188584841, 658578405682719844);

    /// Create a `Value` from the given integer.
    pub const fn int(val: i64) -> Value {
        Value {
            a: num::Rational64::new_raw(val, 1),
            b: Value::RAW_ZERO,
        }
    }

    pub fn into_float(self) -> f32 {
        let a = *self.a.numer() as f32 / *self.a.denom() as f32;
        let b = *self.b.numer() as f32 / *self.b.denom() as f32;
        a + 3.14159265 * b
    }

    /// Attempt to convert a floating point number into
    /// a `Value`. This will fail if the number is out
    /// of range of a 64-bit rational. No attempt is made
    /// to recognize fractions of pi.
    pub fn from_float(val: f32) -> Option<Value> {
        Some(Value {
            a: num::Rational64::approximate_float(val)?,
            b: Value::RAW_ZERO,
        })
    }

    fn to_rat(self) -> Option<num::Rational64> {
        if self.b == Value::RAW_ZERO {
            Some(self.a)
        } else {
            None
        }
    }

    fn to_pi_rat(self) -> Option<num::Rational64> {
        if self.a == Value::RAW_ZERO {
            Some(self.b)
        } else {
            None
        }
    }
}

impl std::fmt::Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.b == Value::RAW_ZERO {
            write!(f, "{}", self.a)
        } else if self.a == Value::RAW_ZERO {
            if *self.b.numer() == 1 {
                write!(f, "Ï€/{}", self.b.denom())
            } else {
                write!(f, "{}Ï€", self.b)
            }
        } else {
            if *self.b.numer() == 1 {
                write!(f, "{}+Ï€/{}", self.a, self.b.denom())
            } else {
                write!(f, "{}+{}Ï€", self.a, self.b)
            }
        }
    }
}

impl Value {
    pub(super) fn add_internal(self, other: Value) -> Result<Value, EvalError> {
        Ok(Value {
            a: self
                .a
                .checked_add(&other.a)
                .ok_or(EvalError::OverflowError)?,
            b: self
                .b
                .checked_add(&other.b)
                .ok_or(EvalError::OverflowError)?,
        })
    }

    pub(super) fn sub_internal(self, other: Value) -> Result<Value, EvalError> {
        Ok(Value {
            a: self
                .a
                .checked_sub(&other.a)
                .ok_or(EvalError::OverflowError)?,
            b: self
                .b
                .checked_sub(&other.b)
                .ok_or(EvalError::OverflowError)?,
        })
    }

    pub(super) fn mul_internal(self, other: Value) -> Result<Value, EvalError> {
        let c = self
            .b
            .checked_mul(&other.b)
            .ok_or(EvalError::OverflowError)?
            .checked_mul(&Value::PI_SQUARED)
            .ok_or(EvalError::OverflowError)?;

        Ok(Value {
            a: self
                .a
                .checked_mul(&other.a)
                .ok_or(EvalError::OverflowError)?
                .checked_add(&c)
                .ok_or(EvalError::OverflowError)?,
            b: self
                .b
                .checked_mul(&other.a)
                .ok_or(EvalError::OverflowError)?
                .checked_add(
                    &self
                        .a
                        .checked_mul(&other.b)
                        .ok_or(EvalError::OverflowError)?,
                )
                .ok_or(EvalError::OverflowError)?,
        })
    }

    pub(super) fn div_internal(self, other: Value) -> Result<Value, EvalError> {
        if other == Value::ZERO {
            Err(EvalError::DivideByZero)
        } else if let Some(rat) = other.to_rat() {
            // value / rational is value:
            Ok(Value {
                a: self.a.checked_div(&rat).ok_or(EvalError::OverflowError)?,
                b: self.b.checked_div(&rat).ok_or(EvalError::OverflowError)?,
            })
        } else if let Some(orat) = other.to_pi_rat() {
            // pi-rational / pi-rational is rational:
            if let Some(srat) = self.to_pi_rat() {
                Ok(Value {
                    a: srat.checked_div(&orat).ok_or(EvalError::OverflowError)?,
                    b: Value::RAW_ZERO,
                })
            } else {
                let f = self.into_float() / other.into_float();
                Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
            }
        } else {
            // The only other rational case is when the numerator
            // and denominator are a rational multiple of each other.
            // (and this must be the case as pi is transcendental).
            let aa = self
                .a
                .checked_div(&other.a)
                .ok_or(EvalError::OverflowError)?;
            let bb = self
                .b
                .checked_div(&other.b)
                .ok_or(EvalError::OverflowError)?;
            if aa == bb {
                Ok(Value {
                    a: aa,
                    b: Value::RAW_ZERO,
                })
            } else {
                let f = self.into_float() / other.into_float();
                Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
            }
        }
    }

    /// Attempt to divide this value by another,
    /// failing if the divisor is zero, or the result
    /// is out of range.
    pub fn checked_div(self, other: Value) -> Option<Value> {
        self.div_internal(other).ok()
    }
}

impl std::ops::Div<Value> for Value {
    type Output = Value;

    fn div(self, other: Value) -> Value {
        self.div_internal(other).unwrap()
    }
}

impl Value {
    fn int_root(val: u64, root: u64) -> Option<u64> {
        if let Ok(root) = u32::try_from(root) {
            let ans = val.nth_root(root);
            if ans.checked_pow(root) == Some(val) {
                Some(ans)
            } else {
                None
            }
        } else {
            None
        }
    }

    fn rat_root(val: num::Rational64, root: u64) -> Option<num::Rational64> {
        // A square root is rational iff its numerator and denominator
        // are both squares.
        if val.is_negative() {
            return None;
        }

        let a = Value::int_root(*val.numer() as u64, root)?;
        let b = Value::int_root(*val.denom() as u64, root)?;
        Some(num::Rational64::new(a as i64, b as i64))
    }

    fn extract_root(val: num::Rational64, root: u64) -> Result<num::Rational64, EvalError> {
        if root == 1 {
            return Ok(val);
        } else if val == 1.into() {
            return Ok(val);
        }

        if let Some(root) = Value::rat_root(val, root) {
            Ok(root)
        } else {
            let f = Value {
                a: val,
                b: Value::RAW_ZERO,
            }
            .into_float();
            let fp = f.powf(1.0 / (root as f32));
            Value::from_float(fp)
                .map(|v| v.a)
                .ok_or(EvalError::ApproximateFail(fp))
        }
    }

    pub(super) fn pow_internal(self, other: Value) -> Result<Value, EvalError> {
        match (self.to_rat(), other.to_rat()) {
            // The only rational case of exponentiation is rational
            // to a rational power, and even then not always.
            (Some(srat), Some(orat)) => {
                let exp = orat.numer().abs() as usize;
                let recip = orat.numer().is_negative();
                let ap = num::traits::checked_pow(srat, exp).ok_or(EvalError::OverflowError)?;
                let aa = Value::extract_root(ap, *orat.denom() as u64)?;

                if recip {
                    if aa == Value::RAW_ZERO {
                        return Err(EvalError::DivideByZero);
                    } else {
                        return Ok(Value {
                            a: aa.recip(),
                            b: Value::RAW_ZERO,
                        });
                    }
                } else {
                    return Ok(Value {
                        a: aa,
                        b: Value::RAW_ZERO,
                    });
                }
            }
            _ => (),
        }

        let f = self.into_float().powf(other.into_float());
        Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
    }

    /// Attempt to find a power of this value, failing if the result is out of range.
    pub fn checked_pow(self, other: Value) -> Option<Value> {
        self.pow_internal(other).ok()
    }
}

impl Value {
    pub(super) fn sqrt_internal(self) -> Result<Value, EvalError> {
        if let Some(srat) = self.to_rat() {
            if let Some(root) = Value::rat_root(srat, 2) {
                return Ok(Value {
                    a: root,
                    b: Value::RAW_ZERO,
                });
            }
        }

        let f = self.into_float().sqrt();
        Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
    }

    pub fn checked_sqrt(self) -> Option<Value> {
        self.sqrt_internal().ok()
    }

    pub(super) fn neg_internal(self) -> Result<Value, EvalError> {
        self.mul_internal(Value::int(-1))
    }

    /// Negate this value.
    pub fn checked_neg(self) -> Option<Value> {
        self.neg_internal().ok()
    }

    pub(super) fn sin_internal(self) -> Result<Value, EvalError> {
        if let Some(srat) = self.to_pi_rat() {
            let mut f = srat
                .checked_div(&2.into())
                .ok_or(EvalError::OverflowError)?
                .fract()
                * 2;
            if f.is_negative() {
                f += 2;
            }

            // By Niven's theorem, the only rational values of sin for
            // 0 <= theta <= pi/2 are sin(0) = 0, sin(pi/6) = 1/2 and sin(pi/2) = 1.
            if f == num::Rational64::new(0, 1) {
                return Ok(Value {
                    a: Value::RAW_ZERO,
                    b: Value::RAW_ZERO,
                });
            } else if f == num::Rational64::new(1, 6) || f == num::Rational64::new(5, 6) {
                return Ok(Value {
                    a: num::Rational64::new(1, 2),
                    b: Value::RAW_ZERO,
                });
            } else if f == num::Rational64::new(1, 2) {
                return Ok(Value {
                    a: num::Rational64::new(1, 1),
                    b: Value::RAW_ZERO,
                });
            } else if f == num::Rational64::new(7, 6) || f == num::Rational64::new(11, 6) {
                return Ok(Value {
                    a: num::Rational64::new(-1, 2),
                    b: Value::RAW_ZERO,
                });
            } else if f == num::Rational64::new(3, 2) {
                return Ok(Value {
                    a: num::Rational64::new(-1, 1),
                    b: Value::RAW_ZERO,
                });
            }
        }

        let f = self.into_float().sin();
        Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
    }

    pub fn checked_sin(self) -> Option<Value> {
        self.sin_internal().ok()
    }

    pub(super) fn cos_internal(self) -> Result<Value, EvalError> {
        self.add_internal(Value::PI_2)?.sin_internal()
    }

    pub fn checked_cos(self) -> Option<Value> {
        self.cos_internal().ok()
    }

    pub(super) fn tan_internal(self) -> Result<Value, EvalError> {
        self.sin_internal()?.div_internal(self.cos_internal()?)
    }

    pub fn checked_tan(self) -> Option<Value> {
        self.tan_internal().ok()
    }

    pub(super) fn exp_internal(self) -> Result<Value, EvalError> {
        let f = self.into_float().exp();
        Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
    }

    pub fn checked_exp(self) -> Option<Value> {
        self.exp_internal().ok()
    }

    pub(super) fn ln_internal(self) -> Result<Value, EvalError> {
        let f = self.into_float().ln();
        Value::from_float(f).ok_or(EvalError::ApproximateFail(f))
    }

    pub fn checked_ln(self) -> Option<Value> {
        self.ln_internal().ok()
    }
}

#[test]
fn int_root() {
    use rand::Rng;
    let mut rng = rand::thread_rng();

    for _ in 0..10000 {
        for k in 2u64..10 {
            let a: u32 = rng.gen();
            let ap = if let Some(ap) = (a as u64 + 1).checked_pow(k as u32) {
                ap
            } else {
                continue;
            };
            let val1 = Value::int_root(ap, k);
            let val2 = Value::int_root(ap + 1, k);

            assert_eq!(val1, Some(a as u64 + 1));
            assert_eq!(val2, None);
        }
    }
}