Skip to content

Commit

Permalink
Fix test random hamiltonian seed for test coverage stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-XiaoyueLi committed Feb 15, 2024
1 parent 588a02a commit 9d663cb
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tests/test_models_dbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
from qibo.quantum_info import random_hermitian

NSTEPS = 1
seed = 10
"""Number of steps for evolution."""


@pytest.mark.parametrize("nqubits", [3, 4, 5])
def test_double_bracket_iteration_canonical(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
mode=DoubleBracketGeneratorType.canonical,
Expand All @@ -30,7 +31,7 @@ def test_double_bracket_iteration_canonical(backend, nqubits):

@pytest.mark.parametrize("nqubits", [3, 4, 5])
def test_double_bracket_iteration_group_commutator(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
Expand All @@ -49,7 +50,7 @@ def test_double_bracket_iteration_group_commutator(backend, nqubits):

@pytest.mark.parametrize("nqubits", [3, 4, 5])
def test_double_bracket_iteration_single_commutator(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
Expand All @@ -66,7 +67,7 @@ def test_double_bracket_iteration_single_commutator(backend, nqubits):

@pytest.mark.parametrize("nqubits", [3, 4, 5])
def test_hyperopt_step(backend, nqubits):
h0 = random_hermitian(2**nqubits, backend=backend)
h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
dbi = DoubleBracketIteration(Hamiltonian(nqubits, h0, backend=backend))

Expand Down Expand Up @@ -115,7 +116,7 @@ def test_energy_fluctuations(backend):
def test_double_bracket_iteration_scheduling_grid_hyperopt(
backend, nqubits, scheduling
):
h0 = random_hermitian(2**nqubits, backend=backend)
h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
Expand All @@ -125,20 +126,20 @@ def test_double_bracket_iteration_scheduling_grid_hyperopt(
for _ in range(NSTEPS):
step1 = dbi.choose_step(d=d, scheduling=scheduling)
dbi(d=d, step=step1)
step2 = dbi.choose_step(scheduling=scheduling)
step2 = dbi.choose_step()
dbi(step=step2)
assert initial_off_diagonal_norm > dbi.off_diagonal_norm


@pytest.mark.parametrize("nqubits", [3, 4, 5])
@pytest.mark.parametrize("nqubits", [3, 4, 6])
@pytest.mark.parametrize("n", [2, 3])
@pytest.mark.parametrize(
"backup_scheduling", [None, DoubleBracketScheduling.use_polynomial_approximation]
)
def test_double_bracket_iteration_scheduling_polynomial(
backend, nqubits, n, backup_scheduling
):
h0 = random_hermitian(2**nqubits, backend=backend)
h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
dbi = DoubleBracketIteration(
Hamiltonian(nqubits, h0, backend=backend),
Expand All @@ -149,6 +150,8 @@ def test_double_bracket_iteration_scheduling_polynomial(
for _ in range(NSTEPS):
step1 = dbi.polynomial_step(n=n, d=d, backup_scheduling=backup_scheduling)
dbi(d=d, step=step1)
step2 = dbi.polynomial_step(n=n)
step2 = dbi.choose_step(
scheduling=DoubleBracketScheduling.use_polynomial_approximation, n=n
)
dbi(step=step2)
assert initial_off_diagonal_norm > dbi.off_diagonal_norm

0 comments on commit 9d663cb

Please sign in to comment.