Beispiel #1
0
def slh_Sec6():
    """SHL for the model in Section 6 of the QSD paper"""
    E = symbols(r'E', positive=True)
    chi = symbols(r'\chi', real=True)
    omega = symbols(r'\omega', real=True)
    eta = symbols(r'\eta', real=True)
    gamma1 = symbols(r'\gamma_1', positive=True)
    gamma2 = symbols(r'\gamma_2', positive=True)
    kappa = symbols(r'\kappa', positive=True)
    A1 = Destroy(hs=hs0)
    Ac1 = A1.dag()
    A2 = Destroy(hs=hs1)
    Ac2 = A2.dag()
    Sp = LocalSigma(j=1, k=0, hs=hs2)
    Sm = Sp.dag()

    H = (E * I * (Ac1 - A1) + 0.5 * chi * I *
         (Ac1 * Ac1 * A2 - A1 * A1 * Ac2) + omega * Sp * Sm + eta * I *
         (A2 * Sp - Ac2 * Sm))
    Lindblads = [
        sqrt(2 * gamma1) * A1,
        sqrt(2 * gamma2) * A2,
        sqrt(2 * kappa) * Sm
    ]

    return SLH(identity_matrix(3), Lindblads, H)
Beispiel #2
0
def test_qsd_codegen_operator_basis():
    a = Destroy(hs=hs1)
    ad = a.dag()
    s = LocalSigma(1, 0, hs=hs2)
    sd = s.dag()
    circuit = SLH(identity_matrix(0), [], a * ad + s + sd)
    codegen = QSDCodeGen(circuit)
    ob = codegen._operator_basis_lines(indent=0)
    assert dedent(ob).strip() == dedent("""
    IdentityOperator Id0(0);
    IdentityOperator Id1(1);
    AnnihilationOperator A0(0);
    FieldTransitionOperator S1_0_1(0,1,1);
    FieldTransitionOperator S1_1_0(1,0,1);
    Operator Id = Id0*Id1;
    Operator Ad0 = A0.hc();
    """).strip()
    circuit = SLH(identity_matrix(0), [], ad)
    codegen = QSDCodeGen(circuit)
    ob = codegen._operator_basis_lines(indent=0)
    assert dedent(ob).strip() == dedent("""
    IdentityOperator Id0(0);
    AnnihilationOperator A0(0);
    Operator Id = Id0;
    Operator Ad0 = A0.hc();
    """).strip()
Beispiel #3
0
def test_driven_tls(datadir):
    hs = LocalSpace('tls', basis=('g', 'e'))
    w = symbols(r'\omega', real=True)
    pi = sympy.pi
    cos = sympy.cos
    t, T, E0 = symbols('t, T, E_0', real=True)
    a = 0.16
    blackman = 0.5 * (1 - a - cos(2 * pi * t / T) + a * cos(4 * pi * t / T))
    H0 = Destroy(hs=hs).dag() * Destroy(hs=hs)
    H1 = LocalSigma('g', 'e', hs=hs) + LocalSigma('e', 'g', hs=hs)
    H = w * H0 + 0.5 * E0 * blackman * H1
    circuit = SLH(identity_matrix(0), [], H)
    num_vals = {w: 1.0, T: 10.0, E0: 1.0 * 2 * np.pi}

    # test qutip conversion
    num_circuit = circuit.substitute(num_vals)
    H_qutip, Ls = SLH_to_qutip(num_circuit, time_symbol=t)
    assert len(Ls) == 0
    assert len(H_qutip) == 3
    times = np.linspace(0, num_vals[T], 201)
    psi0 = qutip.basis(2, 1)
    states = qutip.mesolve(H_qutip, psi0, times, [], []).states
    pop0 = np.array(qutip_population(states, state=0))
    pop1 = np.array(qutip_population(states, state=1))
    datfile = os.path.join(datadir, 'pops.dat')
    #print("DATFILE: %s" % datfile)
    #np.savetxt(datfile, np.c_[times, pop0, pop1, pop0+pop1])
    pop0_expect, pop1_expect = np.genfromtxt(datfile,
                                             unpack=True,
                                             usecols=(1, 2))
    assert np.max(np.abs(pop0 - pop0_expect)) < 1e-12
    assert np.max(np.abs(pop1 - pop1_expect)) < 1e-12

    # Test QSD conversion
    codegen = QSDCodeGen(circuit, num_vals=num_vals, time_symbol=t)
    codegen.add_observable(LocalSigma('e', 'e', hs=hs), name='P_e')
    psi0 = BasisKet('e', hs=hs)
    codegen.set_trajectories(psi_initial=psi0,
                             stepper='AdaptiveStep',
                             dt=0.01,
                             nt_plot_step=5,
                             n_plot_steps=200,
                             n_trajectories=1)
    scode = codegen.generate_code()
    compile_cmd = _cmd_list_to_str(
        codegen._build_compile_cmd(qsd_lib='$HOME/local/lib/libqsd.a',
                                   qsd_headers='$HOME/local/include/qsd/',
                                   executable='test_driven_tls',
                                   path='$HOME/bin',
                                   compiler='mpiCC',
                                   compile_options='-g -O0'))
    print(compile_cmd)
    codefile = os.path.join(datadir, "test_driven_tls.cc")
    #print("CODEFILE: %s" % codefile)
    #with(open(codefile, 'w')) as out_fh:
    #out_fh.write(scode)
    #out_fh.write("\n")
    with open(codefile) as in_fh:
        scode_expected = in_fh.read()
    assert scode.strip() == scode_expected.strip()
