microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
iadavis/3208-leak-fixes

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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

143lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4/// This file defines the type conversion for casting gerneral types.
5/// It is an internal implementation detail for OpenQASM compilation
6/// and is not intended for use outside of this context.
7
8/// The ``BOOL_AS_RESULT`` function is used to implement the cast expr in QASM for bool to bit.
9/// This already exists in the Q# library, but is defined as a marker for casts from QASM.
10function BoolAsResult(input : Bool) : Result {
11 Std.Convert.BoolAsResult(input)
12}
13
14/// The ``BOOL_AS_INT`` function is used to implement the cast expr in QASM for bool to int.
15function BoolAsInt(value : Bool) : Int {
16 if value {
17 1
18 } else {
19 0
20 }
21}
22
23/// The ``BOOL_AS_BIGINT`` function is used to implement the cast expr in QASM for bool to big int.
24function BoolAsBigInt(value : Bool) : BigInt {
25 if value {
26 1L
27 } else {
28 0L
29 }
30}
31
32/// The ``BOOL_AS_DOUBLE`` function is used to implement the cast expr in QASM for bool to float.
33function BoolAsDouble(value : Bool) : Double {
34 if value {
35 1.
36 } else {
37 0.
38 }
39}
40
41/// The ``RESULT_AS_BOOL`` function is used to implement the cast expr in QASM for bit to bool.
42/// This already exists in the Q# library, but is defined as a marker for casts from QASM.
43function ResultAsBool(input : Result) : Bool {
44 Std.Convert.ResultAsBool(input)
45}
46
47/// The ``RESULT_AS_INT`` function is used to implement the cast expr in QASM for bit to int.
48function ResultAsInt(input : Result) : Int {
49 if Std.Convert.ResultAsBool(input) {
50 1
51 } else {
52 0
53 }
54}
55
56/// The ``RESULT_AS_BIGINT`` function is used to implement the cast expr in QASM for bit to big int.
57function ResultAsBigInt(input : Result) : BigInt {
58 if Std.Convert.ResultAsBool(input) {
59 1L
60 } else {
61 0L
62 }
63}
64
65/// The ``ResultAsDouble`` function is used to implement the cast expr in QASM for result to float.
66function ResultAsDouble(value : Result) : Double {
67 if value == One {
68 1.
69 } else {
70 0.
71 }
72}
73
74/// The ``ResultArrayAsBool`` function is used to implement the cast expr in QASM for bit[] to bool.
75/// with big-endian order. This is needed for round-trip conversion for bin ops.
76function ResultArrayAsBool(array : Result[]) : Bool {
77 for result in array {
78 if result == One {
79 return true;
80 }
81 }
82 false
83}
84
85/// The ``ResultArrayAsResultBE`` function is used to implement the cast expr in QASM for bit[] to bit.
86/// with big-endian order. This is needed for round-trip conversion for bin ops.
87function ResultArrayAsResultBE(array : Result[]) : Result {
88 BoolAsResult(ResultArrayAsBool(array))
89}
90
91/// The ``IntAsResultArrayBE`` function is used to implement the cast expr in QASM for int to bit[].
92/// with big-endian order. This is needed for round-trip conversion for bin ops.
93function IntAsResultArrayBE(number : Int, bits : Int) : Result[] {
94 mutable runningValue = number;
95 mutable result = [];
96 for _ in 1..bits {
97 set result += [BoolAsResult((runningValue &&& 1) != 0)];
98 set runningValue >>>= 1;
99 }
100 Std.Arrays.Reversed(result)
101}
102
103/// The ``BoolAsResultArrayBE`` function is used to implement the cast expr in QASM for bool to bit[].
104/// with big-endian order. This is needed for round-trip conversion for bin ops.
105function BoolAsResultArrayBE(value : Bool, bits : Int) : Result[] {
106 IntAsResultArrayBE(BoolAsInt(value), bits)
107}
108
109/// The ``ResultAsResultArrayBE`` function is used to implement the cast expr in QASM for bit to bit[].
110/// with big-endian order. This is needed for round-trip conversion for bin ops.
111function ResultAsResultArrayBE(value : Result, bits : Int) : Result[] {
112 // Since we are in big endian notation, the most significant bit is stored
113 // first, in other words the least significant bit is at the end.
114 return Std.Core.Repeated(Zero, bits - 1) + [value]
115}
116
117/// The ``ResultArrayAsIntBE`` function is used to implement the cast expr in QASM for bit[] to uint.
118/// with big-endian order. This is needed for round-trip conversion for bin ops.
119function ResultArrayAsIntBE(results : Result[]) : Int {
120 Std.Convert.ResultArrayAsInt(Std.Arrays.Reversed(results))
121}
122
123/// The ``IntAsResult`` function is used to implement the cast expr in QASM for int to bit.
124/// This is needed for round-trip conversion for bin ops.
125function IntAsResult(value : Int) : Result {
126 if value == 0 {
127 Zero
128 } else {
129 One
130 }
131}
132
133/// The ``DoubleAsResult`` function is used to implement the cast expr in QASM for float to bit.
134/// This is needed for round-trip conversion for bin ops.
135function DoubleAsResult(value : Double) : Result {
136 if Std.Math.Truncate(value) == 0 {
137 Zero
138 } else {
139 One
140 }
141}
142
143export BoolAsResult, BoolAsInt, BoolAsBigInt, BoolAsDouble, ResultAsBool, ResultAsInt, ResultAsBigInt, ResultAsDouble, ResultArrayAsBool, ResultArrayAsResultBE, IntAsResultArrayBE, BoolAsResultArrayBE, ResultAsResultArrayBE, ResultArrayAsIntBE, IntAsResult, DoubleAsResult;
144