|
| 1 | +""" |
| 2 | +demo example of mipt in tc style, with ideal p for each history trajectory |
| 3 | +p is also jittable now, change parameter p doesn't trigger recompiling |
| 4 | +""" |
| 5 | + |
| 6 | +from functools import partial |
| 7 | +import time |
| 8 | +import numpy as np |
| 9 | +from scipy import stats |
| 10 | +import tensorcircuit as tc |
| 11 | + |
| 12 | +K = tc.set_backend("jax") |
| 13 | +tc.set_dtype("complex128") |
| 14 | +# tf backend is slow (at least on cpu) |
| 15 | + |
| 16 | + |
| 17 | +def delete2(pick, plist): |
| 18 | + # pick = 0, 1 : return plist[pick]/(plist[0]+plist[1]) |
| 19 | + # pick = 2: return 1 |
| 20 | + indicator = (K.sign(1.5 - pick) + 1) / 2 # 0,1 : 1, 2: 0 |
| 21 | + p = 0 |
| 22 | + p += 1 - indicator |
| 23 | + p += indicator / (plist[0] + plist[1]) * (plist[0] * (1 - pick) + plist[1] * pick) |
| 24 | + return p |
| 25 | + |
| 26 | + |
| 27 | +@partial(K.jit, static_argnums=(2, 3)) |
| 28 | +def circuit_output(random_matrix, status, n, d, p): |
| 29 | + """ |
| 30 | + mipt circuit |
| 31 | +
|
| 32 | + :param random_matrix: a float or complex tensor containing 4*4 random haar matrix wth size [d*n, 4, 4] |
| 33 | + :type random_matrix: _type_ |
| 34 | + :param status: a int tensor with element in 0 or 1 or 2 (no meausrement) with size d*n |
| 35 | + :type status: _type_ |
| 36 | + :param n: number of qubits |
| 37 | + :type n: _type_ |
| 38 | + :param d: number of depth |
| 39 | + :type d: _type_ |
| 40 | + :param p: measurement ratio |
| 41 | + :type p: float |
| 42 | + :return: output state |
| 43 | + """ |
| 44 | + random_matrix = K.reshape(random_matrix, [d, n, 4, 4]) |
| 45 | + status = K.reshape(status, [d, n]) |
| 46 | + inputs = None |
| 47 | + bs_history = [] |
| 48 | + prob_history = [] |
| 49 | + for j in range(d): |
| 50 | + if inputs is None: |
| 51 | + c = tc.Circuit(n) |
| 52 | + else: |
| 53 | + c = tc.Circuit(n, inputs=inputs) |
| 54 | + for i in range(0, n, 2): |
| 55 | + c.unitary(i, (i + 1) % n, unitary=random_matrix[j, i]) |
| 56 | + for i in range(1, n, 2): |
| 57 | + c.unitary(i, (i + 1) % n, unitary=random_matrix[j, i]) |
| 58 | + inputs = c.state() |
| 59 | + c = tc.Circuit(n, inputs=inputs) |
| 60 | + for i in range(n): |
| 61 | + pick, plist = c.general_kraus( |
| 62 | + [ |
| 63 | + K.sqrt(p) * K.convert_to_tensor(np.array([[1.0, 0], [0, 0]])), |
| 64 | + K.sqrt(p) * K.convert_to_tensor(np.array([[0, 0], [0, 1.0]])), |
| 65 | + K.sqrt(1 - p) * K.eye(2), |
| 66 | + ], |
| 67 | + i, |
| 68 | + status=status[j, i], |
| 69 | + with_prob=True, |
| 70 | + ) |
| 71 | + bs_history.append(pick) |
| 72 | + prob_history.append(delete2(pick, plist)) |
| 73 | + inputs = c.state() |
| 74 | + c = tc.Circuit(n, inputs=inputs) |
| 75 | + inputs = c.state() |
| 76 | + inputs /= K.norm(inputs) |
| 77 | + bs_history = K.stack(bs_history) |
| 78 | + prob_history = K.stack(prob_history) |
| 79 | + return inputs, bs_history, prob_history, K.sum(K.log(prob_history + 1e-11)) |
| 80 | + |
| 81 | + |
| 82 | +@partial(K.jit, static_argnums=(2, 3)) |
| 83 | +def cals(random_matrix, status, n, d, p): |
| 84 | + state, bs_history, prob_history, prob = circuit_output( |
| 85 | + random_matrix, status, n, d, p |
| 86 | + ) |
| 87 | + rho = tc.quantum.reduced_density_matrix(state, cut=[i for i in range(n // 2)]) |
| 88 | + return ( |
| 89 | + tc.quantum.entropy(rho), |
| 90 | + tc.quantum.renyi_entropy(rho, k=2), |
| 91 | + bs_history, |
| 92 | + prob_history, |
| 93 | + prob, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + n = 12 |
| 99 | + d = 12 |
| 100 | + st = np.random.uniform(size=[d * n]) |
| 101 | + ## assume all X gate instead |
| 102 | + rm = [stats.unitary_group.rvs(4) for _ in range(d * n)] |
| 103 | + rm = [r / np.linalg.det(r) for r in rm] |
| 104 | + rm = np.stack(rm) |
| 105 | + time0 = time.time() |
| 106 | + print(cals(rm, st, n, d, 0.6)) |
| 107 | + time1 = time.time() |
| 108 | + st = np.random.uniform(size=[d * n]) |
| 109 | + print(cals(rm, st, n, d, 0.1)) |
| 110 | + time2 = time.time() |
| 111 | + print(f"compiling time {time1-time0}, running time {time2-time1}") |
0 commit comments