-
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
14 changed files
with
156 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from copy import deepcopy | ||
|
||
import numpy as np | ||
import qailo as q | ||
|
||
|
||
def qpe(n, u, ev): | ||
m = q.num_qubits(u) | ||
assert q.num_qubits(ev) == n + m | ||
v = deepcopy(ev) | ||
|
||
for p in range(n): | ||
v = q.apply(v, q.op.h(), [p]) | ||
|
||
cp = q.op.controlled(u) | ||
rep = 1 | ||
for p in range(n): | ||
for _ in range(rep): | ||
# print(f"apply cu on {p} and {list(range(n,n+m))}") | ||
v = q.apply(v, cp, [p] + list(range(n, n + m))) | ||
rep = rep * 2 | ||
|
||
v = q.apply_seq(v, q.alg.qft.inverse_qft_seq(n)) | ||
return v | ||
|
||
|
||
if __name__ == "__main__": | ||
n = 3 | ||
phi = 2 * np.pi * (1 / 3) | ||
u = q.op.p(phi) | ||
ev = q.sv.state_vector(n + 1) | ||
ev = q.apply(ev, q.op.x(), [n]) | ||
v = qpe(n, u, ev) | ||
prob = np.diag(q.op.matrix(q.op.trace(q.sv.pure_state(v), [n])).real) | ||
for i in range(len(prob)): | ||
print("{} {}".format(q.util.binary2str(n, i), prob[i])) |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import numpy as np | ||
import qailo as q | ||
import qpe | ||
from pytest import approx | ||
|
||
|
||
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) | ||
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .qft import inverse_qft_seq, qft_seq | ||
|
||
__all__ = [ | ||
inverse_qft_seq, | ||
qft_seq, | ||
] |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# ref: https://learn.qiskit.org/course/ch-algorithms/quantum-fourier-transform | ||
|
||
import numpy as np | ||
|
||
import qailo as q | ||
|
||
|
||
def qft_rotations_seq(n): | ||
seq = [] | ||
if n == 0: | ||
return seq | ||
n -= 1 | ||
# print(f"H on [{n}]") | ||
seq.append([q.op.h(), [n]]) | ||
for p in range(n): | ||
# print(f"CP(pi/{2**(n-p)} on [{p}, {n}]") | ||
seq.append([q.op.cp(np.pi / 2 ** (n - p)), [p, n]]) | ||
seq += qft_rotations_seq(n) | ||
return seq | ||
|
||
|
||
def swap_registers_seq(n): | ||
seq = [] | ||
for p in range(n // 2): | ||
# print(f"swap on [{p}, {n-p-1}]") | ||
seq.append([q.op.swap(), [p, n - p - 1]]) | ||
return seq | ||
|
||
|
||
def qft_seq(n): | ||
"""QFT on the first n qubits in circuit""" | ||
return qft_rotations_seq(n) + swap_registers_seq(n) | ||
|
||
|
||
def inverse_qft_seq(n): | ||
return q.op.inverse_seq(qft_seq(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .hconj import hconj | ||
from .type import is_operator | ||
|
||
|
||
def inverse_seq(seq): | ||
res = [] | ||
for p, pos in seq[::-1]: | ||
assert is_operator(p) | ||
res.append([hconj(p), pos]) | ||
return res |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numpy as np | ||
import qailo as q | ||
from pytest import approx | ||
|
||
|
||
def test_qft(): | ||
n = 3 | ||
target = 5 | ||
|
||
# generate result of qft of target | ||
v = q.sv.state_vector(n) | ||
for p in range(n): | ||
v = q.apply(v, q.op.h(), [p]) | ||
v = q.apply(v, q.op.p(target * np.pi / 4), [0]) | ||
v = q.apply(v, q.op.p(target * np.pi / 2), [1]) | ||
v = q.apply(v, q.op.p(target * np.pi), [2]) | ||
|
||
# apply inverse qft | ||
v = q.apply_seq(v, q.alg.inverse_qft_seq(n)) | ||
|
||
v = q.vector(v) | ||
for i in range(2**n): | ||
if i == target: | ||
print("* {} {}".format(q.util.binary2str(n, i), v[i])) | ||
assert v[i] == approx(1) | ||
else: | ||
print(" {} {}".format(q.util.binary2str(n, i), v[i])) | ||
assert v[i] == approx(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_qft() |
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