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-04-21 10:41:03 +00:00
|
|
|
t_stop = 29
|
|
|
|
delta_t = 0.1
|
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-04-14 16:43:59 +00:00
|
|
|
amplitudes_qc = []
|
2020-02-28 15:09:51 +00:00
|
|
|
|
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-04-14 16:43:59 +00:00
|
|
|
errors_sampling.append(bootstrap(result[0], n_sample, n_sample, n_sample // 2, np.average))
|
2020-02-25 09:26:05 +00:00
|
|
|
|
2020-04-21 10:41:03 +00:00
|
|
|
amplitude = np.sum(np.abs(state._qm_state[measure_coefficient_mask])**2)
|
|
|
|
amplitudes_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-04-21 10:41:03 +00:00
|
|
|
amplitudes_qc = np.array(amplitudes_qc)
|
2020-02-25 09:26:05 +00:00
|
|
|
|
2020-04-21 10:41:03 +00:00
|
|
|
errors_trotter = (np.arange(0, t_stop, delta_t))**3 / N_trot**3
|
2020-02-28 15:09:51 +00:00
|
|
|
errors_sampling = np.array(errors_sampling)
|
2020-04-21 10:41:03 +00:00
|
|
|
#errors_sampling = np.abs(results_qc - amplitudes_qc)
|
|
|
|
#errors_sampling = (amplitudes_qc * (1 - amplitudes_qc))**2 / n_sample
|
2020-02-25 09:26:05 +00:00
|
|
|
|
2020-04-21 10:41:03 +00:00
|
|
|
#hm1 = plt.errorbar(np.arange(0, t_stop, delta_t)
|
|
|
|
# , results_qc
|
|
|
|
# , yerr=(errors_sampling + errors_trotter)
|
|
|
|
# , color="red")
|
2020-02-28 15:09:51 +00:00
|
|
|
h0 = plt.errorbar(np.arange(0, t_stop, delta_t)
|
|
|
|
, results_qc
|
2020-04-14 16:43:59 +00:00
|
|
|
, yerr=errors_sampling
|
2020-02-28 15:09:51 +00:00
|
|
|
, 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-04-14 16:43:59 +00:00
|
|
|
#h2, = plt.plot(np.arange(0, t_stop, delta_t), amplitudes_qc, label="QC amplitude")
|
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()
|