Skip to content

Add interactive test notebooks for closed systems #43

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

Merged
merged 13 commits into from
Aug 15, 2025
Merged
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: Test with pytest and generate coverage report
run: |
pip install pytest-cov coveralls
pip install jupytext nbconvert ipykernel pytest-cov coveralls
pytest tests --strict-markers --cov=qutip_qoc --cov-report= --color=yes

- name: Upload to Coveralls
Expand Down
102 changes: 102 additions & 0 deletions tests/interactive/CRAB_gate_closed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
jupyter:
jupytext:
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.17.1
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

# CRAB algorithm for a closed system

```python
import matplotlib.pyplot as plt
import numpy as np
from qutip import gates, qeye, sigmax, sigmay, sigmaz
import qutip as qt
from qutip_qoc import Objective, optimize_pulses

def fidelity(gate, target_gate):
"""
Fidelity used for unitary gates in qutip-qtrl and qutip-qoc
"""
return np.abs(gate.overlap(target_gate) / target_gate.norm())
```

## Problem setup

```python
omega = 0.1 # energy splitting
sx, sy, sz = sigmax(), sigmay(), sigmaz()

Hd = 1 / 2 * omega * sz
Hc = [sx, sy, sz]
H = [Hd, Hc[0], Hc[1], Hc[2]]

# objective for optimization
initial_gate = qeye(2)
target_gate = gates.hadamard_transform()

times = np.linspace(0, np.pi / 2, 250)
```

## CRAB algorithm

```python
n_params = 3 # adjust in steps of 3
control_params = {
"ctrl_x": {"guess": [1 for _ in range(n_params)], "bounds": [(-1, 1)] * n_params},
"ctrl_y": {"guess": [1 for _ in range(n_params)], "bounds": [(-1, 1)] * n_params},
"ctrl_z": {"guess": [1 for _ in range(n_params)], "bounds": [(-1, 1)] * n_params},
}

res_crab = optimize_pulses(
objectives = Objective(initial_gate, H, target_gate),
control_parameters = control_params,
tlist = times,
algorithm_kwargs = {
"alg": "CRAB",
"fid_err_targ": 0.001
},
)

print('Infidelity: ', res_crab.infidelity)

plt.plot(times, res_crab.optimized_controls[0], 'b', label='optimized pulse sx')
plt.plot(times, res_crab.optimized_controls[1], 'g', label='optimized pulse sy')
plt.plot(times, res_crab.optimized_controls[2], 'r', label='optimized pulse sz')
plt.title('CRAB pulses')
plt.xlabel('Time')
plt.ylabel('Pulse amplitude')
plt.legend()
plt.show()
```

```python
H_result = [Hd, [Hc[0], res_crab.optimized_controls[0]], [Hc[1], res_crab.optimized_controls[1]], [Hc[2], res_crab.optimized_controls[2]]]
evolution = qt.sesolve(H_result, initial_gate, times)

plt.plot(times, [fidelity(gate, initial_gate) for gate in evolution.states], label="Overlap with initial gate")
plt.plot(times, [fidelity(gate, target_gate) for gate in evolution.states], label="Overlap with target gate")

plt.title('CRAB performance')
plt.xlabel('Time')
plt.legend()
plt.show()
```

## Validation

```python
assert res_crab.infidelity < 0.001
assert fidelity(evolution.states[-1], target_gate) > 1-0.001
```

```python
qt.about()
```
95 changes: 95 additions & 0 deletions tests/interactive/CRAB_state_closed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
jupyter:
jupytext:
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.17.1
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

# CRAB algorithm for 2 level system

```python
import matplotlib.pyplot as plt
import numpy as np
from qutip import basis, Qobj
import qutip as qt
from qutip_qoc import Objective, optimize_pulses
```

## Problem setup

```python
# Energy levels
E1, E2 = 1.0, 2.0

Hd = Qobj(np.diag([E1, E2]))
Hc = Qobj(np.array([
[0, 1],
[1, 0]
]))
H = [Hd, Hc]

initial_state = basis(2, 0) # |1>
target_state = basis(2, 1) # |2>

times = np.linspace(0, 2 * np.pi, 250)
```

## CRAB algorithm

```python
n_params = 6 # adjust in steps of 3
control_params = {
"ctrl_x": {"guess": [1 for _ in range(n_params)], "bounds": [(-1, 1)] * n_params},
}

res_crab = optimize_pulses(
objectives = Objective(initial_state, H, target_state),
control_parameters = control_params,
tlist = times,
algorithm_kwargs = {
"alg": "CRAB",
"fid_err_targ": 0.001
},
)

print('Infidelity: ', res_crab.infidelity)

plt.plot(times, res_crab.optimized_controls[0], label='optimized pulse')
plt.title('CRAB pulse')
plt.xlabel('Time')
plt.ylabel('Pulse amplitude')
plt.legend()
plt.show()
```

```python
H_result = [Hd, [Hc, np.array(res_crab.optimized_controls[0])]]
evolution = qt.sesolve(H_result, initial_state, times)

plt.plot(times, [np.abs(state.overlap(initial_state)) for state in evolution.states], label="Overlap with initial state")
plt.plot(times, [np.abs(state.overlap(target_state)) for state in evolution.states], label="Overlap with target state")
plt.plot(times, [qt.fidelity(state, target_state) for state in evolution.states], '--', label="Fidelity")

plt.title("CRAB performance")
plt.xlabel('Time')
plt.legend()
plt.show()
```

## Validation

```python
assert res_crab.infidelity < 0.001
assert np.abs(evolution.states[-1].overlap(target_state)) > 1-0.001
```

```python
qt.about()
```
Loading