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()
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)"
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()
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)'
def _space(self): return local_space(self.name, self.namespace, dimension = self.FOCK_DIM)
def _space(self): return local_space(self.name, self.namespace, dimension=self.FOCK_DIM)
def test_convert_to_sympy_matrix(): import pytest N = 4 Hil = local_space('full', basis=range(N)) Hil_q1 = local_space('Q1', basis=range(2)) Hil_q2 = local_space('Q2', basis=range(2)) expr = qnet.algebra.operator_algebra.IdentityOperator assert convert_to_sympy_matrix(expr, Hil) == sympy.eye(N) expr = qnet.algebra.operator_algebra.ZeroOperator assert convert_to_sympy_matrix(expr, Hil) == 0 expr = qnet.algebra.operator_algebra.Create(Hil) assert convert_to_sympy_matrix(expr, Hil) == sympy.Matrix( [[0, 0, 0, 0], [1, 0, 0, 0], [0, sympy.sqrt(2), 0, 0], [0, 0, sympy.sqrt(3), 0]]) expr = qnet.algebra.operator_algebra.Destroy(Hil) assert convert_to_sympy_matrix(expr, Hil) == sympy.Matrix( [[0, 1, 0, 0], [0, 0, sympy.sqrt(2), 0], [0, 0, 0, sympy.sqrt(3)], [0, 0, 0, 0]]) phi = sympy.symbols('phi', real=True) expr = qnet.algebra.operator_algebra.Phase(Hil, phi) assert convert_to_sympy_matrix(expr, Hil) == sympy.Matrix( [[1, 0, 0, 0], [0, sympy.exp(sympy.I*phi), 0, 0], [0, 0, sympy.exp(2*sympy.I*phi), 0], [0, 0, 0, sympy.exp(3*sympy.I*phi)]]) expr = qnet.algebra.operator_algebra.LocalSigma(Hil, 1, 3) assert convert_to_sympy_matrix(expr, Hil) == sympy.Matrix( [[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]]) expr = ( qnet.algebra.operator_algebra.Create(Hil) + qnet.algebra.operator_algebra.Destroy(Hil) ) assert convert_to_sympy_matrix(expr, Hil) == sympy.Matrix( [[0, 1, 0, 0], [1, 0, sympy.sqrt(2), 0], [0, sympy.sqrt(2), 0, sympy.sqrt(3)], [0, 0, sympy.sqrt(3), 0]]) expr = ( qnet.algebra.operator_algebra.Create(Hil) * qnet.algebra.operator_algebra.Destroy(Hil) ) assert convert_to_sympy_matrix(expr, Hil) == sympy.Matrix( [[ 0, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, 2, 0], [ 0, 0, 0, 3]]) w1, w2 = sympy.symbols('omega_1, omega_2', real=True) expr = ( w1 * ( qnet.algebra.operator_algebra.Create(Hil_q1) * qnet.algebra.operator_algebra.Destroy(Hil_q1) ) + w2 * ( qnet.algebra.operator_algebra.Create(Hil_q2) * qnet.algebra.operator_algebra.Destroy(Hil_q2) ) ) assert convert_to_sympy_matrix(expr, expr.space) == sympy.Matrix( [[ 0, 0, 0, 0], [ 0, w2, 0, 0], [ 0, 0, w1, 0], [ 0, 0, 0, w1+w2]]) J = sympy.symbols('J', real=True) expr = J * ( qnet.algebra.operator_algebra.Create(Hil_q1) * qnet.algebra.operator_algebra.Destroy(Hil_q2) + qnet.algebra.operator_algebra.Destroy(Hil_q1) * qnet.algebra.operator_algebra.Create(Hil_q2) ) assert convert_to_sympy_matrix(expr, expr.space) == sympy.Matrix( [[ 0, 0, 0, 0], [ 0, 0, J, 0], [ 0, J, 0, 0], [ 0, 0, 0, 0]]) expr = qnet.algebra.operator_algebra.Adjoint( qnet.algebra.operator_algebra.Create(Hil)) expr2 = qnet.algebra.operator_algebra.Destroy(Hil) assert (convert_to_sympy_matrix(expr, expr.space) == convert_to_sympy_matrix(expr2, expr2.space))