CirQuS-UTS / QuanGuru

QuanGuru (pronounced Kangaroo) is a Python library for numerical modelling of quantum systems. It is still under-development, and it consists of tools for numerical simulations of Quantum systems.
BSD 3-Clause "New" or "Revised" License
5 stars 7 forks source link

First simulation in a sweep of cavity dimension just copies the value of the last simulation in the sweep #264

Closed adriendilonardo closed 2 months ago

adriendilonardo commented 3 months ago
import quanguru as qg
import numpy as np
from scipy.special import factorial
from matplotlib import pyplot as plt
from matplotlib.gridspec import GridSpec

def coherent_state(alpha, d):
    i_vec = np.arange(0, d)
    return (
        np.exp(-np.abs(alpha)**2/2)
        * alpha ** i_vec
        / np.sqrt(factorial(i_vec))
    ).reshape(d, 1)

# Setting Up Parameter dictionary
param = {}

sim = qg.Simulation()

param['step_size'] = 1/8
param['step_count'] = 400

sim.delStates = True
sim.stepSize = param["step_size"]
sim.stepCount = param['step_count']

param["delta"] = 1
param["kappa"] = 0.2
param["epsilon"] = 2
param["alpha"] = - param["epsilon"] / (param["delta"] -1j/2*param["kappa"])

d = -1
pop = 0
while pop < 0.999:
    d += 1
    pop += (
        np.exp(-np.abs(param['alpha'])**2)
        * np.abs(param['alpha'])**(2*d)/factorial(d)
    ) 

# param["cavity_dimension"] = d
param["cavity_dimension"] = 2

# --------------------------------------------------------------------------------------------------------------------

# SETTING UP SYSTEM FOR SIMULATION

#Cavity
H_cavity = qg.Cavity(
    frequency=param['delta'], 
    dimension=param['cavity_dimension']
)
cavity_drive_term_1 = H_cavity.createTerm(operator=qg.destroy, frequency=param['epsilon'], alias='destroy_drive')
cavity_drive_term_2 = H_cavity.createTerm(operator=qg.create, frequency=param['epsilon'], alias='create_drive')

# defining protocol and adding dissipators 
pro = qg.freeEvolution(superSys=H_cavity)

diss_obj = qg.dissipatorObj(system=H_cavity, jOper=qg.destroy(dimension=param['cavity_dimension']), jRate=param['kappa'])
diss_obj.addToProtocol(pro)

sim.addProtocol(pro)

# --------------------------------------------------------------------------------------------------------------------

# coherent state
cavity_state = qg.densityMatrix(
    qg.normalise(
        coherent_state(param['alpha'], param['cavity_dimension'])
    )
)

sim.initialStateSystem = H_cavity
sim.initialState = cavity_state

# --------------------------------------------------------------------------------------------------------------------

# Sweeps

#cavity dimension
cavity_sweep_list = np.arange(2, d*2)

def cavity_sweep_func(sweep):
    val = sweep.sweepList[sweep._sweepIndex]
    H_cavity.dimension = val
    diss_obj.jOper = qg.destroy(dimension=val)
    sim.initialState = qg.densityMatrix(qg.normalise(coherent_state(param['alpha'], val)))

cavity_sweep = sim.Sweep.createSweep(system=H_cavity, sweepKey='dimension', sweepList=cavity_sweep_list, sweepFunction=cavity_sweep_func)

# --------------------------------------------------------------------------------------------------------------------

#Compute
def dyncamicalAnalysis(sim, state):
    state = state[0]
    try:
        state = state.A
    except:
        pass

    #Cavity Populations
    sim.qRes.singleResult = 'cavity_populations', np.abs(np.diag(state))
    mean_cav_pop = np.abs(qg.expectation(qg.number(H_cavity.dimension), state))
    sim.qRes.singleResult = 'mean_cavity_population', mean_cav_pop
    sim.qRes.singleResult = 'purity', qg.trace(state@state).astype(float)

sim.compute = dyncamicalAnalysis

# -------------------------------------------------------------------------------------------------------------------

sim.run(p=False)
github-actions[bot] commented 2 months ago

Branch 264-First_simulation_in_a_sweep_of_cavity_dimension_just_copies_the_value_of_the_last_simulation_in_the_sweep created!

adriendilonardo commented 2 months ago

See comments in PR #265