Exemplo n.º 1
0
def test_qsd_codegen_operator_basis():
    a = Destroy(1)
    a.space.dimension = 10
    ad = a.dag()
    s = LocalSigma(2, 1, 0)
    s.space.dimension = 2
    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()
Exemplo n.º 2
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(0)
    Ac1 = A1.dag()
    N1 = Ac1 * A1
    Id1 = identity_matrix(0)
    A2 = Destroy(1)
    Ac2 = A2.dag()
    N2 = Ac2 * A2
    Id2 = identity_matrix(1)
    Sp = LocalSigma(2, 1, 0)
    Sm = Sp.dag()
    Id3 = identity_matrix(3)

    BasisRegistry.set_basis(A1.space, range(50))
    BasisRegistry.set_basis(A2.space, range(50))
    BasisRegistry.set_basis(Sp.space, range(2))

    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)
Exemplo n.º 3
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(0)
    Ac1 = A1.dag()
    N1 = Ac1 * A1
    Id1 = identity_matrix(0)
    A2 = Destroy(1)
    Ac2 = A2.dag()
    N2 = Ac2 * A2
    Id2 = identity_matrix(1)
    Sp = LocalSigma(2, 1, 0)
    Sm = Sp.dag()
    Id3 = identity_matrix(3)

    BasisRegistry.set_basis(A1.space, range(50))
    BasisRegistry.set_basis(A2.space, range(50))
    BasisRegistry.set_basis(Sp.space, range(2))

    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)
Exemplo n.º 4
0
def test_operator_hilbert_space_check():
    circuit = SLH(identity_matrix(0), [], 1)
    s = LocalSigma("sys", "g", "e")
    codegen = QSDCodeGen(circuit)
    with pytest.raises(ValueError) as exc_info:
        codegen._update_qsd_ops([s])
    assert "not in the circuit's Hilbert space" in str(exc_info.value)
Exemplo n.º 5
0
def test_latex_symbols(slh_Sec6):
    """Test that if any of the symbols contain "special" characters such as
    backslashes (LaTeX code), we still get valid C++ code (basic ASCII words
    only)."""
    k = symbols("\kappa", positive=True)
    x = symbols(r"\chi^{(1)}_{\text{main}}", real=True)
    c = symbols("c")

    a = Destroy(1)
    H = x * (a * a + a.dag() * a.dag()) + (c * a.dag() + c.conjugate() * a)
    L = sqrt(k) * a
    slh = SLH(identity_matrix(1), [L], H)
    codegen = QSDCodeGen(circuit=slh, num_vals={x: 2.0, c: 1 + 2j, k: 2})

    scode = codegen._parameters_lines(indent=0)
    assert (
        dedent(scode).strip()
        == dedent(
            """
    Complex I(0.0,1.0);
    Complex c(1,2);
    double chi_1_textmain = 2;
    double kappa = 2;"""
        ).strip()
    )
Exemplo n.º 6
0
def test_qsd_codegen_parameters():
    k = symbols(r'kappa', positive=True)
    x = symbols(r'chi', real=True)
    c = symbols("c")

    a = Destroy(1)
    H = x * (a * a + a.dag() * a.dag()) + (c * a.dag() + c.conjugate() * a)
    L = sqrt(k) * a
    slh = SLH(identity_matrix(1), [L], H)
    codegen = QSDCodeGen(circuit=slh, num_vals={x: 2., c: 1 + 2j, k: 2})

    scode = codegen._parameters_lines(indent=0)
    assert dedent(scode).strip() == dedent("""
    Complex I(0.0,1.0);
    Complex c(1,2);
    double chi = 2;
    double kappa = 2;""").strip()

    codegen.num_vals.update({c: 1})
    scode = codegen._parameters_lines(indent=0)
    assert dedent(scode).strip() == dedent("""
    Complex I(0.0,1.0);
    Complex c(1,0);
    double chi = 2;
    double kappa = 2;""").strip()

    del codegen.num_vals[c]
    with pytest.raises(KeyError) as excinfo:
        scode = codegen._parameters_lines(indent=0)
    assert "There is no value for symbol c" in str(excinfo.value)
Exemplo n.º 7
0
def test_driven_tls(datadir):
    hs = local_space('tls', namespace='sys', 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).dag() * Destroy(hs)
    H1 = LocalSigma(hs, 'g', 'e') + LocalSigma(hs, 'e', 'g')
    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
    H_qutip, Ls = circuit.substitute(num_vals).HL_to_qutip(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(hs, 'e', 'e'), name='P_e')
    psi0 = BasisKet(hs, 'e')
    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()
