Exemple #1
0
def test_find_kets():
    #                  hs n
    psi_A_0 = BasisKet(0, 0)
    psi_A_1 = BasisKet(0, 1)
    psi_B_0 = BasisKet(1, 0)
    psi_B_1 = BasisKet(1, 1)
    local_psi = [psi_A_0, psi_A_1, psi_B_0, psi_B_1]

    psi_00 = psi_A_0 * psi_B_0
    psi_01 = psi_A_0 * psi_B_1
    psi_10 = psi_A_1 * psi_B_0
    psi_11 = psi_A_1 * psi_B_1
    tensor_psi = [psi_00, psi_01, psi_10, psi_11]
    a1 = Destroy(psi_A_1.space)

    assert set((psi_A_0, )) == find_kets(psi_A_0, LocalKet)

    psi = 0.5 * (psi_00 + psi_01 + psi_10 + psi_11)
    assert set(local_psi) == find_kets(psi, LocalKet)
    assert set(tensor_psi) == find_kets(psi, TensorKet)

    psi = 0.5 * a1 * psi_10  # = 0.5 * psi_00
    assert set([psi_A_0, psi_B_0]) == find_kets(psi, LocalKet)
    assert set([
        psi_00,
    ]) == find_kets(psi, TensorKet)

    with pytest.raises(TypeError):
        find_kets({}, cls=LocalKet)
Exemple #2
0
def test_find_kets():
    #                  hs n
    psi_A_0 = BasisKet(0, 0)
    psi_A_1 = BasisKet(0, 1)
    psi_B_0 = BasisKet(1, 0)
    psi_B_1 = BasisKet(1, 1)
    local_psi = [psi_A_0, psi_A_1, psi_B_0, psi_B_1]

    psi_00 = psi_A_0 * psi_B_0
    psi_01 = psi_A_0 * psi_B_1
    psi_10 = psi_A_1 * psi_B_0
    psi_11 = psi_A_1 * psi_B_1
    tensor_psi = [psi_00, psi_01, psi_10, psi_11]
    a1 = Destroy(psi_A_1.space)

    assert set((psi_A_0,)) == find_kets(psi_A_0, LocalKet)

    psi = 0.5 * (psi_00 + psi_01 + psi_10 + psi_11)
    assert set(local_psi) == find_kets(psi, LocalKet)
    assert set(tensor_psi) == find_kets(psi, TensorKet)

    psi = 0.5 * a1 * psi_10  # = 0.5 * psi_00
    assert set([psi_A_0, psi_B_0]) == find_kets(psi, LocalKet)
    assert set([psi_00]) == find_kets(psi, TensorKet)

    with pytest.raises(TypeError):
        find_kets({}, cls=LocalKet)