Beispiel #4
0
def Sec6_codegen(slh_Sec6, slh_Sec6_vals):
    codegen = QSDCodeGen(circuit=slh_Sec6, num_vals=slh_Sec6_vals)
    A2 = Destroy(hs=hs1)
    Sp = LocalSigma(1, 0, hs=hs2)
    Sm = Sp.dag()
    codegen.add_observable(Sp * A2 * Sm * Sp, name="X1")
    codegen.add_observable(Sm * Sp * A2 * Sm, name="X2")
    codegen.add_observable(A2, name="A2")
    psi0 = BasisKet(0, hs=hs0)
    psi1 = BasisKet(0, hs=hs1)
    psi2 = BasisKet(0, hs=hs2)
    codegen.set_trajectories(psi_initial=psi0 * psi1 * psi2,
                             stepper='AdaptiveStep',
                             dt=0.01,
                             nt_plot_step=100,
                             n_plot_steps=5,
                             n_trajectories=1,
                             traj_save=10)
    return codegen
Beispiel #5
0
def test_labeled_basis_op():
    """Check that in QSD code generation labeled basis states are translated
    into numbered basis states"""
    hs = LocalSpace('tls', basis=('g', 'e'))
    a = Destroy(hs=hs)
    ad = a.dag()
    s = LocalSigma('g', 'e', hs=hs)
    circuit = SLH(identity_matrix(0), [], a * ad)
    codegen = QSDCodeGen(circuit)
    codegen._update_qsd_ops([
        s,
    ])
    assert codegen._qsd_ops[s].instantiator == '(0,1,0)' != '(g,e,0)'
Beispiel #6
0
def test_qsd_codegen_traj(slh_Sec6):
    A2 = Destroy(hs=hs1)
    Sp = LocalSigma(1, 0, hs=hs2)
    Sm = Sp.dag()
    codegen = QSDCodeGen(circuit=slh_Sec6)
    codegen.add_observable(Sp * A2 * Sm * Sp, name="X1")
    codegen.add_observable(Sm * Sp * A2 * Sm, name="X2")
    codegen.add_observable(A2, name="A2")

    with pytest.raises(QSDCodeGenError) as excinfo:
        scode = codegen._trajectory_lines(indent=0)
    assert "No trajectories set up" in str(excinfo.value)

    codegen.set_trajectories(psi_initial=None,
                             stepper='AdaptiveStep',
                             dt=0.01,
                             nt_plot_step=100,
                             n_plot_steps=5,
                             n_trajectories=1,
                             traj_save=10)
    scode = codegen._trajectory_lines(indent=0)
    assert dedent(scode).strip() == dedent(r'''
    ACG gen(rndSeed); // random number generator
    ComplexNormal rndm(&gen); // Complex Gaussian random numbers

    double dt = 0.01;
    int dtsperStep = 100;
    int nOfSteps = 5;
    int nTrajSave = 10;
    int nTrajectory = 1;
    int ReadFile = 0;

    AdaptiveStep stepper(psiIni, H, nL, L);
    Trajectory traj(psiIni, dt, stepper, &rndm);

    traj.sumExp(nOfOut, outlist, flist , dtsperStep, nOfSteps,
                nTrajectory, nTrajSave, ReadFile);
    ''').strip()

    with pytest.raises(ValueError) as excinfo:
        codegen.set_moving_basis(move_dofs=0,
                                 delta=0.01,
                                 width=2,
                                 move_eps=0.01)
    assert "move_dofs must be an integer >0" in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        codegen.set_moving_basis(move_dofs=4,
                                 delta=0.01,
                                 width=2,
                                 move_eps=0.01)
    assert "move_dofs must not be larger" in str(excinfo.value)
    with pytest.raises(QSDCodeGenError) as excinfo:
        codegen.set_moving_basis(move_dofs=3,
                                 delta=0.01,
                                 width=2,
                                 move_eps=0.01)
    assert "A moving basis cannot be used" in str(excinfo.value)
    codegen.set_moving_basis(move_dofs=2, delta=0.01, width=2, move_eps=0.01)
    scode = codegen._trajectory_lines(indent=0)
    assert dedent(scode).strip() == dedent(r'''
    ACG gen(rndSeed); // random number generator
    ComplexNormal rndm(&gen); // Complex Gaussian random numbers

    double dt = 0.01;
    int dtsperStep = 100;
    int nOfSteps = 5;
    int nTrajSave = 10;
    int nTrajectory = 1;
    int ReadFile = 0;

    AdaptiveStep stepper(psiIni, H, nL, L);
    Trajectory traj(psiIni, dt, stepper, &rndm);

    int move = 2;
    double delta = 0.01;
    int width = 2;
    double moveEps = 0.01;

    traj.sumExp(nOfOut, outlist, flist , dtsperStep, nOfSteps,
                nTrajectory, nTrajSave, ReadFile, move,
                delta, width, moveEps);
    ''').strip()
