microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/test/circuits-cases/conditionals.qs
256lines · modecode
| 1 | operation Main() : Unit { |
| 2 | use qs = Qubit[34]; |
| 3 | |
| 4 | ResultComparisonToLiteral(qs[0]); |
| 5 | ResultComparisonToLiteralZero(qs[1]); |
| 6 | ElseBlockOnly(qs[2]); |
| 7 | ResultComparisonToResult(qs[3], qs[4]); |
| 8 | ResultComparisonEmptyBlock(qs[5], qs[6]); |
| 9 | IfElse(qs[7], qs[8]); |
| 10 | SequentialIfs(qs[9], qs[10], qs[11]); |
| 11 | NestedIfs(qs[12], qs[13], qs[14]); |
| 12 | MultiplePossibleFloatValuesInUnitaryArg(qs[15], qs[16]); |
| 13 | CustomIntrinsicVariableArg(qs[17]); |
| 14 | BranchOnDynamicDouble(qs[18], qs[19]); |
| 15 | BranchOnDynamicBool(qs[20], qs[21]); |
| 16 | NestedCallablesInBranch(qs[22], qs[23]); |
| 17 | ConditionalInLoop(qs[24], qs[25]); |
| 18 | NestedConditionalsInCallable(qs[26], qs[27], qs[28]); |
| 19 | MeasurementInConditional(qs[29], qs[30], qs[31]); |
| 20 | BranchDueToBinOpShortCircuit(qs[32], qs[33]); |
| 21 | |
| 22 | ResetAll(qs); |
| 23 | } |
| 24 | |
| 25 | operation ResultComparisonToLiteral(q : Qubit) : Result[] { |
| 26 | H(q); |
| 27 | let r1 = M(q); |
| 28 | if (r1 == One) { |
| 29 | X(q); |
| 30 | } |
| 31 | [r1] |
| 32 | } |
| 33 | |
| 34 | // Test-case operations mirroring source/compiler/qsc/src/interpret/cond_tests.rs |
| 35 | |
| 36 | operation ResultComparisonToLiteralZero(q : Qubit) : Result[] { |
| 37 | H(q); |
| 38 | let r1 = M(q); |
| 39 | if (r1 == Zero) { |
| 40 | X(q); |
| 41 | } |
| 42 | [r1] |
| 43 | } |
| 44 | |
| 45 | operation ElseBlockOnly(q : Qubit) : Result[] { |
| 46 | H(q); |
| 47 | let r1 = M(q); |
| 48 | if (r1 == Zero) {} else { |
| 49 | X(q); |
| 50 | } |
| 51 | [r1] |
| 52 | } |
| 53 | |
| 54 | operation ResultComparisonToResult(q1 : Qubit, q2 : Qubit) : Result[] { |
| 55 | H(q1); |
| 56 | H(q2); |
| 57 | let r1 = M(q1); |
| 58 | let r2 = M(q2); |
| 59 | if (r1 == r2) { |
| 60 | X(q1); |
| 61 | } |
| 62 | [r1, r2] |
| 63 | } |
| 64 | |
| 65 | operation ResultComparisonEmptyBlock(q1 : Qubit, q2 : Qubit) : Int { |
| 66 | H(q1); |
| 67 | H(q2); |
| 68 | let r1 = M(q1); |
| 69 | let r2 = M(q2); |
| 70 | mutable i = 4; |
| 71 | if (r1 == r2) { |
| 72 | set i = 5; |
| 73 | } |
| 74 | i |
| 75 | } |
| 76 | |
| 77 | operation IfElse(q0 : Qubit, q1 : Qubit) : Result[] { |
| 78 | H(q0); |
| 79 | let r = M(q0); |
| 80 | if r == One { |
| 81 | X(q1); |
| 82 | } else { |
| 83 | Y(q1); |
| 84 | } |
| 85 | let r1 = M(q1); |
| 86 | [r, r1] |
| 87 | } |
| 88 | |
| 89 | operation SequentialIfs(q0 : Qubit, q1 : Qubit, q2 : Qubit) : Result[] { |
| 90 | H(q0); |
| 91 | H(q1); |
| 92 | let r0 = M(q0); |
| 93 | let r1 = M(q1); |
| 94 | if r0 == One { |
| 95 | X(q2); |
| 96 | } else { |
| 97 | Z(q2); |
| 98 | } |
| 99 | if r1 == One { |
| 100 | X(q2); |
| 101 | } else { |
| 102 | Y(q2); |
| 103 | } |
| 104 | let r2 = M(q2); |
| 105 | [r0, r1, r2] |
| 106 | } |
| 107 | |
| 108 | operation NestedIfs(q0 : Qubit, q1 : Qubit, q2 : Qubit) : Result[] { |
| 109 | H(q0); |
| 110 | H(q1); |
| 111 | let r0 = M(q0); |
| 112 | let r1 = M(q1); |
| 113 | if r0 == One { |
| 114 | if r1 == One { |
| 115 | X(q2); |
| 116 | } else { |
| 117 | Y(q2); |
| 118 | } |
| 119 | } else { |
| 120 | Z(q2); |
| 121 | } |
| 122 | let r2 = M(q2); |
| 123 | [r0, r1, r2] |
| 124 | } |
| 125 | |
| 126 | operation MultiplePossibleFloatValuesInUnitaryArg(q0 : Qubit, q1 : Qubit) : Result[] { |
| 127 | H(q0); |
| 128 | let r = M(q0); |
| 129 | mutable theta = 1.0; |
| 130 | if r == One { |
| 131 | set theta = 2.0; |
| 132 | }; |
| 133 | Rx(theta, q1); |
| 134 | let r1 = M(q1); |
| 135 | [r, r1] |
| 136 | } |
| 137 | |
| 138 | @SimulatableIntrinsic() |
| 139 | operation foo(q : Qubit, x : Int) : Unit { |
| 140 | for i in 1..x { |
| 141 | H(q); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | operation CustomIntrinsicVariableArg(q : Qubit) : Unit { |
| 146 | mutable x = 4; |
| 147 | H(q); |
| 148 | if (M(q) == One) { |
| 149 | set x = 5; |
| 150 | } |
| 151 | foo(q, x); |
| 152 | } |
| 153 | |
| 154 | operation BranchOnDynamicDouble(q0 : Qubit, q1 : Qubit) : Result[] { |
| 155 | H(q0); |
| 156 | let r = M(q0); |
| 157 | mutable theta = 1.0; |
| 158 | if r == One { |
| 159 | set theta = 2.0; |
| 160 | }; |
| 161 | if theta > 1.5 { |
| 162 | set theta = 3.0; |
| 163 | } else { |
| 164 | set theta = 4.0; |
| 165 | } |
| 166 | Rx(theta, q1); |
| 167 | let r1 = M(q1); |
| 168 | [r, r1] |
| 169 | } |
| 170 | |
| 171 | operation BranchOnDynamicBool(q0 : Qubit, q1 : Qubit) : Result[] { |
| 172 | H(q0); |
| 173 | let r = M(q0); |
| 174 | mutable cond = true; |
| 175 | if r == One { |
| 176 | set cond = false; |
| 177 | }; |
| 178 | if cond { |
| 179 | set cond = false; |
| 180 | } else { |
| 181 | set cond = true; |
| 182 | } |
| 183 | if cond { |
| 184 | X(q1); |
| 185 | } |
| 186 | let r1 = M(q1); |
| 187 | [r, r1] |
| 188 | } |
| 189 | |
| 190 | operation Foo(q : Qubit) : Unit { |
| 191 | Bar(q); |
| 192 | } |
| 193 | |
| 194 | operation Bar(q : Qubit) : Unit { |
| 195 | X(q); |
| 196 | Y(q); |
| 197 | } |
| 198 | |
| 199 | operation NestedCallablesInBranch(q : Qubit, q1 : Qubit) : Unit { |
| 200 | Foo(q); |
| 201 | H(q1); |
| 202 | if (M(q1) == One) { |
| 203 | Foo(q); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | operation ConditionalInLoop(q0 : Qubit, q1 : Qubit) : Unit { |
| 208 | let results = [MResetZ(q1), MResetZ(q1)]; |
| 209 | |
| 210 | for j in 0..1 { |
| 211 | if results[j] == One { |
| 212 | X(q0); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | for j in 0..1 { |
| 217 | Baz(q0); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | operation Baz(q : Qubit) : Unit { |
| 222 | H(q); |
| 223 | } |
| 224 | |
| 225 | operation NestedConditionalsInCallable(q : Qubit, q0 : Qubit, q1 : Qubit) : Unit { |
| 226 | let r0 = MResetZ(q0); |
| 227 | let r1 = MResetZ(q1); |
| 228 | Quux(q, r0, r1); |
| 229 | } |
| 230 | |
| 231 | operation Quux(q : Qubit, r0 : Result, r1 : Result) : Unit { |
| 232 | if r0 == One {} else { |
| 233 | if r1 == One { |
| 234 | X(q); |
| 235 | } else { |
| 236 | Z(q); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | operation MeasurementInConditional(q0 : Qubit, q1 : Qubit, q2 : Qubit) : Unit { |
| 242 | H(q1); |
| 243 | let r1 = M(q1); |
| 244 | |
| 245 | if r1 == One { |
| 246 | H(q0); |
| 247 | let r0 = M(q2); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | operation BranchDueToBinOpShortCircuit(q0 : Qubit, q1 : Qubit) : Unit { |
| 252 | H(q0); |
| 253 | H(q1); |
| 254 | let r = { M(q0) == Zero } and { M(q1) == Zero }; |
| 255 | let r1 = { M(q0) == Zero } or { M(q1) == Zero }; |
| 256 | } |
| 257 | |