-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathFacts.qs
68 lines (60 loc) · 2.4 KB
/
Facts.qs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import Types.FixedPoint;
import Std.Diagnostics.Fact;
import Std.Arrays.IsEmpty, Std.Arrays.Rest, Std.Arrays.Unzipped;
/// # Summary
/// Asserts that a quantum fixed-point number is
/// initialized to zero.
///
/// # Description
/// This assertion succeeds when all qubits are in state $\ket{0}$,
/// representing that the register encodes the fixed-point number $0.0$.
@Config(Unrestricted)
operation AssertAllZeroFxP(fp : FixedPoint) : Unit {
import Std.Diagnostics.CheckAllZero;
Fact(CheckAllZero(fp::Register), "Quantum fixed-point number was not zero.");
}
/// # Summary
/// Assert that all fixed-point numbers in the provided array
/// have identical point positions and qubit numbers.
///
/// # Input
/// ## fixedPoints
/// Array of quantum fixed-point numbers that will be checked for
/// compatibility (using assertions).
function AssertFormatsAreIdenticalFxP(fixedPoints : FixedPoint[]) : Unit {
import Std.Diagnostics.Fact;
if IsEmpty(fixedPoints) {
return ();
}
let (position, register) = fixedPoints[0]!;
Fact(position > 0, "Point position must be greater than zero.");
let n = Length(register);
for fp in Rest(fixedPoints) {
Fact(fp::IntegerBits == position, "FixedPoint numbers must have identical binary point position.");
Fact(Length(fp::Register) == n, "FixedPoint numbers must have identical number of qubits.");
}
}
/// # Summary
/// Assert that all fixed-point numbers in the provided array
/// have identical point positions when counting from the least-
/// significant bit. I.e., number of bits minus point position must
/// be constant for all fixed-point numbers in the array.
///
/// # Input
/// ## fixedPoints
/// Array of quantum fixed-point numbers that will be checked for
/// compatibility (using assertions).
function AssertPointPositionsIdenticalFxP(fixedPoints : FixedPoint[]) : Unit {
if IsEmpty(fixedPoints) {
return ();
}
let (position, register) = fixedPoints[0]!;
Fact(position > 0, "Point position must be greater than zero.");
let n = Length(register);
for fp in Rest(fixedPoints) {
Fact((Length(fp::Register) - fp::IntegerBits) == (n - position), "FixedPoint numbers must have identical point alignment.");
}
}
export AssertAllZeroFxP, AssertFormatsAreIdenticalFxP, AssertPointPositionsIdenticalFxP;