Beispiel #7
0
def test_qsd_codegen_initial_state(slh_Sec6):

    A2 = Destroy(hs=hs1)
    Sp = LocalSigma(1, 0, hs=hs2)
    Sm = Sp.dag()
    psi_cav1 = lambda n: BasisKet(n, hs=hs0)
    psi_cav2 = lambda n: BasisKet(n, hs=hs1)
    psi_spin = lambda n: BasisKet(n, hs=hs2)
    psi_tot = lambda n, m, l: psi_cav1(n) * psi_cav2(m) * psi_spin(l)

    codegen = QSDCodeGen(circuit=slh_Sec6)
    codegen.add_observable(Sp * A2 * Sm * Sp, "X1.out")
    codegen.add_observable(Sm * Sp * A2 * Sm, "X2.out")
    codegen.add_observable(A2, "A2.out")

    psi = (((psi_cav1(0) + psi_cav1(1)) / sympy.sqrt(2)) *
           ((psi_cav2(0) + psi_cav2(1)) / sympy.sqrt(2)) * psi_spin(0))
    codegen.set_trajectories(psi_initial=psi,
                             stepper='AdaptiveStep',
                             dt=0.01,
                             nt_plot_step=100,
                             n_plot_steps=5,
                             n_trajectories=1,
                             traj_save=10)

    scode = codegen._initial_state_lines(indent=0)
    assert scode == dedent(r'''
    State phiL0(50,0,FIELD); // HS 0
    State phiL1(50,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiL3(50,1,FIELD); // HS 0
    State phiL4(50,1,FIELD); // HS 1
    State phiT0List[3] = {(phiL0 + phiL3), (phiL1 + phiL4), phiL2};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2

    State psiIni = (1.0L/2.0L) * (phiT0);
    psiIni.normalize();
    ''').strip()

    alpha = symbols('alpha')
    psi = CoherentStateKet(alpha, hs=hs0) * psi_cav2(0) * psi_spin(0)
    codegen.set_trajectories(psi_initial=psi,
                             stepper='AdaptiveStep',
                             dt=0.01,
                             nt_plot_step=100,
                             n_plot_steps=5,
                             n_trajectories=1,
                             traj_save=10)
    scode = codegen._initial_state_lines(indent=0)
    assert scode == dedent(r'''
    State phiL0(50,0,FIELD); // HS 1
    State phiL1(2,0,FIELD); // HS 2
    State phiL2(50,alpha,FIELD); // HS 0
    State phiT0List[3] = {phiL2, phiL0, phiL1};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2

    State psiIni = phiT0;
    psiIni.normalize();
    ''').strip()

    psi = (psi_tot(1, 0, 0) + psi_tot(0, 1, 0)) / sympy.sqrt(2)
    codegen.set_trajectories(psi_initial=psi,
                             stepper='AdaptiveStep',
                             dt=0.01,
                             nt_plot_step=100,
                             n_plot_steps=5,
                             n_trajectories=1,
                             traj_save=10)
    scode = codegen._initial_state_lines(indent=0)
    assert scode == dedent(r'''
    State phiL0(50,0,FIELD); // HS 0
    State phiL1(50,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiL3(50,1,FIELD); // HS 0
    State phiL4(50,1,FIELD); // HS 1
    State phiT0List[3] = {phiL0, phiL4, phiL2};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    State phiT1List[3] = {phiL3, phiL1, phiL2};
    State phiT1(3, phiT1List); // HS 0 * HS 1 * HS 2

    State psiIni = ((1.0L/2.0L)*sqrt(2)) * ((phiT0 + phiT1));
    psiIni.normalize();
    ''').strip()
