def test_dag_ket():
    r"""Tests the dagger operation :math:`A^{\dagger}` on operator :math:`A`"""
    # test with all real entries
    assert_array_equal(dag(basis(2, 0)), [[1.0, 0.0]])
    assert_array_equal(dag(basis(2, 1)), [[0.0, 1.0]])
    # test with all complex entries
    ket1 = jnp.array(
        [
            [0.04896761 + 0.18014458j],
            [0.6698803 + 0.13728367j],
            [-0.07598839 + 0.38113445j],
            [-0.00505985 + 0.10700243j],
            [-0.18735261 + 0.5476768j],
        ],
        dtype=jnp.complex64,
    )
    ket1_dag = jnp.array(
        [
            [
                0.04896761 - 0.18014458j,
                0.6698803 - 0.13728367j,
                -0.07598839 - 0.38113445j,
                -0.00505985 - 0.10700243j,
                -0.18735261 - 0.5476768j,
            ]
        ],
        dtype=jnp.complex64,
    )
    assert_array_equal(dag(ket1), ket1_dag)
def test_to_dm():
    """Tests the `to_dm` method that converts kets and bras to density matrices"""
    dm0 = jnp.array(
        [[1.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 0.0 + 0.0j]], dtype=jnp.complex64
    )
    dm1 = jnp.array(
        [[0.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 1.0 + 0.0j]], dtype=jnp.complex64
    )
    # testing kets
    assert_array_equal(to_dm(basis(2, 0)), dm0)
    assert_array_equal(to_dm(basis(2, 1)), dm1)
    # testing bras
    assert_array_equal(to_dm(dag(basis(2, 0))), dm0)
    assert_array_equal(to_dm(dag(basis(2, 1))), dm1)
def test_expect_dag(oper, state):
    r"""Reconciles the expectation value of a random operator with the analytic calculation
       
      .. math:: <A> = <\psi|A|\psi>
    """
    expected = jnp.dot(jnp.dot(dag(state), oper), state)
    assert abs(expect(oper, state) - expected) < 1e-6
Exemple #4
0
def show_state(state):
    """Shows the Hinton plot and Wigner function for the state"""
    fig, ax = plt.subplots(1, 2, figsize=(11, 5))
    if state.shape[1] == 1:  # State is a ket
        dm = Qobj(onp.array(jnp.dot(state, dag(state))))
        hinton(dm, ax=ax[0])
        plot_wigner(dm, ax=ax[1])
    plt.show()
def test_isbra():
    """Tests the `isbra` method to see whether a state is a bra based on its shape"""
    for i in range(2, 6):
        assert isbra(rand_ket(i).full()) == False  # tests kets

    for j in range(2, 6):
        assert isbra(dag(rand_ket(j).full())) == True  # tests bras

    for k in range(2, 6):
        assert isbra(rand_dm(k).full()) == False  # tests density matrices
def test_destroy():
    """Tests the annihilation/destroy/lowering operator"""
    # Destruction operator annihilates the bosonic number state
    b9 = basis(10, 9)  # Fock/number state with 1 at 9th index
    d10 = destroy(10)  # 10-dimensional destroy operator
    lowered = jnp.dot(d10, b9)
    assert_array_almost_equal(lowered, 3.0 * basis(10, 8))

    d3 = destroy(3)
    matrix3 = jnp.asarray(
        [
            [0.00000000 + 0.0j, 1.00000000 + 0.0j, 0.00000000 + 0.0j],
            [0.00000000 + 0.0j, 0.00000000 + 0.0j, 1.41421356 + 0.0j],
            [0.00000000 + 0.0j, 0.00000000 + 0.0j, 0.00000000 + 0.0j],
        ]
    )
    assert_equal(np.allclose(matrix3, d3), True)

    assert_equal(np.allclose(dag(destroy(3)), create(3)), True)
def test_rand_unitary():
    for N in range(2, 43, 10):
        unitary = rand_unitary(N)
        assert_array_almost_equal(jnp.dot(unitary, dag(unitary)), jnp.eye(N))
        assert_array_almost_equal(jnp.dot(dag(unitary), unitary), jnp.eye(N))
 def test_unitary(self):
     for N in range(2, 30, 6):
         for thetas, phis, omegas in TestUnitary.generate_params(N):
             unitary = Unitary(N)(thetas, phis, omegas)
             assert_array_almost_equal(jnp.dot(unitary, dag(unitary)), jnp.eye(N))
             assert_array_almost_equal(jnp.dot(dag(unitary), unitary), jnp.eye(N))
def test_make_rot(N, params, idx):
    """Tests the `_make_rot` method"""
    rotation = _make_rot(N, params, idx)
    assert_array_almost_equal(jnp.dot(rotation, dag(rotation)), jnp.eye(N))
    assert_array_almost_equal(jnp.dot(dag(rotation), rotation), jnp.eye(N))
def test_dag_dot():
    """Tests the dagger operation with dot product"""
    i = np.random.randint(3, 10)
    ket = rand_ket(i).full()
    assert_almost_equal(jnp.dot(dag(ket), ket), 1.0)