A2. State preparation with Jaynes-Cummings controls for 4-legged cat state

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,
    jaxify
)
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
import qutip as qt

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

N_cav = 40
def qubit_unitary(alphas):
    alpha_re = alphas[0]
    alpha_im = alphas[1]
    alpha = alpha_re + 1j * alpha_im
    return  tensor(
        jaxify(qt.identity(N_cav)),
        expm(-1j * (alpha * jaxify(qt.sigmap()) + alpha.conjugate() * jaxify(qt.sigmam())) / 2),
    )
def qubit_cavity_unitary(beta_re):
    beta = beta_re
    return expm(
        -1j
        * (
            beta * (tensor(jaxify(qt.destroy(N_cav)), jaxify(qt.sigmap())))
            + beta.conjugate() * (tensor(jaxify(qt.create(N_cav)), jaxify(qt.sigmam())))
        )
        / 2
    )

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
import math

psi0 = tensor(jaxify(qt.basis(N_cav)), jaxify(qt.basis(2)))
psi0 = psi0 / jnp.linalg.norm(psi0)
average_photon_number = 9
# here you can't use jnp.sqrt because qt operators cannot deal with jax arrays and objects
# also here method="analytic" is used because this is the method that is used to define a coherent state in this package as
# well, so to ensure maximal accuracy, though not using it would also work
alpha = math.sqrt(average_photon_number)
cat = (
    jaxify(qt.coherent(N_cav, alpha, method="analytic"))
    + jaxify(qt.coherent(N_cav, -alpha, method="analytic" ))
    + jaxify(qt.coherent(N_cav, 1j * alpha, method="analytic"))
    + jaxify(qt.coherent(N_cav, -1j * alpha, method="analytic"))
)
psi_target = tensor(cat, jaxify(qt.basis(2)))
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(
    init_state=psi0,
    target_state=psi_target,
    system_params=system_params,
    num_time_steps=time_steps,
    max_iter=3000,
    convergence_threshold=None,  # 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.955451
Iteration 1340, Loss: -0.955502
Iteration 1350, Loss: -0.955532
Iteration 1360, Loss: -0.955549
Iteration 1370, Loss: -0.955570
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.956102
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.956350
Iteration 1660, Loss: -0.956341
Iteration 1670, Loss: -0.956431
Iteration 1680, Loss: -0.956501
Iteration 1690, Loss: -0.956556
Iteration 1700, Loss: -0.956608
Iteration 1710, Loss: -0.956664
Iteration 1720, Loss: -0.956722
Iteration 1730, Loss: -0.956783
Iteration 1740, Loss: -0.956847
Iteration 1750, Loss: -0.956914
Iteration 1760, Loss: -0.956986
Iteration 1770, Loss: -0.957062
Iteration 1780, Loss: -0.957142
Iteration 1790, Loss: -0.957227
Iteration 1800, Loss: -0.957317
Iteration 1810, Loss: -0.957413
Iteration 1820, Loss: -0.957516
Iteration 1830, Loss: -0.957624
Iteration 1840, Loss: -0.957739
Iteration 1850, Loss: -0.957812
Iteration 1860, Loss: -0.957952
Iteration 1870, Loss: -0.958128
Iteration 1880, Loss: -0.958272
Iteration 1890, Loss: -0.958425
Iteration 1900, Loss: -0.958586
Iteration 1910, Loss: -0.958752
Iteration 1920, Loss: -0.958925
Iteration 1930, Loss: -0.959103
Iteration 1940, Loss: -0.959285
Iteration 1950, Loss: -0.959473
Iteration 1960, Loss: -0.959664
Iteration 1970, Loss: -0.959860
Iteration 1980, Loss: -0.960060
Iteration 1990, Loss: -0.960267
Iteration 2000, Loss: -0.960481
Iteration 2010, Loss: -0.960705
Iteration 2020, Loss: -0.960943
Iteration 2030, Loss: -0.961199
Iteration 2040, Loss: -0.961454
Iteration 2050, Loss: -0.961723
Iteration 2060, Loss: -0.962108
Iteration 2070, Loss: -0.962515
Iteration 2080, Loss: -0.962990
Iteration 2090, Loss: -0.963558
Iteration 2100, Loss: -0.964260
Iteration 2110, Loss: -0.965153
Iteration 2120, Loss: -0.966310
Iteration 2130, Loss: -0.967795
Iteration 2140, Loss: -0.969609
Iteration 2150, Loss: -0.971660
Iteration 2160, Loss: -0.973877
Iteration 2170, Loss: -0.976118
Iteration 2180, Loss: -0.979280
Iteration 2190, Loss: -0.983008
Iteration 2200, Loss: -0.987019
Iteration 2210, Loss: -0.990233
Iteration 2220, Loss: -0.992811
Iteration 2230, Loss: -0.994444
Iteration 2240, Loss: -0.995219
Iteration 2250, Loss: -0.995551
Iteration 2260, Loss: -0.995746
Iteration 2270, Loss: -0.995898
Iteration 2280, Loss: -0.996041
Iteration 2290, Loss: -0.996159
Iteration 2300, Loss: -0.996257
Iteration 2310, Loss: -0.996340
Iteration 2320, Loss: -0.996412
Iteration 2330, Loss: -0.996475
Iteration 2340, Loss: -0.996523
Iteration 2350, Loss: -0.996556
Iteration 2360, Loss: -0.996622
Iteration 2370, Loss: -0.996666
Iteration 2380, Loss: -0.996701
Iteration 2390, Loss: -0.996735
Iteration 2400, Loss: -0.996765
Iteration 2410, Loss: -0.996793
Iteration 2420, Loss: -0.996818
Iteration 2430, Loss: -0.996830
Iteration 2440, Loss: -0.996824
Iteration 2450, Loss: -0.996871
Iteration 2460, Loss: -0.996900
Iteration 2470, Loss: -0.996922
Iteration 2480, Loss: -0.996941
Iteration 2490, Loss: -0.996959
Iteration 2500, Loss: -0.996975
Iteration 2510, Loss: -0.996991
Iteration 2520, Loss: -0.997006
Iteration 2530, Loss: -0.997021
Iteration 2540, Loss: -0.997030
Iteration 2550, Loss: -0.996945
Iteration 2560, Loss: -0.997024
Iteration 2570, Loss: -0.997070
Iteration 2580, Loss: -0.997087
Iteration 2590, Loss: -0.997099
Iteration 2600, Loss: -0.997111
Iteration 2610, Loss: -0.997123
Iteration 2620, Loss: -0.997135
Iteration 2630, Loss: -0.997146
Iteration 2640, Loss: -0.997157
Iteration 2650, Loss: -0.997168
Iteration 2660, Loss: -0.997178
Iteration 2670, Loss: -0.997188
Iteration 2680, Loss: -0.997198
Iteration 2690, Loss: -0.997208
Iteration 2700, Loss: -0.997195
Iteration 2710, Loss: -0.997203
Iteration 2720, Loss: -0.997227
Iteration 2730, Loss: -0.997235
Iteration 2740, Loss: -0.997253
Iteration 2750, Loss: -0.997261
Iteration 2760, Loss: -0.997270
Iteration 2770, Loss: -0.997278
Iteration 2780, Loss: -0.997286
Iteration 2790, Loss: -0.997294
Iteration 2800, Loss: -0.997301
Iteration 2810, Loss: -0.997308
Iteration 2820, Loss: -0.997315
Iteration 2830, Loss: -0.997322
Iteration 2840, Loss: -0.997329
Iteration 2850, Loss: -0.997335
Iteration 2860, Loss: -0.997341
Iteration 2870, Loss: -0.997347
Iteration 2880, Loss: -0.997353
Iteration 2890, Loss: -0.997352
Iteration 2900, Loss: -0.997178
Iteration 2910, Loss: -0.997365
Iteration 2920, Loss: -0.997355
Iteration 2930, Loss: -0.997376
Iteration 2940, Loss: -0.997382
Iteration 2950, Loss: -0.997386
Iteration 2960, Loss: -0.997391
Iteration 2970, Loss: -0.997395
Iteration 2980, Loss: -0.997399
Iteration 2990, Loss: -0.997402
result.final_fidelity
Array(0.99740549, 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.18439838e-06, -5.89352447e+00]], dtype=float64),
  Array([[-3.26999326]], dtype=float64)],
 [Array([[ 5.19325639e-06, -2.78382418e+00]], dtype=float64),
  Array([[2.16489664]], dtype=float64)],
 [Array([[ 2.09438381e-06, -3.49637737e+00]], dtype=float64),
  Array([[1.57928631]], dtype=float64)],
 [Array([[-5.43958258e-07, -2.99898397e+00]], dtype=float64),
  Array([[-1.98401423]], dtype=float64)],
 [Array([[ 1.38669253e-05, -9.48314592e+00]], dtype=float64),
  Array([[-1.58075144]], dtype=float64)],
 [Array([[-2.39676652e-06, -2.97196831e+00]], dtype=float64),
  Array([[-1.15056484]], dtype=float64)],
 [Array([[ 3.17101719e-06, -2.97124228e+00]], dtype=float64),
  Array([[-1.07037973]], dtype=float64)],
 [Array([[ 3.04287393e-07, -3.72281629e+00]], dtype=float64),
  Array([[-1.68232658]], dtype=float64)],
 [Array([[ 1.05080413e-06, -2.75159050e+00]], dtype=float64),
  Array([[-0.98903212]], dtype=float64)],
 [Array([[ 3.98967706e-06, -4.55474837e+00]], dtype=float64),
  Array([[-0.21115392]], dtype=float64)],
 [Array([[-1.08576259e-06, -4.47310979e+00]], dtype=float64),
  Array([[-1.43879651]], dtype=float64)],
 [Array([[ 1.95271532e-06, -3.52349623e+00]], dtype=float64),
  Array([[-1.07268586]], dtype=float64)],
 [Array([[ 2.37547439e-06, -4.85039787e+00]], dtype=float64),
  Array([[-1.79882622]], dtype=float64)],
 [Array([[-2.12814090e-05, -5.36753204e+00]], dtype=float64),
  Array([[-0.95438139]], dtype=float64)],
 [Array([[-3.20302793e-06, -2.74799823e+00]], dtype=float64),
  Array([[0.54081537]], dtype=float64)],
 [Array([[-3.83618889e-06, -3.40412927e+00]], dtype=float64),
  Array([[0.40098509]], dtype=float64)],
 [Array([[-5.99001451e-06, -4.67939144e+00]], dtype=float64),
  Array([[-1.39831884]], dtype=float64)],
 [Array([[ 9.88167580e-06, -3.91205177e+00]], dtype=float64),
  Array([[-0.87297287]], dtype=float64)],
 [Array([[-7.78530704e-06, -9.49398213e+00]], dtype=float64),
  Array([[-0.79284886]], dtype=float64)],
 [Array([[ 1.09046072e-03, -3.13667746e+00]], dtype=float64),
  Array([[0.00093092]], dtype=float64)]]
