Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set e_ops as keyword parameter #70

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tests/test_qutip/test_mcsolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def H_1_coeff(t, omega):

# Test setup for gradient calculation
def setup_system(size=2):
a = qt.tensor(qt.destroy(size), qt.qeye(2)).to('jaxdia')
sm = qt.qeye(size).to('jaxdia') & qt.sigmax().to('jaxdia')
a = qt.tensor(qt.destroy(size), qt.qeye(2)).to('jaxdia')
sm = qt.qeye(size).to('jaxdia') & qt.sigmax().to('jaxdia')

# Define the Hamiltonian
H_0 = 2.0 * jnp.pi * a.dag() * a + 2.0 * jnp.pi * sm.dag() * sm
Expand All @@ -33,29 +33,29 @@ def setup_system(size=2):

# Time list
tlist = jnp.linspace(0.0, 1.0, 101)

return H, state, tlist, c_ops, e_ops

# Function for which we want to compute the gradient
def f(omega, H, state, tlist, c_ops, e_ops):
result = mcsolve(
H, state, tlist, c_ops, e_ops, ntraj=10,
args={"omega": omega},
H, state, tlist, c_ops, e_ops=e_ops, ntraj=10,
args={"omega": omega},
options={"method": "diffrax"}
)

return result.expect[0][-1].real

# Pytest test case for gradient computation
@pytest.mark.parametrize("omega_val", [2.0])
def test_gradient_mcsolve(omega_val):
H, state, tlist, c_ops, e_ops = setup_system(size=10)

# Compute the gradient with respect to omega
grad_func = jax.grad(lambda omega: f(omega, H, state, tlist, c_ops, e_ops))
gradient = grad_func(omega_val)

# Check if the gradient is not None and has the correct shape
assert gradient is not None
assert gradient.shape == ()
assert jnp.isfinite(gradient)
assert gradient.shape == ()
assert jnp.isfinite(gradient)
Loading