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