Skip to content

Commit

Permalink
refactor: Update fit_rotation and Experiment class to use flattop_ran…
Browse files Browse the repository at this point in the history
…ge for improved time handling
  • Loading branch information
Akinori Machino committed Dec 28, 2024
1 parent eda71b1 commit ae92a32
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
9 changes: 5 additions & 4 deletions src/qubex/analysis/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,8 @@ def residuals(params, times, data):
Omega_z = Omega * np.cos(theta)
print(f"Omega: ({Omega_x:.6f}, {Omega_y:.6f}, {Omega_z:.6f})")

fit = rotate(times, *fitted_params)
times_fine = np.linspace(np.min(times), np.max(times), 1000)
fit = rotate(times_fine, *fitted_params)

if plot:
fig = go.Figure()
Expand All @@ -1810,7 +1811,7 @@ def residuals(params, times, data):
)
fig.add_trace(
go.Scatter(
x=times,
x=times_fine,
y=fit[:, 0],
mode="lines",
name="X (fit)",
Expand All @@ -1828,7 +1829,7 @@ def residuals(params, times, data):
)
fig.add_trace(
go.Scatter(
x=times,
x=times_fine,
y=fit[:, 1],
mode="lines",
name="Y (fit)",
Expand All @@ -1846,7 +1847,7 @@ def residuals(params, times, data):
)
fig.add_trace(
go.Scatter(
x=times,
x=times_fine,
y=fit[:, 2],
mode="lines",
name="Z (fit)",
Expand Down
86 changes: 41 additions & 45 deletions src/qubex/experiment/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6427,7 +6427,7 @@ def cr_hamiltonian_tomography(
self,
control_qubit: str,
target_qubit: str,
time_range: ArrayLike = np.arange(100, 401, 20),
flattop_range: ArrayLike = np.arange(0, 301, 10),
cr_amplitude: float = 1.0,
cr_ramptime: float = 50,
cr_phase: float = 0.0,
Expand All @@ -6437,7 +6437,7 @@ def cr_hamiltonian_tomography(
interval: int = DEFAULT_INTERVAL,
plot: bool = False,
) -> dict:
time_range = np.array(time_range)
time_range = np.array(flattop_range) + cr_ramptime * 2

result_0 = self.execute_cr_sequence(
time_range=time_range,
Expand Down Expand Up @@ -6468,23 +6468,23 @@ def cr_hamiltonian_tomography(
interval=interval,
)

indices = (time_range >= cr_ramptime) & (
time_range < time_range[-1] - cr_ramptime
)
flat_time_range = time_range[indices] - cr_ramptime
indices = time_range >= cr_ramptime * 2
effective_time_range = time_range[indices] - cr_ramptime
target_states_0 = result_0["target_states"][indices]
target_states_1 = result_1["target_states"][indices]

fit_0 = fitting.fit_rotation(
flat_time_range,
effective_time_range,
target_states_0,
plot=plot,
)
vis.display_bloch_sphere(target_states_0)
fit_1 = fitting.fit_rotation(
flat_time_range,
effective_time_range,
target_states_1,
plot=plot,
title="CR Hamiltonian tomography",
xlabel="Effective drive time (ns)",
)
vis.display_bloch_sphere(target_states_1)
Omega_0 = fit_0["Omega"]
Expand Down Expand Up @@ -6528,58 +6528,54 @@ def calibrate_cr_sequence(
control_qubit: str,
target_qubit: str,
*,
time_range: ArrayLike = np.arange(100, 401, 20),
flattop_range: ArrayLike = np.arange(0, 301, 10),
cr_amplitude: float = 1.0,
cr_ramptime: float = 50,
n_iteraions: int = 2,
shots: int = DEFAULT_SHOTS,
interval: int = DEFAULT_INTERVAL,
) -> dict:
cr_phase = 0.0
cancel_amplitude = 0.0
cancel_phase = 0.0

for i in range(n_iteraions):
print(f"Iteration {i + 1}/{n_iteraions}")
step_1 = self.cr_hamiltonian_tomography(
control_qubit=control_qubit,
target_qubit=target_qubit,
time_range=time_range,
cr_amplitude=cr_amplitude,
cr_ramptime=cr_ramptime,
cr_phase=cr_phase,
cancel_amplitude=cancel_amplitude,
cancel_phase=cancel_phase,
shots=shots,
interval=interval,
plot=True,
)
cr_phase = step_1["cr_phase_est"]
print(f"cr_phase : {cr_phase:+.6f} rad")
step_1 = self.cr_hamiltonian_tomography(
control_qubit=control_qubit,
target_qubit=target_qubit,
flattop_range=flattop_range,
cr_amplitude=cr_amplitude,
cr_ramptime=cr_ramptime,
cr_phase=cr_phase,
cancel_amplitude=cancel_amplitude,
cancel_phase=cancel_phase,
shots=shots,
interval=interval,
plot=True,
)
cr_phase = step_1["cr_phase_est"]

step_2 = self.cr_hamiltonian_tomography(
control_qubit=control_qubit,
target_qubit=target_qubit,
time_range=time_range,
cr_amplitude=cr_amplitude,
cr_ramptime=cr_ramptime,
cr_phase=cr_phase,
cancel_amplitude=cancel_amplitude,
cancel_phase=cancel_phase,
shots=shots,
interval=interval,
plot=True,
)
step_2 = self.cr_hamiltonian_tomography(
control_qubit=control_qubit,
target_qubit=target_qubit,
flattop_range=flattop_range,
cr_amplitude=cr_amplitude,
cr_ramptime=cr_ramptime,
cr_phase=cr_phase,
cancel_amplitude=cancel_amplitude,
cancel_phase=cancel_phase,
shots=shots,
interval=interval,
plot=True,
)

cancel_amplitude = step_2["cancel_amplitude_est"]
cancel_phase = step_2["cancel_phase_est"]
print(f"cancel_amplitude : {cancel_amplitude:+.6f}")
print(f"cancel_phase : {cancel_phase:+.6f} rad")
cancel_amplitude = step_2["cancel_amplitude_est"]
cancel_phase = step_2["cancel_phase_est"]
print(f"cancel_amplitude : {cancel_amplitude:+.6f}")
print(f"cancel_phase : {cancel_phase:+.6f} rad")

result = self.cr_hamiltonian_tomography(
control_qubit=control_qubit,
target_qubit=target_qubit,
time_range=time_range,
flattop_range=flattop_range,
cr_amplitude=cr_amplitude,
cr_ramptime=cr_ramptime,
cr_phase=cr_phase,
Expand Down

0 comments on commit ae92a32

Please sign in to comment.