-
Notifications
You must be signed in to change notification settings - Fork 17
/
SelectTest.qs
91 lines (80 loc) · 3.44 KB
/
SelectTest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
namespace Tests {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Logical;
open QsharpCommunity.Qram;
// Basic lookup with all addresses checked, all valid tradeoff parameters tested
// for single-bit data.
@Test("ToffoliSimulator")
operation SelectOracleSingleBitSingleLookupMatchResults() : Unit {
let data = SingleBitData();
let largestAddress = Max(Mapped(AddressLookup, data::DataSet));
for i in 0..largestAddress {
for control in [true, false] {
CreateQueryMeasureOneAddressSelect(data, i, control);
}
}
}
// Basic lookup with all addresses checked, all valid tradeoff parameters tested
// for multi-bit data.
@Test("ToffoliSimulator")
operation SelectOracleMultiBitSingleLookupMatchResults() : Unit {
let data = MultiBitData();
let largestAddress = Max(Mapped(AddressLookup, data::DataSet));
for i in 0..largestAddress {
for control in [true, false] {
CreateQueryMeasureOneAddressSelect(data, i, control);
}
}
}
internal operation CreateQueryMeasureOneAddressSelect(
data : MemoryBank,
queryAddress : Int,
control : Bool
)
: Unit {
// Get the data value you expect to find at queryAddress
let expectedValue = DataAtAddress(data, queryAddress);
let emptyValue = ConstantArray(data::DataSize, false);
// Create the new Qrom oracle
let memory = SelectQromOracle(data::DataSet);
use (addressRegister, targetRegister) =
(Qubit[memory::AddressSize], Qubit[memory::DataSize]);
if control {
use ctl = Qubit();
for active in [true, false] {
within {
// Activate control qubit
ApplyIfA(X, active, ctl);
// Prepare the address register
ApplyXorInPlace(queryAddress, LittleEndian(addressRegister));
} apply {
// Perform the lookup
Controlled memory::Read([ctl], (LittleEndian(addressRegister), targetRegister));
}
// Get results and make sure its the same format as the data provided i.e. Bool[].
let result = ResultArrayAsBoolArray(ForEach(MResetZ, targetRegister));
AllEqualityFactB(result, active ? expectedValue | emptyValue,
$"Expecting value {expectedValue} at address {queryAddress}, got {result}.");
}
} else {
within {
// Prepare the address register
ApplyXorInPlace(queryAddress, LittleEndian(addressRegister));
} apply {
// Perform the lookup
memory::Read(LittleEndian(addressRegister), targetRegister);
}
// Get results and make sure its the same format as the data provided i.e. Bool[].
let result = ResultArrayAsBoolArray(ForEach(MResetZ, targetRegister));
AllEqualityFactB(result, expectedValue,
$"Expecting value {expectedValue} at address {queryAddress}, got {result}.");
}
}
}