-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_phase_estimate.py
More file actions
52 lines (40 loc) · 1.17 KB
/
Copy pathtest_phase_estimate.py
File metadata and controls
52 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# Author: Armit
# Create Time: 2023/03/30
from tiny_q import *
from pprint import pp
import numpy as np
# quantum phase estimation algorithm
# https://en.wikipedia.org/wiki/Quantum_phase_estimation_algorithm
# https://zhuanlan.zhihu.com/p/84568388
# https://blog.csdn.net/qq_45777142/article/details/109904362
# test arbitary gate
u = U1(0, pi/3, pi/4, pi/5)
eigenvals, eigenvecs = np.linalg.eig(u.v)
for i in range(2):
# Ax = λx
assert np.allclose(u.v @ eigenvecs[:, i], eigenvals[i] * eigenvecs[:, i])
print('eigenval:', eigenvals[i])
print('eigenvec:', eigenvecs[:, i])
phi = State(eigenvecs[:, i])
q = phase_estimate(u, phi)
q.info()
r = q > Measure()
print('estimated:', r)
# test known unitary
for u in [H, X, Y, Z, I]:
eigenvals, eigenvecs = np.linalg.eig(u.v)
for i in range(2):
print('eigenval:', eigenvals[i])
print('eigenvec:', eigenvecs[:, i])
phi = State(eigenvecs[:, i])
q = phase_estimate(u, phi)
q.info()
#r = q > Measure()
#print('estimated:', r)
u = U1(pi/3, pi/4, pi/5, pi/6)
q = phase_estimate(u, n_prec=3)
pp(q > Measure())
phi = v0
q = phase_estimate(u, phi, n_prec=3)
pp(q > Measure())