Exemple #3
0
def test_define_atomic_kets(slh_Sec6):
    codegen = QSDCodeGen(circuit=slh_Sec6)
    psi_cav1 = lambda n: BasisKet(0, n)
    psi_cav2 = lambda n: BasisKet(1, n)
    psi_spin = lambda n: BasisKet(2, n)
    psi_tot = lambda n, m, l: psi_cav1(n) * psi_cav2(m) * psi_spin(l)

    with pytest.raises(QSDCodeGenError) as excinfo:
        lines = codegen._define_atomic_kets(psi_cav1(0))
    assert "not in the Hilbert space of the Hamiltonian" in str(excinfo.value)
    BasisRegistry.registry = {}  # reset
    with pytest.raises(QSDCodeGenError) as excinfo:
        lines = codegen._define_atomic_kets(psi_tot(0, 0, 0))
    assert "Unknown dimension for Hilbert space" in str(excinfo.value)

    psi_cav1(0).space.dimension = 10
    psi_cav2(0).space.dimension = 10
    psi_spin(0).space.dimension = 2

    lines = codegen._define_atomic_kets(psi_tot(0, 0, 0))
    scode = "\n".join(lines)
    assert scode == dedent(r'''
    State phiL0(10,0,FIELD); // HS 0
    State phiL1(10,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiT0List[3] = {phiL0, phiL1, phiL2};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    ''').strip()

    psi = (((psi_cav1(0) + psi_cav1(1)) / sympy.sqrt(2)) *
           ((psi_cav2(0) + psi_cav2(1)) / sympy.sqrt(2)) * psi_spin(0))
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert scode == dedent(r'''
    State phiL0(10,0,FIELD); // HS 0
    State phiL1(10,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiL3(10,1,FIELD); // HS 0
    State phiL4(10,1,FIELD); // HS 1
    State phiT0List[3] = {(phiL0 + phiL3), (phiL1 + phiL4), phiL2};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    ''').strip()
    for phi in (list(find_kets(psi, cls=LocalKet)) +
                list(find_kets(psi, cls=TensorKet))):
        assert phi in codegen._qsd_states

    alpha = symbols('alpha')
    psi = CoherentStateKet(0, alpha) * psi_cav2(0) * psi_spin(0)
    with pytest.raises(TypeError) as excinfo:
        lines = codegen._define_atomic_kets(psi)
    assert "neither a known symbol nor a complex number" in str(excinfo.value)
    codegen.syms.add(alpha)
    codegen._update_var_names()
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert scode == dedent(r'''
    State phiL0(10,0,FIELD); // HS 1
    State phiL1(2,0,FIELD); // HS 2
    State phiL2(10,alpha,FIELD); // HS 0
    State phiT0List[3] = {phiL2, phiL0, phiL1};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    ''').strip()
    for phi in (list(find_kets(psi, cls=LocalKet)) +
                list(find_kets(psi, cls=TensorKet))):
        assert phi in codegen._qsd_states

    psi = CoherentStateKet(0, 1j) * psi_cav2(0) * psi_spin(0)
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert scode == dedent(r'''
    State phiL0(10,0,FIELD); // HS 1
    State phiL1(2,0,FIELD); // HS 2
    Complex phiL2_alpha(0,1);
    State phiL2(10,phiL2_alpha,FIELD); // HS 0
    State phiT0List[3] = {phiL2, phiL0, phiL1};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    ''').strip()
    for phi in (list(find_kets(psi, cls=LocalKet)) +
                list(find_kets(psi, cls=TensorKet))):
        assert phi in codegen._qsd_states

    psi = psi_tot(1, 0, 0) + psi_tot(0, 1, 0) + psi_tot(0, 0, 1)
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert scode == dedent(r'''
    State phiL0(10,0,FIELD); // HS 0
    State phiL1(10,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiL3(10,1,FIELD); // HS 0
    State phiL4(10,1,FIELD); // HS 1
    State phiL5(2,1,FIELD); // HS 2
    State phiT0List[3] = {phiL0, phiL1, phiL5};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    State phiT1List[3] = {phiL0, phiL4, phiL2};
    State phiT1(3, phiT1List); // HS 0 * HS 1 * HS 2
    State phiT2List[3] = {phiL3, phiL1, phiL2};
    State phiT2(3, phiT2List); // HS 0 * HS 1 * HS 2
    ''').strip()
    for phi in (list(find_kets(psi, cls=LocalKet)) +
                list(find_kets(psi, cls=TensorKet))):
        assert phi in codegen._qsd_states
