microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/algorithms/Ising/Simple1dIsingOrder1.qs
79lines · modeblame
3f3bde67DmitryVasilevsky1 years ago | 1 | /// # Sample |
| 2 | /// Simulation of a simple Ising model evolution | |
| 3 | /// on a 1D grid with first-order Trotterization. | |
| 4 | /// | |
| 5 | /// # Description | |
| 6 | /// This sample demonstrates simulation of an Ising model Hamiltonian | |
| 7 | /// on 1D grid of size N using a first-order Trotter-Suzuki approximation. | |
| 8 | /// This sample can be easily simulated classically with the grid of size 9 | |
| 9 | /// and 1000 shots. This sample is suitable for Base Profile. | |
| 10 | /// For the purpose of simplicity this sample intentionally doesn't | |
| 11 | /// post-process results or perform eigenvalue estimation. | |
| 12 | operation Main() : Result[] { | |
| 13 | // The size of a 1D grid is N | |
| 14 | let N : Int = 9; | |
| 15 | | |
| 16 | // Total evolution time | |
| 17 | let evolutionTime : Double = 4.0; | |
| 18 | // Number of steps | |
| 19 | let numberOfSteps : Int = 7; | |
| 20 | | |
| 21 | // Coefficient for 2-qubit interactions between neighboring qubits | |
| 22 | let J : Double = 1.0; | |
| 23 | // Coefficient for external field interaction for individual qubits | |
| 24 | let g : Double = 0.7; | |
| 25 | | |
| 26 | IsingModel1DEvolution(N, J, g, evolutionTime, numberOfSteps) | |
| 27 | } | |
| 28 | | |
| 29 | /// # Summary | |
| 30 | /// Simulate simple Ising model evolution | |
| 31 | /// | |
| 32 | /// # Description | |
| 33 | /// Simulates state |𝜓⟩ evolution to find |𝜓(t)⟩=U(t)|𝜓(0)⟩. | |
| 34 | /// |𝜓(0)⟩ is taken to be |0...0⟩. | |
| 35 | /// U(t)=e⁻ⁱᴴᵗ, where H is an Ising model Hamiltonian H = -J·Σ'ᵢⱼZᵢZⱼ + g·ΣᵢXᵢ | |
| 36 | /// Here Σ' is taken over all pairs of neighboring qubits <i,j>. | |
| 37 | /// Simulation is done by performing K steps assuming U(t)≈(U(t/K))ᴷ. | |
| 38 | operation IsingModel1DEvolution( | |
| 39 | N : Int, | |
| 40 | J : Double, | |
| 41 | g : Double, | |
| 42 | evolutionTime : Double, | |
| 43 | numberOfSteps : Int | |
| 44 | ) : Result[] { | |
| 45 | | |
| 46 | // Allocate qubit grid | |
| 47 | use qubits = Qubit[N]; | |
| 48 | | |
71ade5dcDmitryVasilevsky1 years ago | 49 | // Compute the time step |
| 50 | let dt : Double = evolutionTime / Std.Convert.IntAsDouble(numberOfSteps); | |
3f3bde67DmitryVasilevsky1 years ago | 51 | |
71ade5dcDmitryVasilevsky1 years ago | 52 | let theta_x = - g * dt; |
| 53 | let theta_zz = J * dt; | |
3f3bde67DmitryVasilevsky1 years ago | 54 | |
| 55 | // Perform K steps | |
| 56 | for i in 1..numberOfSteps { | |
| 57 | | |
| 58 | // Single-qubit interaction with external field | |
| 59 | for q in qubits { | |
| 60 | Rx(2.0 * theta_x, q); | |
| 61 | } | |
| 62 | | |
| 63 | // All of the following Rzz gates commute. So we apply them between "even" | |
| 64 | // pairs first and then between "odd" pairs to reduce the algorithm depth. | |
| 65 | | |
| 66 | // Interactions between "even" pairs | |
71ade5dcDmitryVasilevsky1 years ago | 67 | for j in 0..2..N-2 { |
| 68 | Rzz(2.0 * theta_zz, qubits[j], qubits[j + 1]); | |
3f3bde67DmitryVasilevsky1 years ago | 69 | } |
| 70 | | |
| 71 | // Interactions between "odd" pairs | |
71ade5dcDmitryVasilevsky1 years ago | 72 | for j in 1..2..N-2 { |
| 73 | Rzz(2.0 * theta_zz, qubits[j], qubits[j + 1]); | |
3f3bde67DmitryVasilevsky1 years ago | 74 | } |
| 75 | | |
| 76 | } | |
| 77 | | |
| 78 | MResetEachZ(qubits) | |
| 79 | } |