microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
library/signed/src/Comparison.qs
31lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import Std.Arrays.Tail, Std.Arrays.Zipped, Std.Arrays.Most, Std.Arrays.Rest; |
| 5 | import Std.Diagnostics.Fact; |
| 6 | import Std.Arithmetic.ApplyIfGreaterLE; |
| 7 | |
| 8 | /// # Summary |
| 9 | /// Wrapper for signed integer comparison: `result = xs > ys`. |
| 10 | /// |
| 11 | /// # Input |
| 12 | /// ## xs |
| 13 | /// First $n$-bit number |
| 14 | /// ## ys |
| 15 | /// Second $n$-bit number |
| 16 | /// ## result |
| 17 | /// Will be flipped if $xs > ys$ |
| 18 | operation CompareGTSI(xs : Qubit[], ys : Qubit[], result : Qubit) : Unit is Adj + Ctl { |
| 19 | use tmp = Qubit(); |
| 20 | within { |
| 21 | CNOT(Tail(xs), tmp); |
| 22 | CNOT(Tail(ys), tmp); |
| 23 | } apply { |
| 24 | X(tmp); |
| 25 | Controlled ApplyIfGreaterLE([tmp], (X, xs, ys, result)); |
| 26 | X(tmp); |
| 27 | CCNOT(tmp, Tail(ys), result); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export CompareGTSI; |