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)
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)
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() )
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)
def test_local_ops(): psa = PseudoNAND() assert isinstance(psa, Circuit) l_ops = local_ops(psa) a = Destroy(psa.space) assert type(local_ops(a)) is set assert set([IdentityOperator, a, a.dag()]) == l_ops assert local_ops(a) == set([a]) assert local_ops(a * a) == set([a]) assert local_ops(a + a.dag()) == set([a, a.dag()]) assert local_ops(10 * a) == set([a]) with pytest.raises(TypeError): local_ops({})
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))));")
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))));')
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()
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)
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)
def test_find_time_dependent_coeffs(): E0, sigma, t, t0, a, b = sympy.symbols("E_0, sigma, t, t_0, a, b", real=True) op_a = Destroy(0) op_n = op_a.dag() * op_a gaussian = E0 * sympy.exp(-(t - t0) ** 2 / (2 * sigma ** 2)) linear = a * t H = b * op_a.dag() + gaussian * op_n + linear * op_a coeffs = list(_find_time_dependent_coeffs(H, t)) assert len(coeffs) == 2 assert gaussian in coeffs assert linear in coeffs H = (gaussian * op_n) * (linear * op_a) + b coeffs = list(_find_time_dependent_coeffs(H, t)) assert len(coeffs) == 1 assert gaussian * linear in coeffs H = b * op_n coeffs = list(_find_time_dependent_coeffs(H, t)) len(coeffs) == 9
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_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()
def test_find_time_dependent_coeffs(): E0, sigma, t, t0, a, b = sympy.symbols('E_0, sigma, t, t_0, a, b', real=True) op_a = Destroy(0) op_n = op_a.dag() * op_a gaussian = E0 * sympy.exp(-(t - t0)**2 / (2 * sigma**2)) linear = a * t H = b * op_a.dag() + gaussian * op_n + linear * op_a coeffs = list(_find_time_dependent_coeffs(H, t)) assert len(coeffs) == 2 assert gaussian in coeffs assert linear in coeffs H = (gaussian * op_n) * (linear * op_a) + b coeffs = list(_find_time_dependent_coeffs(H, t)) assert len(coeffs) == 1 assert gaussian * linear in coeffs H = b * op_n coeffs = list(_find_time_dependent_coeffs(H, t)) len(coeffs) == 9
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)
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_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() )