Skip to content

Commit f7d15c5

Browse files
authored
Fixed angle adjustment size bug (#2845)
There is a subtle bug in `AdjustAngleSize`. When creating the new `Angle` struct in both the truncation and padding branches, the codeuses the original size instead of `new_size`. This means the `Value` is scaled to the new precision, but the `Size` metadata remains incorrect. This then cascades and impacts other functions that make use of `Size`, e.g. `AngleAsResultArrayBE`. I couldn't find any tests for that logic so I added a test with a new mod. The test is also a repro and fails without the change.
1 parent 2987832 commit f7d15c5

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

library/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod intrinsic;
1111
mod logical;
1212
mod math;
1313
mod measurement;
14+
mod openqasm;
1415
mod state_preparation;
1516
mod table_lookup;
1617

library/src/tests/openqasm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
mod angle;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
use crate::tests::test_expression;
5+
use indoc::indoc;
6+
use qsc::interpret::Value;
7+
8+
#[test]
9+
fn check_adjust_angle_size_no_truncation_increases_size() {
10+
test_expression(
11+
indoc! {r#"{
12+
import Std.OpenQASM.Angle.*;
13+
let angle = IntAsAngle(100, 16);
14+
let adjusted_angle = AdjustAngleSizeNoTruncation(angle, 32);
15+
adjusted_angle.Size
16+
}"#},
17+
&Value::Int(32),
18+
);
19+
}

library/std/src/Std/OpenQASM/Angle.qs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ function AdjustAngleSize(angle : Angle, new_size : Int, truncate : Bool) : Angle
126126
upper_bits
127127
}
128128
};
129-
new Angle { Value = value, Size = size }
129+
new Angle { Value = value, Size = new_size }
130130
} elif new_size == size {
131131
// Same size, no change
132132
angle
133133
} else {
134134
// Padding with zeros
135135
let value = value <<< (new_size - size);
136-
new Angle { Value = value, Size = size }
136+
new Angle { Value = value, Size = new_size }
137137
}
138138
}
139139

0 commit comments

Comments
 (0)