2020-02-21 16:59:32 +00:00
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
2020-02-28 15:09:51 +00:00
|
|
|
import matplotlib
|
2020-02-21 16:59:32 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
from scipy.linalg import expm
|
2020-02-21 16:59:32 +00:00
|
|
|
from pyqcs import State, sample
|
2020-02-28 15:09:51 +00:00
|
|
|
|
2020-02-21 16:59:32 +00:00
|
|
|
from transfer_matrix import T_time_slice
|
|
|
|
from hamiltonian import H
|
2020-02-28 15:09:51 +00:00
|
|
|
from bootstrap import bootstrap
|
2020-02-21 16:59:32 +00:00
|
|
|
|
2020-02-24 11:06:33 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
np.random.seed(0xdeadbeef)
|
|
|
|
|
|
|
|
matplotlib.rcParams.update(
|
|
|
|
{'errorbar.capsize': 2
|
|
|
|
, 'figure.figsize': (16, 9)}
|
|
|
|
)
|
|
|
|
|
|
|
|
nqbits = 6
|
|
|
|
g = 3
|
|
|
|
N_trot = 80
|
2020-02-21 16:59:32 +00:00
|
|
|
t_stop = 9
|
2020-02-28 15:09:51 +00:00
|
|
|
delta_t = 0.09
|
2020-02-21 16:59:32 +00:00
|
|
|
qbits = list(range(nqbits))
|
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
n_sample = 2200
|
2020-02-21 16:59:32 +00:00
|
|
|
measure = 0b10
|
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
measure_coefficient_mask = [False if (i & measure) else True for i in range(2**nqbits)]
|
|
|
|
|
2020-02-21 16:59:32 +00:00
|
|
|
|
|
|
|
results_qc = []
|
2020-02-24 11:06:33 +00:00
|
|
|
results_np = []
|
2020-02-28 15:09:51 +00:00
|
|
|
errors_sampling = []
|
|
|
|
|
2020-02-21 16:59:32 +00:00
|
|
|
print()
|
|
|
|
for t in np.arange(0, t_stop, delta_t):
|
|
|
|
# QC simulation
|
|
|
|
state = State.new_zero_state(nqbits)
|
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
T_dt = T_time_slice(qbits, t, g, N_trot)
|
|
|
|
for _ in range(N_trot):
|
|
|
|
state = T_dt * state
|
2020-02-21 16:59:32 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
result = sample(state, measure, n_sample)
|
|
|
|
results_qc.append(result[0] / n_sample)
|
2020-02-21 16:59:32 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
errors_sampling.append(bootstrap(result[0], n_sample, n_sample // 4, n_sample // 10, np.average))
|
2020-02-25 09:26:05 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
#amplitude = np.sqrt(np.sum(np.abs(state._qm_state[measure_coefficient_mask])**2))
|
|
|
|
#results_qc.append(amplitude)
|
2020-02-21 16:59:32 +00:00
|
|
|
|
|
|
|
# Simulation using matrices
|
2020-02-24 11:06:33 +00:00
|
|
|
np_zero_state = np.zeros(2**nqbits)
|
|
|
|
np_zero_state[0] = 1
|
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
itH = np.matrix(-0.5j * t * H(nqbits, g))
|
2020-02-25 09:26:05 +00:00
|
|
|
T = expm(itH)
|
|
|
|
|
2020-02-24 11:06:33 +00:00
|
|
|
np_state = T.dot(np_zero_state)
|
2020-02-28 15:09:51 +00:00
|
|
|
amplitude = (np.sum(np.abs(np_state[measure_coefficient_mask])**2))
|
2020-02-24 11:06:33 +00:00
|
|
|
results_np.append(amplitude)
|
|
|
|
|
2020-02-21 16:59:32 +00:00
|
|
|
print(f"simulating... {int(t/t_stop*100)} % ", end="\r")
|
|
|
|
print()
|
|
|
|
print("done.")
|
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
results_qc = np.array(results_qc)
|
2020-02-25 09:26:05 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
errors_trotter = (np.arange(0, t_stop, delta_t) * g)**2 / N_trot**2
|
|
|
|
errors_sampling = np.array(errors_sampling)
|
2020-02-25 09:26:05 +00:00
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
|
|
|
|
h0 = plt.errorbar(np.arange(0, t_stop, delta_t)
|
|
|
|
, results_qc
|
|
|
|
, yerr=(errors_trotter + errors_sampling)
|
|
|
|
, label=f"Quantum computing ({n_sample} samples, {N_trot} trotterization steps)"
|
|
|
|
, )
|
2020-02-24 11:06:33 +00:00
|
|
|
h1, = plt.plot(np.arange(0, t_stop, delta_t), results_np, label="Classical simulation using explicit transfer matrix")
|
2020-02-21 16:59:32 +00:00
|
|
|
plt.xlabel("t")
|
|
|
|
plt.ylabel(r"$|0\rangle$ probability amplitude for second spin")
|
|
|
|
plt.title(f"{nqbits} site spin chain with g={g} coupling to external field")
|
2020-02-24 11:06:33 +00:00
|
|
|
plt.legend(handles=[h0, h1])
|
|
|
|
|
2020-02-28 15:09:51 +00:00
|
|
|
plt.savefig("time_evo_6spin_g3.png", dpi=400)
|
2020-02-21 16:59:32 +00:00
|
|
|
plt.show()
|