C. State preparation from a thermal state with Jaynes-Cummings controls#
from feedback_grape.fgrape import optimize_pulse
from feedback_grape.utils.operators import (
sigmap,
sigmam,
create,
destroy,
identity,
cosm,
sinm,
)
from feedback_grape.utils.states import basis, fock
from feedback_grape.utils.tensor import tensor
import jax.numpy as jnp
import jax
from jax.scipy.linalg import expm
defining parameterized operations that are repeated num_time_steps times#
N_cav = 20
def qubit_unitary(alphas):
alpha_re, alpha_im = alphas
alpha = alpha_re + 1j * alpha_im
return tensor(
identity(N_cav),
expm(-1j * (alpha * sigmap() + alpha.conjugate() * sigmam()) / 2),
)
def qubit_cavity_unitary(betas):
beta_re, beta_im = betas
beta = beta_re + 1j * beta_im
return expm(
-1j
* (
beta * (tensor(destroy(N_cav), sigmap()))
+ beta.conjugate() * (tensor(create(N_cav), sigmam()))
)
/ 2
)
povm_measure_operator (callable):
#
- It should take a measurement outcome and list of params as input
- The measurement outcome options are either 1 or -1
from feedback_grape.utils.operators import create, destroy
def povm_measure_operator(measurement_outcome, params):
"""
POVM for the measurement of the cavity state.
returns Mm ( NOT the POVM element Em = Mm_dag @ Mm ), given measurement_outcome m, gamma and delta
"""
gamma, delta = params
cav_operator = gamma * create(N_cav) @ destroy(N_cav) + delta / 2 * identity(N_cav)
angle = tensor(cav_operator, identity(2))
meas_op = jnp.where(
measurement_outcome == 1,
cosm(angle),
sinm(angle),
)
return meas_op
defining initial (thermal) state#
# initial state is a thermal state coupled to a qubit in the ground state?
n_average = 1
# natural logarithm
beta = jnp.log((1 / n_average) + 1)
diags = jnp.exp(-beta * jnp.arange(N_cav))
normalized_diags = diags / jnp.sum(diags, axis=0)
rho_cav = jnp.diag(normalized_diags)
rho_cav.shape
(20, 20)
rho0 = tensor(rho_cav, basis(2, 0) @ basis(2, 0).conj().T)
from feedback_grape.utils.povm import (
_probability_of_a_measurement_outcome_given_a_certain_state,
)
_probability_of_a_measurement_outcome_given_a_certain_state(
rho0,
+1,
povm_measure_operator(+1, params=[0.1, -3 * jnp.pi / 2]),
povm_measure_operator(-1, params=[0.1, -3 * jnp.pi / 2]),
evo_type="density"
)
Array(0.40800029, dtype=float64)
defining target state#
psi_target = tensor(
(fock(N_cav, 1) + fock(N_cav, 2) + fock(N_cav, 3)) / jnp.sqrt(3), basis(2)
)
psi_target = psi_target / jnp.linalg.norm(psi_target)
rho_target = psi_target @ psi_target.conj().T
rho_target.shape
(40, 40)
from feedback_grape.utils.fidelity import fidelity
print(fidelity(U_final=rho0, C_target=rho_target, evo_type="density"))
0.14583347658746756
# Print the eigenvalues of rho0
eigenvalues = jnp.linalg.eigvalsh(rho_target)
print("Eigenvalues of rho0:", eigenvalues)
Eigenvalues of rho0: [-2.72829216e-16 -6.02376918e-17 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00]
initialize random params#
import jax
print(
jax.random.uniform(
jax.random.PRNGKey(0),
shape=(1, 2), # 2 for gamma and delta
minval=-jax.numpy.pi,
maxval=jax.numpy.pi,
).tolist()
)
[[-0.5123490775685872, -1.782568231202715]]
import jax
from feedback_grape.fgrape import Gate
num_time_steps = 5
num_of_iterations = 1000
learning_rate = 0.02
# avg_photon_numer = 2 When testing kitten state
# If you provide param_constraints for only one parameter, the current behavior throws an error if you don't provide param_constraints for all parameters for all gates
key1, key2, key3 = jax.random.split(jax.random.PRNGKey(42), 3)
measure = Gate(
gate=povm_measure_operator,
initial_params=jax.random.uniform(
key1,
shape=(2,), # 2 for gamma and delta
minval=-2 * jnp.pi,
maxval=2 * jnp.pi,
),
measurement_flag=True,
# param_constraints=[[0, jnp.pi], [-2*jnp.pi, 2*jnp.pi]],
)
qub_unitary = Gate(
gate=qubit_unitary,
initial_params=jax.random.uniform(
key2,
shape=(2,), # 2 for gamma and delta
minval=-2 * jnp.pi,
maxval=2 * jnp.pi,
),
measurement_flag=False,
# param_constraints=[[-2*jnp.pi, 2*jnp.pi], [-2*jnp.pi, 2*jnp.pi]],
)
qub_cav = Gate(
gate=qubit_cavity_unitary,
initial_params=jax.random.uniform(
key3,
shape=(2,), # 2 for gamma and delta
minval=-2 * jnp.pi,
maxval=2 * jnp.pi,
),
measurement_flag=False,
# param_constraints=[[-jnp.pi, jnp.pi], [-jnp.pi, jnp.pi]],
)
system_params = [measure, qub_unitary, qub_cav]
result = optimize_pulse(
init_state=rho0,
target_state=rho_target,
system_params=system_params,
num_time_steps=num_time_steps,
mode="lookup",
goal="fidelity",
max_iter=num_of_iterations,
learning_rate=learning_rate,
evo_type="density",
batch_size=10,
eval_time_steps=2*num_time_steps,
progress=True,
)
Iteration 10, Reward: 0.318227, Loss: 0.619694, T=3s, eta=290s
Iteration 20, Reward: 0.280946, Loss: 0.559191, T=6s, eta=302s
Iteration 30, Reward: 0.408618, Loss: 0.654462, T=9s, eta=305s
Iteration 40, Reward: 0.397087, Loss: 0.690517, T=13s, eta=305s
Iteration 50, Reward: 0.351640, Loss: 0.606377, T=16s, eta=303s
Iteration 60, Reward: 0.416276, Loss: 0.688028, T=19s, eta=300s
Iteration 70, Reward: 0.390049, Loss: 0.736810, T=22s, eta=297s
Iteration 80, Reward: 0.456227, Loss: 0.865780, T=25s, eta=294s
Iteration 90, Reward: 0.499979, Loss: 0.895460, T=29s, eta=291s
Iteration 100, Reward: 0.514693, Loss: 1.038856, T=32s, eta=287s
Iteration 110, Reward: 0.634698, Loss: 1.158938, T=35s, eta=285s
Iteration 120, Reward: 0.560245, Loss: 1.002144, T=39s, eta=285s
Iteration 130, Reward: 0.601328, Loss: 0.755226, T=42s, eta=282s
Iteration 140, Reward: 0.710599, Loss: 0.877550, T=45s, eta=279s
Iteration 150, Reward: 0.553419, Loss: 1.289478, T=49s, eta=277s
Iteration 160, Reward: 0.588438, Loss: 0.713522, T=52s, eta=274s
Iteration 170, Reward: 0.661415, Loss: 1.031733, T=55s, eta=271s
Iteration 180, Reward: 0.657088, Loss: 1.096958, T=59s, eta=267s
Iteration 190, Reward: 0.697757, Loss: 1.128355, T=62s, eta=264s
Iteration 200, Reward: 0.539165, Loss: 0.994936, T=65s, eta=261s
Iteration 210, Reward: 0.651195, Loss: 1.328315, T=68s, eta=257s
Iteration 220, Reward: 0.674164, Loss: 1.221207, T=72s, eta=254s
Iteration 230, Reward: 0.651914, Loss: 1.432580, T=75s, eta=251s
Iteration 240, Reward: 0.630783, Loss: 1.133913, T=78s, eta=248s
Iteration 250, Reward: 0.719842, Loss: 1.102306, T=81s, eta=244s
Iteration 260, Reward: 0.592557, Loss: 1.132770, T=85s, eta=241s
Iteration 270, Reward: 0.728608, Loss: 1.355784, T=88s, eta=238s
Iteration 280, Reward: 0.689532, Loss: 1.338068, T=91s, eta=235s
Iteration 290, Reward: 0.753003, Loss: 0.966543, T=95s, eta=232s
Iteration 300, Reward: 0.668250, Loss: 1.274789, T=98s, eta=229s
Iteration 310, Reward: 0.681118, Loss: 1.058684, T=101s, eta=226s
Iteration 320, Reward: 0.630267, Loss: 1.207416, T=105s, eta=223s
Iteration 330, Reward: 0.796213, Loss: 1.172187, T=108s, eta=219s
Iteration 340, Reward: 0.653537, Loss: 0.795722, T=111s, eta=216s
Iteration 350, Reward: 0.604482, Loss: 0.934281, T=115s, eta=213s
Iteration 360, Reward: 0.599747, Loss: 0.876484, T=118s, eta=210s
Iteration 370, Reward: 0.736385, Loss: 0.976146, T=121s, eta=207s
Iteration 380, Reward: 0.645553, Loss: 0.924410, T=125s, eta=203s
Iteration 390, Reward: 0.644249, Loss: 1.057753, T=128s, eta=200s
Iteration 400, Reward: 0.629563, Loss: 1.177116, T=131s, eta=197s
Iteration 410, Reward: 0.678983, Loss: 1.000939, T=134s, eta=194s
Iteration 420, Reward: 0.747795, Loss: 1.092718, T=138s, eta=190s
Iteration 430, Reward: 0.755047, Loss: 1.275725, T=141s, eta=187s
Iteration 440, Reward: 0.650971, Loss: 0.881084, T=144s, eta=184s
Iteration 450, Reward: 0.794297, Loss: 1.116676, T=147s, eta=180s
Iteration 460, Reward: 0.649832, Loss: 0.993254, T=151s, eta=177s
Iteration 470, Reward: 0.812325, Loss: 1.205236, T=154s, eta=174s
Iteration 480, Reward: 0.783528, Loss: 0.983355, T=157s, eta=170s
Iteration 490, Reward: 0.833563, Loss: 1.001310, T=161s, eta=167s
Iteration 500, Reward: 0.736976, Loss: 0.760032, T=164s, eta=164s
Iteration 510, Reward: 0.712165, Loss: 0.938214, T=167s, eta=160s
Iteration 520, Reward: 0.735080, Loss: 1.165753, T=170s, eta=157s
Iteration 530, Reward: 0.862699, Loss: 0.697868, T=173s, eta=154s
Iteration 540, Reward: 0.848846, Loss: 0.815368, T=177s, eta=151s
Iteration 550, Reward: 0.817217, Loss: 1.022441, T=180s, eta=147s
Iteration 560, Reward: 0.787957, Loss: 1.295912, T=183s, eta=144s
Iteration 570, Reward: 0.844863, Loss: 1.107398, T=187s, eta=141s
Iteration 580, Reward: 0.790090, Loss: 1.335107, T=190s, eta=137s
Iteration 590, Reward: 0.868780, Loss: 1.032403, T=193s, eta=134s
Iteration 600, Reward: 0.824679, Loss: 1.406895, T=196s, eta=131s
Iteration 610, Reward: 0.786481, Loss: 1.069001, T=200s, eta=128s
Iteration 620, Reward: 0.663400, Loss: 0.852598, T=203s, eta=124s
Iteration 630, Reward: 0.729012, Loss: 0.854650, T=207s, eta=121s
Iteration 640, Reward: 0.858356, Loss: 0.750477, T=210s, eta=118s
Iteration 650, Reward: 0.842964, Loss: 1.008557, T=213s, eta=115s
Iteration 660, Reward: 0.841795, Loss: 1.044254, T=216s, eta=111s
Iteration 670, Reward: 0.705676, Loss: 0.986450, T=220s, eta=108s
Iteration 680, Reward: 0.792911, Loss: 0.951353, T=223s, eta=105s
Iteration 690, Reward: 0.875476, Loss: 0.928878, T=226s, eta=102s
Iteration 700, Reward: 0.641933, Loss: 0.824319, T=229s, eta=98s
Iteration 710, Reward: 0.889916, Loss: 0.972151, T=233s, eta=95s
Iteration 720, Reward: 0.798687, Loss: 0.963490, T=236s, eta=92s
Iteration 730, Reward: 0.798001, Loss: 0.908401, T=240s, eta=89s
Iteration 740, Reward: 0.883211, Loss: 0.970608, T=243s, eta=85s
Iteration 750, Reward: 0.864059, Loss: 0.639949, T=247s, eta=82s
Iteration 760, Reward: 0.919612, Loss: 0.391333, T=250s, eta=79s
Iteration 770, Reward: 0.810643, Loss: 0.332053, T=253s, eta=76s
Iteration 780, Reward: 0.730355, Loss: 0.670661, T=257s, eta=72s
Iteration 790, Reward: 0.886005, Loss: 0.698586, T=260s, eta=69s
Iteration 800, Reward: 0.834374, Loss: 0.777814, T=263s, eta=66s
Iteration 810, Reward: 0.902900, Loss: 0.466045, T=266s, eta=62s
Iteration 820, Reward: 0.801695, Loss: 0.809259, T=270s, eta=59s
Iteration 830, Reward: 0.860341, Loss: 0.826517, T=273s, eta=56s
Iteration 840, Reward: 0.913194, Loss: 0.377347, T=276s, eta=52s
Iteration 850, Reward: 0.839355, Loss: 1.004180, T=280s, eta=49s
Iteration 860, Reward: 0.841706, Loss: 0.861270, T=284s, eta=46s
Iteration 870, Reward: 0.823768, Loss: 1.018132, T=287s, eta=43s
Iteration 880, Reward: 0.877896, Loss: 0.685581, T=290s, eta=39s
Iteration 890, Reward: 0.856942, Loss: 0.949864, T=294s, eta=36s
Iteration 900, Reward: 0.795033, Loss: 0.805575, T=297s, eta=33s
Iteration 910, Reward: 0.926791, Loss: 0.566240, T=301s, eta=30s
Iteration 920, Reward: 0.876606, Loss: 0.672469, T=304s, eta=26s
Iteration 930, Reward: 0.814529, Loss: 0.746736, T=307s, eta=23s
Iteration 940, Reward: 0.832424, Loss: 0.909266, T=311s, eta=20s
Iteration 950, Reward: 0.901748, Loss: 0.814764, T=314s, eta=16s
Iteration 960, Reward: 0.896297, Loss: 0.745589, T=317s, eta=13s
Iteration 970, Reward: 0.839046, Loss: 0.746876, T=320s, eta=10s
Iteration 980, Reward: 0.905184, Loss: 0.851863, T=324s, eta=6s
Iteration 990, Reward: 0.882293, Loss: 0.779292, T=327s, eta=3s
print(result.final_purity)
None
print(
"initial fidelity:",
result.fidelity_each_timestep[0].mean(),
)
print(f"fidelities at timestep {num_time_steps}")
for i, fid in enumerate(result.fidelity_each_timestep[num_time_steps]):
print(f" state {i}: {fid}")
print(f"\n Mean fidelity over timesteps: {jnp.mean(jnp.array(result.fidelity_each_timestep), axis=1)}")
initial fidelity: 0.14583347658746756
fidelities at timestep 5
state 0: 0.7194102725324175
state 1: 0.917003732820672
state 2: 0.9293042738075619
state 3: 0.9375323434789816
state 4: 0.7194102725324175
state 5: 0.8748438040793716
state 6: 0.917003732820672
state 7: 0.9293042738075619
state 8: 0.9293042738075619
state 9: 0.917003732820672
Mean fidelity over timesteps: [0.14583348 0.00675967 0.00995889 0.07404844 0.04403262 0.87901207
0.03969455 0.43256288 0.03206519 0.24084545 0.04127536]
result.returned_params
[[Array([[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16],
[-4.71238898e+00, 3.36106277e-16]], dtype=float64),
Array([[2.9691256 , 1.37871057],
[3.90586387, 1.40552022],
[2.9691256 , 1.37871057],
[3.90586387, 1.40552022],
[2.9691256 , 1.37871057],
[3.90586387, 1.40552022],
[3.90586387, 1.40552022],
[2.9691256 , 1.37871057],
[2.9691256 , 1.37871057],
[3.90586387, 1.40552022]], dtype=float64),
Array([[-2.90869411, -1.61331713],
[-0.30017949, -0.01681701],
[-2.90869411, -1.61331713],
[-0.30017949, -0.01681701],
[-2.90869411, -1.61331713],
[-0.30017949, -0.01681701],
[-0.30017949, -0.01681701],
[-2.90869411, -1.61331713],
[-2.90869411, -1.61331713],
[-0.30017949, -0.01681701]], dtype=float64)],
[Array([[-3.66674334e+00, -1.42839032e-02],
[-3.90362417e+00, -1.00727929e-03],
[-3.66674334e+00, -1.42839032e-02],
[-3.90362417e+00, -1.00727929e-03],
[-3.66674334e+00, -1.42839032e-02],
[-3.90362417e+00, -1.00727929e-03],
[-3.90362417e+00, -1.00727929e-03],
[-3.66674334e+00, -1.42839032e-02],
[-3.66674334e+00, -1.42839032e-02],
[-3.90362417e+00, -1.00727929e-03]], dtype=float64),
Array([[ 4.20867933, 5.55271537],
[ 3.23114167, -0.93475474],
[ 2.69726098, 3.94245224],
[ 3.23114167, -0.93475474],
[ 4.20867933, 5.55271537],
[ 3.71753595, -1.4304324 ],
[ 3.23114167, -0.93475474],
[ 2.69726098, 3.94245224],
[ 2.69726098, 3.94245224],
[ 3.23114167, -0.93475474]], dtype=float64),
Array([[ 2.97100818, 2.46893057],
[-1.78296066, 2.01290947],
[ 1.69782804, 2.46019311],
[-1.78296066, 2.01290947],
[ 2.97100818, 2.46893057],
[-4.59779512, 4.09005064],
[-1.78296066, 2.01290947],
[ 1.69782804, 2.46019311],
[ 1.69782804, 2.46019311],
[-1.78296066, 2.01290947]], dtype=float64)],
[Array([[-3.58629457, -0.34751164],
[-3.32485769, -2.57517181],
[-3.05387078, -0.14219135],
[-3.32485769, -2.57517181],
[-3.58629457, -0.34751164],
[-2.96159936, -0.94803477],
[-3.32485769, -2.57517181],
[-3.05387078, -0.14219135],
[-3.05387078, -0.14219135],
[-3.32485769, -2.57517181]], dtype=float64),
Array([[ 0.81435863, -1.55143555],
[ 1.91677899, -1.44313808],
[ 2.10722953, -1.35314996],
[ 1.91677899, -1.44313808],
[ 0.81435863, -1.55143555],
[ 0.93982217, 2.83962395],
[ 1.91677899, -1.44313808],
[ 2.10722953, -1.35314996],
[ 2.10722953, -1.35314996],
[ 1.91677899, -1.44313808]], dtype=float64),
Array([[-2.6949575 , 1.79298485],
[-1.49393389, 0.94361304],
[-2.22010603, 1.34230254],
[-1.49393389, 0.94361304],
[-2.6949575 , 1.79298485],
[ 0.21115467, 0.49994575],
[-1.49393389, 0.94361304],
[-2.22010603, 1.34230254],
[-2.22010603, 1.34230254],
[-1.49393389, 0.94361304]], dtype=float64)],
[Array([[-3.33738487, -1.89751052],
[-3.07061691, -1.28961672],
[-3.32029117, -2.59503273],
[-3.07061691, -1.28961672],
[-3.33738487, -1.89751052],
[-3.51715905, -1.77660659],
[-3.07061691, -1.28961672],
[-3.32029117, -2.59503273],
[-3.32029117, -2.59503273],
[-3.07061691, -1.28961672]], dtype=float64),
Array([[ 4.22935229, -2.79754046],
[ 1.85265297, -1.9752448 ],
[ 3.1921227 , -0.90706609],
[ 1.76992003, -2.10108568],
[ 4.22935229, -2.79754046],
[-0.03525098, -4.47538246],
[ 1.85265297, -1.9752448 ],
[ 3.1921227 , -0.90706609],
[ 3.1921227 , -0.90706609],
[ 1.85265297, -1.9752448 ]], dtype=float64),
Array([[-3.43064627, 2.63094399],
[-0.90581831, 2.38061408],
[ 2.08645207, -0.11765821],
[-0.59121086, 2.43401043],
[-3.43064627, 2.63094399],
[ 0.48060166, 1.07056246],
[-0.90581831, 2.38061408],
[ 2.08645207, -0.11765821],
[ 2.08645207, -0.11765821],
[-0.90581831, 2.38061408]], dtype=float64)],
[Array([[-3.50669978, -3.33381557],
[-2.96761546, -0.94221841],
[-3.42181766, -0.85628622],
[-3.1703016 , -3.00725554],
[-3.50669978, -3.33381557],
[-3.23596439, -1.62521713],
[-2.96761546, -0.94221841],
[-3.42181766, -0.85628622],
[-3.42181766, -0.85628622],
[-2.96761546, -0.94221841]], dtype=float64),
Array([[ 2.09674318, 2.15815973],
[ 2.35890339, 1.64604467],
[ 1.78591974, -1.02782378],
[ 2.51928901, 1.36356427],
[ 2.09674318, 2.15815973],
[ 1.16929332, 2.19826794],
[ 2.35890339, 1.64604467],
[ 1.78591974, -1.02782378],
[ 1.78591974, -1.02782378],
[ 2.35890339, 1.64604467]], dtype=float64),
Array([[-1.34493269, 2.28960236],
[-2.71702843, 1.79906331],
[-2.34812635, 1.82992484],
[-2.63369401, 2.01873721],
[-1.34493269, 2.28960236],
[-3.00124022, 1.16948004],
[-2.71702843, 1.79906331],
[-2.34812635, 1.82992484],
[-2.34812635, 1.82992484],
[-2.71702843, 1.79906331]], dtype=float64)],
[Array([[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487]], dtype=float64),
Array([[2.76708315, 0.48123403],
[2.96155498, 3.43476071],
[2.30337215, 1.22416252],
[3.28481501, 1.16398892],
[2.76708315, 0.48123403],
[2.59847912, 1.28536899],
[2.96155498, 3.43476071],
[2.51928901, 1.36356427],
[2.30337215, 1.22416252],
[2.96155498, 3.43476071]], dtype=float64),
Array([[-2.18042252, 2.5851866 ],
[-2.77492441, 3.8331377 ],
[-2.44627684, 3.57203484],
[-3.06128734, 3.48343822],
[-2.18042252, 2.5851866 ],
[-2.43553881, 2.8284392 ],
[-2.77492441, 3.8331377 ],
[-2.63369401, 2.01873721],
[-2.44627684, 3.57203484],
[-2.77492441, 3.8331377 ]], dtype=float64)],
[Array([[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487]], dtype=float64),
Array([[ 2.65630515, 0.75808467],
[ 1.78591974, -1.02782378],
[ 2.04811262, 3.2897024 ],
[ 2.76708315, 0.48123403],
[ 2.65630515, 0.75808467],
[ 2.66045762, 1.22569333],
[ 2.23997867, 0.38932321],
[ 2.66045762, 1.22569333],
[ 2.04811262, 3.2897024 ],
[ 1.78591974, -1.02782378]], dtype=float64),
Array([[-2.37551672, 2.69659584],
[-2.34812635, 1.82992484],
[-1.92393166, 2.95643325],
[-2.18042252, 2.5851866 ],
[-2.37551672, 2.69659584],
[-2.07077223, 2.70297908],
[-3.12068514, -0.81738863],
[-2.07077223, 2.70297908],
[-1.92393166, 2.95643325],
[-2.34812635, 1.82992484]], dtype=float64)],
[Array([[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487]], dtype=float64),
Array([[2.59847912, 1.28536899],
[2.51928901, 1.36356427],
[2.65630515, 0.75808467],
[2.88874417, 0.30828026],
[2.91830017, 0.9171571 ],
[2.09674318, 2.15815973],
[2.59431839, 1.37541847],
[2.12022319, 1.90158275],
[2.65630515, 0.75808467],
[2.30337215, 1.22416252]], dtype=float64),
Array([[-2.43553881, 2.8284392 ],
[-2.63369401, 2.01873721],
[-2.37551672, 2.69659584],
[-2.93594635, 3.39189121],
[-1.28914466, 3.32553057],
[-1.34493269, 2.28960236],
[-2.20114012, 2.59472681],
[-0.9248274 , 2.48595711],
[-2.37551672, 2.69659584],
[-2.44627684, 3.57203484]], dtype=float64)],
[Array([[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487]], dtype=float64),
Array([[2.66045762, 1.22569333],
[2.66045762, 1.22569333],
[2.91830017, 0.9171571 ],
[2.73915365, 0.63449692],
[2.04811262, 3.2897024 ],
[2.76708315, 0.48123403],
[1.72614227, 1.84281944],
[2.12022319, 1.90158275],
[2.59847912, 1.28536899],
[2.04811262, 3.2897024 ]], dtype=float64),
Array([[-2.07077223, 2.70297908],
[-2.07077223, 2.70297908],
[-1.28914466, 3.32553057],
[-2.54670468, 2.56801675],
[-1.92393166, 2.95643325],
[-2.18042252, 2.5851866 ],
[-1.94932674, 2.6911449 ],
[-0.9248274 , 2.48595711],
[-2.43553881, 2.8284392 ],
[-1.92393166, 2.95643325]], dtype=float64)],
[Array([[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487],
[-3.32166965, -1.29192487]], dtype=float64),
Array([[2.09674318, 2.15815973],
[2.12022319, 1.90158275],
[2.76440674, 1.0107749 ],
[2.96155498, 3.43476071],
[2.65630515, 0.75808467],
[2.65630515, 0.75808467],
[2.73915365, 0.63449692],
[2.12022319, 1.90158275],
[2.66045762, 1.22569333],
[2.65630515, 0.75808467]], dtype=float64),
Array([[-1.34493269, 2.28960236],
[-0.9248274 , 2.48595711],
[-2.26482251, 2.64246411],
[-2.77492441, 3.8331377 ],
[-2.37551672, 2.69659584],
[-2.37551672, 2.69659584],
[-2.54670468, 2.56801675],
[-0.9248274 , 2.48595711],
[-2.07077223, 2.70297908],
[-2.37551672, 2.69659584]], dtype=float64)]]
print(result.iterations)
1000
Tracking the reward during training#
With progress=True the optimizer prints the loss. In measurement modes (mode="lookup"/"nn") the loss also contains the log-probability REINFORCE term that makes the stochastic measurement outcomes differentiable, so its value is unbounded and bounces around — notice the printed loss above climbing well past 1.0. It is the gradient of that term that matters, not its magnitude.
The quantity you actually care about — the reward being maximized (here the batch-mean fidelity, bounded in [0, 1]) — is now recorded at every iteration in result.reward_history. Plotting it shows the real training progress.
result.reward_history
Array([0.2017635 , 0.27609802, 0.27699045, 0.25185768, 0.20119278,
0.29380338, 0.25809957, 0.25470822, 0.35729483, 0.2583866 ,
0.3182274 , 0.33460855, 0.24742522, 0.29469893, 0.28000912,
0.33730408, 0.33507897, 0.32145235, 0.35931963, 0.30352521,
0.28094622, 0.33900439, 0.34743769, 0.41394088, 0.37729948,
0.36089231, 0.32024568, 0.3836614 , 0.38201975, 0.29574407,
0.40861752, 0.33215277, 0.40311749, 0.3463629 , 0.46097609,
0.37472311, 0.34259639, 0.33987323, 0.3628366 , 0.30110804,
0.39708705, 0.38458216, 0.35959599, 0.34994825, 0.34019074,
0.40093019, 0.40149355, 0.42110819, 0.3441032 , 0.42752651,
0.35163962, 0.42408419, 0.30514093, 0.30462171, 0.44088872,
0.39200538, 0.42870984, 0.43542252, 0.43710945, 0.47524226,
0.41627553, 0.39299616, 0.35298589, 0.37894356, 0.39617467,
0.37789529, 0.46620899, 0.45143458, 0.4465277 , 0.42331975,
0.39004881, 0.48885334, 0.46315576, 0.46120702, 0.36826345,
0.41230344, 0.40027503, 0.46902383, 0.50032474, 0.50464326,
0.45622693, 0.44762425, 0.52488894, 0.49004641, 0.49315373,
0.35746244, 0.53496512, 0.43920923, 0.50734301, 0.60098566,
0.49997855, 0.45868946, 0.56681091, 0.51666708, 0.46078191,
0.45837094, 0.52681942, 0.41157727, 0.57203794, 0.5987932 ,
0.51469324, 0.49535618, 0.56859467, 0.45284517, 0.50459179,
0.66393484, 0.5451473 , 0.60238949, 0.64049359, 0.66928139,
0.63469754, 0.55710347, 0.62283447, 0.61262723, 0.61114663,
0.51425366, 0.59281522, 0.61868798, 0.49045376, 0.58410018,
0.56024489, 0.54128238, 0.61284942, 0.57267552, 0.66137463,
0.52377321, 0.47223708, 0.70608845, 0.53339187, 0.56089833,
0.60132822, 0.64501772, 0.63441406, 0.53677128, 0.61478007,
0.58421407, 0.58360334, 0.67727217, 0.64578031, 0.60525938,
0.71059936, 0.67648096, 0.65373866, 0.71907244, 0.59338177,
0.54754166, 0.61248319, 0.55788826, 0.71807349, 0.62634999,
0.55341856, 0.54627092, 0.66483238, 0.73180552, 0.59789713,
0.61478545, 0.6093827 , 0.73716109, 0.65204901, 0.63922981,
0.5884381 , 0.71766252, 0.6554855 , 0.63838857, 0.72172811,
0.65964566, 0.60648335, 0.64859751, 0.5991933 , 0.72031934,
0.66141484, 0.54458514, 0.65979231, 0.69412123, 0.75363631,
0.62352266, 0.5119768 , 0.66256875, 0.64895322, 0.69531194,
0.65708789, 0.64239296, 0.60003131, 0.63495043, 0.63620403,
0.68669189, 0.82164166, 0.71146139, 0.69001014, 0.61892062,
0.69775742, 0.73648361, 0.73051413, 0.69076665, 0.5893418 ,
0.62057633, 0.57953249, 0.69304618, 0.5439414 , 0.80015799,
0.53916516, 0.70521412, 0.62600238, 0.77936414, 0.64372021,
0.77910834, 0.62126959, 0.62090568, 0.68549647, 0.63921418,
0.65119476, 0.60117142, 0.7439426 , 0.65329568, 0.75951049,
0.6291361 , 0.58378338, 0.66850168, 0.5972951 , 0.55848775,
0.67416369, 0.70878717, 0.69439963, 0.73913046, 0.70064452,
0.6149923 , 0.69926079, 0.68333376, 0.64950733, 0.65381508,
0.65191414, 0.68341022, 0.63964677, 0.74690857, 0.63488036,
0.63346069, 0.72941711, 0.73041829, 0.55238432, 0.78154996,
0.63078339, 0.62548358, 0.76767166, 0.75087608, 0.61056601,
0.69703415, 0.76186353, 0.77166035, 0.69540587, 0.61869963,
0.71984153, 0.65153777, 0.53737413, 0.68764727, 0.65393926,
0.74796171, 0.60553391, 0.74112519, 0.77845928, 0.67708933,
0.59255683, 0.66854946, 0.63940642, 0.81534551, 0.67064438,
0.83013764, 0.7574386 , 0.68096685, 0.75650508, 0.67054931,
0.72860834, 0.61160179, 0.74051048, 0.70929989, 0.78455005,
0.73143482, 0.66342605, 0.70314403, 0.66993866, 0.78719463,
0.68953194, 0.57957971, 0.70004072, 0.69514636, 0.64441399,
0.6998916 , 0.66835322, 0.65760721, 0.69116878, 0.55207506,
0.75300318, 0.6586573 , 0.72800942, 0.66841105, 0.78397593,
0.74889094, 0.70644887, 0.70608981, 0.59808905, 0.79987204,
0.66825026, 0.67069934, 0.74073826, 0.7322327 , 0.72184103,
0.76901755, 0.5799191 , 0.74586897, 0.70789247, 0.80865384,
0.68111836, 0.72207407, 0.76258146, 0.72722995, 0.75906492,
0.81532202, 0.75402022, 0.84960111, 0.66642774, 0.7411016 ,
0.63026684, 0.67393432, 0.65450533, 0.73664632, 0.78338165,
0.75405328, 0.69502883, 0.65885956, 0.64889221, 0.7621282 ,
0.79621255, 0.65206082, 0.71085189, 0.58200763, 0.72187383,
0.74704058, 0.76176138, 0.78714892, 0.52517694, 0.75190873,
0.65353719, 0.74231227, 0.64144376, 0.66593539, 0.73881244,
0.60196633, 0.7144778 , 0.59620526, 0.73190951, 0.67699754,
0.60448175, 0.72748462, 0.68990313, 0.79705546, 0.79625664,
0.71604096, 0.71092382, 0.76327467, 0.66161899, 0.76315548,
0.59974724, 0.53468559, 0.81438245, 0.64677287, 0.77837057,
0.65512305, 0.81983198, 0.64773732, 0.69637197, 0.65708838,
0.73638483, 0.74611846, 0.66336398, 0.7396995 , 0.61894486,
0.7512558 , 0.59557635, 0.71466953, 0.73264752, 0.6324612 ,
0.64555327, 0.64472538, 0.70945054, 0.78643162, 0.65213594,
0.75096537, 0.81964378, 0.64420757, 0.74917388, 0.85605196,
0.64424851, 0.71344451, 0.68706347, 0.82036389, 0.71320545,
0.62369483, 0.69334446, 0.71484766, 0.7679706 , 0.63829893,
0.62956314, 0.72325151, 0.70086075, 0.74561496, 0.68976524,
0.64043468, 0.66927331, 0.72743926, 0.75501656, 0.79842335,
0.67898327, 0.80773697, 0.65225044, 0.80278514, 0.59763808,
0.79794324, 0.80115984, 0.72965395, 0.7420258 , 0.90279535,
0.74779549, 0.77268255, 0.72341857, 0.86119767, 0.78332381,
0.76700135, 0.7659628 , 0.66967383, 0.75094417, 0.750349 ,
0.75504698, 0.8294501 , 0.8269283 , 0.72510683, 0.75216203,
0.65455671, 0.78221827, 0.85766651, 0.71784808, 0.70451273,
0.65097056, 0.67635717, 0.8348752 , 0.66237197, 0.81114774,
0.84789897, 0.81963026, 0.75187688, 0.6926166 , 0.78324039,
0.79429715, 0.85306201, 0.7999448 , 0.81295493, 0.78087017,
0.81536678, 0.9000186 , 0.85754177, 0.79745725, 0.8588193 ,
0.64983181, 0.79518841, 0.82143113, 0.80597285, 0.70288909,
0.63807476, 0.74634209, 0.79426777, 0.75153219, 0.76617287,
0.81232461, 0.83281977, 0.66349794, 0.8667949 , 0.81661125,
0.84033428, 0.75392133, 0.74427587, 0.86012831, 0.79240275,
0.78352828, 0.76962504, 0.61418878, 0.78924133, 0.7505817 ,
0.69352933, 0.79756126, 0.75306564, 0.78797154, 0.74786822,
0.83356285, 0.70711923, 0.77730714, 0.81541002, 0.84997223,
0.71110386, 0.83925462, 0.7730966 , 0.77703325, 0.80735617,
0.73697573, 0.70547411, 0.75703794, 0.74065367, 0.79344293,
0.7469455 , 0.78026118, 0.84285856, 0.81574813, 0.80951048,
0.71216516, 0.79147053, 0.80156228, 0.84571216, 0.68230925,
0.82392792, 0.7647072 , 0.72527304, 0.60323458, 0.6556173 ,
0.73507975, 0.80787259, 0.77703519, 0.74631833, 0.73895631,
0.72297909, 0.83824752, 0.77386088, 0.7938971 , 0.82658258,
0.86269937, 0.83401439, 0.72284881, 0.74667737, 0.73132208,
0.86566985, 0.77163378, 0.81014209, 0.61742465, 0.8157438 ,
0.84884584, 0.85013193, 0.78305373, 0.75486484, 0.8253993 ,
0.54639328, 0.86780171, 0.71277168, 0.82272407, 0.82742102,
0.81721693, 0.79456132, 0.691349 , 0.72553111, 0.86326933,
0.79928788, 0.73098594, 0.7417468 , 0.83190214, 0.81428248,
0.78795739, 0.80532309, 0.75515983, 0.73771189, 0.84278698,
0.82290816, 0.75736555, 0.87174837, 0.80405932, 0.77545813,
0.84486347, 0.8493201 , 0.72588181, 0.72960726, 0.86348649,
0.77965498, 0.84160545, 0.85104777, 0.79341997, 0.89821488,
0.79008957, 0.8944136 , 0.84636413, 0.73471566, 0.77595883,
0.83216718, 0.67307059, 0.89267915, 0.84015739, 0.88070279,
0.8687799 , 0.81861896, 0.81354895, 0.75011821, 0.82580562,
0.82462186, 0.80951347, 0.86085849, 0.87814262, 0.87568749,
0.82467864, 0.72896849, 0.80559461, 0.67575205, 0.84630309,
0.87136449, 0.75493815, 0.8353776 , 0.84778263, 0.82723989,
0.78648099, 0.75902369, 0.80114775, 0.749058 , 0.77340719,
0.84505629, 0.77184836, 0.76871213, 0.8367901 , 0.84108881,
0.66339961, 0.85649699, 0.86092051, 0.81443709, 0.81926481,
0.80876012, 0.82879998, 0.80347288, 0.86233228, 0.68155448,
0.72901208, 0.75876003, 0.86387069, 0.83785717, 0.86864573,
0.87577335, 0.68167312, 0.83276019, 0.69431137, 0.84243207,
0.85835638, 0.80120084, 0.90731931, 0.81708914, 0.81478006,
0.74478258, 0.81925667, 0.89547686, 0.85024491, 0.80966068,
0.84296397, 0.78706824, 0.71822139, 0.85795131, 0.87123726,
0.84172698, 0.77202771, 0.6508098 , 0.80321987, 0.85862634,
0.84179504, 0.84591356, 0.86506096, 0.84126952, 0.84562183,
0.84528734, 0.88981953, 0.81733704, 0.87195009, 0.80491162,
0.7056757 , 0.81158454, 0.77565841, 0.86247361, 0.80909118,
0.89834397, 0.71028773, 0.90786938, 0.87057002, 0.78311054,
0.7929108 , 0.88108347, 0.81452386, 0.79988122, 0.91806945,
0.83961911, 0.79881573, 0.86260492, 0.82156897, 0.85405244,
0.87547568, 0.84653234, 0.76783725, 0.88126453, 0.92929527,
0.8443021 , 0.83498423, 0.81857319, 0.79822423, 0.83107279,
0.64193288, 0.75602861, 0.79313731, 0.82142473, 0.87355467,
0.89101142, 0.89809032, 0.82447311, 0.91387418, 0.91771575,
0.88991617, 0.89205958, 0.73153062, 0.86053765, 0.83706898,
0.84313025, 0.88584486, 0.72799194, 0.80808901, 0.85152235,
0.79868679, 0.80830042, 0.80615145, 0.84343367, 0.75643662,
0.85040634, 0.87978815, 0.88127016, 0.84568327, 0.79613073,
0.79800131, 0.8425993 , 0.89946225, 0.89216467, 0.81457606,
0.76854912, 0.88813623, 0.78181221, 0.76046368, 0.69412285,
0.88321127, 0.79362642, 0.84425893, 0.91788579, 0.89995368,
0.91421038, 0.82006126, 0.86183675, 0.88084145, 0.87521187,
0.86405858, 0.87690483, 0.89401839, 0.89743652, 0.82647327,
0.70071132, 0.92656917, 0.88886505, 0.8356056 , 0.84904138,
0.91961202, 0.89299023, 0.82336051, 0.77715598, 0.83165552,
0.83181568, 0.85917843, 0.88296839, 0.86120011, 0.91282398,
0.81064277, 0.88015648, 0.86422986, 0.91874087, 0.74062714,
0.83704206, 0.85348609, 0.79226008, 0.92253033, 0.88053539,
0.73035457, 0.85733181, 0.85300236, 0.90579223, 0.79526878,
0.84101671, 0.86111672, 0.84930235, 0.82186933, 0.91589538,
0.88600531, 0.88523891, 0.85527677, 0.84314781, 0.92381199,
0.81496703, 0.93216511, 0.80378404, 0.86960294, 0.85921183,
0.83437442, 0.76386592, 0.93450726, 0.94316173, 0.79730237,
0.74347865, 0.85531307, 0.78066335, 0.80173626, 0.83807505,
0.90290016, 0.88750409, 0.8497161 , 0.88578751, 0.79431739,
0.94056369, 0.68331566, 0.7730895 , 0.76598634, 0.8318359 ,
0.80169517, 0.82385107, 0.78793622, 0.92445874, 0.81918926,
0.86500998, 0.91872681, 0.85650929, 0.793601 , 0.89468972,
0.86034134, 0.90755677, 0.87682512, 0.93461346, 0.87315535,
0.86463119, 0.88046494, 0.9295376 , 0.84264448, 0.79460758,
0.91319384, 0.81679812, 0.88408868, 0.81865552, 0.77459482,
0.83487331, 0.81885783, 0.84110816, 0.84217269, 0.83349564,
0.83935464, 0.84189798, 0.8493825 , 0.83739071, 0.81718704,
0.88097817, 0.88012755, 0.88080112, 0.871131 , 0.83101195,
0.84170563, 0.90346413, 0.9247137 , 0.87194294, 0.8523832 ,
0.90098215, 0.87152425, 0.88744755, 0.87330002, 0.82658878,
0.823768 , 0.89642159, 0.88365068, 0.73045832, 0.91487204,
0.90620671, 0.86806633, 0.77344142, 0.83304509, 0.87241106,
0.87789589, 0.89608253, 0.83303148, 0.88509045, 0.90705543,
0.78370868, 0.83268226, 0.84410397, 0.92478163, 0.90914182,
0.85694243, 0.68993667, 0.71474763, 0.92002935, 0.88224189,
0.83146325, 0.85794869, 0.86288156, 0.90933547, 0.8154366 ,
0.79503295, 0.7252982 , 0.82590499, 0.80451217, 0.79659492,
0.91050926, 0.85527524, 0.92092028, 0.83190666, 0.93135486,
0.9267913 , 0.92163371, 0.8430316 , 0.80556588, 0.84872595,
0.92862808, 0.88222326, 0.88877888, 0.81333793, 0.87307225,
0.87660613, 0.85971752, 0.89878078, 0.89557384, 0.80293526,
0.87903928, 0.8926007 , 0.84002524, 0.85806281, 0.88865781,
0.81452856, 0.88151475, 0.87197513, 0.91870598, 0.83573531,
0.8415674 , 0.86411992, 0.79197911, 0.93204571, 0.83398037,
0.83242396, 0.76662546, 0.84886187, 0.91362648, 0.88410089,
0.87223676, 0.87603587, 0.85585349, 0.9157063 , 0.83216791,
0.90174772, 0.84414959, 0.77887159, 0.84401933, 0.89840957,
0.68538543, 0.84465167, 0.86774847, 0.88858803, 0.8423164 ,
0.89629744, 0.90716593, 0.85502839, 0.82834762, 0.89655692,
0.87634778, 0.8675785 , 0.92354599, 0.92825119, 0.90964737,
0.83904649, 0.901032 , 0.90454151, 0.89820742, 0.91048292,
0.92914603, 0.9193518 , 0.82223913, 0.89202444, 0.89195956,
0.90518363, 0.89356037, 0.9221184 , 0.91966589, 0.88806616,
0.85737237, 0.86677688, 0.90989635, 0.90146259, 0.8714775 ,
0.88229251, 0.89348685, 0.87838221, 0.75911156, 0.81252398,
0.75640634, 0.83978608, 0.91751651, 0.8944292 , 0.76473957], dtype=float64)
import matplotlib.pyplot as plt
# `result.reward_history` holds the reward (here the batch-mean fidelity, in
# [0, 1]) at every training iteration, as opposed to the printed loss which
# includes the unbounded log-probability REINFORCE term used for the gradient.
reward_history = result.reward_history
iters = jnp.arange(reward_history.shape[0])
# The reward is estimated over `batch_size` stochastic trajectories, so it is
# noisy; overlay a rolling mean to see the underlying trend.
window = 20
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(iters, reward_history, alpha=0.35, label="reward (per-iteration, batch mean)")
if reward_history.shape[0] >= window:
smoothed = jnp.convolve(reward_history, jnp.ones(window) / window, mode="valid")
ax.plot(iters[window - 1:], smoothed, color="C3", lw=2, label=f"rolling mean (window={window})")
ax.axhline(1.0, ls="--", color="gray", lw=1, label="max reward")
ax.set_xlabel("iteration")
ax.set_ylabel("reward (mean fidelity)")
ax.set_ylim(0, 1.05)
ax.set_title("Reward evolution during training")
ax.legend()
plt.show()
print(f"initial reward: {float(reward_history[0]):.4f}")
print(f"final reward: {float(reward_history[-1]):.4f}")
print(f"best reward: {float(jnp.nanmax(reward_history)):.4f}")
initial reward: 0.2018
final reward: 0.7647
best reward: 0.9432