Skip to content

Commit 55728e2

Browse files
DmitryVasilevskymsoekenDmitry Vasilevsky
authored
Unit test for over-large address in Select/Unselect (#2765)
Unit test for #2762 fix. Address register is allowed to be larger than needed to address data. Fails before the fix, passes after fix. Includes the fix commit. --------- Co-authored-by: Mathias Soeken <mathias.soeken@outlook.com> Co-authored-by: Dmitry Vasilevsky <dmitryv@microsoft.com>
1 parent 0d37829 commit 55728e2

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

library/src/tests/resources/src/select.qs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ namespace Test {
6060
Fact(equal, "Select+Unselect should be equivalent to identity up to global phase.");
6161
}
6262

63+
internal operation TestSelectLongerAddress() : Unit {
64+
use addressRegister = Qubit[5];
65+
use targetRegister = Qubit[4];
66+
67+
// Could be random, but fixed for reproducibility
68+
let data = [
69+
[false, false, false, false],
70+
[false, false, true, false],
71+
[true, true, false, false],
72+
[false, true, false, false],
73+
];
74+
75+
// Select followed by unselect. This should be equivalent to identity.
76+
within {
77+
Select(data, addressRegister, targetRegister);
78+
} apply {
79+
// Do nothing.
80+
}
81+
82+
Fact(CheckAllZero(targetRegister), "Target register must be in |0⟩ state after unlookup.");
83+
Fact(CheckAllZero(addressRegister), "Address register must be in |0⟩ state after unlookup.");
84+
}
85+
6386
internal operation TestSelectFuzz(rounds : Int) : Unit {
6487
for _ in 1..rounds {
6588
let addressBits = DrawRandomInt(2, 6);

library/src/tests/table_lookup.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ fn check_select_phase() {
7070
&Value::Tuple(vec![].into(), None),
7171
);
7272
}
73+
74+
#[test]
75+
fn check_select_longer_address() {
76+
test_expression_with_lib(
77+
"Test.TestSelectLongerAddress()",
78+
SELECT_TEST_LIB,
79+
&Value::Tuple(vec![].into(), None),
80+
);
81+
}

library/std/src/Std/TableLookup.qs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ operation Unlookup(
251251
let phaseData = Padded(-2^addressBitsNeeded, false, phaseData);
252252

253253
// Apply phase lookup to correct phases in the address register
254-
PhaseLookup(address, phaseData);
254+
PhaseLookup(address[...addressBitsNeeded - 1], phaseData);
255255
}
256256
}
257257

0 commit comments

Comments
 (0)