Exemplo n.º 8
0
def test_operator_hilbert_space_check():
    circuit = SLH(identity_matrix(0), [], 1)
    s = LocalSigma("sys", 'g', 'e')
    codegen = QSDCodeGen(circuit)
    with pytest.raises(ValueError) as exc_info:
        codegen._update_qsd_ops([
            s,
        ])
    assert "not in the circuit's Hilbert space" in str(exc_info.value)
Exemplo n.º 9
0
 def testABCD(self):
     a = Destroy(1)
     slh = SLH(identity_matrix(1), [a], 2 * a.dag() * a).coherent_input(3)
     A, B, C, D, a, c = getABCD(slh, doubled_up=True)
     self.assertEquals(A[0, 0], -sympyOne / 2 - 2 * I)
     self.assertEquals(A[1, 1], -sympyOne / 2 + 2 * I)
     self.assertEquals(B[0, 0], -1)
     self.assertEquals(C[0, 0], 1)
     self.assertEquals(D[0, 0], 1)
Exemplo n.º 10
0
 def testABCD(self):
     a = Destroy(1)
     slh = SLH(identity_matrix(1), [a],
               2*a.dag() * a).coherent_input(3)
     A, B, C, D, a, c = getABCD(slh, doubled_up=True)
     self.assertEquals(A[0, 0], -sympyOne / 2 - 2 * I)
     self.assertEquals(A[1, 1], -sympyOne / 2 + 2 * I)
     self.assertEquals(B[0, 0],  -1)
     self.assertEquals(C[0, 0], 1)
     self.assertEquals(D[0, 0], 1)
Exemplo n.º 11
0
def test_labeled_basis_op():
    """Check that in QSD code generation labeled basis states are translated
    into numbered basis states"""
    hs = local_space("tls", namespace="sys", basis=("g", "e"))
    a = Destroy(hs)
    ad = a.dag()
    s = LocalSigma(hs, "g", "e")
    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)"
Exemplo n.º 12
0
def test_driven_tls(datadir):
    hs = local_space('tls', namespace='sys', 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).dag() * Destroy(hs)
    H1 = LocalSigma(hs, 'g', 'e') + LocalSigma(hs, 'e', 'g')
    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
    H_qutip, Ls = circuit.substitute(num_vals).HL_to_qutip(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(hs, 'e', 'e'), name='P_e')
    psi0 = BasisKet(hs, 'e')
    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()
Exemplo n.º 13
0
def test_labeled_basis_op():
    """Check that in QSD code generation labeled basis states are translated
    into numbered basis states"""
    hs = local_space('tls', namespace='sys', basis=('g', 'e'))
    a = Destroy(hs)
    ad = a.dag()
    s = LocalSigma(hs, 'g', 'e')
    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)'
Exemplo n.º 14
0
def test_qsd_codegen_operator_basis():
    a = Destroy(1)
    a.space.dimension = 10
    ad = a.dag()
    s = LocalSigma(2, 1, 0)
    s.space.dimension = 2
    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()
    )
Exemplo n.º 15
0
def test_qsd_codegen_hamiltonian():
    k = symbols(r"\kappa", positive=True)
    x = symbols(r"\chi", real=True)
    c = symbols("c", real=True)

    a = Destroy(1)
    H = x * (a * a + a.dag() * a.dag()) + (c * a.dag() + c.conjugate() * a)
    L = sqrt(k) * a
    slh = SLH(identity_matrix(1), [L], H)
    codegen = QSDCodeGen(circuit=slh, num_vals={x: 2.0, c: 1, k: 2})

    codegen._operator_basis_lines(indent=0)
    scode = codegen._hamiltonian_lines(indent=0)
    assert scode.strip() == (r"Operator H = ((c) * (Ad0) + (c) * (A0) + (chi) " r"* (((Ad0 * Ad0) + (A0 * A0))));")
Exemplo n.º 16
0
    def _toSLH(self):

        a = Destroy(self.space)
        a_d = a.adjoint()
        S = identity_matrix(1)

        if self.sub_index == 0:
            # Include the Hamiltonian only with the first port of the kerr cavity circuit object
            H = self.Delta * (a_d * a) + self.chi * (a_d * a_d * a * a)
            L = Matrix([[sqrt(self.kappa_1) * a]])
        else:
            H = 0
            L = Matrix([[sqrt(self.kappa_2) * a]])

        return SLH(S, L, H)
