microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/Ising/Simple1dIsingOrder1.qs
79lines · modecode
| 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 | |
| 49 | // Compute the time step |
| 50 | let dt : Double = evolutionTime / Std.Convert.IntAsDouble(numberOfSteps); |
| 51 | |
| 52 | let theta_x = - g * dt; |
| 53 | let theta_zz = J * dt; |
| 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 |
| 67 | for j in 0..2..N-2 { |
| 68 | Rzz(2.0 * theta_zz, qubits[j], qubits[j + 1]); |
| 69 | } |
| 70 | |
| 71 | // Interactions between "odd" pairs |
| 72 | for j in 1..2..N-2 { |
| 73 | Rzz(2.0 * theta_zz, qubits[j], qubits[j + 1]); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | MResetEachZ(qubits) |
| 79 | } |
| 80 | |