From 1d3a03bba03d2164f8da816938bf3aadfa71c54c Mon Sep 17 00:00:00 2001 From: Synge Todo Date: Sun, 10 Dec 2023 15:55:34 +0900 Subject: [PATCH] update --- example/qpe.py | 2 +- example/test_qpe.py | 14 +++++++------- src/qailo/mps/svd.py | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/example/qpe.py b/example/qpe.py index ba37dbb..8f1429a 100644 --- a/example/qpe.py +++ b/example/qpe.py @@ -26,7 +26,7 @@ def qpe(n, u, v): if __name__ == "__main__": n = 3 - phi = 2 * np.pi * (1 / 3) + phi = 2 * np.pi * 0.7 u = q.op.p(phi) v = q.sv.zero() v = q.apply(v, q.op.x()) diff --git a/example/test_qpe.py b/example/test_qpe.py index 138a78d..cc5d132 100644 --- a/example/test_qpe.py +++ b/example/test_qpe.py @@ -6,7 +6,7 @@ def test_qpe(): n = 3 - phi = 2 * np.pi * (1 / 8) + phi = 2 * np.pi * 0.7 u = q.op.p(phi) v = q.sv.zero() @@ -14,24 +14,24 @@ def test_qpe(): v = q.sv.product_state([q.sv.zero(n), v]) v = qpe(n, u, v) prob = q.probability(v, list(range(n))) - assert prob[4] == approx(1) - assert prob[0] == approx(0) + assert prob[0] == approx(0.02159321892578291) + assert prob[3] == approx(0.5775210180698607) v = q.mps.zero(1) v = q.apply(v, q.op.x()) v = q.mps.product_state([q.mps.zero(n), v]) v = qpe(n, u, v) prob = q.probability(v, list(range(n))) - assert prob[4] == approx(1) - assert prob[0] == approx(0) + assert prob[0] == approx(0.02159321892578291) + assert prob[3] == approx(0.5775210180698607) v = q.mps.zero(1, mps=q.mps.projector_mps) v = q.apply(v, q.op.x()) v = q.mps.product_state([q.mps.zero(n, mps=q.mps.projector_mps), v]) v = qpe(n, u, v) prob = q.probability(v, list(range(n))) - assert prob[4] == approx(1) - assert prob[0] == approx(0) + assert prob[0] == approx(0.02159321892578291) + assert prob[3] == approx(0.5775210180698607) if __name__ == "__main__": diff --git a/src/qailo/mps/svd.py b/src/qailo/mps/svd.py index 26c42be..934c7e2 100644 --- a/src/qailo/mps/svd.py +++ b/src/qailo/mps/svd.py @@ -11,14 +11,14 @@ def compact_svd(A, nkeep=None, tol=1e-12): def tensor_svd(T, partition, canonical="center", nkeep=None, tol=1e-12): - legs0 = len(partition[0]) - legs1 = len(partition[1]) - assert len(T.shape) == legs0 + legs1 + legsL = len(partition[0]) + legsR = len(partition[1]) + assert len(T.shape) == legsL + legsR assert sorted(partition[0] + partition[1]) == list(range(len(T.shape))) - dims0 = [T.shape[i] for i in partition[0]] - dims1 = [T.shape[i] for i in partition[1]] + dimsL = [T.shape[i] for i in partition[0]] + dimsR = [T.shape[i] for i in partition[1]] m = np.einsum(T, partition[0] + partition[1]).reshape( - np.prod(dims0), np.prod(dims1) + np.prod(dimsL), np.prod(dimsR) ) S, U, V = compact_svd(m, nkeep=nkeep, tol=tol) L = U @@ -32,6 +32,6 @@ def tensor_svd(T, partition, canonical="center", nkeep=None, tol=1e-12): L = np.einsum("ij,j->ij", L, S) else: raise ValueError - L = L.reshape(dims0 + [S.shape[0]]) - R = R.reshape([S.shape[0]] + dims1) + L = L.reshape(dimsL + [S.shape[0]]) + R = R.reshape([S.shape[0]] + dimsR) return L, R