Skip to content

Commit

Permalink
Remove use in scheduling names
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-XiaoyueLi committed Feb 28, 2024
1 parent bfec996 commit 54ced5b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
35 changes: 23 additions & 12 deletions examples/dbi/dbi_scheduling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The default scheduling strategy is grid search: `DoubleBracketScheduling.use_grid_serach`. This strategy specifies a list of step durations to test one by one and finds the one that maximizes the cost function (off-digonal norm of Hamiltonian)"
"The default scheduling strategy is grid search: `DoubleBracketScheduling.\n",
"grid_serach`. This strategy specifies a list of step durations to test one by one and finds the one that maximizes the cost function (off-digonal norm of Hamiltonian)"
]
},
{
Expand All @@ -97,16 +98,26 @@
"outputs": [],
"source": [
"# grid_search\n",
"step_grid = dbi.choose_step(scheduling=DoubleBracketScheduling.use_grid_search)\n",
"step_grid = dbi.choose_step(scheduling=DoubleBracketScheduling.grid_search)\n",
"print('grid_search step:', step_grid)\n",
"# hyperopt\n",
"step_hyperopt = dbi.choose_step(scheduling=DoubleBracketScheduling.use_hyperopt, max_evals=100, step_max=0.6)\n",
"step_hyperopt = dbi.choose_step(scheduling=DoubleBracketScheduling.hyperopt, max_evals=100, step_max=0.6)\n",
"print('hyperopt_search step:', step_hyperopt)\n",
"# polynomial expansion\n",
"step_poly = dbi.choose_step(scheduling=DoubleBracketScheduling.use_polynomial_approximation, n=5)\n",
"step_poly = dbi.choose_step(scheduling=DoubleBracketScheduling.polynomial_approximation, n=5)\n",
"print('polynomial_approximation step:', step_poly)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"step_poly = dbi.polynomial_step(n=5)\n",
"print(step_poly)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -169,15 +180,15 @@
"outputs": [],
"source": [
"# grid_search\n",
"step_grid = dbi.choose_step(scheduling=DoubleBracketScheduling.use_grid_search, step_max=0.6, d=d)\n",
"step_grid = dbi.choose_step(scheduling=DoubleBracketScheduling.grid_search, step_max=0.6, d=d)\n",
"grid_min = dbi.loss(step=step_grid, d=d)-dbi.off_diagonal_norm\n",
"print('grid_search step:', step_grid, 'loss', grid_min)\n",
"# hyperopt\n",
"step_hyperopt = dbi.choose_step(scheduling=DoubleBracketScheduling.use_hyperopt, d=d, max_evals=100, step_max=0.6)\n",
"step_hyperopt = dbi.choose_step(scheduling=DoubleBracketScheduling.hyperopt, d=d, max_evals=100, step_max=0.6)\n",
"hyperopt_min = dbi.loss(step=step_hyperopt, d=d)-dbi.off_diagonal_norm\n",
"print('hyperopt_search step:', step_hyperopt, 'loss', hyperopt_min)\n",
"# polynomial expansion\n",
"step_poly = dbi.choose_step(scheduling=DoubleBracketScheduling.use_polynomial_approximation, d=d, n=5)\n",
"step_poly = dbi.choose_step(scheduling=DoubleBracketScheduling.polynomial_approximation, d=d, n=5)\n",
"poly_min = dbi.loss(step=step_poly, d=d)-dbi.off_diagonal_norm\n",
"print('polynomial_approximation step:', step_poly, 'loss', poly_min)"
]
Expand Down Expand Up @@ -230,10 +241,10 @@
" step_min = step_poly - search_range/2\n",
" step_max = step_poly + search_range/2\n",
"# grid_search\n",
"step_grid = dbi.choose_step(scheduling=DoubleBracketScheduling.use_grid_search, step_min=step_min, step_max=step_max, d=d)\n",
"step_grid = dbi.choose_step(scheduling=DoubleBracketScheduling.grid_search, step_min=step_min, step_max=step_max, d=d)\n",
"print('grid_search step:', step_grid)\n",
"# hyperopt\n",
"step_hyperopt = dbi.choose_step(scheduling=DoubleBracketScheduling.use_hyperopt, step_min=step_min, step_max=step_max, max_evals=100, d=d,)\n",
"step_hyperopt = dbi.choose_step(scheduling=DoubleBracketScheduling.hyperopt, step_min=step_min, step_max=step_max, max_evals=100, d=d,)\n",
"print('hyperopt_search step:', step_hyperopt)"
]
},
Expand Down Expand Up @@ -313,9 +324,9 @@
"outputs": [],
"source": [
"NSTEPS = 8\n",
"scheduling_list = [DoubleBracketScheduling.use_grid_search,\n",
" DoubleBracketScheduling.use_hyperopt,\n",
" DoubleBracketScheduling.use_polynomial_approximation,]\n",
"scheduling_list = [DoubleBracketScheduling.grid_search,\n",
" DoubleBracketScheduling.hyperopt,\n",
" DoubleBracketScheduling.polynomial_approximation,]\n",
"scheduling_labels = ['grid search',\n",
" 'hyperopt',\n",
" 'polynomial',]\n",
Expand Down
18 changes: 9 additions & 9 deletions src/qibo/models/dbi/double_bracket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class DoubleBracketGeneratorType(Enum):
class DoubleBracketScheduling(Enum):
"""Define the DBI scheduling strategies."""

use_hyperopt = auto()
hyperopt = auto()
"""Use hyperopt package."""
use_grid_search = auto()
grid_search = auto()
"""Use greedy grid search."""
use_polynomial_approximation = auto()
polynomial_approximation = auto()
"""Use polynomial expansion (analytical) of the loss function."""


Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(
self,
hamiltonian: Hamiltonian,
mode: DoubleBracketGeneratorType = DoubleBracketGeneratorType.canonical,
scheduling: DoubleBracketScheduling = DoubleBracketScheduling.use_grid_search,
scheduling: DoubleBracketScheduling = DoubleBracketScheduling.grid_search,
):
self.h = hamiltonian
self.h0 = deepcopy(self.h)
Expand Down Expand Up @@ -223,7 +223,7 @@ def polynomial_step(
d = self.diagonal_h_matrix

if backup_scheduling is None:
backup_scheduling = DoubleBracketScheduling.use_grid_search
backup_scheduling = DoubleBracketScheduling.grid_search

def sigma(h: np.array):
return h - self.backend.cast(np.diag(np.diag(self.backend.to_numpy(h))))
Expand Down Expand Up @@ -270,7 +270,7 @@ def Gamma(k: int):
return min(real_positive_roots)
# solution does not exist, resort to backup scheduling
elif (
backup_scheduling == DoubleBracketScheduling.use_polynomial_approximation
backup_scheduling == DoubleBracketScheduling.polynomial_approximation
and n < n_max + 1
):
return self.polynomial_step(
Expand All @@ -287,11 +287,11 @@ def choose_step(
):
if scheduling is None:
scheduling = self.scheduling
if scheduling is DoubleBracketScheduling.use_grid_search:
if scheduling is DoubleBracketScheduling.grid_search:
return self.grid_search_step(d=d, **kwargs)
if scheduling is DoubleBracketScheduling.use_hyperopt:
if scheduling is DoubleBracketScheduling.hyperopt:
return self.hyperopt_step(d=d, **kwargs)
if scheduling is DoubleBracketScheduling.use_polynomial_approximation:
if scheduling is DoubleBracketScheduling.polynomial_approximation:
return self.polynomial_step(d=d, **kwargs)

def loss(self, step: float, d: np.array = None, look_ahead: int = 1):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_models_dbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_energy_fluctuations(backend):

@pytest.mark.parametrize(
"scheduling",
[DoubleBracketScheduling.use_grid_search, DoubleBracketScheduling.use_hyperopt],
[DoubleBracketScheduling.grid_search, DoubleBracketScheduling.hyperopt],
)
@pytest.mark.parametrize("nqubits", [3, 4, 5])
def test_double_bracket_iteration_scheduling_grid_hyperopt(
Expand All @@ -136,7 +136,7 @@ def test_double_bracket_iteration_scheduling_grid_hyperopt(
@pytest.mark.parametrize("nqubits", [3, 4, 6])
@pytest.mark.parametrize("n", [2, 3])
@pytest.mark.parametrize(
"backup_scheduling", [None, DoubleBracketScheduling.use_polynomial_approximation]
"backup_scheduling", [None, DoubleBracketScheduling.polynomial_approximation]
)
def test_double_bracket_iteration_scheduling_polynomial(
backend, nqubits, n, backup_scheduling
Expand All @@ -146,14 +146,14 @@ def test_double_bracket_iteration_scheduling_polynomial(
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
mode=DoubleBracketGeneratorType.single_commutator,
scheduling=DoubleBracketScheduling.use_polynomial_approximation,
scheduling=DoubleBracketScheduling.polynomial_approximation,
)
initial_off_diagonal_norm = dbi.off_diagonal_norm
for _ in range(NSTEPS):
step1 = dbi.polynomial_step(n=n, d=d, backup_scheduling=backup_scheduling)
dbi(d=d, step=step1)
step2 = dbi.choose_step(
scheduling=DoubleBracketScheduling.use_polynomial_approximation, n=n
scheduling=DoubleBracketScheduling.polynomial_approximation, n=n
)
dbi(step=step2)
assert initial_off_diagonal_norm > dbi.off_diagonal_norm

0 comments on commit 54ced5b

Please sign in to comment.