|
| 1 | +import datetime |
| 2 | +import sys |
| 3 | +import uuid |
| 4 | +import cpuinfo |
| 5 | + |
| 6 | +import tensorcircuit as tc |
| 7 | + |
| 8 | +tc.set_backend("tensorflow") |
| 9 | +import tensorflow as tf |
| 10 | +from functools import reduce |
| 11 | +from operator import mul |
| 12 | +import qibo |
| 13 | + |
| 14 | +qibo.set_backend("tensorflow") |
| 15 | +from qibo import gates, models, hamiltonians |
| 16 | +from qibo.symbols import I, X, Z |
| 17 | +import utils |
| 18 | + |
| 19 | + |
| 20 | +def qibo_benchmark(uuid, n, nlayer, nitrs, timeLimit, minus=1): |
| 21 | + @tf.function |
| 22 | + def geth(s, n): |
| 23 | + ctc = tc.Circuit(n, inputs=s) |
| 24 | + loss = 0.0 |
| 25 | + for i in range(n - 1): |
| 26 | + loss += ctc.expectation_ps(z=[i, i + 1]) |
| 27 | + for i in range(n): |
| 28 | + loss -= ctc.expectation_ps(x=[i]) |
| 29 | + return loss |
| 30 | + |
| 31 | + @tf.function # jit will raise error |
| 32 | + def optimize(params): |
| 33 | + with tf.GradientTape() as tape: |
| 34 | + c = models.Circuit(n) |
| 35 | + for i in range(n): |
| 36 | + c.add(gates.H(i)) |
| 37 | + for j in range(nlayer): |
| 38 | + for i in range(n - minus): |
| 39 | + c.add(gates.CNOT(i, i + 1)) |
| 40 | + c.add(gates.RZ(i + 1, theta=params[j, i, 0])) |
| 41 | + c.add(gates.CNOT(i, i + 1)) |
| 42 | + for i in range(n): |
| 43 | + c.add(gates.RX(0, theta=params[j, i, 1])) |
| 44 | + s = c().state() |
| 45 | + # h = hamiltonians.TFIM(n, h=-1) |
| 46 | + # loss = h.expectation(s) |
| 47 | + # failed in jit |
| 48 | + # qibo lacks the API for local expectation, using tc as assistance |
| 49 | + # we must ensure the observable is computed term by term |
| 50 | + # instead of the expectation for the Hamiltonian as a whole |
| 51 | + # to ensure the comparison is fair |
| 52 | + |
| 53 | + loss = geth(s, n) |
| 54 | + |
| 55 | + grads = tape.gradient(loss, params) |
| 56 | + return loss, grads |
| 57 | + |
| 58 | + meta = {} |
| 59 | + meta["Software"] = "qibo" |
| 60 | + meta["minus"] = minus |
| 61 | + meta["Cpuinfo"] = cpuinfo.get_cpu_info()["brand_raw"] |
| 62 | + meta["Version"] = { |
| 63 | + "sys": sys.version, |
| 64 | + "qibo": qibo.__version__, |
| 65 | + } |
| 66 | + meta["VQE test parameters"] = { |
| 67 | + "nQubits": n, |
| 68 | + "nlayer": nlayer, |
| 69 | + "nitrs": nitrs, |
| 70 | + "timeLimit": timeLimit, |
| 71 | + } |
| 72 | + meta["UUID"] = uuid |
| 73 | + meta["Benchmark Time"] = ( |
| 74 | + datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M %Z") |
| 75 | + ) |
| 76 | + meta["Results"] = {} |
| 77 | + params = tf.Variable(tf.random.normal((nlayer, n, 2), dtype=tf.float64)) |
| 78 | + opt = utils.Opt(optimize, params, tuning=True, backend="tensorflow") |
| 79 | + ct, it, Nitrs = utils.timing(opt.step, nitrs, timeLimit) |
| 80 | + meta["Results"]["with jit"] = { |
| 81 | + "Construction time": ct, |
| 82 | + "Iteration time": it, |
| 83 | + "# of actual iterations": Nitrs, |
| 84 | + } |
| 85 | + print(meta) |
| 86 | + return meta |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + _uuid = str(uuid.uuid4()) |
| 91 | + ( |
| 92 | + n, |
| 93 | + nlayer, |
| 94 | + nitrs, |
| 95 | + timeLimit, |
| 96 | + isgpu, |
| 97 | + minus, |
| 98 | + path, |
| 99 | + ) = utils.arg() |
| 100 | + |
| 101 | + r = qibo_benchmark( |
| 102 | + _uuid, |
| 103 | + n, |
| 104 | + nlayer, |
| 105 | + nitrs, |
| 106 | + timeLimit, |
| 107 | + minus=minus, |
| 108 | + ) |
| 109 | + utils.save(r, _uuid, path) |
0 commit comments