Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wistaria committed Nov 17, 2023
1 parent f3fe1f2 commit 099e784
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 54 deletions.
2 changes: 1 addition & 1 deletion example/test_grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_grover():
assert prob[0] == approx(0.581704139709473)
assert prob[1] == approx(0.027886390686035)

v = q.mps_p.zero(n)
v = q.mps.zero(n, mps=q.mps.projector_mps)
prob = q.probability(grover(v, target, iter))
assert prob[0] == approx(0.581704139709473)
assert prob[1] == approx(0.027886390686035)
4 changes: 2 additions & 2 deletions example/test_qpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_qpe():
assert prob[4] == approx(1)
assert prob[0] == approx(0)

v = q.mps_p.zero(1)
v = q.mps.zero(1, mps=q.mps.projector_mps)
v = q.apply(v, q.op.x())
v = q.mps_p.product_state([q.mps.zero(n), v])
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)
Expand Down
2 changes: 1 addition & 1 deletion example/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_simple():
assert np.allclose(q.vector(v, [0, 0, 0]), 1 / np.sqrt(2))
assert np.allclose(q.vector(v, [1, 1, 1]), 1 / np.sqrt(2))

v = q.mps_p.zero(3)
v = q.mps.zero(3, mps=q.mps.projector_mps)
v = simple.main(v)
assert np.allclose(q.vector(v, [0, 0, 0]), 1 / np.sqrt(2))
assert np.allclose(q.vector(v, [1, 1, 1]), 1 / np.sqrt(2))
3 changes: 1 addition & 2 deletions src/qailo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import alg, mps, mps_p, operator, state_vector, util
from . import alg, mps, operator, state_vector, util
from . import operator as op
from . import state_vector as sv
from ._version import version
Expand All @@ -7,7 +7,6 @@
__all__ = [
alg,
mps,
mps_p,
operator,
state_vector,
util,
Expand Down
11 changes: 6 additions & 5 deletions src/qailo/mps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from .apply import apply, apply_seq
from .mps_c import MPS_C
from .mps_c import canonical_mps
from .mps_p import projector_mps
from .norm import norm
from .product_state import one, product_state, tensor_decomposition, zero
from .projector import projector
from .state_vector import state_vector
from .svd import compact_svd, tensor_svd
from .type import is_canonical, is_mps, num_qubits

__all__ = [
apply,
apply_seq,
MPS_C,
canonical_mps,
projector_mps,
norm,
one,
product_state,
tensor_decomposition,
zero,
projector,
state_vector,
compact_svd,
tensor_svd,
is_canonical,
is_mps,
num_qubits,
Expand Down
4 changes: 2 additions & 2 deletions src/qailo/mps/mps_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from ..operator import type as op
from .svd import tensor_svd
from .type import MPS
from .type import mps


class MPS_C(MPS):
class canonical_mps(mps):
"""
MPS representation of quantum pure state
Expand Down
4 changes: 2 additions & 2 deletions src/qailo/mps/mps_p.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import numpy as np

from ..mps.svd import tensor_svd
from ..mps.type import MPS
from ..mps.type import mps
from ..operator import type as op
from .projector import projector


class MPS_P(MPS):
class projector_mps(mps):
"""
MPS representation of quantum pure state
Expand Down
8 changes: 4 additions & 4 deletions src/qailo/mps/product_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ..state_vector.state_vector import one as sv_one
from ..state_vector.state_vector import zero as sv_zero
from ..state_vector.vector import vector
from .mps_c import MPS_C
from .mps_c import canonical_mps
from .svd import tensor_svd
from .type import is_mps

Expand All @@ -23,16 +23,16 @@ def tensor_decomposition(v, nkeep=None, tol=1e-12):
return tensors


def product_state(states, nkeep=None, mps=MPS_C):
def product_state(states, nkeep=None, mps=canonical_mps):
tensors = []
for s in states:
tensors = tensors + tensor_decomposition(s, nkeep)
return mps(tensors, nkeep)


def zero(n=1, nkeep=None, mps=MPS_C):
def zero(n=1, nkeep=None, mps=canonical_mps):
return product_state([sv_zero()] * n, nkeep, mps)


def one(n=1, nkeep=None, mps=MPS_C):
def one(n=1, nkeep=None, mps=canonical_mps):
return product_state([sv_one()] * n, nkeep, mps)
4 changes: 2 additions & 2 deletions src/qailo/mps/type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MPS(object):
class mps(object):
def __init__(self):
True

Expand All @@ -8,7 +8,7 @@ def is_canonical(m):


def is_mps(m):
return isinstance(m, MPS)
return isinstance(m, mps)


def num_qubits(m):
Expand Down
10 changes: 5 additions & 5 deletions test/mps/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def test_apply():
p = 64
nkeep = 2

m0 = q.mps.zero(n, mps=q.mps.MPS_C)
m1 = q.mps.zero(n, mps=q.mps_p.MPS_P)
m2 = q.mps.zero(n, nkeep=nkeep, mps=q.mps.MPS_C)
m3 = q.mps.zero(n, nkeep=nkeep, mps=q.mps_p.MPS_P)
m0 = q.mps.zero(n, mps=q.mps.canonical_mps)
m1 = q.mps.zero(n, mps=q.mps.projector_mps)
m2 = q.mps.zero(n, nkeep=nkeep, mps=q.mps.canonical_mps)
m3 = q.mps.zero(n, nkeep=nkeep, mps=q.mps.projector_mps)
v = q.sv.zero(n)
seq = []

Expand Down Expand Up @@ -77,7 +77,7 @@ def test_apply():
assert f0 == approx(1)
assert f1 == approx(1)

m4 = q.mps.zero(n, mps=q.mps_p.MPS_P)
m4 = q.mps.zero(n, mps=q.mps.projector_mps)
f4 = q.sv.fidelity(q.mps.state_vector(q.mps.apply_seq(m4, seq)), v)
assert f4 == approx(1)

Expand Down
4 changes: 2 additions & 2 deletions test/mps/test_canonical.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_canonical():
tensors.append(np.random.random((d, 2, dn)))
d = dn
tensors.append(np.random.random((d, 2, 1)))
for mps in [q.mps.MPS_C, q.mps_p.MPS_P]:
for mps in [q.mps.canonical_mps, q.mps.projector_mps]:
m = mps(tensors)
norm = q.mps.norm(m)
assert q.mps.norm(m) == approx(norm)
Expand All @@ -36,7 +36,7 @@ def test_canonical():
v = np.random.random(2**n).reshape((2,) * n + (1,))
v /= np.linalg.norm(v)
tensors = q.mps.tensor_decomposition(v, maxdim)
for mps in [q.mps.MPS_C, q.mps_p.MPS_P]:
for mps in [q.mps.canonical_mps, q.mps.projector_mps]:
m = mps(tensors)
norm = q.mps.norm(m)

Expand Down
4 changes: 2 additions & 2 deletions test/mps/test_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_swap():
tensors.append(np.random.random((d, 2, dn)))
d = dn
tensors.append(np.random.random((d, 2, 1)))
for mps in [q.mps.MPS_C, q.mps_p.MPS_P]:
for mps in [q.mps.canonical_mps, q.mps.projector_mps]:
m = mps(tensors)
q.mps.is_canonical(m)
norm = q.mps.norm(m)
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_move():
tensors.append(np.random.random((d, 2, dn)))
d = dn
tensors.append(np.random.random((d, 2, 1)))
for mps in [q.mps.MPS_C, q.mps_p.MPS_P]:
for mps in [q.mps.canonical_mps, q.mps.projector_mps]:
m = mps(tensors)
q.mps.is_canonical(m)
norm = q.mps.norm(m)
Expand Down
2 changes: 1 addition & 1 deletion test/mps/test_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_mps():
states = [q.sv.one(), q.sv.one(), q.sv.zero(), q.sv.zero()]
v = q.sv.product_state(states)
m0 = q.mps.product_state(states)
m1 = q.mps_p.product_state(states)
m1 = q.mps.product_state(states, mps=q.mps.projector_mps)
v0 = q.mps.state_vector(m0)
v1 = q.mps.state_vector(m1)
assert np.allclose(v0, v)
Expand Down
13 changes: 7 additions & 6 deletions test/mps/test_projector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import qailo as q
from qailo.mps.svd import compact_svd, tensor_svd


def test_projector():
Expand All @@ -9,26 +10,26 @@ def test_projector():
n0, n1, n2, d = np.random.randint(2, maxn, size=(4,))
T0 = np.random.random((n0, n1))
T1 = np.random.random((n1, n2))
_, WLh, WR = q.mps_p.projector(T0, [0, 2], T1, [2, 1], nkeep=d)
_, WLh, WR = q.mps.projector(T0, [0, 2], T1, [2, 1], nkeep=d)
assert WLh.shape[1] <= d and WR.shape[1] <= d
assert WLh.shape[0] == n1 and WR.shape[0] == n1
A = np.einsum(T0, [0, 2], WR, [2, 3], WLh.T, [3, 4], T1, [4, 1])

S, U, V = q.mps.compact_svd(np.einsum(T0, [0, 2], T1, [2, 1]), nkeep=d)
S, U, V = compact_svd(np.einsum(T0, [0, 2], T1, [2, 1]), nkeep=d)
B = np.einsum("k,ik,jk->ij", S, U, V)
assert np.allclose(A, B)

for _ in range(nt):
n0, n1, n2, n3, n4, n5, d = np.random.randint(2, maxn, size=(7,))
T0 = np.random.random((n0, n1, n2, n3))
T1 = np.random.random((n3, n2, n4, n5))
_, WLh, WR = q.mps_p.projector(T0, [0, 1, 4, 5], T1, [5, 4, 2, 3], nkeep=d)
_, WLh, WR = q.mps.projector(T0, [0, 1, 4, 5], T1, [5, 4, 2, 3], nkeep=d)
assert WLh.shape[2] <= d and WR.shape[2] <= d
assert WLh.shape[0] == n2 and WLh.shape[1] == n3
assert WR.shape[0] == n2 and WR.shape[1] == n3
A = np.einsum(T0, [0, 1, 4, 5], WR, [4, 5, 6], WLh, [7, 8, 6], T1, [8, 7, 2, 3])

L, R = q.mps.tensor_svd(
L, R = tensor_svd(
np.einsum(T0, [0, 1, 4, 5], T1, [5, 4, 2, 3]), [[0, 1], [2, 3]], nkeep=d
)
B = np.einsum(L, [0, 1, 4], R, [4, 2, 3])
Expand All @@ -48,7 +49,7 @@ def test_projector():
assert np.allclose(np.einsum(t0, [0, 1, 4, 5], t1, [5, 4, 2, 3]), B)
tt0 = np.einsum(w0, [0, 4], t0, [4, 1, 2, 3])
tt1 = np.einsum(t1, [0, 1, 2, 4], w2, [4, 3])
_, WLh, WR = q.mps_p.projector(tt0, [0, 1, 4, 5], tt1, [5, 4, 2, 3])
_, WLh, WR = q.mps.projector(tt0, [0, 1, 4, 5], tt1, [5, 4, 2, 3])
assert np.allclose(
np.einsum(WLh, [2, 3, 0], WR, [2, 3, 1]), np.identity(WLh.shape[2])
)
Expand All @@ -72,7 +73,7 @@ def test_projector():
assert np.allclose(np.einsum(t0, [0, 1, 4, 5], t1, [4, 5, 2, 3]), B)
tt0 = np.einsum(w0, [0, 4], t0, [4, 1, 2, 3])
tt1 = np.einsum(t1, [0, 1, 2, 4], w2, [4, 3])
_, WLh, WR = q.mps_p.projector(tt0, [0, 1, 4, 5], tt1, [4, 5, 2, 3])
_, WLh, WR = q.mps.projector(tt0, [0, 1, 4, 5], tt1, [4, 5, 2, 3])
assert np.allclose(
np.einsum(WLh, [2, 3, 0], WR, [2, 3, 1]), np.identity(WLh.shape[2])
)
Expand Down
Loading

0 comments on commit 099e784

Please sign in to comment.