print(result.final_state)
[[[ 2.21763511e-02+7.73158599e-06j]
  [-9.37541028e-05+1.54762098e-07j]
  [ 3.85645769e-08-8.48507070e-08j]
  [ 1.45031688e-07-7.62256113e-05j]
  [ 1.00263491e-04+5.24496116e-08j]
  [-2.27141324e-04+1.55541420e-07j]
  [-6.43024072e-08-1.14652028e-04j]
  [-1.73960356e-07-2.05181176e-04j]
  [ 3.67932201e-01+1.28704550e-04j]
  [-1.07784127e-04+9.10812841e-08j]
  [ 1.69004467e-08-4.89635111e-05j]
  [ 9.04790000e-08-2.22236838e-04j]
  [ 7.81264944e-05+2.81132810e-08j]
  [-1.84131917e-04-5.90272409e-09j]
  [-1.17684722e-07-3.56582751e-05j]
  [-5.54659841e-08-5.62736934e-04j]
  [ 7.27259274e-01+2.54036444e-04j]
  [ 3.69902209e-04-5.41547525e-07j]
  [-8.78563549e-08+5.09630656e-04j]
  [ 6.02122822e-07+2.33492980e-04j]
  [-3.37413743e-04+2.79136401e-08j]
  [ 1.50586393e-03-6.77531839e-07j]
  [-2.22008604e-08+4.98970676e-04j]
  [ 3.71565881e-07+1.47815547e-03j]
  [ 5.39564085e-01+1.88540753e-04j]
  [-1.42406583e-04-8.58344414e-08j]
  [ 7.95183843e-08-3.82137267e-04j]
  [ 5.72696657e-07+2.54200001e-04j]
  [-2.03560634e-04-1.41616493e-07j]
  [-3.78268583e-03+1.29337196e-06j]
  [ 6.47292748e-07-1.14692058e-03j]
  [-5.66800983e-07-1.63409965e-03j]
  [ 2.09848353e-01+7.35181231e-05j]
  [-7.80142168e-04+9.05986117e-08j]
  [-5.34867898e-08+1.93654541e-05j]
  [-9.16219608e-08-1.07862641e-03j]
  [ 6.00035526e-03+2.10502544e-06j]
  [ 1.45819375e-05-1.01143642e-08j]
  [-6.43066868e-08+1.84808546e-04j]
  [ 4.27099820e-09-1.26282022e-05j]
  [ 0.00000000e+00+0.00000000e+00j]
  [ 3.84696070e-07+1.33860312e-10j]
  [ 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]
  [ 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.9974055, 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(fock(N_cav, n)), ket2dm(basis(2)))
    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()
../../_images/b1de7b7d5dd3d278ab8c0d8b83810d0eb5b02175d4daf4399e811f59c22be347.png