2020-02-21 16:59:32 +00:00
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
from pyqcs import State, sample
|
|
|
|
from transfer_matrix import T_time_slice
|
|
|
|
from hamiltonian import H
|
|
|
|
|
2020-02-24 11:06:33 +00:00
|
|
|
from scipy.linalg import expm
|
|
|
|
|
2020-02-25 09:26:05 +00:00
|
|
|
nqbits = 2
|
|
|
|
g = 0.20
|
|
|
|
N = 50
|
2020-02-21 16:59:32 +00:00
|
|
|
t_stop = 9
|
|
|
|
delta_t = 0.05
|
|
|
|
qbits = list(range(nqbits))
|
|
|
|
|
2020-02-25 09:26:05 +00:00
|
|
|
n_sample = 400
|
2020-02-21 16:59:32 +00:00
|
|
|
measure = 0b10
|
|
|
|
|
|
|
|
|
|
|
|
results_qc = []
|
2020-02-24 11:06:33 +00:00
|
|
|
results_np = []
|
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)
|
|
|
|
|
|
|
|
for _ in range(N):
|
|
|
|
state = T_time_slice(qbits, t, g, N) * state
|
|
|
|
|
2020-02-25 09:26:05 +00:00
|
|
|
#result = sample(state, measure, n_sample)
|
2020-02-21 16:59:32 +00:00
|
|
|
|
2020-02-25 09:26:05 +00:00
|
|
|
#results_qc.append(result[0] / n_sample)
|
|
|
|
|
|
|
|
amplitude = np.sqrt(np.sum(np.abs(state._qm_state[[False if (i & measure) else True for i in range(2**nqbits)]])**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-25 09:26:05 +00:00
|
|
|
itH = np.matrix(-1j * t * H(nqbits, g))
|
|
|
|
T = expm(itH)
|
|
|
|
|
2020-02-24 11:06:33 +00:00
|
|
|
np_state = T.dot(np_zero_state)
|
2020-02-25 09:26:05 +00:00
|
|
|
amplitude = np.sqrt(np.sum(np.abs(np_state[[False if (i & measure) else True for i in range(2**nqbits)]])**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-25 09:26:05 +00:00
|
|
|
errors_trotter = np.arange(0, t_stop, delta_t)**2 / N**2
|
|
|
|
|
|
|
|
|
|
|
|
h0 = plt.errorbar(np.arange(0, t_stop, delta_t), results_qc, yerr=errors_trotter, label=f"Quantum computing ({n_sample} samples, {N} 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-21 16:59:32 +00:00
|
|
|
plt.show()
|
|
|
|
|