A2. State preparation with Jaynes-Cummings controls for 4-legged cat state#
from feedback_grape.fgrape import optimize_pulse
from feedback_grape.utils.operators import (
sigmap,
sigmam,
create,
destroy,
identity,
)
from feedback_grape.utils.states import basis, fock
from feedback_grape.utils.tensor import tensor
import jax.numpy as jnp
from jax.scipy.linalg import expm
As a preliminary step, we consider state preparation of a target state starting from a pure state. In addition, we assume that any coupling to an external environment is negligible and that the parametrized controls can be implemented perfectly.
Here no feedback is required, we are just testing the parameterized gates setup.
As a first example, we consider the state preparation of a cavity resonantly coupled to an externally driven qubit
Here, we consider a particular sequence of parametrized unitary gates originally introduced by Law and Eberly
from feedback_grape.utils.modeling import QubitCavity
N_cav = 40
hs = QubitCavity(1, N_cav)
q = hs.qubits[0]
c = hs.cavities[0]
sigp = q.sigmap
sigm = q.sigmam
def qubit_unitary(alphas):
alpha = alphas[0] + 1j * alphas[1]
U = expm(-1j * (alpha * sigp + alpha.conjugate() * sigm) / 2)
return U
def qubit_unitary_old(alphas):
alpha_re = alphas[0]
alpha_im = alphas[1]
alpha = alpha_re + 1j * alpha_im
return tensor(
expm(-1j * (alpha * sigmap() + alpha.conjugate() * sigmam()) / 2),
identity(N_cav),
)
jnp.allclose(
qubit_unitary_old([1.0, 0.6]),
qubit_unitary([1.0, 0.6]),
)
Array(True, dtype=bool)
a = c.a
adag = c.adag
def qubit_cavity_unitary(betas):
beta = betas
H_int = beta * (a @ sigp) + beta.conjugate() * (adag @ sigm)
return expm(-1j * H_int / 2)
def qubit_cavity_unitary_old(beta_re):
beta = beta_re
return expm(
-1j
* (
beta * (tensor(sigmap(), destroy(N_cav)))
+ beta.conjugate() * (tensor(sigmam(), create(N_cav)))
)
/ 2
)
jnp.allclose(
qubit_cavity_unitary_old(jnp.array([1.0])),
qubit_cavity_unitary(jnp.array([1.0])),
atol=1e-20,
) # True
Array(True, dtype=bool)
In their groundbreaking work, Law and Eberly have shown that any arbitrary superposition of Fock states with maximal excitation number N can be prepared out of the ground state in a sequence of N such interleaved gates, also providing an algorithm to find the correct angles and interaction durations
First target is the 4 component cat state#
time_steps = 20
from feedback_grape.utils.states import coherent
psi0 = tensor(basis(2), basis(N_cav))
psi0 = psi0 / jnp.linalg.norm(psi0)
average_photon_number = 9
cat = (
coherent(N_cav, 3)
+ coherent(N_cav, -3)
+ coherent(N_cav, 1j * 3)
+ coherent(N_cav, -1j * 3)
)
psi_target = tensor(basis(2), cat)
psi_target = psi_target / jnp.linalg.norm(psi_target)
psi0.shape
(80, 1)
psi_target.shape
(80, 1)
print(fock(N_cav, 1))
[[0.+0.j]
[1.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]
[0.+0.j]]
Law and Eberly provided an algorithm to determine the correct parameters for state preparation. These include:
The rotation angle \( |\alpha| \),
The azimuthal angle \( \arg\left(\frac{\alpha}{|\alpha|}\right) \),
The interaction duration \( |\beta| \).
So Goal is to find the best control vector (rather than control amplitudes, this time) that leads to finding the optimal state-preparation strategies. Performing as well as the Law-Eberly algorithm.
Optimizing#
Currently l-bfgs with the same learning rate of 0.3 converges at a local minimum of 0.5, adam also converges at 0.5 but at smaller learning rates
from feedback_grape.fgrape import Gate
import jax
key = jax.random.PRNGKey(42)
# not provideing param_constraints just propagates the same initial_parameters for each time step
qub_unitary = Gate(
gate=qubit_unitary,
initial_params=jax.random.uniform(
key,
shape=(2,), # 2 for gamma and delta
minval=-5,
maxval=5,
dtype=jnp.float64,
),
measurement_flag=False,
)
qub_cav = Gate(
gate=qubit_cavity_unitary,
initial_params=jax.random.uniform(
key,
shape=(1,),
minval=-5,
maxval=5,
dtype=jnp.float64,
),
measurement_flag=False,
)
system_params = [qub_unitary, qub_cav]
result = optimize_pulse(
U_0=psi0,
C_target=psi_target,
system_params=system_params,
num_time_steps=time_steps,
max_iter=3000,
convergence_threshold=None, # 0.0 or None to reach max iterations
evo_type="state",
mode="no-measurement",
goal="fidelity",
learning_rate=0.01,
batch_size=10,
eval_batch_size=1,
progress=True,
)
Iteration 0, Loss: -0.032482
Iteration 10, Loss: -0.119766
Iteration 20, Loss: -0.218807
Iteration 30, Loss: -0.290923
Iteration 40, Loss: -0.331432
Iteration 50, Loss: -0.363328
Iteration 60, Loss: -0.385187
Iteration 70, Loss: -0.392812
Iteration 80, Loss: -0.398971
Iteration 90, Loss: -0.406213
Iteration 100, Loss: -0.414206
Iteration 110, Loss: -0.424084
Iteration 120, Loss: -0.436752
Iteration 130, Loss: -0.455166
Iteration 140, Loss: -0.483714
Iteration 150, Loss: -0.526234
Iteration 160, Loss: -0.587400
Iteration 170, Loss: -0.658891
Iteration 180, Loss: -0.710771
Iteration 190, Loss: -0.743793
Iteration 200, Loss: -0.767970
Iteration 210, Loss: -0.785470
Iteration 220, Loss: -0.794869
Iteration 230, Loss: -0.799156
Iteration 240, Loss: -0.801985
Iteration 250, Loss: -0.804416
Iteration 260, Loss: -0.806428
Iteration 270, Loss: -0.808274
Iteration 280, Loss: -0.809989
Iteration 290, Loss: -0.811600
Iteration 300, Loss: -0.813111
Iteration 310, Loss: -0.814524
Iteration 320, Loss: -0.815841
Iteration 330, Loss: -0.817068
Iteration 340, Loss: -0.818218
Iteration 350, Loss: -0.819303
Iteration 360, Loss: -0.820341
Iteration 370, Loss: -0.821346
Iteration 380, Loss: -0.822334
Iteration 390, Loss: -0.823330
Iteration 400, Loss: -0.824376
Iteration 410, Loss: -0.825539
Iteration 420, Loss: -0.826936
Iteration 430, Loss: -0.828753
Iteration 440, Loss: -0.831279
Iteration 450, Loss: -0.834815
Iteration 460, Loss: -0.839446
Iteration 470, Loss: -0.845438
Iteration 480, Loss: -0.853808
Iteration 490, Loss: -0.864693
Iteration 500, Loss: -0.876147
Iteration 510, Loss: -0.890039
Iteration 520, Loss: -0.905138
Iteration 530, Loss: -0.918734
Iteration 540, Loss: -0.929061
Iteration 550, Loss: -0.935838
Iteration 560, Loss: -0.939936
Iteration 570, Loss: -0.942359
Iteration 580, Loss: -0.943913
Iteration 590, Loss: -0.945100
Iteration 600, Loss: -0.946135
Iteration 610, Loss: -0.947034
Iteration 620, Loss: -0.947801
Iteration 630, Loss: -0.948456
Iteration 640, Loss: -0.949025
Iteration 650, Loss: -0.949528
Iteration 660, Loss: -0.949979
Iteration 670, Loss: -0.950387
Iteration 680, Loss: -0.950758
Iteration 690, Loss: -0.951097
Iteration 700, Loss: -0.951405
Iteration 710, Loss: -0.951687
Iteration 720, Loss: -0.951943
Iteration 730, Loss: -0.952177
Iteration 740, Loss: -0.952389
Iteration 750, Loss: -0.952581
Iteration 760, Loss: -0.952754
Iteration 770, Loss: -0.952908
Iteration 780, Loss: -0.953045
Iteration 790, Loss: -0.953168
Iteration 800, Loss: -0.953279
Iteration 810, Loss: -0.953380
Iteration 820, Loss: -0.953474
Iteration 830, Loss: -0.953563
Iteration 840, Loss: -0.953649
Iteration 850, Loss: -0.953732
Iteration 860, Loss: -0.953814
Iteration 870, Loss: -0.953894
Iteration 880, Loss: -0.953972
Iteration 890, Loss: -0.954047
Iteration 900, Loss: -0.954119
Iteration 910, Loss: -0.954189
Iteration 920, Loss: -0.954255
Iteration 930, Loss: -0.954318
Iteration 940, Loss: -0.954379
Iteration 950, Loss: -0.954437
Iteration 960, Loss: -0.954492
Iteration 970, Loss: -0.954545
Iteration 980, Loss: -0.954595
Iteration 990, Loss: -0.954643
Iteration 1000, Loss: -0.954688
Iteration 1010, Loss: -0.954731
Iteration 1020, Loss: -0.954772
Iteration 1030, Loss: -0.954811
Iteration 1040, Loss: -0.954847
Iteration 1050, Loss: -0.954882
Iteration 1060, Loss: -0.954915
Iteration 1070, Loss: -0.954946
Iteration 1080, Loss: -0.954976
Iteration 1090, Loss: -0.955004
Iteration 1100, Loss: -0.955031
Iteration 1110, Loss: -0.955057
Iteration 1120, Loss: -0.955082
Iteration 1130, Loss: -0.955105
Iteration 1140, Loss: -0.955128
Iteration 1150, Loss: -0.955150
Iteration 1160, Loss: -0.955172
Iteration 1170, Loss: -0.955193
Iteration 1180, Loss: -0.955213
Iteration 1190, Loss: -0.955233
Iteration 1200, Loss: -0.955253
Iteration 1210, Loss: -0.955272
Iteration 1220, Loss: -0.955291
Iteration 1230, Loss: -0.955310
Iteration 1240, Loss: -0.955328
Iteration 1250, Loss: -0.955347
Iteration 1260, Loss: -0.955365
Iteration 1270, Loss: -0.955384
Iteration 1280, Loss: -0.955402
Iteration 1290, Loss: -0.955421
Iteration 1300, Loss: -0.955439
Iteration 1310, Loss: -0.955457
Iteration 1320, Loss: -0.955476
Iteration 1330, Loss: -0.955399
Iteration 1340, Loss: -0.955490
Iteration 1350, Loss: -0.955523
Iteration 1360, Loss: -0.955551
Iteration 1370, Loss: -0.955571
Iteration 1380, Loss: -0.955591
Iteration 1390, Loss: -0.955611
Iteration 1400, Loss: -0.955631
Iteration 1410, Loss: -0.955652
Iteration 1420, Loss: -0.955674
Iteration 1430, Loss: -0.955695
Iteration 1440, Loss: -0.955718
Iteration 1450, Loss: -0.955740
Iteration 1460, Loss: -0.955764
Iteration 1470, Loss: -0.955788
Iteration 1480, Loss: -0.955812
Iteration 1490, Loss: -0.955837
Iteration 1500, Loss: -0.955863
Iteration 1510, Loss: -0.955890
Iteration 1520, Loss: -0.955918
Iteration 1530, Loss: -0.955946
Iteration 1540, Loss: -0.955975
Iteration 1550, Loss: -0.956005
Iteration 1560, Loss: -0.956036
Iteration 1570, Loss: -0.956068
Iteration 1580, Loss: -0.956101
Iteration 1590, Loss: -0.956136
Iteration 1600, Loss: -0.956171
Iteration 1610, Loss: -0.956208
Iteration 1620, Loss: -0.956246
Iteration 1630, Loss: -0.956285
Iteration 1640, Loss: -0.956326
Iteration 1650, Loss: -0.956324
Iteration 1660, Loss: -0.956357
Iteration 1670, Loss: -0.956443
Iteration 1680, Loss: -0.956503
Iteration 1690, Loss: -0.956556
Iteration 1700, Loss: -0.956609
Iteration 1710, Loss: -0.956664
Iteration 1720, Loss: -0.956722
Iteration 1730, Loss: -0.956783
Iteration 1740, Loss: -0.956847
Iteration 1750, Loss: -0.956915
Iteration 1760, Loss: -0.956986
Iteration 1770, Loss: -0.957062
Iteration 1780, Loss: -0.957142
Iteration 1790, Loss: -0.957227
Iteration 1800, Loss: -0.957318
Iteration 1810, Loss: -0.957414
Iteration 1820, Loss: -0.957515
Iteration 1830, Loss: -0.957545
Iteration 1840, Loss: -0.957728
Iteration 1850, Loss: -0.957856
Iteration 1860, Loss: -0.957992
Iteration 1870, Loss: -0.958128
Iteration 1880, Loss: -0.958274
Iteration 1890, Loss: -0.958427
Iteration 1900, Loss: -0.958587
Iteration 1910, Loss: -0.958753
Iteration 1920, Loss: -0.958926
Iteration 1930, Loss: -0.959104
Iteration 1940, Loss: -0.959287
Iteration 1950, Loss: -0.959475
Iteration 1960, Loss: -0.959667
Iteration 1970, Loss: -0.959863
Iteration 1980, Loss: -0.960063
Iteration 1990, Loss: -0.960269
Iteration 2000, Loss: -0.960376
Iteration 2010, Loss: -0.960675
Iteration 2020, Loss: -0.960926
Iteration 2030, Loss: -0.961193
Iteration 2040, Loss: -0.961475
Iteration 2050, Loss: -0.961783
Iteration 2060, Loss: -0.962129
Iteration 2070, Loss: -0.962528
Iteration 2080, Loss: -0.962997
Iteration 2090, Loss: -0.963565
Iteration 2100, Loss: -0.964271
Iteration 2110, Loss: -0.965170
Iteration 2120, Loss: -0.966337
Iteration 2130, Loss: -0.967835
Iteration 2140, Loss: -0.969382
Iteration 2150, Loss: -0.971635
Iteration 2160, Loss: -0.973842
Iteration 2170, Loss: -0.976328
Iteration 2180, Loss: -0.979326
Iteration 2190, Loss: -0.983073
Iteration 2200, Loss: -0.987108
Iteration 2210, Loss: -0.990260
Iteration 2220, Loss: -0.992860
Iteration 2230, Loss: -0.994457
Iteration 2240, Loss: -0.995198
Iteration 2250, Loss: -0.995550
Iteration 2260, Loss: -0.995746
Iteration 2270, Loss: -0.995901
Iteration 2280, Loss: -0.996036
Iteration 2290, Loss: -0.996152
Iteration 2300, Loss: -0.996248
Iteration 2310, Loss: -0.996331
Iteration 2320, Loss: -0.996402
Iteration 2330, Loss: -0.996465
Iteration 2340, Loss: -0.996521
Iteration 2350, Loss: -0.996571
Iteration 2360, Loss: -0.996616
Iteration 2370, Loss: -0.996656
Iteration 2380, Loss: -0.996693
Iteration 2390, Loss: -0.996726
Iteration 2400, Loss: -0.996757
Iteration 2410, Loss: -0.996785
Iteration 2420, Loss: -0.996811
Iteration 2430, Loss: -0.996835
Iteration 2440, Loss: -0.996858
Iteration 2450, Loss: -0.996879
Iteration 2460, Loss: -0.996899
Iteration 2470, Loss: -0.996918
Iteration 2480, Loss: -0.996936
Iteration 2490, Loss: -0.996953
Iteration 2500, Loss: -0.996970
Iteration 2510, Loss: -0.996986
Iteration 2520, Loss: -0.997001
Iteration 2530, Loss: -0.997016
Iteration 2540, Loss: -0.997030
Iteration 2550, Loss: -0.997044
Iteration 2560, Loss: -0.997057
Iteration 2570, Loss: -0.997071
Iteration 2580, Loss: -0.997083
Iteration 2590, Loss: -0.997096
Iteration 2600, Loss: -0.997108
Iteration 2610, Loss: -0.996913
Iteration 2620, Loss: -0.997108
Iteration 2630, Loss: -0.997139
Iteration 2640, Loss: -0.997137
Iteration 2650, Loss: -0.997159
Iteration 2660, Loss: -0.997174
Iteration 2670, Loss: -0.997184
Iteration 2680, Loss: -0.997194
Iteration 2690, Loss: -0.997204
Iteration 2700, Loss: -0.997213
Iteration 2710, Loss: -0.997222
Iteration 2720, Loss: -0.997231
Iteration 2730, Loss: -0.997240
Iteration 2740, Loss: -0.997249
Iteration 2750, Loss: -0.997258
Iteration 2760, Loss: -0.997266
Iteration 2770, Loss: -0.997274
Iteration 2780, Loss: -0.997282
Iteration 2790, Loss: -0.997290
Iteration 2800, Loss: -0.997297
Iteration 2810, Loss: -0.997304
Iteration 2820, Loss: -0.997311
Iteration 2830, Loss: -0.997318
Iteration 2840, Loss: -0.997325
Iteration 2850, Loss: -0.997332
Iteration 2860, Loss: -0.997338
Iteration 2870, Loss: -0.997344
Iteration 2880, Loss: -0.997350
Iteration 2890, Loss: -0.997355
Iteration 2900, Loss: -0.997361
Iteration 2910, Loss: -0.997366
Iteration 2920, Loss: -0.997371
Iteration 2930, Loss: -0.997376
Iteration 2940, Loss: -0.997381
Iteration 2950, Loss: -0.997385
Iteration 2960, Loss: -0.997389
Iteration 2970, Loss: -0.997393
Iteration 2980, Loss: -0.997397
Iteration 2990, Loss: -0.997401
result.final_fidelity
Array(0.9973549, dtype=float64)
len(result.returned_params)
20
# here makes sense for each batch size we have a different set of parameters since there are no measurements and therefore no stochasticisty or randomness
result.returned_params
[[Array([[-4.62324000e-06, -5.89085067e+00]], dtype=float64),
Array([[-3.27048219]], dtype=float64)],
[Array([[ 5.31659163e-06, -2.77998502e+00]], dtype=float64),
Array([[2.16754583]], dtype=float64)],
[Array([[ 2.16053119e-06, -3.49835706e+00]], dtype=float64),
Array([[1.577439]], dtype=float64)],
[Array([[-5.79042605e-07, -2.99807494e+00]], dtype=float64),
Array([[-1.98513254]], dtype=float64)],
[Array([[ 1.42242780e-05, -9.48269605e+00]], dtype=float64),
Array([[-1.58182228]], dtype=float64)],
[Array([[-2.44106373e-06, -2.97355662e+00]], dtype=float64),
Array([[-1.15054257]], dtype=float64)],
[Array([[ 3.13095836e-06, -2.96698403e+00]], dtype=float64),
Array([[-1.06901013]], dtype=float64)],
[Array([[ 3.06445444e-07, -3.72277902e+00]], dtype=float64),
Array([[-1.67999419]], dtype=float64)],
[Array([[ 1.27359212e-06, -2.75372716e+00]], dtype=float64),
Array([[-0.98807547]], dtype=float64)],
[Array([[ 4.34239745e-06, -4.55148888e+00]], dtype=float64),
Array([[-0.2123829]], dtype=float64)],
[Array([[-6.98637353e-07, -4.47998988e+00]], dtype=float64),
Array([[-1.43869226]], dtype=float64)],
[Array([[ 3.09658427e-06, -3.51750340e+00]], dtype=float64),
Array([[-1.07470747]], dtype=float64)],
[Array([[ 2.54252598e-06, -4.84349964e+00]], dtype=float64),
Array([[-1.7941053]], dtype=float64)],
[Array([[-2.22474043e-05, -5.37498860e+00]], dtype=float64),
Array([[-0.95934704]], dtype=float64)],
[Array([[-3.28939507e-06, -2.74645141e+00]], dtype=float64),
Array([[0.54110028]], dtype=float64)],
[Array([[-4.04616022e-06, -3.40028139e+00]], dtype=float64),
Array([[0.40006139]], dtype=float64)],
[Array([[-6.21003422e-06, -4.68820791e+00]], dtype=float64),
Array([[-1.39992878]], dtype=float64)],
[Array([[ 1.02069290e-05, -3.91162463e+00]], dtype=float64),
Array([[-0.8732439]], dtype=float64)],
[Array([[-8.04187891e-06, -9.49385119e+00]], dtype=float64),
Array([[-0.7933838]], dtype=float64)],
[Array([[ 1.09706534e-03, -3.13666393e+00]], dtype=float64),
Array([[0.00032511]], dtype=float64)]]
print(result.final_state)
[[[ 2.23466738e-02+7.84550303e-06j]
[-2.31721988e-07+4.33264185e-04j]
[-1.56142601e-03-4.14421977e-07j]
[ 7.42628180e-08-1.65285074e-04j]
[ 3.70931245e-01+1.30460223e-04j]
[-3.83724733e-07+9.38851425e-04j]
[-1.63092324e-03-4.89843605e-07j]
[-1.82484728e-07+1.39583128e-04j]
[ 7.26747656e-01+2.55296655e-04j]
[-1.20972163e-08+2.94081030e-04j]
[ 8.92335808e-04+4.71348031e-07j]
[-2.77637098e-07+1.43128676e-03j]
[ 5.38219950e-01+1.89297973e-04j]
[-2.55255229e-07+3.33341178e-04j]
[ 3.30934084e-04+7.03711466e-08j]
[ 4.38880309e-07-5.05634058e-04j]
[ 2.09684579e-01+7.40010850e-05j]
[-1.25301653e-07+2.07428596e-04j]
[ 5.95928058e-03+2.10652138e-06j]
[-5.82309032e-08+1.66143333e-04j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[-1.46333631e-04+2.32017903e-07j]
[ 1.72035638e-07-3.10396521e-04j]
[ 5.17747501e-05+7.00603770e-08j]
[-2.80409213e-07-6.61505415e-04j]
[ 1.49736674e-03-5.77949811e-07j]
[ 7.93392852e-07+2.30161769e-03j]
[ 2.53440705e-04-2.03788903e-07j]
[-9.56802234e-07-3.62046456e-03j]
[ 8.09009728e-04-7.20472996e-07j]
[ 1.04771745e-06+2.95318372e-03j]
[ 1.55046085e-03-6.75116086e-07j]
[-3.88701371e-09+3.08177859e-04j]
[ 1.55427387e-05-1.42468994e-07j]
[ 5.03121863e-07+1.24612351e-03j]
[-3.91671677e-03+1.34561437e-06j]
[-6.53325824e-07-1.87887663e-03j]
[-7.59975220e-04+8.53622311e-08j]
[-1.84695581e-07-8.18498642e-04j]
[ 1.19877549e-05-9.22799781e-09j]
[ 1.49291851e-09-4.63199469e-06j]
[ 1.20782590e-07+4.23326003e-11j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]
[ 0.00000000e+00+0.00000000e+00j]]]
from feedback_grape.utils.fidelity import fidelity, ket2dm
fidelity(
U_final=result.final_state[0],
C_target=ket2dm(psi_target),
evo_type="density",
)
Array(0.99735492, dtype=float64)
import matplotlib.pyplot as plt
import jax.numpy as jnp
from feedback_grape.utils.operators import identity
from feedback_grape.utils.tensor import tensor
from feedback_grape.utils.states import (
fock,
) # Assuming this provides the Fock state function
# Parameters
target_levels = [0, 4, 8, 12, 16, 20, 24, 28]
# Projectors and probabilities
probs = []
for n in target_levels:
# Projector: |n⟩⟨n| ⊗ I_qubit
# probability that the final state is in the projection subspace of the
# cavity Fock state |n⟩
proj = tensor(ket2dm(basis(2)), ket2dm(fock(N_cav, n)))
prob = jnp.real(
jnp.vdot(result.final_state[0], proj @ result.final_state[0])
)
probs.append(prob)
# Plotting
plt.figure(figsize=(8, 5))
plt.bar([str(n) for n in target_levels], probs, color='orange')
plt.yscale('log')
plt.yticks(
[1e-32, 1e-16, 1e-8, 1e-4, 1e-2, 1e0],
labels=[
'$10^{-32}$',
'$10^{-16}$',
'$10^{-8}$',
'$10^{-4}$',
'$10^{-2}$',
'$10^0$',
],
)
plt.xlabel('Cavity Fock State |n⟩')
plt.ylabel('Excitation Probability $P_n$ (log scale)')
plt.title('Excitation Number Distribution $\\langle n | \\rho | n \\rangle$')
plt.grid(True, axis='y', linestyle='--', alpha=0.6)
plt.tight_layout()
plt.show()