Exemple #4
0
def test_define_atomic_kets(slh_Sec6):
    codegen = QSDCodeGen(circuit=slh_Sec6)
    psi_cav1 = lambda n: BasisKet(0, n)
    psi_cav2 = lambda n: BasisKet(1, n)
    psi_spin = lambda n: BasisKet(2, n)
    psi_tot = lambda n, m, l: psi_cav1(n) * psi_cav2(m) * psi_spin(l)

    with pytest.raises(QSDCodeGenError) as excinfo:
        lines = codegen._define_atomic_kets(psi_cav1(0))
    assert "not in the Hilbert space of the Hamiltonian" in str(excinfo.value)
    BasisRegistry.registry = {}  # reset
    with pytest.raises(QSDCodeGenError) as excinfo:
        lines = codegen._define_atomic_kets(psi_tot(0, 0, 0))
    assert "Unknown dimension for Hilbert space" in str(excinfo.value)

    psi_cav1(0).space.dimension = 10
    psi_cav2(0).space.dimension = 10
    psi_spin(0).space.dimension = 2

    lines = codegen._define_atomic_kets(psi_tot(0, 0, 0))
    scode = "\n".join(lines)
    assert (
        scode
        == dedent(
            r"""
    State phiL0(10,0,FIELD); // HS 0
    State phiL1(10,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiT0List[3] = {phiL0, phiL1, phiL2};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    """
        ).strip()
    )

    psi = ((psi_cav1(0) + psi_cav1(1)) / sympy.sqrt(2)) * ((psi_cav2(0) + psi_cav2(1)) / sympy.sqrt(2)) * psi_spin(0)
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert (
        scode
        == dedent(
            r"""
    State phiL0(10,0,FIELD); // HS 0
    State phiL1(10,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiL3(10,1,FIELD); // HS 0
    State phiL4(10,1,FIELD); // HS 1
    State phiT0List[3] = {(phiL0 + phiL3), (phiL1 + phiL4), phiL2};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    """
        ).strip()
    )
    for phi in list(find_kets(psi, cls=LocalKet)) + list(find_kets(psi, cls=TensorKet)):
        assert phi in codegen._qsd_states

    alpha = symbols("alpha")
    psi = CoherentStateKet(0, alpha) * psi_cav2(0) * psi_spin(0)
    with pytest.raises(TypeError) as excinfo:
        lines = codegen._define_atomic_kets(psi)
    assert "neither a known symbol nor a complex number" in str(excinfo.value)
    codegen.syms.add(alpha)
    codegen._update_var_names()
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert (
        scode
        == dedent(
            r"""
    State phiL0(10,0,FIELD); // HS 1
    State phiL1(2,0,FIELD); // HS 2
    State phiL2(10,alpha,FIELD); // HS 0
    State phiT0List[3] = {phiL2, phiL0, phiL1};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    """
        ).strip()
    )
    for phi in list(find_kets(psi, cls=LocalKet)) + list(find_kets(psi, cls=TensorKet)):
        assert phi in codegen._qsd_states

    psi = CoherentStateKet(0, 1j) * psi_cav2(0) * psi_spin(0)
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert (
        scode
        == dedent(
            r"""
    State phiL0(10,0,FIELD); // HS 1
    State phiL1(2,0,FIELD); // HS 2
    Complex phiL2_alpha(0,1);
    State phiL2(10,phiL2_alpha,FIELD); // HS 0
    State phiT0List[3] = {phiL2, phiL0, phiL1};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    """
        ).strip()
    )
    for phi in list(find_kets(psi, cls=LocalKet)) + list(find_kets(psi, cls=TensorKet)):
        assert phi in codegen._qsd_states

    psi = psi_tot(1, 0, 0) + psi_tot(0, 1, 0) + psi_tot(0, 0, 1)
    lines = codegen._define_atomic_kets(psi)
    scode = "\n".join(lines)
    assert (
        scode
        == dedent(
            r"""
    State phiL0(10,0,FIELD); // HS 0
    State phiL1(10,0,FIELD); // HS 1
    State phiL2(2,0,FIELD); // HS 2
    State phiL3(10,1,FIELD); // HS 0
    State phiL4(10,1,FIELD); // HS 1
    State phiL5(2,1,FIELD); // HS 2
    State phiT0List[3] = {phiL0, phiL1, phiL5};
    State phiT0(3, phiT0List); // HS 0 * HS 1 * HS 2
    State phiT1List[3] = {phiL0, phiL4, phiL2};
    State phiT1(3, phiT1List); // HS 0 * HS 1 * HS 2
    State phiT2List[3] = {phiL3, phiL1, phiL2};
    State phiT2(3, phiT2List); // HS 0 * HS 1 * HS 2
    """
        ).strip()
    )
    for phi in list(find_kets(psi, cls=LocalKet)) + list(find_kets(psi, cls=TensorKet)):
        assert phi in codegen._qsd_states