def test_expect_sigmaxyz(op, state):
    """Tests the `expect` function on Pauli-X, Pauli-Y and Pauli-Z."""
    # The stacked pytest decorators check all the argument combinations like a Cartesian product
    if jnp.all(op != sigmaz()):
        assert expect(op, state) == 0.0
    elif jnp.all(state == basis(2, 0)):
        assert expect(op, state) == 1.0
    else:
        assert expect(op, state) == -1.0
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
def cost(params, op):
    """Cost function to optimize
    
    Args:
        params (list): rotation angles for 
            x, y, and z rotations respectively
        op (:obj:`jnp.ndarray`): Operator
        with respect to which the expectation
        value is to be calculated
    
    Returns:
        float: Expectation value of the evloved
        state w.r.t the given operator `op`
        
    """
    state = circuit(params)
    return jnp.real(expect(op, state))
def test_coherent():
    """Tests the coherent state method"""
    assert abs(expect(destroy(10), coherent(10, 0.5)) - 0.5) < 1e-4
def test_expect_herm(oper, state):
    """Tests that the expectation value of a hermitian operator is real and that of 
       the non-hermitian operator is complex"""
    assert jnp.imag(expect(oper, state)) == 0.0
def test_coherent():
    """Tests the coherent state method"""
    assert abs(expect(destroy(10), coherent(10, 0.5)) - 0.5) < 1e-4
    # Tests the border case with alpha = 0
    for N in range(2, 30, 5):
        assert_array_almost_equal(coherent(N, 0), basis(N, 0))
Exemple #7
0
def cost(params, op):
    state = circuit(params)
    return jnp.real(expect(op, state))