Skip to content

Commit

Permalink
Test coverage for the branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-XiaoyueLi committed Feb 28, 2024
1 parent 4124519 commit 3bdee0f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/qibo/models/dbi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,16 @@ def derivative_product(k1, k2):
def gradient_onsite_Z(
dbi_object: DoubleBracketIteration,
d: np.array,
onsite_Z_ops,
n_taylor=2,
onsite_Z_ops=None,
use_ds=False,
):
r"""Calculate the gradient of loss function with respect to onsite Pauli-Z coefficients
Args:
dbi_object (DoubleBracketIteration): the target dbi object
d (np.array): the diagonal operator
n_taylor (int): the highest order of the taylore expansion of w.r.t `s`
onsite_Z_ops (list): list of Pauli-Z operators
taylor_coef (list): coefficients of `s` in the taylor expansion of math:`\\frac{\\partial ||\sigma(e^{sW}He^{-sW})||^2}{\\partial s}`
use_ds (boolean): if False, ds is set to 0
"""
Expand Down
50 changes: 0 additions & 50 deletions tests/test_models_dbi_utils 2.py

This file was deleted.

49 changes: 48 additions & 1 deletion tests/test_models_dbi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@pytest.mark.parametrize("nqubits", [2, 3])
def test_generate_Z_operators(backend, nqubits):
def test_generate_Z_operators(nqubits):
h0 = random_hermitian(2**nqubits)
dbi = DoubleBracketIteration(Hamiltonian(nqubits=nqubits, matrix=h0))
generate_Z = generate_Z_operators(nqubits)
Expand Down Expand Up @@ -47,3 +47,50 @@ def test_select_best_dbr_generator(backend, nqubits, step):
)

assert initial_off_diagonal_norm > dbi.off_diagonal_norm


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("d_option", ["delta_H", "min_max"])
def test_gradient_descent_onsite_Z(backend, nqubits, d_option):
h0 = random_hermitian(2**nqubits, seed=1, backend=backend)
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
mode=DoubleBracketGeneratorType.single_commutator,
)
initial_off_diagonal_norm = dbi.off_diagonal_norm
onsite_Z_ops = generate_onsite_Z_ops(nqubits)
if d_option == "delta_H":
d_coef = onsite_Z_decomposition(dbi.h.matrix, onsite_Z_ops)
if d_option == "min_max":
d_min_max = diagonal_min_max(dbi.h.matrix)
d_coef = onsite_Z_decomposition(d_min_max, onsite_Z_ops)
d = sum([d_coef[i] * onsite_Z_ops[i] for i in range(nqubits)])
iters = 15
for _ in range(iters):
# calculate elements of gradient descent
s, d_coef, d = gradient_descent_onsite_Z(
dbi, d_coef, d, onsite_Z_ops=onsite_Z_ops, max_evals=100
)
# double bracket rotation with the results
dbi(step=s, d=d)
# when onsite_Z_ops not given
s, d_coef, d = gradient_descent_onsite_Z(dbi, d_coef, max_evals=100)
dbi(step=s, d=d)
assert initial_off_diagonal_norm > dbi.off_diagonal_norm


@pytest.mark.parametrize("nqubits", [3, 4, 5])
def test_dGamma_di_onsite_Z(nqubits):
h0 = random_hermitian(2**nqubits, seed=1)
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0),
mode=DoubleBracketGeneratorType.single_commutator,
)
onsite_Z_ops = generate_onsite_Z_ops(nqubits)
d_coef = onsite_Z_decomposition(dbi.h.matrix, onsite_Z_ops)
d = sum([d_coef[i] * onsite_Z_ops[i] for i in range(nqubits)])
# provide onsite_Z_ops or not gives the same result
dGamma_di_onsite_Z_with_Z_ops = dGamma_di_onsite_Z(dbi, 3, 1, d, onsite_Z_ops)
assert (
dGamma_di_onsite_Z_with_Z_ops[-1] == dGamma_di_onsite_Z(dbi, 3, 1, d)[-1]
).all()

0 comments on commit 3bdee0f

Please sign in to comment.