microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/multi_qubit_measurements/multi_qubit_probabilities.qs
40lines · modecode
| 1 | namespace Kata { |
| 2 | open Microsoft.Quantum.Diagnostics; |
| 3 | open Microsoft.Quantum.Math; |
| 4 | |
| 5 | operation SetInitialState(qs : Qubit[]) : Unit is Adj + Ctl { |
| 6 | // Next two lines set the second qubit into the desired state |
| 7 | let second_bit_angle = 2.0 * ArcCos(2.0 / 3.0); |
| 8 | Ry(second_bit_angle, qs[1]); |
| 9 | |
| 10 | // Next two lines set the first qubit into the desired state |
| 11 | let first_bit_angle = 2.0 * ArcCos(1.0 / Sqrt(5.0)); |
| 12 | Controlled Ry([qs[1]], (first_bit_angle, qs[0])); |
| 13 | } |
| 14 | |
| 15 | operation ChangeBasis(qs : Qubit[]) : Unit is Adj + Ctl { |
| 16 | H(qs[0]); |
| 17 | H(qs[1]); |
| 18 | } |
| 19 | |
| 20 | @EntryPoint() |
| 21 | operation CalculateProbabilities() : Unit { |
| 22 | // This allocates qubits for us to work with |
| 23 | use qs = Qubit[2]; |
| 24 | |
| 25 | SetInitialState(qs); |
| 26 | |
| 27 | // Check that we've prepared the state |𝜓⟩ |
| 28 | Message("The state of the system before the transformation:"); |
| 29 | DumpMachine(); |
| 30 | |
| 31 | ChangeBasis(qs); |
| 32 | |
| 33 | Message("Final state of the two-qubit system:"); |
| 34 | DumpMachine(); |
| 35 | |
| 36 | // This returns the qubit array into state |00❭ |
| 37 | ResetAll(qs); |
| 38 | } |
| 39 | |
| 40 | } |
| 41 | |