-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
147 lines (117 loc) · 3.62 KB
/
main.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
%load_ext autoreload
%autoreload 2
import numpy as np
H = np.zeros(shape=(4, 4))
H[0, 0] = H[3, 3] = 1
H[1, 2] = H[2, 1] = -1
print("Hamiltonian:")
print(H)
print(f"Eigenvalues: {np.linalg.eigvals(H)}")
from vqe.vqe import RYRZAnsatz, RXAnsatz
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 4))
RYRZAnsatz(reps=3, barriers=True).draw("mpl", ax=ax)
fig.suptitle("RYRZ ansatz with 3 repetitions")
fig.savefig("static/images/ryrz_ansatz.png", dpi=90, bbox_inches="tight")
plt.close()
fig, ax = plt.subplots(figsize=(6, 4))
RXAnsatz(reps=3, barriers=True).draw("mpl", ax=ax)
fig.suptitle("RX ansatz with 3 repetitions")
fig.savefig("static/images/rx_ansatz.png", dpi=90, bbox_inches="tight")
plt.close()
from vqe.utils import pauli_decomposition
pauli_decomposition(H)
from vqe.vqe import energy
from vqe.optimizers import SPSA
# The expectation value of the Hamiltonian
def parameterized_energy(params, H, ansatz, **kwargs):
return energy(H, ansatz, params=params, **kwargs)
# Random number generator
seed = 42
rng = np.random.default_rng(seed)
# Optimizer
maxiter = 1000
save_steps = 50
a = 2 * np.pi * 0.1
c = 0.1
A = 0.0001
spsa = SPSA(a=a, c=c, A=A)
# VQE with RYRZ ansatz
reps = 1
thetas_yz = rng.uniform(0, 2 * np.pi, size=(4 * (reps + 1)))
ryrz_ansatz = RYRZAnsatz(reps=reps)
result_yz = spsa.minimize(
parameterized_energy,
thetas_yz,
maxiter=maxiter,
save_steps=save_steps,
seed=seed,
H=H,
ansatz=ryrz_ansatz,
)
print(f"Lowest eigenvalue is {result_yz['fun']:.4f}.")
# VQE with RX ansatz
reps = 1
thetas_x = rng.uniform(0, 2 * np.pi, size=reps)
rx_ansatz = RXAnsatz(reps=reps)
result_x = spsa.minimize(
parameterized_energy,
thetas_x,
maxiter=maxiter,
save_steps=save_steps,
seed=seed,
H=H,
ansatz=rx_ansatz,
)
print(f"Lowest eigenvalue is {result_x['fun']:.4f}.")
from qiskit import Aer
from qiskit.test.mock import FakeVigo
from qiskit.providers.aer.noise import NoiseModel
# Vigo noise model
device_backend = FakeVigo()
coupling_map = device_backend.configuration().coupling_map
noise_model = NoiseModel.from_backend(device_backend)
basis_gates = noise_model.basis_gates
# BasicAer does not support noise, we need the simulator from Aer
backend = Aer.get_backend("qasm_simulator")
# Noisy VQE with RYRZ ansatz
result_yz_noisy = spsa.minimize(
parameterized_energy,
thetas_yz,
maxiter=maxiter,
save_steps=save_steps,
seed=seed,
H=H,
ansatz=ryrz_ansatz,
backend=backend,
noise_model=noise_model,
coupling_map=coupling_map,
basis_gates=basis_gates
)
print(f"Lowest eigenvalue is {result_yz_noisy['fun']:.4f}.")
# Noisy VQE with RX ansatz
result_x_noisy = spsa.minimize(
parameterized_energy,
thetas_x,
maxiter=maxiter,
save_steps=save_steps,
seed=seed,
H=H,
ansatz=rx_ansatz,
backend=backend,
noise_model=noise_model,
coupling_map=coupling_map,
basis_gates=basis_gates
)
print(f"Lowest eigenvalue is {result_x_noisy['fun']:.4f}.")
iters = np.arange(0, maxiter + save_steps, save_steps)
fig, ax = plt.subplots(figsize=(10, 8))
ax.plot(iters, result_yz["log"]["fevals"], color="darkorange", linestyle="solid", label="RYRZ (noiseless)")
ax.plot(iters, result_yz_noisy["log"]["fevals"], color="darkorange", linestyle="dashed", label="RYRZ (noisy)")
ax.plot(iters, result_x["log"]["fevals"], color="dodgerblue", linestyle="solid", label="RX (noiseless)")
ax.plot(iters, result_x_noisy["log"]["fevals"], color="dodgerblue", linestyle="dashed", label="RX (noisy)")
ax.set_xlabel("Iterations")
ax.set_ylabel("Energy")
ax.legend()
fig.savefig("static/images/rx_log.png", bbox_inches="tight", dpi=90)
plt.close()