Exemplo n.º 17
0
    def _toSLH(self):

        a = Destroy(self.space)
        a_d = a.adjoint()
        S = identity_matrix(1)
        
        if self.sub_index == 0: 
            # Include the Hamiltonian only with the first port of the kerr cavity circuit object
            H = self.Delta * (a_d * a) + self.chi * (a_d * a_d * a * a)
            L = Matrix([[sqrt(self.kappa_1) * a]])
        else:
            H = 0
            L = Matrix([[sqrt(self.kappa_2) * a]])
        
        return SLH(S, L, H)
Exemplo n.º 18
0
def test_qsd_codegen_hamiltonian():
    k = symbols(r'\kappa', positive=True)
    x = symbols(r'\chi', real=True)
    c = symbols("c", real=True)

    a = Destroy(1)
    H = x * (a * a + a.dag() * a.dag()) + (c * a.dag() + c.conjugate() * a)
    L = sqrt(k) * a
    slh = SLH(identity_matrix(1), [L], H)
    codegen = QSDCodeGen(circuit=slh, num_vals={x: 2., c: 1, k: 2})

    codegen._operator_basis_lines(indent=0)
    scode = codegen._hamiltonian_lines(indent=0)
    assert scode.strip() == (r'Operator H = ((c) * (Ad0) + (c) * (A0) + (chi) '
                             r'* (((Ad0 * Ad0) + (A0 * A0))));')
Exemplo n.º 19
0
def test_latex_symbols(slh_Sec6):
    """Test that if any of the symbols contain "special" characters such as
    backslashes (LaTeX code), we still get valid C++ code (basic ASCII words
    only)."""
    k = symbols("\kappa", positive=True)
    x = symbols(r'\chi^{(1)}_{\text{main}}', real=True)
    c = symbols("c")

    a = Destroy(1)
    H = x * (a * a + a.dag() * a.dag()) + (c * a.dag() + c.conjugate() * a)
    L = sqrt(k) * a
    slh = SLH(identity_matrix(1), [L], H)
    codegen = QSDCodeGen(circuit=slh, num_vals={x: 2., c: 1 + 2j, k: 2})

    scode = codegen._parameters_lines(indent=0)
    assert dedent(scode).strip() == dedent("""
    Complex I(0.0,1.0);
    Complex c(1,2);
    double chi_1_textmain = 2;
    double kappa = 2;""").strip()
Exemplo n.º 20
0
def test_qsd_codegen_parameters():
    k = symbols(r"kappa", positive=True)
    x = symbols(r"chi", real=True)
    c = symbols("c")

    a = Destroy(1)
    H = x * (a * a + a.dag() * a.dag()) + (c * a.dag() + c.conjugate() * a)
    L = sqrt(k) * a
    slh = SLH(identity_matrix(1), [L], H)
    codegen = QSDCodeGen(circuit=slh, num_vals={x: 2.0, c: 1 + 2j, k: 2})

    scode = codegen._parameters_lines(indent=0)
    assert (
        dedent(scode).strip()
        == dedent(
            """
    Complex I(0.0,1.0);
    Complex c(1,2);
    double chi = 2;
    double kappa = 2;"""
        ).strip()
    )

    codegen.num_vals.update({c: 1})
    scode = codegen._parameters_lines(indent=0)
    assert (
        dedent(scode).strip()
        == dedent(
            """
    Complex I(0.0,1.0);
    Complex c(1,0);
    double chi = 2;
    double kappa = 2;"""
        ).strip()
    )

    del codegen.num_vals[c]
    with pytest.raises(KeyError) as excinfo:
        scode = codegen._parameters_lines(indent=0)
    assert "There is no value for symbol c" in str(excinfo.value)
Exemplo n.º 21
0
 def testDrawSLH(self):
     self.assertCanBeDrawn(
         SLH(identity_matrix(1), Matrix([[Create(1)]]),
             Create(1) * Destroy(1)))
Exemplo n.º 22
0
 def testDrawSLH(self):
     self.assertCanBeDrawn(SLH(identity_matrix(1), Matrix([[Create(1)]]), Create(1) * Destroy(1)))