microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/fuzz/seed_inputs/qsharp/input.qs
450lines · modecode
| 1 | // |
| 2 | namespace Fuzz.Testing { |
| 3 | import Std.Arrays.*; |
| 4 | import Std.Canon.*; |
| 5 | import Std.Characterization.*; |
| 6 | import Std.Convert.*; |
| 7 | import Std.Diagnostics.*; |
| 8 | import Std.Intrinsic.*; |
| 9 | import Std.Logical.*; |
| 10 | import Std.MachineLearning.*; |
| 11 | import Std.MachineLearning.Datasets as Datasets.*; |
| 12 | import Std.Math.*; |
| 13 | import Std.Measurement.*; |
| 14 | import Std.Preparation.*; |
| 15 | import Std.Random.*; |
| 16 | import Std.Simulation.*; |
| 17 | import Std.Synthesis.*; |
| 18 | import Std.Targeting.*; |
| 19 | |
| 20 | function IfRetExpr(cond : Bool, a : Int, b : Int) : Int { |
| 21 | let x = if cond { a } else { b }; |
| 22 | x |
| 23 | } |
| 24 | |
| 25 | function NestedRetFail(arg : Int ) : String { |
| 26 | if arg < 5 { |
| 27 | fail "< 5"; |
| 28 | } |
| 29 | elif arg > 9 { |
| 30 | return "> 9"; |
| 31 | } |
| 32 | "[5, 9]" |
| 33 | } |
| 34 | |
| 35 | operation Repeat(limit : Int) : Int { |
| 36 | mutable dec = limit + 5; |
| 37 | repeat { |
| 38 | set dec = dec - 1; |
| 39 | } |
| 40 | until (dec < limit); |
| 41 | dec |
| 42 | } |
| 43 | |
| 44 | function Loops(limit : Int) : Int { |
| 45 | |
| 46 | mutable count = 0 - 7; |
| 47 | while (count < limit) |
| 48 | { |
| 49 | set count = count + 1; |
| 50 | } |
| 51 | return count + 5; |
| 52 | } |
| 53 | |
| 54 | function PrnArr(arr : Bool[]) : Unit { |
| 55 | Message(AsString(arr)); |
| 56 | } |
| 57 | |
| 58 | function CopyAndUpdate() : Unit { |
| 59 | let mask = [false, size = 10]; |
| 60 | |
| 61 | for i in Length(mask)-2 .. -1 .. 0 { |
| 62 | let nbPair = mask |
| 63 | w/ i <- true |
| 64 | w/ i + 1 <- true; |
| 65 | PrnArr(nbPair); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function Mul(d : Double) : Double { |
| 70 | return 1.0 * d; |
| 71 | } |
| 72 | |
| 73 | function Ternary(nSites : Int, amplitude : Double, idxQubit : Int) : Double { |
| 74 | return idxQubit == nSites - 1 |
| 75 | ? 0.0 |
| 76 | | amplitude; |
| 77 | } |
| 78 | |
| 79 | operation EmptyOpWithSomeParams( |
| 80 | nSites : Int, hXInitial : Double, hXFinal : Double, |
| 81 | jFinal : Double, adiabaticTime : Double, |
| 82 | qubits : Qubit[] |
| 83 | ) : Unit |
| 84 | is Adj + Ctl { |
| 85 | } |
| 86 | |
| 87 | operation RetResultArr(nSites : Int, hXInitial : Double, jFinal : Double, adiabaticTime : Double, trotterStepSize : Double, trotterOrder : Int) : Result[] { |
| 88 | let hXFinal = 0.0; |
| 89 | use qubits = Qubit[nSites]; |
| 90 | return [One]; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | operation SomeQubitManip(a : Qubit, b : Qubit) : Unit is Adj + Ctl { |
| 95 | Message("Classical version"); |
| 96 | CNOT(a, b); |
| 97 | } |
| 98 | |
| 99 | operation CallAndWithinApply(a : Qubit, b : Qubit) : Unit is Adj + Ctl { |
| 100 | let _ = SomeQubitManip; |
| 101 | |
| 102 | within { |
| 103 | CNOT(a, b); |
| 104 | H(a); |
| 105 | } apply { |
| 106 | CNOT(a, b); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @EntryPoint() |
| 111 | operation PassQubits() : Unit { |
| 112 | use a = Qubit(); |
| 113 | use b = Qubit(); |
| 114 | CallAndWithinApply(a, b); |
| 115 | } |
| 116 | |
| 117 | @EntryPoint() |
| 118 | operation CallControlled(time : Double, angle : Double, lambda : ((Double, Qubit[]) => Unit is Ctl), qs : Qubit[]) : Result { |
| 119 | mutable result = Zero; |
| 120 | |
| 121 | use controlQubit = Qubit(); |
| 122 | H(controlQubit); |
| 123 | Rz(-time * angle, controlQubit); |
| 124 | Controlled lambda([controlQubit], (time, qs)); |
| 125 | return Zero; |
| 126 | } |
| 127 | |
| 128 | operation NestedFor() : Unit { |
| 129 | let dt = 0.1; |
| 130 | let nTimes = 101; |
| 131 | let nSamples = 100; |
| 132 | let eigenphase = PI(); |
| 133 | let angle = 0.5 * PI(); |
| 134 | |
| 135 | use eigenstate = Qubit(); |
| 136 | within { |
| 137 | X(eigenstate); |
| 138 | } apply { |
| 139 | for idxTime in 0 .. nTimes - 1 { |
| 140 | let time = dt * IntAsDouble(idxTime); |
| 141 | mutable nOnesObserved = 0; |
| 142 | |
| 143 | for idxSample in 0 .. nSamples - 1 { |
| 144 | let sample = Zero; |
| 145 | |
| 146 | if (sample == One) { |
| 147 | set nOnesObserved += 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | let obs = IntAsDouble(nOnesObserved) / IntAsDouble(nSamples); |
| 152 | let mean = 0.1; |
| 153 | |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | function Indexing(xs : Double[], ys : Double[]) : Double { |
| 159 | mutable sum = 0.0; |
| 160 | |
| 161 | for idxPoint in 0 .. Length(xs) - 2 { |
| 162 | let trapezoidalHeight = (ys[idxPoint + 1] + ys[idxPoint]) * 0.5; |
| 163 | let trapezoidalBase = xs[idxPoint + 1] - xs[idxPoint]; |
| 164 | set sum += trapezoidalBase * trapezoidalHeight; |
| 165 | } |
| 166 | |
| 167 | return sum; |
| 168 | } |
| 169 | |
| 170 | function CopyAndUpd(left : Double[], right : Double[]) : Double[] { |
| 171 | mutable product = [0.0, size = Length(left)]; |
| 172 | |
| 173 | for idxElement in IndexRange(left) { |
| 174 | set product w/= idxElement <- left[idxElement] * right[idxElement]; |
| 175 | } |
| 176 | |
| 177 | return product; |
| 178 | } |
| 179 | |
| 180 | internal operation Fail(pattern : Bool[], queryRegister : Qubit[], target : Qubit) : Unit { |
| 181 | if (Length(queryRegister) != Length(pattern)) { |
| 182 | fail "Length of input register must be equal to the pattern length."; |
| 183 | } |
| 184 | |
| 185 | for (patternBit, controlQubit) in [(pattern[0], queryRegister[0])] { |
| 186 | if (patternBit) { |
| 187 | Controlled X([controlQubit], target); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | operation InvokedOp(data : Qubit, auxiliaryQubits : Qubit[]) : Unit |
| 193 | is Adj + Ctl |
| 194 | { |
| 195 | } |
| 196 | |
| 197 | operation InvokeAdjoints () : Unit { |
| 198 | use data = Qubit(); |
| 199 | use auxiliaryQubits = Qubit[2]; |
| 200 | let register = [data] + auxiliaryQubits; |
| 201 | Rx(PI() / 3.0, data); |
| 202 | |
| 203 | InvokedOp(data, auxiliaryQubits); |
| 204 | |
| 205 | let parity01 = Measure([PauliZ, PauliZ, PauliI], register); |
| 206 | let parity12 = Measure([PauliI, PauliZ, PauliZ], register); |
| 207 | |
| 208 | Adjoint InvokedOp(data, auxiliaryQubits); |
| 209 | Adjoint Rx(PI() / 3.0, data); |
| 210 | } |
| 211 | |
| 212 | operation InvokeCtrlAdjoint (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj + Ctl { |
| 213 | CCNOT(control1, control2, target); |
| 214 | Controlled (Adjoint S)([control1], control2); |
| 215 | } |
| 216 | operation Body (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Ctl { |
| 217 | body (...) { |
| 218 | Adjoint T(control1); |
| 219 | H(target); |
| 220 | CNOT(target, control1); |
| 221 | T(target); |
| 222 | Adjoint T(control1); |
| 223 | T(control1); |
| 224 | } |
| 225 | |
| 226 | adjoint self; |
| 227 | } |
| 228 | |
| 229 | operation ThreeQubitParams (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj + Ctl { |
| 230 | use auxiliary = Qubit(); |
| 231 | } |
| 232 | |
| 233 | operation BodyControlled (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit { |
| 234 | body (...) { |
| 235 | use auxillaryQubit = Qubit(); |
| 236 | ThreeQubitParams(control1, control2, auxillaryQubit); |
| 237 | S(auxillaryQubit); |
| 238 | CNOT(auxillaryQubit, target); |
| 239 | H(auxillaryQubit); |
| 240 | |
| 241 | if (M(auxillaryQubit) == One) { |
| 242 | Controlled Z([control2], control1); |
| 243 | X(auxillaryQubit); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | controlled (controls, ...) { |
| 248 | Controlled X(controls + [control1, control2], target); |
| 249 | } |
| 250 | |
| 251 | adjoint self; |
| 252 | } |
| 253 | @EntryPoint() |
| 254 | operation LetTuple() : Int { |
| 255 | use q = Qubit[3]; |
| 256 | mutable mismatch = 0; |
| 257 | for _ in 1..q::Length - 2 { |
| 258 | H(q[0]); |
| 259 | CNOT(q[0], q[1]); |
| 260 | |
| 261 | let (r0, r1, r2) = (One, Zero, One); |
| 262 | |
| 263 | if not (r0 == r1 and r1 == r2) { |
| 264 | set mismatch += 1; |
| 265 | } |
| 266 | } |
| 267 | return mismatch; |
| 268 | } |
| 269 | |
| 270 | operation IntDiv (bitsPerColor : Int, register : Qubit[]) : Int[] { |
| 271 | let nVertices = Length(register) / bitsPerColor; |
| 272 | return [0]; |
| 273 | } |
| 274 | |
| 275 | operation NestedMath() : Unit { |
| 276 | for nDatabaseQubits in 4 .. 6 { |
| 277 | for nIterations in 0 .. 5 { |
| 278 | use markedQubit = Qubit(); |
| 279 | use databaseRegister = Qubit[nDatabaseQubits]; |
| 280 | |
| 281 | let markedElements = [1, 4, 9]; |
| 282 | let nMarkedElements = Length(markedElements); |
| 283 | |
| 284 | let successAmplitude = Sin(IntAsDouble(2 * nIterations + 1) * ArcSin(Sqrt(IntAsDouble(nMarkedElements) / IntAsDouble(2 ^ nDatabaseQubits)))); |
| 285 | let successProbability = successAmplitude * successAmplitude; |
| 286 | |
| 287 | let result = One; |
| 288 | let number = 5; |
| 289 | |
| 290 | if (result == One) { |
| 291 | fail "Found index should be in MarkedElements."; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | function IsEven(element : Int) : Bool { |
| 297 | element % 2 == 0; |
| 298 | } |
| 299 | |
| 300 | function IsSingleDigit(element : Int) : Bool { |
| 301 | return element >= 0 and element < 10; |
| 302 | } |
| 303 | |
| 304 | operation RetDoubleArr(coefficients : Double[], evaluationPoints : Double[], |
| 305 | numBits : Int, pointPos : Int, odd : Bool, even : Bool) |
| 306 | : Double[] |
| 307 | { |
| 308 | mutable results = [0.0, size = Length(evaluationPoints)]; |
| 309 | for i in IndexRange(evaluationPoints) { |
| 310 | let point = evaluationPoints[i]; |
| 311 | use xQubits = Qubit[numBits]; |
| 312 | use yQubits = Qubit[numBits]; |
| 313 | |
| 314 | ResetAll(xQubits + yQubits); |
| 315 | } |
| 316 | return results; |
| 317 | } |
| 318 | function RetConstArrIndexed (idxBondLength : Int) : Double[] { |
| 319 | return [ |
| 320 | [0.5678, -1.4508, 0.6799, 0.0791, 0.0791], |
| 321 | [0.0984, 0.0679, 0.3329, 0.1475, 0.1475] |
| 322 | ][idxBondLength]; |
| 323 | } |
| 324 | |
| 325 | function PowerCos(Results : Int, theta_1 : Double, theta_2 : Double, Measurements : Int): Unit{ |
| 326 | let DoubleVal = PI() * IntAsDouble(Results) / IntAsDouble(2 ^ (Measurements-1)); |
| 327 | let InnerProductValue = -Cos(DoubleVal); |
| 328 | } |
| 329 | operation TrippleFor() : Unit { |
| 330 | let testList = [ (3, 5) |
| 331 | ]; |
| 332 | |
| 333 | for (actual, expected) in testList { |
| 334 | for totalNumberOfQubits in 1 .. 8 { |
| 335 | for numberOfControls in 1 .. totalNumberOfQubits - 1 { |
| 336 | Message("msg" + AsString(actual)); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | function RichTrippleFor(func : Int[]) : Int[] { |
| 342 | let bits = BitSizeI(func::Length - 1); |
| 343 | mutable res = func; |
| 344 | for m in 0..bits - 1 { |
| 345 | mutable s = 1 <<< m; |
| 346 | for i in 0..(2 * s)..Length(func) - 1 { |
| 347 | mutable k = i + s; |
| 348 | for j in i..i + s - 1 { |
| 349 | mutable t = res[j]; |
| 350 | set res w/= j <- res[j] + res[k]; |
| 351 | set res w/= k <- t - res[k]; |
| 352 | set k = k + 1; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | return res; |
| 357 | } |
| 358 | function DoublePower(sigma : Double, mu : Double, N : Int) : Double { |
| 359 | let n = IntAsDouble(N); |
| 360 | return -((n - mu) ^ 2.) / sigma ^ 2.; |
| 361 | } |
| 362 | |
| 363 | operation Fixup (target : Qubit) : Unit { |
| 364 | |
| 365 | body (...) { |
| 366 | use aux0 = Qubit(); |
| 367 | use aux1 = Qubit(); |
| 368 | |
| 369 | repeat { |
| 370 | BodyControlled(aux0, aux1, target); |
| 371 | S(target); |
| 372 | |
| 373 | BodyControlled(aux0, aux1, target); |
| 374 | Z(target); |
| 375 | |
| 376 | let outcome0 = Measure([PauliX], [aux0]); |
| 377 | let prob = outcome0 == One ? 0.5 | 5.0 / 6.0; |
| 378 | let outcome1 = Measure([PauliX], [aux1]); |
| 379 | } |
| 380 | until (outcome0 == Zero and outcome1 == Zero) |
| 381 | fixup { |
| 382 | if (outcome1 == One) { |
| 383 | Z(aux1); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | } |
| 388 | |
| 389 | adjoint (...) { |
| 390 | within { |
| 391 | X(target); |
| 392 | } apply { |
| 393 | Fixup(target); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | operation BitShift( |
| 399 | generator : Int, |
| 400 | modulus : Int, |
| 401 | bitsize : Int |
| 402 | ) |
| 403 | : Int { |
| 404 | mutable frequencyEstimate = 0; |
| 405 | let bitsPrecision = 2 * bitsize + 1; |
| 406 | |
| 407 | use eigenstateRegister = Qubit[bitsize]; |
| 408 | |
| 409 | use c = Qubit(); |
| 410 | for idx in bitsPrecision - 1..-1..0 { |
| 411 | within { |
| 412 | H(c); |
| 413 | } apply { |
| 414 | R1Frac(frequencyEstimate, bitsPrecision - 1 - idx, c); |
| 415 | } |
| 416 | if true { |
| 417 | set frequencyEstimate += 1 <<< (bitsPrecision - 1 - idx); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | ResetAll(eigenstateRegister); |
| 422 | |
| 423 | return frequencyEstimate; |
| 424 | } |
| 425 | |
| 426 | internal operation RetTuple ( |
| 427 | inputValues : Bool[], |
| 428 | encodingBases : Pauli[], |
| 429 | qubitIndices : Int[] |
| 430 | ) : (Result, Result[]) { |
| 431 | if ((Length(inputValues) != Length(encodingBases)) |
| 432 | or (Length(inputValues) != Length(qubitIndices))) { |
| 433 | fail "Lengths of input values, encoding bases and qubitIndices must be equal."; |
| 434 | } |
| 435 | |
| 436 | use block = Qubit[Length(inputValues)]; |
| 437 | use auxiliary = Qubit(); |
| 438 | for (qubit, value, basis) in [(block[0], inputValues[0], encodingBases[0])] { |
| 439 | } |
| 440 | |
| 441 | H(auxiliary); |
| 442 | for (index, basis) in [(qubitIndices[0], encodingBases[0])] { |
| 443 | } |
| 444 | let auxiliaryResult = Measure([PauliX], [auxiliary]); |
| 445 | let dataResult = [One]; |
| 446 | |
| 447 | return (auxiliaryResult, dataResult); |
| 448 | } |
| 449 | |
| 450 | } |
| 451 | |