Skip to content

Commit

Permalink
Merge branch 'dbf_migrate' of https://github.com/qiboteam/qibo into d…
Browse files Browse the repository at this point in the history
…bi_magnetic_field
  • Loading branch information
Sam-XiaoyueLi committed Feb 22, 2024
2 parents 892fda4 + 0ff852b commit ed7580e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/qibo/models/dbi/double_bracket.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def off_diag_h(self):

@property
def off_diagonal_norm(self):
r"""Hilbert Schmidt norm of off-diagonal part of H matrix: \Tr(\sqrt{A^\dag A})"""
r"""Hilbert Schmidt norm of off-diagonal part of H matrix, namely :math:`\\text{Tr}(\\sqrt{A^{\\dagger} A})`."""
off_diag_h_dag = self.backend.cast(
np.matrix(self.backend.to_numpy(self.off_diag_h)).getH()
)
Expand Down
22 changes: 9 additions & 13 deletions src/qibo/models/dbi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import numpy as np

from qibo import symbols
from qibo.config import raise_error
from qibo.hamiltonians import Hamiltonian, SymbolicHamiltonian
from qibo.hamiltonians import SymbolicHamiltonian
from qibo.models.dbi.double_bracket import (
DoubleBracketGeneratorType,
DoubleBracketIteration,
Expand Down Expand Up @@ -42,10 +41,10 @@ def generate_Z_operators(nqubits: int):
combination_strings = product("ZI", repeat=nqubits)
output_dict = {}

for op_string_tup in combination_strings:
for zi_string_combination in combination_strings:
# except for the identity
if "Z" in op_string_tup:
op_name = "".join(op_string_tup)
if "Z" in zi_string_combination:
op_name = "".join(zi_string_combination)
tensor_op = str_to_symbolic(op_name)
# append in output_dict
output_dict[op_name] = SymbolicHamiltonian(tensor_op).dense.matrix
Expand Down Expand Up @@ -76,7 +75,7 @@ def select_best_dbr_generator(
scheduling: DoubleBracketScheduling = None,
**kwargs,
):
"""Selects the best double bracket rotation generator from a list and runs the
"""Selects the best double bracket rotation generator from a list and execute the rotation.
Args:
dbi_object (`DoubleBracketIteration`): the target DoubleBracketIteration object.
Expand All @@ -94,12 +93,11 @@ def select_best_dbr_generator(
norms_off_diagonal_restriction = [
dbi_object.off_diagonal_norm for _ in range(len(d_list))
]
optimal_steps = [0 for _ in range(len(d_list))]
flip_list = [1 for _ in range(len(d_list))]
optimal_steps, flip_list = [], []
for i, d in enumerate(d_list):
# prescribed step durations
dbi_eval = deepcopy(dbi_object)
flip_list[i] = cs_angle_sgn(dbi_eval, d)
flip_list.append(cs_angle_sgn(dbi_eval, d))
if flip_list[i] != 0:
if step is None:
step_best = dbi_eval.choose_step(
Expand All @@ -108,7 +106,7 @@ def select_best_dbr_generator(
else:
step_best = step
dbi_eval(step=step_best, d=flip_list[i] * d)
optimal_steps[i] = step_best
optimal_steps.append(step_best)
norms_off_diagonal_restriction[i] = dbi_eval.off_diagonal_norm
# canonical
if compare_canonical is True:
Expand All @@ -123,9 +121,7 @@ def select_best_dbr_generator(
optimal_steps.append(step_best)
norms_off_diagonal_restriction.append(dbi_eval.off_diagonal_norm)
# find best d
idx_max_loss = norms_off_diagonal_restriction.index(
min(norms_off_diagonal_restriction)
)
idx_max_loss = np.argmin(norms_off_diagonal_restriction)
flip = flip_list[idx_max_loss]
step_optimal = optimal_steps[idx_max_loss]
dbi_eval = deepcopy(dbi_object)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_models_dbi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np
import pytest

from qibo import set_backend
from qibo.hamiltonians import Hamiltonian
from qibo.models.dbi.double_bracket import (
DoubleBracketGeneratorType,
Expand All @@ -16,7 +15,7 @@
"""Number of steps for evolution."""


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [2, 3])
def test_generate_Z_operators(backend, nqubits):
h0 = random_hermitian(2**nqubits)
dbi = DoubleBracketIteration(Hamiltonian(nqubits=nqubits, matrix=h0))
Expand All @@ -30,7 +29,7 @@ def test_generate_Z_operators(backend, nqubits):
assert norm_diff < 1e-3


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [2, 3])
@pytest.mark.parametrize("step", [0.1, None])
def test_select_best_dbr_generator(backend, nqubits, step):
h0 = random_hermitian(2**nqubits, seed=1, backend=backend)
Expand Down

0 comments on commit ed7580e

Please sign in to comment.