|
| 1 | +""" |
| 2 | +demonstration of TFIM VQE on V100 with lager qubit number counts |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +import sys |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +logger = logging.getLogger("tensorcircuit") |
| 10 | +logger.setLevel(logging.INFO) |
| 11 | +ch = logging.StreamHandler() |
| 12 | +ch.setLevel(logging.DEBUG) |
| 13 | +logger.addHandler(ch) |
| 14 | + |
| 15 | +sys.setrecursionlimit(10000) |
| 16 | + |
| 17 | +import tensorflow as tf |
| 18 | +import tensorcircuit as tc |
| 19 | +from tensorcircuit import keras |
| 20 | +import cotengra as ctg |
| 21 | + |
| 22 | +optr = ctg.ReusableHyperOptimizer( |
| 23 | + methods=["greedy", "kahypar"], |
| 24 | + parallel=True, |
| 25 | + minimize="flops", |
| 26 | + max_time=120, |
| 27 | + max_repeats=4096, |
| 28 | + progbar=True, |
| 29 | +) |
| 30 | +tc.set_contractor("custom", optimizer=optr, preprocessing=True) |
| 31 | +# tc.set_contractor("custom_stateful", optimizer=oem.RandomGreedy, max_time=60, max_repeats=128, minimize="size") |
| 32 | +tc.set_dtype("complex64") |
| 33 | +tc.set_backend("tensorflow") |
| 34 | +dtype = np.complex64 |
| 35 | + |
| 36 | +import time |
| 37 | + |
| 38 | +nwires, nlayers = 50, 7 |
| 39 | + |
| 40 | + |
| 41 | +def vqe_forward(param, structures): |
| 42 | + split_conf = { |
| 43 | + "max_singular_values": 2, |
| 44 | + "fixed_choice": 1, |
| 45 | + } |
| 46 | + structuresc = tc.backend.cast(structures, dtype="complex64") |
| 47 | + paramc = tc.backend.cast(param, dtype="complex64") |
| 48 | + c = tc.Circuit(nwires, split=split_conf) |
| 49 | + for i in range(nwires): |
| 50 | + c.H(i) |
| 51 | + for j in range(nlayers): |
| 52 | + for i in range(0, nwires - 1): |
| 53 | + c.exp1( |
| 54 | + i, |
| 55 | + (i + 1) % nwires, |
| 56 | + theta=paramc[2 * j, i], |
| 57 | + unitary=tc.gates._zz_matrix, |
| 58 | + ) |
| 59 | + |
| 60 | + for i in range(nwires): |
| 61 | + c.rx(i, theta=paramc[2 * j + 1, i]) |
| 62 | + |
| 63 | + obs = [] |
| 64 | + for i in range(nwires): |
| 65 | + obs.append( |
| 66 | + [ |
| 67 | + tc.gates.Gate( |
| 68 | + sum( |
| 69 | + [ |
| 70 | + structuresc[i, k] * g.tensor |
| 71 | + for k, g in enumerate(tc.gates.pauli_gates) |
| 72 | + ] |
| 73 | + ) |
| 74 | + ), |
| 75 | + (i,), |
| 76 | + ] |
| 77 | + ) |
| 78 | + loss = c.expectation(*obs, reuse=False) |
| 79 | + return tc.backend.real(loss) |
| 80 | + |
| 81 | + |
| 82 | +slist = [] |
| 83 | +for i in range(nwires): |
| 84 | + t = np.zeros(nwires) |
| 85 | + t[i] = 1 |
| 86 | + slist.append(t) |
| 87 | +for i in range(nwires): |
| 88 | + t = np.zeros(nwires) |
| 89 | + t[i] = 3 |
| 90 | + t[(i + 1) % nwires] = 3 |
| 91 | + slist.append(t) |
| 92 | +structures = np.array(slist, dtype=np.int32) |
| 93 | +structures = tc.backend.onehot(structures, num=4) |
| 94 | +structures = tc.backend.reshape(structures, [-1, nwires, 4]) |
| 95 | +print(structures.shape) |
| 96 | +time0 = time.time() |
| 97 | + |
| 98 | +tc_vag = tc.backend.jit( |
| 99 | + tc.backend.vectorized_value_and_grad(vqe_forward, argnums=0, vectorized_argnums=1) |
| 100 | +) |
| 101 | +param = tf.Variable(tf.random.normal(stddev=0.1, shape=[2 * nlayers, nwires])) |
| 102 | + |
| 103 | +print(tc_vag(param, structures[:50])) |
| 104 | + |
| 105 | +time1 = time.time() |
| 106 | +print("staging time: ", time1 - time0) |
| 107 | + |
| 108 | +try: |
| 109 | + keras.save_func(tc_vag, "./funcs/%s_%s_10_tfim" % (nwires, nlayers)) |
| 110 | +except ValueError as e: |
| 111 | + print(e) # keras.save_func now has issues to be resolved |
| 112 | + |
| 113 | + |
| 114 | +def train_step(param): |
| 115 | + vag_list = [] |
| 116 | + for i in range(2): |
| 117 | + vag_list.append(tc_vag(param, structures[i * nwires : i * nwires + nwires])) |
| 118 | + loss = tc.backend.sum(vag_list[0][0] - vag_list[1][0]) |
| 119 | + gr = vag_list[0][1] - vag_list[1][1] |
| 120 | + return loss, gr |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + opt = tf.keras.optimizers.Adam(0.02) |
| 125 | + for j in range(5000): |
| 126 | + loss, gr = train_step(param) |
| 127 | + opt.apply_gradients([(gr, param)]) |
| 128 | + if j % 20 == 0: |
| 129 | + print("loss", loss.numpy()) |
0 commit comments