You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
* Implementing Library functions in Q#
This change implements several Q# functions that were previously `body intrinsic` via existing Q# functionality. This improves compatibility with QIR generation and allows us to remove the corresponding manual C# implementation in favor of compiler generated ones. This will also improve compatibility of programs that use these functions with full QIR targets like resource estimation.
* Update src/Simulation/QSharpFoundation/Convert/Convert.qs
Co-authored-by: Robin Kuzmin <[email protected]>
* Check length of array
* Fix two's complement handling
* Simplify bounds check
Co-authored-by: Robin Kuzmin <[email protected]>
/// Converts a given big integer to an equivalent integer, if possible.
64
64
/// The function returns a pair of the resulting integer and a Boolean flag
65
65
/// which is true, if and only if the conversion was possible.
66
-
/// # Remarks
67
-
/// See [C# BigInteger constructor](https://docs.microsoft.com/dotnet/api/system.numerics.biginteger.-ctor?view=netframework-4.7.2#System_Numerics_BigInteger__ctor_System_Int64_) for more details.
// BigIntAsBoolArray always returns padded results with minimum length 8, so the below
74
+
// logic can assume the last entry is the sign bit for two's complement.
75
+
mutableval=0;
76
+
foriin0..(len-2) {
77
+
setval+=arr[i] ?2^i|0;
78
+
}
79
+
ifarr[len-1] {
80
+
setval-=2^ (len-1);
81
+
}
82
+
83
+
return (val, true);
70
84
}
71
85
72
86
73
87
/// # Summary
74
88
/// Converts a given big integer to an array of Booleans.
75
89
/// The 0 element of the array is the least significant bit of the big integer.
76
-
/// # Remarks
77
-
/// See [C# BigInteger constructor](https://docs.microsoft.com/dotnet/api/system.numerics.biginteger.-ctor?view=netframework-4.7.2#System_Numerics_BigInteger__ctor_System_Int64_) for more details.
78
90
functionBigIntAsBoolArray(a : BigInt) : Bool[] {
79
-
bodyintrinsic;
91
+
// To use two's complement, little endian representation of the integer, we fisrt need to track if the input
92
+
// is a negative number. If so, flip it back to positive and start tracking a carry bit.
93
+
letisNegative=a<0L;
94
+
mutablecarry=isNegative;
95
+
mutableval=isNegative?-a|a;
96
+
97
+
mutablearr= [];
98
+
whileval!=0L {
99
+
letnewBit=val%2L==1L;
100
+
ifisNegative {
101
+
// For negative numbers we must invert the calculated bit, so treat "true" as "0"
102
+
// and "false" as "1". This means when the carry bit is set, we want to record the
103
+
// calculated new bit and set the carry to the opposite, otherwise record the opposite
104
+
// of the calculate bit.
105
+
ifcarry {
106
+
setarr+= [newBit];
107
+
setcarry=notnewBit;
108
+
}
109
+
else {
110
+
setarr+= [notnewBit];
111
+
}
112
+
}
113
+
else {
114
+
// For positive numbers just accumulate the calculated bits into the array.
115
+
setarr+= [newBit];
116
+
}
117
+
118
+
setval/=2L;
119
+
}
120
+
121
+
// Pad to the next higher byte length (size 8) if the length is not a non-zero multiple of 8 or
122
+
// if the last bit does not agree with the sign bit.
123
+
letlen=Length(arr);
124
+
iflen==0orlen%8!=0orarr[len-1] !=isNegative {
125
+
setarr+= [isNegative, size=8- (len%8)];
126
+
}
127
+
returnarr;
80
128
}
81
129
82
130
83
131
/// # Summary
84
132
/// Converts a given array of Booleans to an equivalent big integer.
85
133
/// The 0 element of the array is the least significant bit of the big integer.
86
134
/// # Remarks
87
-
/// See [C# BigInteger constructor](https://docs.microsoft.com/dotnet/api/system.numerics.biginteger.-ctor?view=netframework-4.7.2#System_Numerics_BigInteger__ctor_System_Int64_) for more details.
88
-
/// Note that the Boolean array is padded of the right with `false` values to a length that is a multiple of 8,
135
+
/// Note that the Boolean array is padded on the right with `false` values to a length that is a multiple of 8,
89
136
/// and then treated as a little-endian notation of a positive or negative number following two's complement semantics.
/// See [C# Int64.ToString](https://docs.microsoft.com/dotnet/api/system.int64.tostring?view=netframework-4.7.1#System_Int64_ToString_System_String_) for more details.
0 commit comments