microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/tests/CliffordIsing.qs
92lines · modecode
| 1 | import Std.Math.PI; |
| 2 | |
| 3 | @EntryPoint(Base) |
| 4 | operation CliffordIsing() : Result[] { |
| 5 | // Use specifically tailored parameters to get Clifford only |
| 6 | // rotation values. |
| 7 | IsingModel2DEvolution( |
| 8 | 34, |
| 9 | 36, |
| 10 | PI() / 2.0, |
| 11 | PI() / 2.0, |
| 12 | 40.0, |
| 13 | 40 |
| 14 | ) |
| 15 | } |
| 16 | |
| 17 | /// # Summary |
| 18 | /// Simulate simple Ising model evolution |
| 19 | /// |
| 20 | /// # Description |
| 21 | /// Simulates state |𝜓⟩ evolution to find |𝜓(t)⟩=U(t)|𝜓(0)⟩. |
| 22 | /// |𝜓(0)⟩ is taken to be |0...0⟩. |
| 23 | /// U(t)=e⁻ⁱᴴᵗ, where H is an Ising model Hamiltonian H = -J·Σ'ᵢⱼZᵢZⱼ + g·ΣᵢXᵢ |
| 24 | /// Here Σ' is taken over all pairs of neighboring qubits <i,j>. |
| 25 | /// Simulation is done by performing K steps assuming U(t)≈(U(t/K))ᴷ. |
| 26 | operation IsingModel2DEvolution( |
| 27 | N1 : Int, |
| 28 | N2 : Int, |
| 29 | J : Double, |
| 30 | g : Double, |
| 31 | evolutionTime : Double, |
| 32 | numberOfSteps : Int |
| 33 | ) : Result[] { |
| 34 | |
| 35 | // Allocate qubit grid and structure it as a 2D array. |
| 36 | use qubits = Qubit[N1 * N2]; |
| 37 | let qubitsAs2D = Std.Arrays.Chunks(N2, qubits); |
| 38 | |
| 39 | // Compute the time step |
| 40 | let dt : Double = evolutionTime / Std.Convert.IntAsDouble(numberOfSteps); |
| 41 | |
| 42 | let theta_x = - g * dt; |
| 43 | let theta_zz = J * dt; |
| 44 | |
| 45 | // Perform K steps |
| 46 | for i in 1..numberOfSteps { |
| 47 | |
| 48 | // Single-qubit interaction with external field |
| 49 | for q in qubits { |
| 50 | Rx(2.0 * theta_x, q); |
| 51 | } |
| 52 | |
| 53 | // All Rzz gates applied in the following two loops commute so they can be |
| 54 | // applied in any order. To reduce the depth of the algorithm, Rzz gates |
| 55 | // between horizontal "even" pairs of qubits are applied first - pairs |
| 56 | // that start at even indices. Then Rzz gates between "odd" pairs are |
| 57 | // applied. That way all Rzz between horizontal "even" pairs can potentially |
| 58 | // be done in parallel. Same is true about horizontal "odd" pairs, |
| 59 | // vertical "even" pairs and vertical "odd" pairs. |
| 60 | |
| 61 | // Horizontal two-qubit interactions |
| 62 | for row in 0..N1-1 { |
| 63 | // Horizontal interactions between "even" pairs |
| 64 | for col in 0..2..N2-2 { |
| 65 | Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row][col + 1]); |
| 66 | } |
| 67 | |
| 68 | // Horizontal interactions between "odd" pairs |
| 69 | for col in 1..2..N2-2 { |
| 70 | Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row][col + 1]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Vertical two-qubit interactions |
| 75 | for col in 0..N2-1 { |
| 76 | |
| 77 | // Vertical interactions between "even" pairs |
| 78 | for row in 0..2..N1-2 { |
| 79 | Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row + 1][col]); |
| 80 | } |
| 81 | |
| 82 | // Vertical interactions between "odd" pairs |
| 83 | for row in 1..2..N1-2 { |
| 84 | Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row + 1][col]); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | MResetEachZ(qubits) |
| 92 | } |
| 93 | |