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