|
| 1 | +""" |
| 2 | +boosting the monte carlo noise simulation on general error with circuit layerwise slicing |
| 3 | +""" |
| 4 | + |
| 5 | +from functools import partial |
| 6 | +import time |
| 7 | +import jax |
| 8 | + |
| 9 | + |
| 10 | +import tensorcircuit as tc |
| 11 | + |
| 12 | +tc.set_backend("jax") |
| 13 | + |
| 14 | +n = 10 |
| 15 | +nlayer = 4 |
| 16 | + |
| 17 | + |
| 18 | +@partial(tc.backend.jit, static_argnums=(2, 3)) |
| 19 | +def f1(key, param, n, nlayer): |
| 20 | + if key is not None: |
| 21 | + tc.backend.set_random_state(key) |
| 22 | + c = tc.Circuit(n) |
| 23 | + for i in range(n): |
| 24 | + c.H(i) |
| 25 | + for j in range(nlayer): |
| 26 | + for i in range(n - 1): |
| 27 | + c.cnot(i, i + 1) |
| 28 | + c.apply_general_kraus(tc.channels.phasedampingchannel(0.15), i) |
| 29 | + c.apply_general_kraus(tc.channels.phasedampingchannel(0.15), i + 1) |
| 30 | + for i in range(n): |
| 31 | + c.rx(i, theta=param[j, i]) |
| 32 | + return tc.backend.real(c.expectation((tc.gates.z(), [int(n / 2)]))) |
| 33 | + |
| 34 | + |
| 35 | +@partial(tc.backend.jit, static_argnums=(2)) |
| 36 | +def templatecnot(s, param, i): |
| 37 | + c = tc.Circuit(n, inputs=s) |
| 38 | + c.cnot(i, i + 1) |
| 39 | + return c.state() |
| 40 | + |
| 41 | + |
| 42 | +@partial(tc.backend.jit, static_argnums=(3)) |
| 43 | +def templatenoise(key, s, param, i): |
| 44 | + c = tc.Circuit(n, inputs=s) |
| 45 | + status = tc.backend.stateful_randu(key)[0] |
| 46 | + c.apply_general_kraus(tc.channels.phasedampingchannel(0.15), i, status=status) |
| 47 | + return c.state() |
| 48 | + |
| 49 | + |
| 50 | +@partial(tc.backend.jit, static_argnums=(2)) |
| 51 | +def templaterz(s, param, j): |
| 52 | + c = tc.Circuit(n, inputs=s) |
| 53 | + for i in range(n): |
| 54 | + c.rx(i, theta=param[j, i]) |
| 55 | + return c.state() |
| 56 | + |
| 57 | + |
| 58 | +@partial(tc.backend.jit, static_argnums=(2, 3)) |
| 59 | +def f2(key, param, n, nlayer): |
| 60 | + c = tc.Circuit(n) |
| 61 | + for i in range(n): |
| 62 | + c.H(i) |
| 63 | + s = c.state() |
| 64 | + for j in range(nlayer): |
| 65 | + for i in range(n - 1): |
| 66 | + s = templatecnot(s, param, i) |
| 67 | + key, subkey = tc.backend.random_split(key) |
| 68 | + s = templatenoise(subkey, s, param, i) |
| 69 | + key, subkey = tc.backend.random_split(key) |
| 70 | + s = templatenoise(subkey, s, param, i + 1) |
| 71 | + s = templaterz(s, param, j) |
| 72 | + return tc.backend.real(tc.expectation((tc.gates.z(), [int(n / 2)]), ket=s)) |
| 73 | + |
| 74 | + |
| 75 | +vagf1 = tc.backend.jit(tc.backend.value_and_grad(f1, argnums=1), static_argnums=(2, 3)) |
| 76 | + |
| 77 | +vagf2 = tc.backend.jit(tc.backend.value_and_grad(f2, argnums=1), static_argnums=(2, 3)) |
| 78 | + |
| 79 | +param = tc.backend.ones([nlayer, n]) |
| 80 | + |
| 81 | + |
| 82 | +def benchmark(f, tries=3): |
| 83 | + time0 = time.time() |
| 84 | + key = tc.backend.get_random_state(42) |
| 85 | + print(f(key, param, n, nlayer)[0]) |
| 86 | + time1 = time.time() |
| 87 | + for _ in range(tries): |
| 88 | + print(f(key, param, n, nlayer)[0]) |
| 89 | + time2 = time.time() |
| 90 | + print( |
| 91 | + "staging time: ", |
| 92 | + time1 - time0, |
| 93 | + "running time: ", |
| 94 | + (time2 - time1) / tries, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +print("without layerwise slicing jit") |
| 99 | +benchmark(vagf1) |
| 100 | +print("=============================") |
| 101 | +print("with layerwise slicing jit") |
| 102 | +benchmark(vagf2) |
| 103 | + |
| 104 | +# 235/0.36 vs. 26/0.04 |
0 commit comments