Beispiel #8
0
def test_qsd_codegen_observables(caplog, slh_Sec6, slh_Sec6_vals):
    A2 = Destroy(hs=hs1)
    Sp = LocalSigma(1, 0, hs=hs2)
    Sm = Sp.dag()
    codegen = QSDCodeGen(circuit=slh_Sec6, num_vals=slh_Sec6_vals)

    with pytest.raises(QSDCodeGenError) as excinfo:
        scode = codegen._observables_lines(indent=0)
    assert "Must register at least one observable" in str(excinfo.value)

    name = 'a_1 sigma_10^[2]'
    codegen.add_observable(Sp * A2 * Sm * Sp, name=name)
    filename = codegen._observables[name][1]
    assert filename == 'a_1_sigma_10_2.out'
    codegen.add_observable(Sp * A2 * Sm * Sp, name=name)
    assert 'Overwriting existing operator' in caplog.text

    with pytest.raises(ValueError) as exc_info:
        codegen.add_observable(Sp * A2 * A2 * Sm * Sp, name="xxxx" * 20)
    assert "longer than limit" in str(exc_info.value)
    name = 'A2^2'
    codegen.add_observable(Sp * A2 * A2 * Sm * Sp, name=name)
    assert name in codegen._observables
    filename = codegen._observables[name][1]
    assert filename == 'A2_2.out'

    with pytest.raises(ValueError) as exc_info:
        codegen.add_observable(A2, name='A2_2')
    assert "Cannot generate unique filename" in str(exc_info.value)

    with pytest.raises(ValueError) as exc_info:
        codegen.add_observable(A2, name="A2\t2")
    assert "invalid characters" in str(exc_info.value)

    with pytest.raises(ValueError) as exc_info:
        codegen.add_observable(A2, name="A" * 100)
    assert "longer than limit" in str(exc_info.value)

    with pytest.raises(ValueError) as exc_info:
        codegen.add_observable(A2, name="()")
    assert "Cannot generate filename" in str(exc_info.value)

    codegen = QSDCodeGen(circuit=slh_Sec6, num_vals=slh_Sec6_vals)
    codegen.add_observable(Sp * A2 * Sm * Sp, name="X1")
    codegen.add_observable(Sm * Sp * A2 * Sm, name="X2")
    assert codegen._observables["X2"] == (Sm * Sp * A2 * Sm, 'X2.out')
    codegen.add_observable(A2, name="A2")
    assert codegen._observables["A2"] == (A2, 'A2.out')
    scode = codegen._observables_lines(indent=0)
    assert dedent(scode).strip() == dedent(r'''
    const int nOfOut = 3;
    Operator outlist[nOfOut] = {
      (A1 * S2_1_0),
      (A1 * S2_0_1),
      A1
    };
    char *flist[nOfOut] = {"X1.out", "X2.out", "A2.out"};
    int pipe[4] = {1,2,3,4};
    ''').strip()
    # Note how the observables have been simplified
    assert Sp * A2 * Sm * Sp == Sp * A2
    assert codegen._operator_str(Sp * A2) == '(A1 * S2_1_0)'
    assert Sm * Sp * A2 * Sm == Sm * A2
    assert codegen._operator_str(Sm * A2) == '(A1 * S2_0_1)'
    # If the oberservables introduce new operators or symbols, these should
    # extend the existing ones
    P1 = LocalSigma(1, 1, hs=hs2)
    zeta = symbols("zeta", real=True)
    codegen.add_observable(zeta * P1, name="P1")
    assert P1 in codegen._local_ops
    assert str(codegen._qsd_ops[P1]) == 'S2_1_1'
    assert zeta in codegen.syms
    codegen.num_vals.update({zeta: 1.0})
    assert 'zeta' in codegen._parameters_lines(indent=0)
    assert str(codegen._qsd_ops[P1]) in codegen._operator_basis_lines(indent=0)
    assert Sp * A2 in set(codegen.observables)
    assert Sm * A2 in set(codegen.observables)
    assert zeta * P1 in set(codegen.observables)
    assert list(codegen.observable_names) == ['X1', 'X2', 'A2', 'P1']
    assert codegen.get_observable('X1') == Sp * A2 * Sm * Sp