|
| 1 | +import time |
| 2 | +import optax |
| 3 | +import tensorcircuit as tc |
| 4 | +from tensorcircuit.templates.lattice import SquareLattice, get_compatible_layers |
| 5 | +from tensorcircuit.templates.hamiltonians import heisenberg_hamiltonian |
| 6 | + |
| 7 | +# Use JAX for high-performance, especially on GPU. |
| 8 | +K = tc.set_backend("jax") |
| 9 | +tc.set_dtype("complex64") |
| 10 | +# On Windows, cotengra's multiprocessing can cause issues. |
| 11 | +tc.set_contractor("cotengra-8192-8192", parallel=False) |
| 12 | + |
| 13 | + |
| 14 | +def run_vqe(): |
| 15 | + n, m, nlayers = 4, 4, 6 |
| 16 | + lattice = SquareLattice(size=(n, m), pbc=True, precompute_neighbors=1) |
| 17 | + h = heisenberg_hamiltonian(lattice, j_coupling=[1.0, 1.0, 0.8]) # Jx, Jy, Jz |
| 18 | + nn_bonds = lattice.get_neighbor_pairs(k=1, unique=True) |
| 19 | + gate_layers = get_compatible_layers(nn_bonds) |
| 20 | + |
| 21 | + def singlet_init(circuit): |
| 22 | + # A good initial state for Heisenberg ground state search |
| 23 | + nq = circuit._nqubits |
| 24 | + for i in range(0, nq - 1, 2): |
| 25 | + j = (i + 1) % nq |
| 26 | + circuit.X(i) |
| 27 | + circuit.H(i) |
| 28 | + circuit.cnot(i, j) |
| 29 | + circuit.X(j) |
| 30 | + return circuit |
| 31 | + |
| 32 | + def vqe_forward(param): |
| 33 | + """ |
| 34 | + Defines the VQE ansatz and computes the energy expectation. |
| 35 | + The ansatz consists of nlayers of RZZ, RXX, and RYY entangling layers. |
| 36 | + """ |
| 37 | + c = tc.Circuit(n * m) |
| 38 | + c = singlet_init(c) |
| 39 | + |
| 40 | + for i in range(nlayers): |
| 41 | + for layer in gate_layers: |
| 42 | + for j, k in layer: |
| 43 | + c.rzz(int(j), int(k), theta=param[i, 0]) |
| 44 | + for layer in gate_layers: |
| 45 | + for j, k in layer: |
| 46 | + c.rxx(int(j), int(k), theta=param[i, 1]) |
| 47 | + for layer in gate_layers: |
| 48 | + for j, k in layer: |
| 49 | + c.ryy(int(j), int(k), theta=param[i, 2]) |
| 50 | + |
| 51 | + return tc.templates.measurements.operator_expectation(c, h) |
| 52 | + |
| 53 | + vgf = K.jit(K.value_and_grad(vqe_forward)) |
| 54 | + param = tc.backend.implicit_randn(stddev=0.02, shape=[nlayers, 3]) |
| 55 | + optimizer = optax.adam(learning_rate=3e-3) |
| 56 | + opt_state = optimizer.init(param) |
| 57 | + |
| 58 | + @K.jit |
| 59 | + def train_step(param, opt_state): |
| 60 | + """A single training step, JIT-compiled for maximum speed.""" |
| 61 | + loss_val, grads = vgf(param) |
| 62 | + updates, opt_state = optimizer.update(grads, opt_state, param) |
| 63 | + param = optax.apply_updates(param, updates) |
| 64 | + return param, opt_state, loss_val |
| 65 | + |
| 66 | + print("Starting VQE optimization...") |
| 67 | + for i in range(1000): |
| 68 | + time0 = time.time() |
| 69 | + param, opt_state, loss = train_step(param, opt_state) |
| 70 | + time1 = time.time() |
| 71 | + if i % 10 == 0: |
| 72 | + print( |
| 73 | + f"Step {i:4d}: Loss = {loss:.6f} \t (Time per step: {time1 - time0:.4f}s)" |
| 74 | + ) |
| 75 | + |
| 76 | + print("Optimization finished.") |
| 77 | + print(f"Final Loss: {loss:.6f}") |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + run_vqe() |
0 commit comments