-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamiltonian_simulation.py
More file actions
71 lines (57 loc) · 2.55 KB
/
hamiltonian_simulation.py
File metadata and controls
71 lines (57 loc) · 2.55 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# !/usr/bin/env python3
import numpy as np
from scipy.special import jv
from laurent_polynomial import LaurentPolynomial
def truncate_exp(d, tau):
"""
Truncate an exponential function of the form exp(-1.j * tau * cos(theta))
using Chebyshev approximation.
Args:
d (int): Degree of the Laurent polynomial.
tau (float): Parameter for the exponential function.
Returns:
tuple: A tuple containing the truncated Laurent polynomial and the exponential function.
- LaurentPolynomial: Truncated Laurent polynomial.
- function: Exponential function evaluable at given angles.
"""
f_cheb = np.zeros(d + 1, dtype=complex)
for k in range(d + 1):
f_cheb[k] = 2 * jv(k, tau) * [1, - 1.j, -1, 1.j][k % 4]
f_cheb[0] /= 2
f_w_coef = np.hstack((f_cheb[-1:0:-1] / 2, f_cheb[0], f_cheb[1:] / 2))
f_w = LaurentPolynomial(f_w_coef, -d, d)
return f_w, lambda theta: np.exp(-1.j * tau * np.cos(theta))
def truncate_cos(d, cos_scale, noise_scale):
"""
Truncate a cosine function of the form cos_scale * cos(d * theta) + noise.
Args:
d (int): Degree of the Laurent polynomial.
cos_scale (float): Scaling factor for the coefficients.
noise_scale (float): Amplitude of random noise.
Returns:
tuple: A tuple containing the truncated Laurent polynomial and the cosine function.
- LaurentPolynomial: Truncated Laurent polynomial.
- function: Cosine function evaluable at given angles.
"""
f_w_coef = np.random.rand(d * 2 + 1) * noise_scale
f_w_coef[0] += cos_scale
f_w_coef[-1] += cos_scale
f_w = LaurentPolynomial(f_w_coef, -d, d)
return f_w, lambda theta: f_w.eval(theta)
def truncate_exp_plus_cos(d, tau, cos_scale, noise_scale):
"""
Truncate a combination of exponential and cosine functions.
Args:
d (int): Degree of the Laurent polynomials.
tau (float): Parameter for the exponential function.
cos_scale (float): Scaling factor for the cosine function coefficients.
noise_scale (float): Amplitude of random noise for the cosine function.
Returns:
tuple:
A tuple containing the combined truncated Laurent polynomial and the combined function.
- LaurentPolynomial: Combined truncated Laurent polynomial.
- function: Combined function evaluable at given angles.
"""
p_exp, exp = truncate_exp(d, tau)
p_cos, cos = truncate_cos(d, cos_scale, noise_scale)
return p_exp + p_cos, lambda theta: exp(theta) + cos(theta)