-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
95 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import numpy as np | ||
import qailo as q | ||
import qpe | ||
from pytest import approx | ||
from qpe import qpe | ||
|
||
|
||
def test_qpe(): | ||
n = 3 | ||
phi = 2 * np.pi * (1 / 8) | ||
u = q.op.p(phi) | ||
ev = q.sv.state_vector(n + 1) | ||
ev = q.apply(ev, q.op.x(), [n]) | ||
v = qpe.qpe(n, u, ev) | ||
ev = q.sv.zero() | ||
ev = q.apply(ev, q.op.x()) | ||
v = qpe(n, u, ev) | ||
prob = np.diag(q.op.matrix(q.op.trace(q.sv.pure_state(v), [n])).real) | ||
assert prob[4] == approx(1) | ||
assert prob[0] == approx(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
import numpy as np | ||
|
||
from ..state_vector.state_vector import one as sv_one | ||
from ..state_vector.state_vector import zero as sv_zero | ||
from ..state_vector.type import num_qubits | ||
from ..state_vector.vector import vector | ||
from .mps_c import MPS_C | ||
from .svd import tensor_svd | ||
|
||
|
||
def product_state(n, c=0): | ||
assert n > 0 | ||
tensors = [] | ||
for t in range(n): | ||
tensor = np.zeros((1, 2, 1)) | ||
tensor[0, (c >> (n - t - 1)) & 1, 0] = 1 | ||
tensors.append(tensor) | ||
return tensors | ||
|
||
|
||
def tensor_decomposition(v, nkeep=None, tol=1e-12): | ||
n = num_qubits(v) | ||
vv = vector(v).reshape((1, 2**n)) | ||
w = vector(v).reshape((1, 2**n)) | ||
tensors = [] | ||
for t in range(n - 1): | ||
dims = vv.shape | ||
vv = vv.reshape(dims[0], 2, dims[1] // 2) | ||
t, vv = tensor_svd(vv, [[0, 1], [2]], "left", nkeep, tol) | ||
dims = w.shape | ||
w = w.reshape(dims[0], 2, dims[1] // 2) | ||
t, w = tensor_svd(w, [[0, 1], [2]], "left", nkeep, tol) | ||
tensors.append(t) | ||
tensors.append(vv.reshape(vv.shape + (1,))) | ||
tensors.append(w.reshape(w.shape + (1,))) | ||
return tensors | ||
|
||
|
||
def product_state(states, mps=MPS_C): | ||
tensors = [] | ||
for s in states: | ||
tensors = tensors + tensor_decomposition(s) | ||
return mps(tensors) | ||
|
||
|
||
def zero(n, mps=MPS_C): | ||
return product_state([sv_zero()] * n, mps) | ||
|
||
|
||
def one(n, mps=MPS_C): | ||
return product_state([sv_one()] * n, mps) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
import numpy as np | ||
|
||
|
||
def zero(): | ||
return np.array((1, 0)).reshape((2, 1)) | ||
|
||
|
||
def one(): | ||
return np.array((0, 1)).reshape((2, 1)) | ||
from .type import is_state_vector, num_qubits | ||
|
||
|
||
def product_state(states): | ||
n = len(states) | ||
assert n > 0 | ||
m = len(states) | ||
assert m > 0 | ||
v = states[0] | ||
print(0, v.shape) | ||
for i in range(1, n): | ||
v = np.einsum(v.reshape((2,) * (i)), list(range(i)), states[i], [i + 1, i + 2]) | ||
print(i, v.shape) | ||
assert is_state_vector(v) | ||
for i in range(1, m): | ||
n0 = num_qubits(v) | ||
n1 = num_qubits(states[i]) | ||
v = np.einsum( | ||
v.reshape((2,) * n0), | ||
list(range(n0)), | ||
states[i], | ||
list(range(n0, n0 + n1 + 1)), | ||
) | ||
return v | ||
|
||
|
||
def state_vector(n, c=0): | ||
v = np.zeros(2**n) | ||
v[c] = 1 | ||
return v.reshape((2,) * n + (1,)) | ||
def zero(n=1): | ||
assert n > 0 | ||
if n == 1: | ||
return np.array((1, 0)).reshape((2, 1)) | ||
else: | ||
return product_state([zero(1)] * n) | ||
|
||
|
||
def one(n=1): | ||
assert n > 0 | ||
if n == 1: | ||
return np.array((0, 1)).reshape((2, 1)) | ||
else: | ||
return product_state([one(1)] * n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters